use quantr::{
states::{ProductState, Qubit, SuperPosition},
Circuit, Gate, Measurement, Printer, QuantrError,
};
const CIRCUIT_SIZE: usize = 6;
fn main() -> Result<(), QuantrError> {
let mut qc: Circuit = Circuit::new(CIRCUIT_SIZE)?;
qc.add_repeating_gate(Gate::X, &[0, 1, 2, 3, 4, 5])?
.add_gate(
Gate::Custom(multicnot::<CIRCUIT_SIZE>, &[0, 1, 2, 3, 4], "X".to_string()),
5,
)?;
let mut circuit_printer: Printer = Printer::new(&qc);
circuit_printer.print_diagram();
qc.toggle_simulation_progress();
qc.simulate();
if let Ok(Measurement::Observable(bin_count)) = qc.repeat_measurement(50) {
println!("\nStates observed over 50 measurements:");
for (states, count) in bin_count.into_iter() {
println!("|{}> : {}", states.to_string(), count);
}
}
Ok(())
}
fn multicnot<const NUM_CONTROL: usize>(input_state: ProductState) -> Option<SuperPosition> {
let mut copy_state = input_state.clone();
if input_state.get_qubits() == [Qubit::One; NUM_CONTROL] {
copy_state.get_mut_qubits()[NUM_CONTROL - 1] = Qubit::Zero;
return Some(copy_state.into());
} else if copy_state.get_qubits() == {
let mut temp = [Qubit::One; NUM_CONTROL];
temp[NUM_CONTROL - 1] = Qubit::Zero;
temp
} {
copy_state.get_mut_qubits()[NUM_CONTROL - 1] = Qubit::One;
return Some(copy_state.into());
} else {
None
}
}