Quantum Error Correction
ASecurityUse this skill when the user says 'stabilizer code', 'surface code circuit', 'syndrome extraction', 'decoder', 'MWPM', 'PyMatching', 'Stim', 'simulate logical error rate', 'repetition code', 'detector error model', 'code distance experiment'. Hands-on QEC engineering: stabilizer formalism, repetition/Steane/surface codes, syndrome-extraction circuits, detectors and decoding with Stim + PyMatching, and threshold/logical-error-rate experiments. Do NOT use for: conceptual overhead and milestone ...
- 46 stars
- 0 votes
- 0 copies
- 1 view
- Added November 27, 2026
Works with
Security analysis
100/100Pro scans all 2 files and shows the line behind each finding
npx -y skills add j4flmao/agent-skills --skill quantum-error-correction --agent claude-codeAre you the author of Quantum Error Correction?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/j4flmao-quantum-error-correction)---
name: quantum-error-correction
description: >
Use this skill when the user says 'stabilizer code', 'surface code circuit', 'syndrome extraction', 'decoder', 'MWPM', 'PyMatching', 'Stim', 'simulate logical error rate', 'repetition code', 'detector error model', 'code distance experiment'. Hands-on QEC engineering: stabilizer formalism, repetition/Steane/surface codes, syndrome-extraction circuits, detectors and decoding with Stim + PyMatching, and threshold/logical-error-rate experiments. Do NOT use for: conceptual overhead and milestone interpretation (fundamental-truths/quantum-error-correction), basic gates (quantum-computing/qubits-and-gates), or general Qiskit usage (quantum-computing/qiskit-framework).
version: "2.0.0"
author: "j4flmao"
license: "MIT"
tags: [quantum-computing, quantum-error-correction, surface-code, stim, pymatching, decoding]
---
# Quantum Error Correction: Codes, Circuits & Decoders
## Purpose
Build and evaluate error-correcting codes the way QEC researchers do: describe the code by its stabilizers, generate a noisy syndrome-extraction circuit, convert it into detectors and a detector error model, decode with a matching decoder, and measure the logical error rate versus distance and physical noise. Stim (fast Clifford simulation) and PyMatching (minimum-weight perfect matching) are the standard open-source tools.
## Agent Protocol
### Trigger
"stabilizer", "surface code", "repetition code", "Steane code", "syndrome extraction", "detector", "decoder", "MWPM", "PyMatching", "Stim", "logical error rate", "threshold plot".
### Input Context
- Code family and distances; noise model (circuit-level depolarizing rate p, measurement error).
- Rounds of syndrome extraction; memory (Z or X basis) experiment vs logical gates.
- Compute budget (shots).
### Output Artifact
Simulation script, logical-error-rate table/plot vs p and d, threshold estimate, and interpretation.
### Response Format
```
Code: {rotated surface code} d={3,5,7} rounds={d} basis={Z}
Noise: circuit-level depolarizing p={values}
Decoder: {PyMatching MWPM|union-find|BP-OSD}
Result: p_L per shot/round for each (d, p)
Threshold: curves cross near p ≈ {value}
```
### Completion Criteria
- [ ] Detectors and observables defined (or generated) correctly.
- [ ] Enough shots for statistically meaningful p_L (report error bars).
- [ ] Logical error normalized per round when comparing distances with different round counts.
## Stabilizer Formalism
- An [[n, k, d]] stabilizer code encodes k logical qubits into n physical qubits with distance d (corrects ⌊(d−1)/2⌋ arbitrary errors).
- The stabilizer group S is generated by commuting Pauli operators; code states are +1 eigenstates of all generators.
- Measuring generators yields the **syndrome**; errors anticommuting with a generator flip its outcome. Logical operators commute with S but are not in S.
| Code | Parameters | Notes |
|---|---|---|
| 3-qubit bit-flip (repetition) | [[3,1,1]] | Corrects one X error; Z errors go undetected (distance 1 as a quantum code) |
| Shor code | [[9,1,3]] | First full QEC code (1995) |
| Steane code | [[7,1,3]] | CSS code; transversal Clifford gates |
| Rotated surface code | [[d², 1, d]] | 2D nearest-neighbour; d² − 1 measure qubits; threshold ≈ 1% (circuit noise) |
| Toric code | [[2d², 2, d]] | Periodic boundaries; star (Aᵥ = ∏X) and plaquette (B_p = ∏Z) stabilizers |
| qLDPC (e.g. bivariate bicycle) | e.g. [[144,12,12]] | Lower overhead, needs long-range connectivity |
## Syndrome Extraction
- Z-type stabilizer: ancilla in |0⟩, CNOTs from each data qubit to the ancilla, measure Z.
- X-type stabilizer: ancilla in |+⟩ (H), CNOTs from ancilla to data, H, measure.
- CNOT ordering matters: the "hook" error ordering in surface codes is chosen so ancilla faults do not reduce effective distance.
- Repeat for ~d rounds; a **detector** is the XOR of consecutive measurements of the same stabilizer (should be 0 with no errors), which also handles measurement errors.
## Simulation with Stim + PyMatching
```python
from __future__ import annotations
import numpy as np
import pymatching
import stim
def logical_error_rate(distance: int, p: float, shots: int = 100_000) -> float:
circuit = stim.Circuit.generated(
"surface_code:rotated_memory_z",
distance=distance,
rounds=distance,
after_clifford_depolarization=p,
before_round_data_depolarization=p,
before_measure_flip_probability=p,
after_reset_flip_probability=p,
)
dem = circuit.detector_error_model(decompose_errors=True)
matcher = pymatching.Matching.from_detector_error_model(dem)
sampler = circuit.compile_detector_sampler()
detections, observables = sampler.sample(shots, separate_observables=True)
predictions = matcher.decode_batch(detections)
failures = np.any(predictions != observables, axis=1)
return float(failures.mean())
for p in (0.002, 0.005, 0.008, 0.012):
row = {d: logical_error_rate(d, p) for d in (3, 5, 7)}
print(p, row)
# Below threshold, p_L drops as d grows; above threshold it rises. The crossing estimates p_th.
```
For production-scale sweeps, use `sinter` (bundled with Stim's ecosystem) to parallelize sampling and stop at a target number of errors. Report per-round error: p_round = 1 − (1 − p_L)^(1/rounds).
## Repetition Code by Hand (to understand detectors)
```python
import stim
c = stim.Circuit.generated("repetition_code:memory", distance=5, rounds=5,
before_round_data_depolarization=0.03,
before_measure_flip_probability=0.01)
print(c.num_detectors, c.num_observables) # detectors = stabilizer comparisons over rounds
print(c.diagram("timeline-text"))
```
## Decoders
| Decoder | Idea | Trade-off |
|---|---|---|
| MWPM (PyMatching) | Pair detection events with minimum total weight on the matching graph | Accurate for surface codes; fast implementations |
| Union-Find | Grow clusters, peel corrections | Near-linear time; slightly lower accuracy |
| Correlated / belief-matching | Uses X-Z correlations | Better accuracy, more compute |
| BP-OSD | Belief propagation + ordered statistics | Common for qLDPC codes |
| Neural decoders (e.g. AlphaQubit) | Learned from data | Adapt to real noise; latency/training cost |
Real hardware needs decoders that keep up with the syndrome rate (around a microsecond per round for superconducting qubits); otherwise a backlog grows.
## Anti-Patterns
- Comparing p_L across distances with different round counts without per-round normalization.
- Too few shots (zero observed failures is not p_L = 0).
- Code-capacity noise (data errors only) presented as realistic thresholds; circuit-level noise thresholds are lower.
- Forgetting measurement errors (single-round decoding).
## Checklist
- [ ] Noise model stated (code-capacity, phenomenological, or circuit-level).
- [ ] Detectors/observables validated (e.g. zero detections with p = 0).
- [ ] Sweep over d and p with error bars.
- [ ] Threshold and Λ reported with assumptions.
- [ ] Decoder choice and runtime noted.
## References
- Gottesman, "Stabilizer Codes and Quantum Error Correction" (PhD thesis, 1997).
- Dennis, Kitaev, Landahl & Preskill, "Topological quantum memory" (2002); Fowler et al., "Surface codes" (PRA 2012).
- Gidney, "Stim: a fast stabilizer circuit simulator" (Quantum, 2021); Higgott & Gidney, "Sparse Blossom" (PyMatching v2, 2023).
- Conceptual companion: `fundamental-truths/quantum-error-correction` skill.
### Merged Material
Content folded in from overlapping skills and notes.
- `references/merged-fundamental-quantum-error-correction.md` — Quantum Error Correction: Foundations (merged from `fundamental-truths/quantum-error-correction`)
Files in this skill
- SKILL.md
- references/merged-fundamental-quantum-error-correction.md
Attribution
Related skills
View all in ai-agents →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.