open-standards/wai/wai-quantum/examples/bell.rs
Transaction Science e546a87a2f wai-quantum circuit2: the crate's consumers follow the circuit's gateset
QuantumReceipt gains gateset_of, which names the gateset under which a
circuit's ops hash to the receipt's circuit_hash, and gateset_note, which
turns that into the text to show when a reconstruction fails for that
reason. verify_reconstruction stays strict. from_json refuses an n_qubits
that does not fit a u8 and an n_ops that does not fit a u32 instead of
truncating them.

The fabric names its gate kernel and random corpus once per gateset:
wai.quantum.circuit2/apply and wai.quantum.circuit2/random-v1 join the
circuit/1 names. certify_lanes and lanes::random_circuit take a gateset and
draw the same ops from a seed in both. detect_local offers the reference
kernel of each gateset. The lanes32 corpus digests for seed 7 and 200 cases
are pinned: d18454c6 for circuit/1, as 0.3.41 certified it, and 3dbde276
for circuit2.

noisy_expect_z and route_line build their circuits with empty_like, so a
derived circuit keeps its source's gateset. from_qasm refuses a gate that
names one qubit twice as an invalid circuit when it reads it, in every
gateset. quantum_spd's controlled_phase refuses a phase on 32 or more
qubits instead of shifting out of range. The QIR and QASM export docs say
that a circuit/1 circuit exports ideal angles, not its own numerics. The
bell and receipt examples print the capability and handle the fallible
calls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-09 11:19:49 -04:00

44 lines
1.4 KiB
Rust

//! The smallest complete thing: build a circuit, simulate it exactly, and get a
//! hash that is the same on every machine.
//!
//! cargo run --example bell
use wai_quantum::quantum::{circuit_hash, Circuit};
use wai_quantum::quantum_toolchain as qt;
fn main() {
// A new circuit is in the circuit2 gateset, whose phase table is correctly
// rounded; `Circuit::with_gateset(2, Gateset::V1)` reproduces circuit/1.
let mut c = Circuit::new(2);
c.h(0).cx(0, 1); // a Bell pair
let sv = c.simulate().expect("simulate");
// The circuit's identity covers its capability; a circuit that simulates
// always has one.
let id = circuit_hash(&c).expect("a valid circuit has a hash");
println!("circuit {} qubits, {} gates, {}", c.n_qubits, c.ops.len(), c.gateset.capability());
print!("circuit-id ");
for b in id {
print!("{b:02x}");
}
println!();
let (backend, why) = qt::recommend_backend(&c);
println!("backend {backend:?} — {why}");
println!("outcomes");
for (state, p) in qt::top_outcomes(&sv, 4) {
if p > 0.0 {
println!(" |{state:02b}> {p:.6}");
}
}
// Not "a hash of this run" — the reconstruction is byte-exact, so this is the
// same value natively, in a browser, and in a WASI component at the edge.
let h = sv.statevector_hash();
print!("state-hash ");
for b in h {
print!("{b:02x}");
}
println!();
}