Quantum Error Correction
Physical qubits experience decoherence, control errors, leakage and measurement faults. Quantum error correction encodes logical information across multiple physical systems so that sufficiently small sets of faults can be detected and corrected without directly measuring the logical state.
Three obstacles that classical coding does not face
Classical repetition coding can store 0 as 000 and use a majority vote. Quantum information introduces three additional obstacles.
- No cloning. The no-cloning theorem forbids for an arbitrary unknown state, so encoding cannot consist of making independent copies.
- Direct measurement reveals logical information. Measuring the data qubits in a basis that distinguishes the codewords would generally disturb their protected superposition.
- Errors are continuous. A qubit does not just flip; it can rotate by any small angle, or drift into any of a continuum of states. There appear to be infinitely many errors to correct.
All three have answers. The first is met by entangling rather than copying, the second by measuring only the relationships between qubits and never the qubits themselves, and the third by a fact that makes the whole field possible: measuring those relationships projects a continuous error onto a discrete one.
The three-qubit bit-flip code
Encode with two CNOTs from the data qubit onto two fresh |0⟩ ancillas:
This is not three independent copies of ; the product would contain cross terms such as . For generic nonzero and , the encoded state is entangled. For either basis input, however, the codeword is the product state or .
To detect an error without learning or , measure the two parity checks and . Each asks “do these two qubits agree?” and nothing more. Both codewords and are eigenstates of both checks, so the answer is independent of and . The pair of outcomes, called the syndrome, identifies a single bit flip:
| Diagnosis | ||
|---|---|---|
| no error | ||
| flip on qubit 0 | ||
| flip on qubit 1 | ||
| flip on qubit 2 |
Apply X to the indicated qubit and the original state returns exactly, for any and . The four syndromes are distinct, which is what makes the diagnosis unambiguous.
The code’s limits are just as instructive. Two simultaneous flips produce the syndrome of a single flip on the third qubit, so applying the single-error correction produces a logical bit flip. As a distance‑3 code, it corrects arbitrary errors on at most one qubit when used against its intended bit-flip error model. A phase error Z on any qubit commutes with both checks, so this code alone does not detect or correct phase errors.
The phase-flip code
Phase errors become bit errors in the Hadamard basis, since . Encode as before and then apply H to all three qubits, giving codewords and . The parity checks become and , the syndrome table is identical, and the correction is Z instead of X. Now single phase errors are caught and single bit flips slip through; the mirror image of the previous code.
Shor’s nine-qubit code
Nesting one code inside the other catches both. Take the phase-flip code and replace each of its three qubits with a bit-flip‑encoded triple. The result uses nine physical qubits:
The circuit is two CNOTs from the data qubit to qubits 3 and 6, a Hadamard on each of qubits 0, 3 and 6, and then two CNOTs inside each block of three.
Eight parity checks describe the code; six for bit flips ( and within each block) and two for phase flips ( and , comparing the sign of block 1 against block 2, and block 2 against block 3). Eight generators for nine qubits leaves exactly one logical qubit, matching the count . The logical operators are and : they commute with every check, so they move states around inside the code space rather than out of it, and they anticommute with each other exactly as X and Z should.
This code corrects an arbitrary error acting on any one of the nine qubits, including the Pauli errors X, Y and Z and their linear combinations.
Degeneracy
The syndromes are worth looking at closely. There are 27 single-qubit Pauli errors (three Paulis on each of nine qubits), but they produce only 21 distinct syndromes. The collisions are all of one kind: a error on any of the three qubits inside a given block yields the same syndrome as a on either of its two neighbours, so the nine possible errors give only three distinct syndromes.
That is not a defect. The three Z errors within one block have the same action on the code space up to stabilizers, which act trivially on codewords. Correcting with a representative error therefore repairs any of them. A quantum code in which distinct correctable errors have the same action on the code space is called degenerate.
Why continuous errors are not a problem
The obstacle that looked fatal dissolves here. Write an arbitrary single-qubit error as a combination of Paulis, which is always possible since spans the matrices:
Applied to a codeword, produces a linear combination of the untouched state and Pauli-corrupted states. Syndrome measurement projects onto orthogonal syndrome subspaces, removing coherence between distinguishable error classes. In a nondegenerate case with a normalized coherent error and distinct syndromes, branch probabilities are ; in a degenerate code, coefficients belonging to the same syndrome combine and this simple formula need not apply term by term. A recovery operation corrects the resulting error class without learning the logical amplitudes.
Because every single-qubit operator is a linear combination of , the quantum error-correction conditions extend correction of these Pauli operators to arbitrary noise on one qubit. General channels, including amplitude damping, are described by one or more Kraus operators, and linearity applies the same reasoning to each operator.
Syndrome extraction reveals nothing
The other thing to check is that the diagnosis is safe. If the syndrome carried any information about and , extracting it would partially collapse the logical qubit and corrupt the computation.
For correctable errors, it does not. Every stabilizer generator acts as the identity on the code space, so syndrome outcomes distinguish error subspaces without distinguishing the encoded logical basis states. In typical syndrome-extraction circuits, data qubits interact with ancillas and the ancillas are measured; this indirectly measures stabilizers rather than individual data-qubit values. Fault-tolerant designs must also prevent ancilla and gate faults from spreading dangerously to the data.
From codes to fault tolerance
Shor’s code is primarily pedagogical rather than the leading architecture for scalable hardware. Surface codes use geometrically local checks on a two-dimensional layout, which can better match hardware with local interactions.
Two ideas carry the field from here. First, correction circuitry is itself faulty, so it must be designed so that a small number of component failures does not spread into an uncorrectable error. Second, threshold theorems show that arbitrarily long quantum computation is possible with polylogarithmic overhead when the noise satisfies specified assumptions and its strength is below an architecture-dependent threshold. Often quoted surface-code thresholds are of order for particular circuit-level noise models and decoders, not a universal hardware constant. Being above a quoted threshold does not imply that every increase in code size must make performance worse; the outcome depends on the code, decoder and noise model.
Threshold behavior motivates improvements in gates, measurements, decoding and qubit connectivity. Resource estimates for large instances of Shor’s algorithm depend strongly on the target integer, physical error model, code, decoder, gate speeds and required success probability, so no single physical-to-logical qubit ratio applies universally.
Simulator Lab: Test a Repetition Code Coherently
The Quantum Simulator does not provide a dedicated stabilizer-measurement control, but its standard gates can demonstrate encoding, a chosen fault and coherent recovery for the three-qubit bit-flip code.
Encode and inject one fault
Use three qubits with q0 as the data qubit:
- Prepare a generic state on
q0, for exampleRY(1.1)followed byRZ(0.7). - Add CNOT targets
0,1, followed by CNOT targets0,2. This produces even though QSim displays the bit string inq2 q1 q0order. - Export this encoding circuit as JSON. For separate trials, import it and add
Xtoq0,q1orq2. Never inject two test faults if the claim being tested is single-error correction.
The Z-basis probability table alone cannot show the relative phase of the encoded amplitudes. Preserve the generic input and complete the decode before deciding whether recovery worked.
Decode and correct without branch sampling
After the injected X, append CNOT 0,1, CNOT 0,2, and Toffoli targets 1,2,0. The first two operations coherently extract disagreement information into q1 and q2; the Toffoli corrects q0 when both indicate that the data qubit was the faulty one. This network restores the logical state on q0 for any one of the three bit flips, while the other qubits retain an error-dependent syndrome.
After recovery, the syndrome appears directly in the two higher-index qubits:
| Injected fault | Final q2q1 | Meaning |
|---|---|---|
| none | 00 | no disagreement |
X on q0 | 11 | data qubit differed from both partners |
X on q1 | 01 | first partner differed |
X on q2 | 10 | second partner differed |
Because the recovered logical state on q0 can remain a superposition, the full measurement table generally has two nonzero basis rows. Within both rows, however, q2q1 has the fixed syndrome shown above. Read those two leftmost displayed bits independently of the rightmost logical-data bit.
Run a one-qubit reference containing only RY(1.1) and RZ(0.7), then compare its Bloch values with q0 after each three-qubit recovery trial. Matching only the Z-basis probabilities is insufficient because an unnoticed phase error could leave those probabilities unchanged.
Demonstrate the code’s boundary
Repeat with a Z fault on one encoded qubit. The bit-flip recovery does not remove it; the final q0 Bloch vector differs from the reference. Then inject two X faults. The recovery interprets them as the complementary single fault and produces a logical error. These negative tests are as important as the successful single-flip cases because they establish exactly what the circuit does and does not correct.
For the phase-flip analogue, perform the two-CNOT encoding and then apply H to all three qubits. Inject one Z fault, apply H to all three again, and finish with the same two CNOTs and Toffoli recovery. The Hadamards convert the phase-code basis back to the bit-code basis, using . Save the bit-flip and phase-flip experiments as separate JSON files so that basis-change gates are not accidentally carried between tests.
This laboratory is an ideal, noiseless code demonstration. It does not model faulty syndrome ancillas, leakage, correlated errors, repeated correction cycles or a surface-code threshold. Those require a specialized fault-tolerance simulator rather than merely increasing QSim’s shot count.