Skip to main content

threshold/
threshold.rs

1//! A quantum error correction threshold sweep.
2//!
3//! Below threshold a bigger code protects better; above it, worse. That crossing
4//! is the whole reason error correction is believed to work, and it is a few
5//! hundred thousand decodes away.
6//!
7//!     cargo run --release --example threshold --features quantum_qec
8
9use wai_quantum::quantum_qec::{benchmark_uf, x_distance, CssCode};
10
11fn main() {
12    let trials = 4000;
13    println!("rotated surface code, union-find decoder, {trials} shots per point\n");
14
15    let codes: Vec<CssCode> = [3usize, 5, 7].iter().map(|&d| CssCode::surface(d)).collect();
16    for c in &codes {
17        // the distance is computed, not taken on faith from the constructor's name
18        let d = x_distance(c, 6).map(|d| d.to_string()).unwrap_or_else(|| ">5".into());
19        println!("  {} : n={} k={} d={}", c.id, c.n, c.k, d);
20    }
21
22    println!("\n  p        d=3        d=5        d=7");
23    for p in [0.01f64, 0.02, 0.05, 0.08, 0.12, 0.16] {
24        print!("  {p:<8}");
25        for c in &codes {
26            let (t, f) = benchmark_uf(c, p, trials, 11);
27            print!(" {:>10.4}", f as f64 / t as f64);
28        }
29        println!();
30    }
31    println!("\n  Read down each column: at low p a larger code wins. Read across the\n  bottom rows: past threshold that reverses, and more qubits buy less.");
32}