Skip to main content

controlled_gate

Function controlled_gate 

Source
pub fn controlled_gate(
    gate: QuantumGate,
    control: &[usize],
    target: usize,
) -> Result<BasicBlock, KetError>
Expand description

Return a block with a controlled gate.

ยงErrors

Propagates any KetError that occurs during the control operation.

Examples found in repository?
examples/qft.rs (lines 30-34)
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}