Error correction and the ASL bridge

Import std.qec to construct CSS codes, extract syndromes from a std.stabilizer.Tableau, and run local stabilizer measurements. The 15-to-1 factory also emits a std.quantum.Circuit that runs on CpuSim. Run the complete example with jihoo run examples/qec.jh.

Rotated surface code

SurfaceCode.new(d) supports distances 3, 5 and 7. The d² data qubits are numbered in row-major order. Each axis has (d² − 1)/2 checks. The checkerboard interior has weight-four checks. Weight-two X checks lie across the top and bottom edges, and weight-two Z checks lie down the left and right edges. A vertical X string and a horizontal Z string meet on one wire. Each logical has d disjoint parallel representatives, which force its anticommuting logical to have weight at least d.

Pauli supports are u64 masks with wire zero in the least significant bit. Check outcomes follow the order of checks(axis). A set syndrome bit is a −1 measurement outcome. prepare(false, seed) produces logical zero, and prepare(true, seed) produces logical plus, both with positive stabilizer signs.

import std.qec
import std.stabilizer

fn main() {
    let code = qec.SurfaceCode.new(3).unwrap()
    var state = code.prepare(false, 42)
    qec.apply_pauli(&state, qec.Axis.X, 0b10000)
    code.recover(&state, qec.Axis.X)
    assert(qec.pauli_expectation(state, qec.Axis.Z, code.logical(qec.Axis.Z)) ==
        stabilizer.ZExpectation.Zero, "logical Z has eigenvalue +1")
}

measure_syndrome(&state, error_axis) measures the checks of the opposite axis. decode(error_axis, syndrome) returns a recovery mask, and recover combines measurement, decoding, and Pauli application. For simulations that track a physical error mask, logical_error(error_axis, error ^ recovery) reads the residual logical bit by its commutation with the conjugate logical.

Each data qubit is an edge in the appropriate decoding graph. A shortest path between defects has unit cost per data qubit, the Manhattan metric in the tilted check lattice. Paths may also end at either compatible boundary. For m defects, the complete matching graph includes m virtual vertices. Real-to-virtual costs are distances to the nearest compatible boundary, and virtual-to-virtual edges cost zero. The recovery is the XOR of the selected physical paths.

blossom_mwpm(n, weights) accepts an even vertex count and a symmetric row-major n × n matrix of signed i64 costs. It returns zero-based mates, the original cost as an i128, and contraction and expansion counts. The implementation grows alternating forests on tight edges, contracts odd cycles, and adjusts integer dual variables. When an inner blossom’s dual reaches zero it expands the blossom to continue the search. Contracted edges retain their original endpoints for lifting augmenting paths through nested cycles. Tests compare against exhaustive matching on complete graphs through 12 vertices and exercise nested contractions and inner expansion. Edmonds’ weighted matching algorithm

surface_sweep estimates independent, single-axis Bernoulli data-noise rates with exact syndrome extraction in the binary model. It samples uniformly within each physical error weight and reweights by binomial probabilities. This resolves rare low-rate events within the example’s shot budget. The example shows decreasing logical error at 1%, 3%, and 6%, and the reverse distance ordering at 14%. These are finite-size results near the roughly 10.3% minimum-weight code-capacity threshold. The model assumes perfect check measurements. Topological quantum memory threshold

Local ZZ merge and split

ZzSurgery.new() joins two distance-three patches along smooth edges. In the array coordinates the shared edge is horizontal. The second patch is reflected so the checkerboard continues across the seam. The two new Z checks have weights two and four, with disjoint supports. Their product is exactly the chosen joint logical ZZ. The product of the two facing X checks replaces them in the merged patch. merged_checks(axis) returns the resulting check layout.

merge(&state) measures the two seam checks and returns their outcome bits in signs and their XOR in m_zz. split(&state) remeasures the original boundary Z checks, restores the two facing X checks, and applies a local seam Pauli to fix both X signs if needed. These operations model an ideal measurement round.

The tests verify both branches on logical |+⟩|+⟩. The final state has joint ZZ eigenvalue (-1)^m_zz, joint XX eigenvalue +1, and random individual logical Z outcomes. The four logical computational inputs give m_ZZ values 0, 1, 1, 0. Surface code quantum computing by lattice surgery

15-to-1 magic-state factory

