# Neural quantum error correction

A neural network trained to decode the surface code, the most widely studied quantum error-correcting code. It reads
the measured syndrome and decides which correction to apply. It is trained only on simulated noise and is compared
with standard decoders.

## Status (23 November 2026)

- **Prototype.** One plain fully connected network per distance and noise model, written in numpy and trained on CPU.
  The work was done on 18 November 2026. Code, weights and results are all in this folder.
- **Not pre-registered.** There is no frozen protocol: the results were written after the runs. This folder is not
  part of the claim ledger in `../operational-math/GROUNDED.md`.
- **Each network was trained once, with one seed.** There is no estimate of how much results vary between training
  runs. Each point in the tables below is one evaluation of 200 000 shots.
- **What holds up.** At d = 3 the network matches exact maximum likelihood to within sampling noise. Under
  code-capacity noise it beats matching that decodes X and Z independently. At circuit level it beats matching at
  d = 3, but not at d = 5.
- **Corrected after the 23 November audit.** The circuit-level d = 5, p = 0.002 advantage ("13% fewer errors") did
  not reproduce on fresh shots; see [Re-evaluation on fresh shots](#re-evaluation-on-fresh-shots-added-23-november-2026).
  The baseline wording, the threshold sentence and the test runtime were also corrected.

## What is here

| file | role |
|---|---|
| `surface_code.py` | rotated surface code of any odd distance d: stabilizers, logical operators, depolarizing noise, syndromes, and the decoding task as a 4-way classification of the logical class; reference decoders (exhaustive maximum likelihood for d = 3, minimum-weight matching via PyMatching) |
| `neural_decoder.py` | the network: a multilayer perceptron in numpy (ReLU, softmax, cross-entropy, Adam, cosine learning rate), trained on freshly simulated errors so it never sees a sample twice |
| `run_code_capacity.py` | trains and evaluates decoders for d = 3, 5, 7 under depolarizing code-capacity noise |
| `circuit_level.py` | trains and evaluates decoders on full noisy syndrome-extraction circuits generated by Stim |
| `fresh_check.py` | reloads the saved weights and re-scores network and matching on the same fresh shots, with a paired sign test (added 23 November 2026) |
| `test_neural_qec.py` | checks: code invariants, single-qubit errors, and a short training run that must beat matching |
| `weights/` | trained networks (`d<d>.npz` code capacity, `circuit_d<d>.npz` circuit level) |
| `code_capacity_results.json`, `circuit_level_results.json` | logical error rates, training settings and loss histories |
| `fresh_check_results.json` | output of `fresh_check.py` |
| `test_neural_qec.log` | output of the test run in the 23 November audit, with its run time |

How decoding works: a syndrome fixes the error up to a stabilizer and one of four logical operators (I, X̄, Z̄, Ȳ).
The code computes a fixed "pure error" for each syndrome, and the network predicts which logical operator to apply on
top of it. A decoder fails when it picks the wrong one.

**Networks** (layer widths from the weight files; training settings from the results files):

| network | layers | parameters | training steps × batch | training time, 4 CPU cores |
|---|---|---|---|---|
| code capacity, d = 3 | 8-128-128-4 | 18 180 | 4 000 × 2 048 | 16 s |
| code capacity, d = 5 | 24-512-512-256-4 | 407 812 | 20 000 × 4 096 | 21 min |
| code capacity, d = 7 | 48-1024-1024-512-4 | 1 626 628 | 20 000 × 4 096 | 48 min |
| circuit level, d = 3 | 24-256-256-2 | 72 706 | 10 000 × 4 096 | 2.4 min |
| circuit level, d = 5 | 120-1024-1024-512-2 | 1 699 330 | 20 000 × 4 096 | 46 min |

Code-capacity networks are trained on error rates drawn uniformly from 0.01–0.2. Circuit-level networks are trained
on p = 0.002–0.012.

## Results

Logical error rate per shot on 200 000 shots per point, independent of the training data.

### Code-capacity depolarizing noise (perfect syndrome measurement)

The matching baseline decodes the X and Z parts **independently**, with two PyMatching graphs. It therefore ignores
the correlation between X and Z errors that depolarizing noise creates (a Y error is both). Any decoder that models
that correlation is expected to beat it. For d ≥ 5 no stronger baseline was run: no correlated matching, BP-OSD,
tensor-network maximum likelihood, or published neural decoder.

Distance 3, against the provably optimal decoder:

| p | no correction | matching | neural network | exact maximum likelihood |
|---|---|---|---|---|
| 0.02 | 7.1% | 0.60% | 0.51% | 0.50% |
| 0.04 | 13.5% | 2.21% | 1.92% | 1.91% |
| 0.08 | 25.1% | 7.86% | 6.91% | 6.88% |
| 0.12 | 34.9% | 15.2% | 13.9% | 13.8% |
| 0.16 | 43.1% | 23.7% | 21.9% | 21.9% |

Distance 5:

| p | no correction | matching | neural network | fewer errors than matching |
|---|---|---|---|---|
| 0.02 | 11.7% | 0.135% | 0.076% | 44% |
| 0.04 | 21.8% | 0.88% | 0.53% | 39% |
| 0.08 | 37.3% | 5.64% | 3.85% | 32% |
| 0.12 | 48.8% | 14.3% | 11.0% | 23% |
| 0.16 | 57.0% | 25.4% | 20.9% | 18% |

Distance 7:

| p | no correction | matching | neural network | fewer errors than matching |
|---|---|---|---|---|
| 0.02 | 16.1% | 0.026% | 0.012% | 54% (24 vs 52 failures; ±20% statistical) |
| 0.04 | 28.7% | 0.34% | 0.21% | 38% |
| 0.08 | 46.7% | 4.01% | 2.59% | 35% |
| 0.12 | 57.7% | 13.2% | 9.99% | 24% |
| 0.16 | 64.5% | 26.6% | 22.1% | 17% |

- **d = 3: maximum-likelihood level.** The network is within 0.04 percentage points of exact maximum likelihood at
  every p evaluated (at most 2% relative, at p = 0.02). At p = 0.06 and 0.14 it is slightly *below* maximum
  likelihood on the same shots, which is sampling noise. It makes 7.5–14.3% fewer errors than independent matching.
- **Why it beats matching:** it learns the X–Z correlation that independent matching ignores. The comparison shows
  that the network learns this noise model. It does not show that it beats strong decoders.
- **Larger codes do better below threshold:** at p = 0.04 the network's error is 1.92% at d = 3, 0.53% at d = 5 and
  0.21% at d = 7.
- **Where the curves cross:**
  - at p = 0.14 the d = 7 network is still the best (15.7%, against 15.9% for d = 5 and 17.7% for d = 3);
  - at p = 0.16 it is not (22.1%, against 20.9% and 21.9%).

  With one trained network per distance, this puts the crossing of *these networks* between p = 0.14 and 0.16. It
  is not a threshold estimate with error bars. Matching's own d = 5 and d = 7 curves are already almost equal at
  p = 0.14 (19.9% and 19.7%), so the gain in threshold over matching is small. The optimal threshold for this noise
  is about 0.19 in the literature; that value was not computed here.

### Circuit-level noise (Stim surface-code memory, d rounds)

Noise on every gate, reset and measurement and on idle data qubits, all at rate p. The network reads the detection
events over all rounds and predicts whether the logical observable flipped. The baseline is PyMatching on the
circuit's detector error model (`decompose_errors=True`, the standard matching decoder without X–Z correlations).

Distance 3 (24 detection events per shot):

| p | no correction | matching | neural network | fewer errors than matching |
|---|---|---|---|---|
| 0.002 | 4.4% | 0.298% | 0.253% | 15% |
| 0.004 | 8.4% | 1.11% | 0.97% | 13% |
| 0.006 | 12.2% | 2.38% | 2.11% | 11% |
| 0.008 | 15.7% | 4.03% | 3.66% | 9% |
| 0.010 | 18.6% | 5.88% | 5.48% | 7% |

Distance 5 (120 detection events per shot):

| p | no correction | matching | neural network | relative to matching |
|---|---|---|---|---|
| 0.002 | 10.9% | 0.117% | 0.102% | 13% fewer on these shots; **not reproduced** on 400 000 fresh shots (0.103% vs 0.105%); a tie within noise, see below |
| 0.004 | 19.2% | 0.74% | 0.84% | 14% more |
| 0.006 | 26.2% | 2.29% | 2.70% | 18% more |
| 0.008 | 31.3% | 4.91% | 5.94% | 21% more |
| 0.010 | 35.5% | 8.49% | 10.07% | 19% more |

At circuit level the network beats matching at d = 3. At d = 5 it ties matching at p = 0.002 and is 14–21% worse
above that. There a plain fully connected network has to learn the space-time structure of 120 detection events
from scratch in 20 000 training steps, and it falls short. It still corrects the large majority of errors.

Matching is given the circuit's detector error model directly as edge weights. The network learns the same noise
model only from samples: it is trained on that same circuit at six error rates.

### Re-evaluation on fresh shots (added 23 November 2026)

`fresh_check.py` reloads the saved weights and scores network and matching on the *same* shots, so the comparison is
paired. A two-sided sign test is applied to the shots that only one of the two decoders got wrong. Output is in
`fresh_check_results.json`.

| point | shots | matching | network | only network wrong | only matching wrong | sign test p |
|---|---|---|---|---|---|---|
| circuit d = 5, p = 0.002, original seed (re-scored) | 200 000 | 234 (0.117%) | 204 (0.102%) | 71 | 101 | 0.027 |
| circuit d = 5, p = 0.002, fresh seed | 400 000 | 411 (0.103%) | 420 (0.105%) | 173 | 164 | 0.66 |
| circuit d = 5, p = 0.002, both samples pooled | 600 000 | 645 | 624 | 244 | 265 | 0.38 |
| circuit d = 5, p = 0.004, original seed (re-scored) | 200 000 | 1 482 (0.741%) | 1 687 (0.844%) | 788 | 583 | 3 × 10⁻⁸ |
| circuit d = 5, p = 0.006, fresh seed | 400 000 | 9 231 (2.31%) | 10 864 (2.72%) | 5 203 | 3 570 | < 10⁻⁶⁰ |
| circuit d = 3, p = 0.004, fresh seed | 400 000 | 4 521 (1.13%) | 3 891 (0.97%) | 572 | 1 202 | < 10⁻⁵⁰ |
| code capacity d = 3, p = 0.08, fresh seed | 200 000 | 7.75% | 6.87% (max. likelihood 6.87%) | 2 658 | 4 421 | < 10⁻⁹⁰ |
| code capacity d = 5, p = 0.08, fresh seed | 200 000 | 5.52% | 3.88% | 1 829 | 5 122 | < 10⁻³⁰⁰ |
| code capacity d = 7, p = 0.08, fresh seed | 200 000 | 3.96% | 2.56% | 1 430 | 4 232 | < 10⁻³⁰⁰ |

- **The original seed reproduces exactly.** Re-scoring it gives the same 204 vs 234 failures as the README table.
- **The d = 5, p = 0.002 advantage did not replicate.** On its own sample it looked borderline (p = 0.027). On
  400 000 fresh shots the network was 2% *worse* (p = 0.66). Pooled over 600 000 shots, the difference (3% fewer
  failures for the network) is within noise (p = 0.38). Treat network and matching as tied at this point.
- **Every other re-evaluated point reproduces** in direction and size: the code-capacity points, circuit-level d = 3, and
  circuit-level d = 5 at p ≥ 0.004.

## Running

```
pip install numpy pymatching stim
python -m unittest test_neural_qec           # about 2 minutes: it includes a short training run
OMP_NUM_THREADS=4 python fresh_check.py      # about a minute; re-scores the saved weights
OMP_NUM_THREADS=4 python run_code_capacity.py 3 5 7
OMP_NUM_THREADS=4 python circuit_level.py 3 5
```

The tests took 121 s on a shared 4-core machine in the 23 November audit (`test_neural_qec.log`). Earlier versions
of this README said about 7 s, and the test file said about 30 s. The training times in the networks table are from
the results files.

## Limits

- The networks are plain fully connected perceptrons, one per distance and noise model. They do not transfer between
  distances, and their size grows quickly with d. At circuit level and d = 5 the network falls behind matching above
  p = 0.002. Decoders that share weights across space and rounds (convolutional or recurrent) are reported in the
  literature to scale better; that was not tested here. Longer training might also help, but that was not tested
  either.
- Each network was trained once, with one seed. Each table point is one evaluation sample, except the re-evaluated
  points above.
- The baselines are uncorrelated matching (both noise models) and, at d = 3 only, exact maximum likelihood. There is
  no comparison with correlated matching, BP-OSD, tensor-network decoders or published neural decoders.
- "Neural" here means a classical neural network decoding a quantum code. The quantum part is simulated: Pauli-frame
  noise and Stim's stabilizer circuits. No hardware data was used.
