Shor's 9-Qubit Code
An interactive guide to the first quantum error-correcting code — from classical bits to quantum fault tolerance.
The central challenge: Quantum information is fragile. A single qubit can suffer bit-flip errors (X), phase-flip errors (Z), or any combination. Unlike classical bits, we cannot simply copy a qubit (the no-cloning theorem forbids it). Shor's 1995 code showed that quantum error correction is possible by encoding 1 logical qubit into 9 physical qubits.
Roadmap: We'll build understanding layer by layer:
1. Classical 3-bit repetition code (corrects bit-flips)
2. Quantum 3-qubit bit-flip code (quantum analogue)
3. The Hadamard transform and its magic: turning phase-flips into bit-flips
4. Quantum 3-qubit phase-flip code
5. Shor's 9-qubit code: combining both
1. Classical 3-Bit Repetition Code
The simplest error-correcting code: to protect a single bit, repeat it three times. If noise flips one bit, majority voting recovers the original.
How majority voting works
| Received | Syndrome | Correction | Decoded |
|---|---|---|---|
| 000 | No error | None | 0 |
| 001 | Bit 3 flipped | Flip bit 3 | 0 |
| 010 | Bit 2 flipped | Flip bit 2 | 0 |
| 100 | Bit 1 flipped | Flip bit 1 | 0 |
| 111 | No error | None | 1 |
| 110 | Bit 3 flipped | Flip bit 3 | 1 |
| 101 | Bit 2 flipped | Flip bit 2 | 1 |
| 011 | Bit 1 flipped | Flip bit 1 | 1 |
Key insight: We detect errors by comparing bits (computing the syndrome), not by looking at the value of any individual bit. This same principle extends to quantum codes.
Self-test: Why can the 3-bit code correct 1 error but not 2?
Self-test: What does the syndrome (1,0) tell us?
2. Quantum 3-Qubit Bit-Flip Code
We can't clone a qubit, but we can entangle it with ancillaAn extra "helper" qubit used to extract error information without disturbing the encoded data. Measured and discarded after syndrome extraction. qubits. The encoding maps:
The No-Cloning Theorem: It is mathematically impossible to create an exact copy of an unknown quantum state. If cloning were possible, we could violate the uncertainty principle and enable faster-than-light communication. This means quantum error correction must use entanglement rather than copying. The encoding $|\psi\rangle \to \alpha|000\rangle + \beta|111\rangle$ does not create three copies of $|\psi\rangle$ -- it creates an entangled state where no single qubit carries the full information about $\alpha$ and $\beta$.
Classical Repetition
- Encode: Copy the bit 3 times
- Detect: Compare bits directly
- Correct: Majority vote
- Handles: Bit-flip errors only
Quantum Bit-Flip
- Encode: Entangle with ancilla qubits
- Detect: Syndrome (parity) checks
- Correct: Apply correction gate
- Handles: X errors only
This is achieved with two CNOTControlled-NOT gate: flips the target qubit if and only if the control qubit is |1⟩. The fundamental two-qubit gate for entangling and encoding. gates. If a bit-flip (X) hits one qubit, we detect it using syndromeA set of parity-check outcomes that identifies which error occurred, without revealing the encoded quantum information. Like an error fingerprint. measurements — parity checks that reveal which qubit flipped without revealing α or β.
Syndrome table for bit-flip code
| Syndrome ($Z_1Z_2,\; Z_2Z_3$) | Error | Correction |
|---|---|---|
| 0, 0 | None | None |
| 1, 0 | X on qubit 1 | Apply X to qubit 1 |
| 1, 1 | X on qubit 2 | Apply X to qubit 2 |
| 0, 1 | X on qubit 3 | Apply X to qubit 3 |
The syndrome measurements check parity between adjacent qubits without collapsing the encoded state. This is the quantum magic: we learn where the error is, but nothing about α or β.
How do we measure syndromes without disturbing the data? We use extra qubits called ancillas (from Latin ancilla, "helper"). The procedure works in three steps:
- Prepare an ancilla qubit in state $|0\rangle$.
- Entangle it with the data qubits using CNOT gates. For example, to measure $Z_1Z_2$ parity, apply CNOT from qubit 1 to the ancilla, then CNOT from qubit 2 to the ancilla. The ancilla now holds the parity $q_1 \oplus q_2$.
- Measure only the ancilla. If it reads $|0\rangle$, parity is even (qubits agree); if $|1\rangle$, parity is odd (qubits disagree).
The data qubits are never measured directly — the ancilla absorbs only the parity information, leaving the superposition $\alpha$ and $\beta$ intact.
Try it: syndrome decoder
Toggle the two syndrome bits to see which qubit the decoder identifies as errored.
Syndrome (0,1): Error on qubit 3 — apply X to qubit 3.
Limitation: This code only corrects bit-flip (X) errors. But qubits also suffer phase-flip (Z) errors, where |1⟩ picks up a minus sign: |1⟩ → −|1⟩. The bit-flip code is completely blind to these!
Self-test: Why can't we just measure the qubits to check for errors?
3. The Hadamard Transform: Changing the Nature of Errors
The Hadamard gate H is the key that connects bit-flips and phase-flips. It maps between the computational basis $\{|0\rangle, |1\rangle\}$ and the Hadamard basis $\{|+\rangle, |-\rangle\}$:
The crucial insight
In the Hadamard basis, the roles of X and Z are swapped:
Bit-flip X:
$X|0\rangle = |1\rangle$
$X|1\rangle = |0\rangle$
In the $\{|+\rangle,|-\rangle\}$ basis:
$X|+\rangle = |+\rangle$
$X|-\rangle = -|-\rangle$
X acts like a phase-flip in the Hadamard basis!
Phase-flip Z:
$Z|0\rangle = |0\rangle$
$Z|1\rangle = -|1\rangle$
In the $\{|+\rangle,|-\rangle\}$ basis:
$Z|+\rangle = |-\rangle$
$Z|-\rangle = |+\rangle$
Z acts like a bit-flip in the Hadamard basis!
Mathematically: $HXH = Z$ and $HZH = X$
The Hadamard Matrix
Key properties:
$H^2 = I$ — applying H twice gives back the original state (H is its own inverse)
$H = H^\dagger$ — H is both unitary and Hermitian
H is a Fourier transform on a single qubit — it rotates between conjugate bases
Apply H, then an error, then H again. Watch how the error type transforms.
Why this matters for error correction: If we encode in the Hadamard basis (using |+⟩ and |−⟩ instead of |0⟩ and |1⟩), then a phase-flip error looks like a bit-flip error. So we can reuse the same repetition code structure to detect phase-flips! This is the key idea behind Shor's code.
Self-test: If HXH = Z and HZH = X, what is HYH?
Self-test: Why is the Hadamard gate its own inverse?
4. The 3-Qubit Phase-Flip Code
By encoding in the Hadamard basis, we can correct phase-flip errors using the same repetition structure:
The encoding circuit: first apply CNOTs (as in the bit-flip code), then apply H to each qubit:
A phase-flip Z on any single qubit is detected via syndrome measurements in the X-basis (measuring $X_1X_2$ and $X_2X_3$).
Bit-flip code
Encodes: $|0\rangle\!\to\!|000\rangle$, $|1\rangle\!\to\!|111\rangle$
Corrects: X errors
Syndrome: Z-basis parities
Blind to: Z errors
Phase-flip code
Encodes: $|0\rangle\!\to\!|{+}{+}{+}\rangle$, $|1\rangle\!\to\!|{-}{-}{-}\rangle$
Corrects: Z errors
Syndrome: X-basis parities
Blind to: X errors
Shor's idea: What if we use both codes together? Encode each qubit of the phase-flip code using the bit-flip code. This gives a 9-qubit code that corrects both X and Z errors — and therefore any single-qubit error!
Self-test: Why does the phase-flip code use X-basis syndrome measurements?
5. Shor's 9-Qubit Code
Shor's code is a concatenationBuilding a larger code by nesting one code inside another. Shor's code nests the 3-qubit bit-flip code inside the 3-qubit phase-flip code, yielding 9 qubits total. of the phase-flip code (outer) and the bit-flip code (inner). The encoding is:
Structure: two layers of protection
The 9 qubits are organized into 3 blocks of 3:
Inner code (within each block): The 3-qubit repetition code protects against X (bit-flip) errors. Each block uses Z-parity checks.
Outer code (across blocks): The 3-block structure (in the Hadamard basis) protects against Z (phase-flip) errors. Cross-block X-parity checks detect which block was affected.
Encoding circuit
The full encoding circuit has two stages: first the outer (phase-flip) code, then the inner (bit-flip) code within each block.
Error correction procedure
Step 1: Detect bit-flip errors (within each block). Measure $Z_iZ_j$ parity checks for each block. This identifies which qubit (if any) within a block has flipped. Apply X to correct.
Step 2: Detect phase-flip errors (across blocks). Measure the 6-qubit operators $X_1X_2X_3X_4X_5X_6$ and $X_4X_5X_6X_7X_8X_9$. This identifies which block (if any) has a sign flip. Apply Z to any qubit in that block to correct.
Why this corrects any single-qubit error
Any single-qubit error can be decomposed as a linear combination of I, X, Z, and Y = iXZ. When we measure the syndrome, the state collapses into one of these cases:
| Error | Bit-flip syndrome | Phase-flip syndrome | Correction |
|---|---|---|---|
| I (no error) | All 0 | All 0 | None |
| X on qubit k | Identifies k within block | All 0 | Apply Xk |
| Z on qubit k | All 0 | Identifies block | Apply Zk |
| Y on qubit k | Identifies k within block | Identifies block | Apply Yk |
The measurement projects the error into one of these discrete cases, and then we apply the appropriate correction. This is the digitization of errors — continuous errors become discrete through measurement.
Self-test: Why does Shor's code need 9 qubits, not 6?
Self-test: What happens to both syndromes when a Y error occurs?
6. When the Code Fails: Understanding Limits
Shor's code can correct any single-qubit error, but what happens when two or more qubits are hit? With distance $d = 3$, the decoder's assumption — at most one error — breaks down, and it can misidentify the problem.
Try it: multi-error failure modes
Toggle X errors on the 3 qubits of a bit-flip block. Watch how the syndrome misleads the decoder when multiple errors occur.
No errors. Syndrome (0,0) — the decoder sees no problem.
Beyond Shor's code
Shor's 9-qubit code was the first quantum error-correcting code, but it is far from optimal. Later discoveries reduced the qubit overhead and increased error tolerance:
- Steane code $[[7,1,3]]$ — corrects the same errors with only 7 qubits using CSS construction
- Perfect 5-qubit code $[[5,1,3]]$ — the smallest possible single-error-correcting code
- Surface codes — the leading approach for real hardware, achieving high distance with nearest-neighbor qubits on a 2D grid
All share the same core principles we've explored: encode redundantly, measure syndromes without disturbing data, and correct based on the syndrome pattern.
7. Summary & Key Takeaways
The three big ideas
1. Redundancy without cloning: We can't copy quantum states, but we can spread quantum information across entangled qubits. The encoding creates a code space where errors move the state out of the code space in detectable ways.
2. The Hadamard duality: The Hadamard gate swaps X ↔ Z errors. A phase-flip code is just a bit-flip code conjugated by H. This is a deep consequence of the structure of quantum mechanics — the X and Z bases are related by Fourier transform.
3. Code concatenation: By nesting the bit-flip code inside the phase-flip code (or vice versa), Shor's code corrects both types of errors — and therefore any single-qubit error. The syndrome measurement discretizes continuous errors.
Code parameters
| Property | Value |
|---|---|
| Physical qubits | 9 |
| Logical qubits | 1 |
| Distance | 3 |
| Correctable errors | Any single-qubit error (X, Z, or Y) |
| Notation | [[9, 1, 3]] |
| Stabilizer generators | 8 (= 9 - 1) |
Understanding code distance. The notation $[[n, k, d]]$ describes a quantum code encoding $k$ logical qubits into $n$ physical qubits with distance $d$. The distance is the minimum number of single-qubit operations needed to convert one valid codeword into another.
For Shor's $[[9,1,3]]$ code, distance $d = 3$ means:
- Correct any $\lfloor (d{-}1)/2 \rfloor = 1$ error
- Detect up to $d - 1 = 2$ errors (but cannot correct both)
This is a fundamental trade-off: higher distance costs more physical qubits but protects against more errors. The surface code, for example, achieves distance $d$ using roughly $2d^2$ physical qubits.
What are stabilizers?
A stabilizerAn operator that leaves every valid codeword unchanged. Measuring stabilizers extracts error syndromes without collapsing the encoded information. is an operator that leaves every valid codeword unchanged: if $|\psi\rangle$ is in the code space, then $S|\psi\rangle = |\psi\rangle$. Think of stabilizers as membership checks — they verify that a state belongs to the code space without disturbing it.
When an error $E$ occurs, some stabilizers will anti-commute with it. Measuring those stabilizers returns $-1$ instead of $+1$, forming a pattern called the syndrome. Each correctable error produces a unique syndrome, so the decoder can identify and reverse the error:
Shor's code has 8 stabilizer generators (since $n - k = 9 - 1 = 8$), producing an 8-bit syndrome. Six use $Z$ operators for bit-flip detection within blocks; two use $X$ operators for phase-flip detection between blocks.
Stabilizer generators
| Generator | Role |
|---|---|
| $Z_1 Z_2$ | Block A, qubits 1-2 parity |
| $Z_2 Z_3$ | Block A, qubits 2-3 parity |
| $Z_4 Z_5$ | Block B, qubits 4-5 parity |
| $Z_5 Z_6$ | Block B, qubits 5-6 parity |
| $Z_7 Z_8$ | Block C, qubits 7-8 parity |
| $Z_8 Z_9$ | Block C, qubits 8-9 parity |
| $X_1 X_2 X_3 X_4 X_5 X_6$ | Block A-B parity |
| $X_4 X_5 X_6 X_7 X_8 X_9$ | Block B-C parity |
Historical significance: Peter Shor published this code in 1995, disproving the widespread belief that quantum error correction was impossible. It paved the way for the theory of fault-tolerant quantum computation and all modern quantum error-correcting codes (CSS codes, surface codes, etc.).