Function ctrl

Source
pub fn ctrl<F>(
    process: &mut Process,
    control: &[LogicalQubit],
    f: F,
) -> Result<()>
where F: FnMut(&mut Process) -> Result<()>,
Examples found in repository?
examples/grover.rs (lines 53-55)
12fn main() -> Result<(), KetError> {
13    set_log_level(3);
14
15    let config = ExecutionTarget {
16        num_qubits: 12,
17        qpu: Some(QPU {
18            coupling_graph: Some(ket::ex_arch::GRID12.to_vec()),
19            u2_gates: U2Gates::RzSx,
20            u4_gate: U4Gate::CZ,
21        }),
22        execution_protocol: ExecutionProtocol::ManagedByTarget {
23            sample: Capability::Basic,
24            measure: Capability::Unsupported,
25            exp_value: Capability::Unsupported,
26            dump: Capability::Unsupported,
27        },
28        gradient: None,
29    };
30
31    let mut process = Process::new(config, None);
32
33    let size = 8;
34
35    let qubits: Vec<_> = (0..size).map(|_| process.alloc().unwrap()).collect();
36
37    for qubit in &qubits {
38        process.gate(QuantumGate::Hadamard, *qubit)?;
39    }
40
41    let steps = ((FRAC_PI_4) * f64::sqrt((1 << size) as f64)) as i64;
42
43    for _ in 0..steps {
44        around(
45            &mut process,
46            |process| {
47                for qubit in &qubits {
48                    process.gate(QuantumGate::PauliX, *qubit)?;
49                }
50                Ok(())
51            },
52            |process| {
53                ctrl(process, &qubits[1..], |process| {
54                    process.gate(QuantumGate::PauliZ, qubits[0])
55                })
56            },
57        )?;
58
59        around(
60            &mut process,
61            |process| {
62                for qubit in &qubits {
63                    process.gate(QuantumGate::Hadamard, *qubit)?;
64                }
65
66                for qubit in &qubits {
67                    process.gate(QuantumGate::PauliX, *qubit)?;
68                }
69                Ok(())
70            },
71            |process| {
72                ctrl(process, &qubits[1..], |process| {
73                    process.gate(QuantumGate::PauliZ, qubits[0])
74                })
75            },
76        )?;
77    }
78
79    let _ = process.sample(&qubits, 1024)?;
80
81    println!("{:#?}", process.metadata());
82
83    Ok(())
84}