Skip to main content

swap_gate

Function swap_gate 

Source
pub fn swap_gate(a: usize, b: usize) -> Result<BasicBlock, KetError>
Expand description

Return a block with a SWAP gate.

ยงErrors

Propagates any KetError that occurs during the controlled_gate operation.

Examples found in repository?
examples/qft.rs (line 42)
18fn qft(qubits: &[usize], do_swap: bool) -> Result<BasicBlock, KetError> {
19    let mut quantum_code = BasicBlock::new();
20    if qubits.len() == 1 {
21        quantum_code.append_gate(QuantumGate::Hadamard, qubits[0]);
22        return Ok(quantum_code);
23    }
24
25    let init = &qubits[..qubits.len() - 1];
26    let last = qubits[qubits.len() - 1];
27    quantum_code.append_gate(QuantumGate::Hadamard, last);
28    for (i, c) in init.iter().enumerate() {
29        quantum_code.append_block(
30            controlled_gate(
31                QuantumGate::Phase(Param::Value(PI / 2.0_f64.powi(i as i32 + 1))),
32                &[*c],
33                last,
34            )?,
35            None,
36        );
37    }
38    quantum_code.append_block(qft(init, false)?, None);
39
40    if do_swap {
41        for i in 0..qubits.len() / 2 {
42            quantum_code.append_block(swap_gate(qubits[i], qubits[qubits.len() - i - 1])?, None);
43        }
44    }
45
46    Ok(quantum_code)
47}