Where the repetition code fits
The repetition code is the smallest code that shows every moving part of quantum error correction: an encoding, parity checks, a syndrome, a decoder, a logical error rate that falls polynomially with the code size, and a memory experiment that runs for many rounds. It protects against only one kind of error, so it is not a complete quantum code, but it is far from a toy. It is a building block of Shor's nine-qubit code (Shor 1995), and large repetition codes are run on hardware to measure rare error events that smaller experiments cannot see. In this lesson we work through the code exactly, then simulate it.
Throughout, code-capacity noise means: each data qubit independently suffers the error with probability , once, and the syndrome is measured perfectly. It is the simplest noise model and the only one for which the answers below are exact.
The three-qubit bit-flip code
The code space is spanned by and . Two CNOTs encode an arbitrary input on :
The parity checks are and . Both have eigenvalue on every code state, and an error on qubit flips exactly the checks that contain qubit . With for eigenvalue :
| Error pattern | Syndrome | Decoder applies | Net effect |
|---|---|---|---|
| nothing | correct | ||
| correct | |||
| correct | |||
| correct | |||
| : logical flip | |||
| logical flip | |||
| logical flip | |||
| nothing | logical flip |
Each syndrome is produced by exactly two error patterns, complements of each other: and . They differ by the logical operator , which commutes with both checks and maps to . The decoder cannot tell them apart, so it picks the lighter one, the more likely explanation when . This is minimum-weight decoding.
Deriving the logical error rate
The decoder succeeds exactly on the four patterns of weight 0 or 1 and fails on the four of weight 2 or 3. Under code-capacity noise a particular pattern of weight has probability , and there are of them, so
Worked example. At : . At : . The code helps when , that is , which holds for all (at , ).
The phase-flip code
Phase errors are invisible to the bit-flip code. Conjugating by Hadamards exchanges the roles of and , because and . The phase-flip code uses and , obtained by appending a Hadamard on every qubit to the bit-flip encoder:
Its checks are and , and a error on qubit flips the checks containing , so the syndrome table is the one above with and exchanged, and the logical error rate under independent phase flips is again . To measure an -type check onto an ancilla, prepare the ancilla in (Hadamard after reset), use it as the control of CNOTs onto the data, and measure it in the basis (Hadamard, then measure):
The ancilla picks up a phase for each data qubit in , which the final Hadamard turns into a bit. Compare this with the -check circuit of the previous lesson, where data qubits are the controls and the ancilla the target. Surface-code circuits use both patterns side by side.
Neither three-qubit code handles both error types. Shor's code concatenates them: three phase-flip blocks, each made of three bit-flip-encoded qubits, correct any single-qubit error. The next lesson describes both codes in one language, the stabilizer formalism.
The -qubit repetition code
Using qubits ( odd) with checks for gives the distance- bit-flip repetition code. The syndrome has bits, and again each syndrome is shared by exactly two error patterns, and its complement. A convenient decoder reconstructs one of them directly from the syndrome: set and ; if this candidate has weight more than , use its complement. The decoder corrects every pattern of weight at most and fails on every pattern of weight more than , so
This is the formula you will test in the exercise. A few values, computed directly from the sum:
| 0.01 | |||||
| 0.1 | |||||
| 0.3 |
import math
def p_logical(n, p):
return sum(math.comb(n, k) * p**k * (1 - p) ** (n - k) for k in range(n // 2 + 1, n + 1))
for p in (0.01, 0.1, 0.3):
print(p, [f"{p_logical(n, p):.3e}" for n in (1, 3, 5, 7, 9)])
Two things are worth reading off the table. Far below , each step multiplies by roughly a constant factor (about 30 at , about 3 at ), so falls exponentially in . Close to the gain per step is small. The distance is the smallest number of single-qubit errors that produce an undetected logical flip; the code corrects up to of them.
The distance against phase errors, however, is 1: a single acts as the logical on the bit-flip code. Quantum codes must have large distance against both types at once, which is exactly what the surface code achieves.
The repetition code as a memory
A quantum memory must hold its state for many rounds. Suppose that in every round each qubit flips with probability , after which the syndrome is measured perfectly and the decoder's correction applied. Each round is then an independent copy of the experiment above, and the logical bit is flipped at the end of round with probability , independently of other rounds. After rounds the stored bit is wrong if an odd number of rounds failed:
using . For small this is about ; for large it tends to , a completely random bit. Worked example: , , gives and . The same formula converts between "per shot" and "per round" logical error rates later in the course.
When the syndrome itself is noisy
Real syndrome measurements fail too. If a check outcome is wrong with probability , a decoder that trusts each round's syndrome would apply a spurious correction, creating an error instead of removing one. The remedy is to measure the checks repeatedly and look at changes: define a detection event in round as for each check. A data error that appears between rounds and changes the syndrome permanently and causes detection events in neighbouring checks in one round; a measurement error in round causes detection events in the same check in rounds and . Decoding becomes a problem on a two-dimensional space-time grid of detection events, solved by matching in the matching lesson, with circuits built in the Stim lesson. With qubits measured over rounds, both data errors and measurement errors need about faults to fool the decoder, so the code keeps its distance in time as well as in space.
Repetition codes are also used on hardware to probe rare, correlated errors. In a published experiment on a superconducting processor, Google Quantum AI ran a distance-25 repetition code and reported a logical error floor of per round, set by a single high-energy event ( per round when that event was excluded) (Google Quantum AI 2023). These are published values for that device and protocol; we do not reproduce them here.
Simulating code-capacity noise
A Monte Carlo simulation of the code-capacity model needs only NumPy, and the
KetQat SDK's repetition-code product provides an independent reference that
reports the exact value beside its estimate and a 95% Wilson interval (the
interval is explained in the thresholds lesson):
from ketqat_runner import catalog
record = catalog.run("[email protected]", seed=7, n=5, physical_error_rate=0.1, shots=100_000)
r = record.result
print(r["logical_failures"], r["logical_error_rate"])
print(r["confidence_interval"]["lower"], r["confidence_interval"]["upper"])
print(r["exact_logical_error_rate"], r["exact_within_interval"])
With the pinned SDK this prints 829 failures (rate 0.00829), the interval
, and the exact value , which lies inside it. The
run writes a small record under $KETQAT_HOME (default ~/.ketqat).
In the exercise you write your own vectorised simulator: sample a
(shots, n) array of flips, compute the syndromes with one XOR, decode them
with the running-XOR decoder, and count failures. You then compare against the
exact formula, the SDK product and the multi-round formula above. A good habit
starts here: never trust a simulation pipeline that has not reproduced a case
with a known answer.
Common mistakes
- Decoding from the error instead of the syndrome. A decoder that looks at the sampled error pattern is not a decoder. Your decoding function should receive only the syndrome.
- Counting weight- ties. For even some syndromes have two equally light explanations, and majority vote is undefined. Use odd , or state how ties are broken.
- Confusing "per shot" with "per round". A memory of rounds that fails with probability does not fail with probability per round; invert .
- Expecting the bit-flip code to protect against errors. Its distance for phase errors is 1.
- Using the code-capacity formula for circuit-level noise. Once measurements and gates are noisy the exact formula no longer applies; it becomes a sanity check for the noiseless-measurement limit only.
- Comparing two Monte Carlo runs that share a seed. Identical seeds give correlated samples and make differences look more precise than they are.
Summary
- The bit-flip code measures , the phase-flip code ; Hadamards map one onto the other.
- Every syndrome has exactly two explanations, an error and its complement; the minimum-weight decoder picks the lighter one.
- Under code-capacity noise for three qubits and for qubits.
- Over independent rounds the logical error is .
- Noisy syndrome measurements are handled by repeating them and decoding detection events in space and time.
Next, the stabilizer formalism gives a single language for these codes, for Shor's and Steane's codes and for the surface code. For background reading see Nielsen and Chuang, section 10.1.