Skip to main content

bell/
bell.rs

1//! The smallest complete thing: build a circuit, simulate it exactly, and get a
2//! hash that is the same on every machine.
3//!
4//!     cargo run --example bell
5
6use wai_quantum::quantum::Circuit;
7use wai_quantum::quantum_toolchain as qt;
8
9fn main() {
10    let mut c = Circuit::new(2);
11    c.h(0).cx(0, 1); // a Bell pair
12
13    let sv = c.simulate().expect("simulate");
14
15    println!("circuit    {} qubits, {} gates", c.n_qubits, c.ops.len());
16    let (backend, why) = qt::recommend_backend(&c);
17    println!("backend    {backend:?} — {why}");
18
19    println!("outcomes");
20    for (state, p) in qt::top_outcomes(&sv, 4) {
21        if p > 0.0 {
22            println!("  |{state:02b}>  {p:.6}");
23        }
24    }
25
26    // Not "a hash of this run" — the reconstruction is byte-exact, so this is the
27    // same value natively, in a browser, and in a WASI component at the edge.
28    let h = sv.statevector_hash();
29    print!("state-hash ");
30    for b in h {
31        print!("{b:02x}");
32    }
33    println!();
34}