import std.fmt
import std.qec
import std.stabilizer as stab
fn surface_codes() {
println("Rotated surface code, exact Blossom MWPM under independent bit flips")
println("Seed 7, 1,000 uniform samples per sampled error weight")
let p = [0.01, 0.03, 0.06, 0.10, 0.14]
let d3 = qec.surface_sweep(qec.SurfaceCode.new(3).unwrap(), qec.Axis.X, p, 1000, 7)
let d5 = qec.surface_sweep(qec.SurfaceCode.new(5).unwrap(), qec.Axis.X, p, 1000, 7)
let d7 = qec.surface_sweep(qec.SurfaceCode.new(7).unwrap(), qec.Axis.X, p, 1000, 7)
println(" p | d = 3 | d = 5 | d = 7")
for i in 0..5 {
fmt.println(" {:.2} | {:.5e} | {:.5e} | {:.5e}", (p[i], d3[i], d5[i], d7[i]))
}
for i in 0..3 {
assert(d7[i] < d5[i] && d5[i] < d3[i], "below-threshold suppression")
}
assert(d3[4] < d5[4] && d5[4] < d7[4], "above-threshold distance ordering")
println("Larger patches suppress errors at p = 0.01–0.06. The ordering reverses by p = 0.14.")
}
fn lattice_surgery() {
println("\nLattice surgery on two distance-three patches")
let surgery = qec.ZzSurgery.new()
var t = surgery.prepare(true, false, false, 12)
let outcome = surgery.merge(&t)
surgery.split(&t)
let xx = surgery.logical(qec.Axis.X, 0) | surgery.logical(qec.Axis.X, 1)
let zz = surgery.logical(qec.Axis.Z, 0) | surgery.logical(qec.Axis.Z, 1)
let expected = if outcome.m_zz { stab.ZExpectation.One } else { stab.ZExpectation.Zero }
assert(qec.pauli_expectation(t, qec.Axis.Z, zz) == expected, "joint ZZ eigenvalue")
assert(
qec.pauli_expectation(t, qec.Axis.X, xx) == stab.ZExpectation.Zero,
"joint XX eigenvalue"
)
let sign = if outcome.m_zz { "-1" } else { "+1" }
fmt.println("|+_L>|+_L> → logical Bell state, m_ZZ = {}, ZZ = {}, XX = +1",
(outcome.m_zz as i64, sign))
var parity: [i64; 4] = [0; 4]
for i in 0..4 {
var input = surgery.prepare(false, i >= 2, (i & 1) != 0, i as u64)
parity[i] = surgery.merge(&input).m_zz as i64
surgery.split(&input)
}
assert(parity == [0, 1, 1, 0], "computational parity")
fmt.println("M_ZZ on |00_L>, |01_L>, |10_L>, |11_L> gives {}, {}, {}, {}",
(parity[0], parity[1], parity[2], parity[3]))
}
fn distillation() {
println("\n15-to-1 magic-state distillation with the [[15, 1, 3]] Reed–Muller code")
let enumerator = qec.distillation_enumerator()
fmt.println(
"A_0 = {}, A_1 = {}, A_2 = {}, A_3 = {}",
(enumerator.trivial[0], enumerator.logical[1], enumerator.logical[2], enumerator.logical[3])
)
fmt.println(
"Undetected logical Z weights 3, 5, 7, 9, 11, 15 have counts {}, {}, {}, {}, {}, {}",
(enumerator.logical[3], enumerator.logical[5], enumerator.logical[7],
enumerator.logical[9], enumerator.logical[11], enumerator.logical[15])
)
let p = [0.05, 0.02, 0.01, 0.005]
let estimates = qec.distillation_sweep(p, 8192, 341)
println(" p_in | accept (%) | exact round 1 | Monte Carlo | exact round 2")
for i in 0..4 {
let once = enumerator.rate(p[i])
let twice = enumerator.rate(once.output_error)
fmt.println(" {:.3} | {:.3} | {:.5e} | {:.5e} | {:.5e}",
(p[i], once.acceptance * 100.0, once.output_error, estimates[i].output_error,
twice.output_error))
}
}
fn asl_bridge() {
println("\nASL quantum doubles")
let data = qec.toric_data()
let torus = qec.ToricCode.from_anyons(data, 3).unwrap()
let e = torus.string(torus.sectors().e, 0, 0, true, 2).unwrap()
let m = torus.string(torus.sectors().m, 2, 0, false, 2).unwrap()
let phase = torus.braiding_phase(e, m)
var t = torus.prepare(97)
qec.apply_pauli(&t, qec.Axis.Z, e)
qec.apply_pauli(&t, qec.Axis.X, m)
assert(torus.syndrome(t, qec.Axis.X) == 5 && torus.syndrome(t, qec.Axis.Z) == 72,
"torus string endpoints")
assert(phase == -1 && data.s(torus.sectors().e, torus.sectors().m).exponent == 1, "ASL braid")
fmt.println("D(ℤ₂) torus [[{}, {}, {}]], crossing phase {} = S(e,m)/S(1,1)",
(torus.qubits(), torus.encoded_qubits(), torus.side(), phase))
let planar = qec.PlanarCode.from_anyons(data, 3).unwrap()
var pt = planar.prepare(301)
let e_boundary = planar.boundary_string(planar.sectors().e, 1, 3).unwrap()
let m_boundary = planar.boundary_string(planar.sectors().m, 1, 3).unwrap()
qec.apply_pauli(&pt, qec.Axis.Z, e_boundary)
qec.apply_pauli(&pt, qec.Axis.X, m_boundary)
assert(planar.syndrome(pt, qec.Axis.X) == 0 && planar.syndrome(pt, qec.Axis.Z) == 0,
"boundary absorption")
fmt.println(
"Planar [[{}, {}, {}]] patch absorbs e at rough boundaries and m at smooth boundaries.",
(planar.qubits(), planar.encoded_qubits(), planar.distance())
)
let semion = qec.double_semion_data()
let s = semion.sectors()
fmt.println("D^ω(ℤ₂) twist exponents mod {} for (1, s, sbar, b) are ({}, {}, {}, {})",
(semion.conductor(), s[0].twist_exp, s[1].twist_exp, s[2].twist_exp, s[3].twist_exp))
let carry = qec.twisted_z3_data()
let flux = carry.sector_id("x").unwrap()
var power = 0
for i in 1..10 {
power = carry.pointed_product(power, flux).unwrap()
if i == 3 {
assert(power == carry.sector_id("charge").unwrap(), "unit flux cubes to charge")
}
assert((power == 0) == (i == 9), "order nine")
}
fmt.println(
"D^ω(ℤ₃) has {} sectors at conductor {}. The unit flux has x³ = charge and order 9.",
(carry.sectors().len, carry.conductor())
)
}
fn main() {
surface_codes()
lattice_surgery()
distillation()
asl_bridge()
}
View on GitHub