Fall 2026 · 03 · qec-discovery 0.1.0
Quantum error correction
Searches for small Clifford encoder circuits that define stabilizer codes, then scores those codes under noise.
The wired check
The source is ai-unc/qec.
The concrete check already in that repository is the 3-qubit bit-flip
repetition code. Three physical qubits protect one logical qubit.
Independent bit-flip noise is set at p = 0.05, and the logical
error rate is 0.00725, or 0.725 percent. That target is
3 p² (1 − p) + p³, the chance of two or three bit flips.
runs/ and results/ are gitignored output.
Install
Python is 3.10 or newer. numpy is required. stim,
jax, flax, and optax are optional
extras, and only some paths import them.
pip install -e ".[stim,sampling,train,model,dev]"
PYTHONPATH=src python -m unittest discover -s tests -v
PYTHONPATH=src python3 -m unittest discover -s src/qec_discovery/transformer/tests -v
python scripts/demo_pipeline.py walks the reference circuit.
python scripts/discover.py runs the separate [[3,1]]
baseline and writes results/baseline.csv. It needs Stim.
The search command line is python -m qec_discovery.search, with
sample, search, and benchmark.
Conventions
Ancillas come first. Of n qubits, wires 0 .. n−k−1
start in |0⟩ and carry the checks +Z. The last
k wires hold the logical data. A circuit is an encoder
U. Each starting operator P is pushed through as
P → U P U†. That produces exactly n−k stabilizer
generators, plus one logical X and one logical Z
per data qubit. Using all n checks would describe one stabilizer
state, where this package is building an [[n,k]] code.
Pauli strings are signed, with qubit 0 on the left: "+XIZ".
The sign is optional on input and always present on output. The binary form
used by the Pauli package is a length-2n vector
[x | z]: the x bit is 1 for X or
Y, and the z bit is 1 for Z or
Y. Signs are dropped.
Gates are only H, S, and CNOT. A CNOT
is (control, target). The same circuit can appear as tuples
("CNOT", 2, 0), as a GateOp or
CircuitCandidate, or as an integer row [id, a, b]
with ids 0=H, 1=S, 2=CNOT and
b=−1 for one-qubit gates.
clifford/adapters.py reads all three by duck typing and does
not import the search package. ROW_CNOT_ORDER is the single
place that decides integer-row CNOT operand order. A comment there says
that order was still open.
The reference encoder is [("CNOT", 2, 0), ("CNOT", 2, 1)].
Qubit 2 is the data qubit. Copying it onto ancillas 0 and 1 produces the
checks ZIZ and IZZ, which protect the same way as
the textbook ZZI and IZZ.
Packages
Pauli arithmetic
src/qec_discovery/pauli/ is binary symplectic arithmetic.
pauli_to_bin converts a letter string.
commutation_check returns 0 for commute and 1 for anticommute.
gen_ind is meant to reject a set if any pair anticommutes or
any generator is a product of the others. validate_code is
gen_ind. group_member(error, group) appends the
error and returns true when the set stops being independent, which means
the error was already in the group. group_member re-parses a
Pauli string. The group must already be binary vectors.
gen_ind.py has a known bug. Its commutation loop is
range(index + 1, m − 1), so the last generator is never
compared with the others. For two generators, no pair is checked at all.
The row reduction used for independence is also nonstandard.
tests/test_pauli.py only checks a few commuting independent
sets, including ZZI and IZZ, so those tests pass.
search/baseline.py does not trust validate_code.
It recomputes GF(2) rank and commutation itself. Root qec.py
still calls validate_code, so it can accept a two-generator
set that anticommutes.
Clifford conjugation
src/qec_discovery/clifford/ conjugates Paulis through gates.
pauli_gates.py is the in-house lookup table for
H, S, and CNOT, including signs.
pauli_gates_stim.py is the same interface implemented with Stim.
CNOT becomes Stim CX. That file is a cross-check,
and it is not the default. simulate.py uses the in-house table.
simulate_circuit returns the n−k signed checks,
logical_operators returns the logical X and
Z lists, and encode returns all three. Bad
circuits raise ValueError.
adapters.py converts circuits into internal tuples, drops signs
with unsigned, and builds the [x | z] arrays with
to_binary. Signs must be stripped before syndrome calculation.
calculate_syndrome compares raw characters, so a leading
+ shifts every letter.
Noise
src/qec_discovery/noise/ scores errors. models.py
defines independent per-qubit noise. Bit-flip is I: 1−p, X: p.
Phase-flip is I: 1−p, Z: p. Depolarizing is
I: 1−p and X, Y, Z: p/3 each.
error_probability multiplies those.
enumerate_pauli.py lists all 4**n Pauli strings
with NumPy. syndrome.py returns a list of 0 and 1, one bit per
check, where 1 means that check anticommutes with the error. It does not
use the binary symplectic product. It counts positions where both letters
are non-identity and differ.
decoder.py builds a lookup table from syndrome to the single
most likely Pauli, keeping the first error on a probability tie, and
decode looks a syndrome up. error_sampler.py
samples one error with JAX and is not used by either search. Importing
qec_discovery.noise does not import JAX. The package docstring
that says syndrome and decoder load the sampler is stale.
Attention
src/qec_discovery/model/attention.py is a Flax module. The class
is named MultiHeadLatentAttention, and the forward pass is
ordinary multi-head attention. q_latent_dim and
kv_latent_dim are stored and never used. Nothing in the
searches calls it.
Search
src/qec_discovery/search/ contains two unrelated stacks.
The formal Pair 4 stack is models.py, operators.py,
evaluation.py, search.py, benchmark.py,
export.py, and __main__.py. A candidate is an
immutable CircuitCandidate: n_qubits,
k_logical, and a tuple of GateOp records.
Generation draws a length uniformly from 0 through
max_length (default 8*n), then draws
H, S, or CNOT with equal type
probability, which is not equal probability over every instruction.
Mutation is one of replace, insert, or delete. Empty circuits can only
grow. Circuits at the cap can only replace or delete.
random_search draws independent candidates.
one_plus_one_search keeps one parent and accepts a strictly
better child, or an exactly tied child with probability 1/2.
Ranking is lexicographic: primary score, then fewer CNOTs, then fewer gates.
The budget counts every evaluator call. There is no cache. The archive
keeps the ten best distinct gate sequences, which is not the same as ten
equivalent codes. Seeds split into initial,
proposals, mutation, and acceptance
streams via SHA-256 of pair4:v1:<seed>:<label>.
The bundled evaluator is synthetic: negative Levenshtein distance to a
fixed six-gate sequence. It has no quantum meaning. A real evaluator must
expose objective and
evaluate(candidate) -> EvaluationResult and be loaded as
module:factory. There is no fallback to the demo. Runs write
manifest.json, history.jsonl,
summary.json, and invocation.json under
runs/. Benchmarks add per-seed folders,
best_so_far.csv, and report.html.
This stack never calls Clifford simulation, Pauli validation, or the noise
code. docs/search.md and docs/search-explanation.md
describe this stack only. They are accurate for it, and they are a stale
description of the whole repository.
examples/depolarizing.json is a proposed experiment request.
It asks for harmful-undetected probability, depolarizing p=0.01,
post-encoding data noise, ideal syndromes, exact enumeration, first
(n,k)=(4,2) and then (5,1). The factory name
team_evaluator:make does not exist. That documented metric is
decoder-independent. It sums p(E) over Paulis that commute with
every check and are outside the stabilizer group. It is a different number
from the logical failure rate computed by the scripts below.
The small helpers random_circuits.py and
mutations.py build and edit plain gate lists. Root
qec.py uses random_gate from there. They are not
the Pair 4 operators.
search/baseline.py is a separate [[3,1]] search.
It seeds checks ZII and IZI, conjugates them with
the Stim gate functions, validates with its own rank and commutation code,
deduplicates by the full stabilizer span, and scores with a coset-aware
maximum-likelihood decoder. For each syndrome it groups probability by
stabilizer coset and assumes the decoder can save only the most likely
coset. It checks that ZZI and IZZ score
0.00725 before searching.
What qec.py does
Root qec.py is a teaching script, and it is not the package
entry point. It does not use the Pair 4 search or the baseline scorer. It
simulates the reference circuit with simulate_circuit, strips
signs, and checks validity with validate_code. It then
enumerates all 64 Paulis, builds the single-error lookup decoder, multiplies
error by correction with pauli_product (phase dropped), and
counts a failure when the leftover is outside the stabilizer group via
group_member.
It prints syndromes for XII, IXI, and
IIX, scores bit-flip and phase-flip noise, and aborts if the
bit-flip rate is not 0.00725. Phase-flip is expected to be
worse than 5 percent, because these checks catch X and a single
Z can still be logical, so the distance is 1. It then draws
1000 depth-12 circuits with seed 42, drops invalid and duplicate check
lists, ranks by that logical error rate, and expects the reference checks
to appear.
Its decoder can disagree with baseline.py on other codes,
because it picks one Pauli per syndrome and breaks ties by enumeration
order. On this repetition code both scorers hit 0.00725.
python scripts/demo_pipeline.py is a shorter walk of the same
reference circuit through encode, unsigned syndromes, and a
check that to_binary matches pauli_to_bin.
Still open
There is no Evaluator that connects a
CircuitCandidate to Clifford simulation and a noise score, so
the Pair 4 benchmark cannot yet run a scientific objective. There are no
tests for Clifford conjugation, syndromes, decoding, qec.py, or
baseline.py. Populations, crossover, code-equivalence
simplification inside Pair 4, hardware connectivity, and sampled scoring
are still deferred.
A separate Pair 5 package, qec_discovery/transformer, is in
the repository. It is a JAX and Flax decoder-only model that can emit
Clifford gate sequences and score unsigned stabilizer generators through
evaluate_candidate_generators. On ZZI and
IZZ that bridge returns a valid code and a logical error rate
near 0.00725. training.py is a synthetic MLP
sandbox: fake regression data, Optax Adam, 200 steps. It does not train the
transformer. Pauli-token training waits on the stabilizer tokenizer, and
logical error rate is not used as a differentiable loss.
PYTHONPATH=src python3 -m qec_discovery.transformer.evaluate_generated --seed 0
PYTHONPATH=src python3 -m qec_discovery.transformer.training
AI@UNC