post_select/
post_select.rs1use quantr::{
12    complex_re_array,
13    states::{ProductState, Qubit, SuperPosition},
14    Circuit, Gate, Measurement, Printer, QuantrError,
15};
16
17fn main() -> Result<(), QuantrError> {
18    let mut qc = Circuit::new(3)?;
19
20    qc.add_repeating_gate(Gate::H, &[0, 1, 2])?
21        .add_gate(Gate::Custom(post_select, vec![], "P".to_string()), 1)?;
22
23    let mut printer = Printer::new(&qc);
24    printer.print_diagram();
25
26    qc.set_print_progress(true);
27    let simulated_qc = qc.simulate();
28
29    if let Measurement::NonObservable(final_sup) = simulated_qc.get_state() {
30        println!("\nThe final superposition is:");
31        for (state, amplitude) in final_sup.into_iter() {
32            println!("|{}> : {}", state, amplitude);
33        }
34    }
35
36    Ok(())
37}
38
39fn post_select(state_in: ProductState) -> Option<SuperPosition> {
40    let qubit: Qubit = state_in.get_qubits()[0];
41    match qubit {
42        Qubit::Zero => Some(SuperPosition::new_with_amplitudes_unchecked(
43            &complex_re_array!(2f64.sqrt(), 0f64),
44        )), Qubit::One => Some(SuperPosition::new_with_amplitudes_unchecked(
46            &complex_re_array!(0f64, 0f64),
47        )),
48    }
49}