ReedMuller.new() constructs the [[15, 1, 3]] punctured quantum Reed–Muller code. Indexing the wires by the nonzero four-bit column labels gives the same punctured RM(1, 4)* evaluation matrix used in T-count optimization. The four degree-one rows are weight-eight X checks. Adding the constant row gives the logical coset, whose minimum X weight is seven. The ten independent Z checks span the even part of the simplex dual. Labels 1, 2, and 3 give a weight-three logical Z. Every single Z error has a different nonzero syndrome and all 105 pairs are detected. Bravyi and Kitaev’s distillation protocol

distillation_enumerator() walks all 32,768 masks in Gray-code order and counts detected, accepted trivial, and accepted logical masks by weight. The nonzero accepted counts at weights 0 through 15 are

Weight 0 3 4 5 6 7 8 9 10 11 12 15
Trivial 1 0 105 0 280 0 435 0 168 0 35 0
Logical Z 0 35 0 168 0 435 0 280 0 105 0 1

Each weight-w contribution is its count times p^w (1-p)^(15-w). enumerator.rate(p) returns total acceptance and accepted logical probability divided by acceptance, giving p_out = 35 p³ + O(p⁴). At p = 0.01 this is 3.60877e-5 after one round, about 277-fold suppression, and 1.64510e-12 after a second round with independent accepted inputs. distillation_sweep cross-checks these probabilities with seeded, weight-stratified Monte Carlo.

distillation_circuit(error_mask) records the five-wire parity-rotation form with 15 injected T† rotations. An input Z fault flips the associated parity phase. Wires 1–4 contain the acceptance syndrome in the computational basis, and wire 0 contains the candidate magic state. The CpuSim tests check both the recorded syndrome and the complex amplitudes of the candidate. Five-wire parity-rotation circuit

ASL quantum double exchange

parse_asl_double(text) imports ASL’s conjugated crossing convention and returns owned AnyonData. Version 1 is whitespace-delimited. Blank lines and lines starting with # are ignored. The first two records are asl-double 1 and group GROUP_ORDER CONDUCTOR SECTOR_COUNT. The remaining records are written in the order below.

Record Fields after the tag
sector id name flux dim twist_exp dual
s Row ID, then one numerator:exponent per sector
f Row ID, then one fusion product per sector. Each product is a comma-separated list of sector:multiplicity terms
boundary Name, then space-separated sector:multiplicity terms

Sector zero is the vacuum. All numbered rows start at zero and are consecutive. Terms within a product or condensate have ascending sector IDs. An S entry is numerator × ζ^exponent / GROUP_ORDER, where ζ is the positive primitive root at the specified conductor. The parser checks dimensions, duals, S² = C, twist balancing, Verlinde fusion, and condensate invariance with exact integer polynomial arithmetic modulo the cyclotomic polynomial. Version 1 supports |G|, conductor, and sector count through 256.

toric_data() has sectors 1, e, m, psi at conductor two. Its condensates are {1, e} on a rough boundary and {1, m} on a smooth boundary. double_semion_data() has sectors 1, s, sbar, b at conductor four, with twist exponents 0, 1, 3, 0. twisted_z3_data() matches the carry cocycle in ASL’s examples/twisted-drinfeld-double.asl. Its nine sectors have conductor nine. The unit flux x is sector 3 and its cube is the charge at sector 2. Its ninth power is the vacuum.

ToricCode.from_anyons(data, L) uses fluxes, twists, fusion and mutual braiding to identify the electric and magnetic sectors. It places qubits on the 2L² edges of the torus and constructs X stars and Z plaquettes. At L = 3 the two sets each have rank eight. The two logical Pauli pairs have weight-three representatives, giving [[18, 2, 3]]. A Z chain excites its endpoint stars and an X chain excites its endpoint plaquettes. braiding_phase uses a control qubit on the Tableau to measure the string commutator. A single crossing gives −1, equal to S(e,m)/S(1,1). Kitaev’s quantum double model

PlanarCode.from_anyons(data, d) uses the rough and smooth condensates to compile the corresponding vertical and horizontal sides of the open square lattice, with d² + (d-1)² data wires. boundary_string starts an electric string on the rough side or a magnetic string on the smooth side. A partial string excites the interior end, and a complete string to the opposite matching side has zero syndrome.

The library, example and tests use the bundled snapshots. With a sibling ASL checkout, python3 scripts/asl_qec_bridge.py queries the ASL interpreter for each sector, exact S entry, fusion product and condensate and compares them with the snapshots. Pass --asl-bin to select an existing interpreter.

Esc