generalised_control_not_gate/
generalised_control_not_gate.rs1use quantr::{
15 states::{ProductState, Qubit, SuperPosition},
16 Circuit, Gate, Measurement, Printer, QuantrError,
17};
18
19const CIRCUIT_SIZE: usize = 6;
20
21fn main() -> Result<(), QuantrError> {
22 let mut qc: Circuit = Circuit::new(CIRCUIT_SIZE)?;
23
24 qc.add_repeating_gate(Gate::X, &[0, 1, 2, 3, 4, 5])?
26 .add_gate(
27 Gate::Custom(
28 multicnot::<CIRCUIT_SIZE>,
29 vec![0, 1, 2, 3, 4],
30 "X".to_string(),
31 ),
32 5,
33 )?;
34
35 let mut circuit_printer: Printer = Printer::new(&qc);
36 circuit_printer.print_diagram();
37
38 qc.set_print_progress(true);
39 let simulated = qc.simulate();
40
41 if let Measurement::Observable(bin_count) = simulated.measure_all(50) {
43 println!("\nStates observed over 50 measurements:");
44 for (states, count) in bin_count.into_iter() {
45 println!("|{}> : {}", states, count);
46 }
47 }
48
49 Ok(())
50}
51
52fn multicnot<const NUM_CONTROL: usize>(input_state: ProductState) -> Option<SuperPosition> {
54 let mut copy_state = input_state;
55 if copy_state.get_qubits() == [Qubit::One; NUM_CONTROL] {
56 copy_state.get_mut_qubits()[NUM_CONTROL - 1] = Qubit::Zero;
57 return Some(copy_state.into());
58 } else if copy_state.get_qubits() == {
59 let mut temp = [Qubit::One; NUM_CONTROL];
60 temp[NUM_CONTROL - 1] = Qubit::Zero;
61 temp
62 } {
63 copy_state.get_mut_qubits()[NUM_CONTROL - 1] = Qubit::One;
64 return Some(copy_state.into());
65 } else {
66 None
67 }
68}