Step 1 of 8
The repetition code
Quantum Error Correction in Practice, part 1: Errors and decoding
A qubit left alone does not stay put. Now and then something in its surroundings flips it, |0⟩ to |1⟩ or back, and nothing tells you that it happened. Error correction starts from a simple idea: store one bit in several qubits, so that one flip can be outvoted. This page builds that idea, the repetition code, and measures how well it works.
One bit, written d times
To store a 0, set d qubits to |0⟩|0⟩…|0⟩. To store a 1, set them all to |1⟩. The number d is the distance of the code: the fewest flips that turn one codeword into the other. For a quantum state a|0⟩ + b|1⟩ the encoding gives a|00…0⟩ + b|11…1⟩. That is not d copies of the state, which quantum mechanics forbids; it is one state spread over d qubits.
Now the catch. To find a flipped qubit you might read every qubit and take the majority. On a quantum computer that reading destroys the superposition you were trying to protect. The way out is to ask a weaker question. A parity check compares two neighbours and reports only whether they agree. Written as an operator it is ZiZi+1, with outcome +1 when the two qubits agree and −1 when they differ. Both |00…0⟩ and |11…1⟩ give +1 on every check, so measuring the checks says nothing about a or b and leaves the stored state alone.
The list of check outcomes is the syndrome. A check that reports −1 has fired. In the figure below, click a qubit (or move to it with the arrow keys and press Enter) to flip it, and watch which checks fire. Try a single flip in the middle, one at the end, and then two or three neighbours in a row.
check, quiet check fired flipped qubit
This figure needs JavaScript.
A single flip in the middle fires the two checks on either side of it. A flip on an end qubit fires only one check, because there is no neighbour on the outside. A run of flipped neighbours fires only the checks at its two ends: inside the run, neighbours still agree. The syndrome shows where an error begins and ends, not what lies between.
Decoding: a guess from the syndrome
A decoder turns the syndrome into a correction: a set of qubits to flip back. Every syndrome of the repetition code has exactly two explanations, and they are complements of each other. If flipping the qubits in a set E reproduces the syndrome, so does flipping every qubit outside E. One explanation flips k qubits and the other flips d − k. When each qubit flips on its own with a probability p below 1/2, the explanation with fewer flips is the more likely one, so the decoder picks it.
The decoder on this page is a union-find decoder, the one the decoding and threshold pages use; Errors, syndromes and decoding introduces it. Lesson 1, next, corrects with a simpler lookup table instead. On a line of qubits it gives the same answer as a majority vote; the course's tests check this for every error pattern at d = 3, 5 and 7. Flip some qubits, or draw random flips at rate p, and press Decode. Then try to make it fail: at d = 3, flip two qubits; at d = 5, three.
check fired flipped qubit decoder flips it back
This figure needs JavaScript.
With up to (d − 1)/2 flips the decoder always restores 0. With (d + 1)/2 flips or more, the complement has fewer flips, the decoder chooses it, and the correction completes the error: every qubit ends up reading 1. The checks are all quiet again, but the stored bit has changed. That is a logical error, and no decoder can avoid it, because the syndrome is the same whichever of the two explanations is true.
Does it help?
So the code fails when more than half of its qubits flip. With independent flips at rate p, that happens with probability
PL(d, p) = ∑k > d/2 C(d, k) pk (1 − p)d−k,
the chance that a binomial count of flips passes d/2. For d = 3 this is 3p2 − 2p3, which is smaller than p whenever p is below 1/2. The plot draws this formula as lines, with d = 1 (a bare qubit, no code) for comparison. Press Run to add points from a Monte Carlo simulation: the engine draws random flips, decodes each sample and counts the failures, in a background thread so the page stays responsive.
This figure needs JavaScript.
The points sit on the lines, within their error bars. Every curve passes through PL = 1/2 at p = 1/2, where a flipped qubit is as likely as an unflipped one and the syndrome carries no information about the stored bit. Below p = 1/2 the curves fan out: more qubits give a lower logical error rate, and the gap grows quickly as p falls, because PL scales like p(d+1)/2 for small p. Above p = 1/2 more qubits make things worse. A point where adding qubits switches from hurting to helping is called a threshold; for the repetition code against independent bit flips it is p = 1/2.
What the repetition code cannot do
Bit flips are not the only error a qubit suffers. A phase flip, the operator Z, leaves |0⟩ alone and sends |1⟩ to −|1⟩. A single phase flip on any one of the d qubits turns a|00…0⟩ + b|11…1⟩ into a|00…0⟩ − b|11…1⟩. Every parity check still reports +1, so nothing fires, and the stored state is wrong. Against phase flips the repetition code is worse than a bare qubit: it offers d places for the error to land instead of one.
A quantum code has to catch both kinds at once. The surface code does it with two families of checks laid out on a grid, one for bit flips and one for phase flips, so that each sees the errors the other cannot. The smallest one that can correct an error holds one logical qubit in nine data qubits.
Cite this lesson T. Pandey, "The repetition code", Quantum Error Correction in Practice (2026), https://pandey-tushar.com/qec-in-practice/repetition-code.html