Build a CSS code and check distance
Define your own parity checks, generate a noisy memory circuit and test a logical-distance bound.
1. Define X and Z checks
CSS codes use two binary parity-check matrices, Hx and Hz. Each row specifies one stabilizer; columns identify data qubits. Valid checks commute when Hx · Hzᵀ = 0 over GF(2). The encoded-qubit count is n − rank(Hx) − rank(Hz).
Define the seven-qubit Steane checks explicitly. Here both matrices have three independent rows, all X/Z overlaps have even weight, and k = 7 − 3 − 3 = 1. Replace these files with your own commuting checks to construct another code.
cat > hx.json <<'JSON'
{"format":"sparse_rows","num_cols":7,"rows":[[0,3,5,6],[1,3,4,6],[2,4,5,6]]}
JSON
cp hx.json hz.json
Row indices are zero-based. An omitted column is 0; a listed column is 1. You can export a built-in family with qec-code code css export steane hx, or look up construction specifications in the qec-code reference.
2. Generate the memory circuit
The direct rstim gen interface accepts custom CSS matrices. Its options use underscores; the structured circuit gen interface does not currently expose these custom matrix arguments.
rstim gen --code css --task memory \
--hx hx.json --hz hz.json --basis z --rounds 3 \
--after_clifford_depolarization 0.002 --out css-memory.stim
rstim circuit stats --in css-memory.stim
instruction_count: 229
repeat_blocks: 0
max_repeat_depth: 0
num_qubits: 13
num_measurements: 25
num_detectors: 18
num_observables: 1
num_ticks: 49
num_sweep_bits: 0
Seven data qubits and six check ancillas produce 18 detector events over three rounds, followed by seven data measurements. Generation rejects mismatched dimensions or noncommuting checks.
3. Sample and export a model
rstim circuit detect --in css-memory.stim --shots 128 --seed 7 \
--out-format b8 --out css-detectors.b8 \
--obs-out css-answers.b8 --obs-out-format b8
rstim circuit dem --in css-memory.stim --out css-model.dem
Keep the detector rows, DEM and private answers together. A CSS code is not necessarily graphlike: matching needs each decomposed error component to involve at most two detectors. For non-graphlike models use a suitable BP-based or solver backend rather than silently discarding correlations.
4. Check a distance bound
qec-code code css-distance randomized-upper-bound \
--hx hx.json --hz hz.json --iterations 100 --seed 7 --json
The seeded run finds a weight-3 X-like witness and reports bound_type: upper. It proves distance ≤ 3 for these matrices; it does not certify exact distance.
For the built-in small Steane code, run a separate exhaustive search:
qec-code code steane distance
distance: 3
logical_class: XLike
witness: x=[1, 1, 0, 1, 0, 0, 0] z=[0, 0, 0, 0, 0, 0, 0] weight=3
This exact result rules out lower weights. Larger custom exact searches require an optional ILP backend; see exact-search inputs and solver options. The random-window benchmark pipeline is local-only workflow evidence, rather than a published distance claim.