In short
The 3-qubit phase-flip code is the bit-flip code rotated into the X basis. The encoding is |0\rangle_L = |+++\rangle and |1\rangle_L = |---\rangle, where |\pm\rangle = (|0\rangle \pm |1\rangle)/\sqrt 2 are the X-basis states. A single Z (phase-flip) error on any of the three physical qubits is detected by measuring the X-basis parities X_1 X_2 and X_2 X_3, which output a syndrome (s_1, s_2) that uniquely identifies the affected qubit. Recovery is a single Z on the flagged qubit. The code is Hadamard-conjugate to the bit-flip code: sandwiching the bit-flip encoding circuit with H^{\otimes 3} on either side produces the phase-flip code, because H X H = Z turns every bit-flip-detecting mechanism into a phase-flip-detecting one. The catch: the phase-flip code is blind to X errors the same way the bit-flip code is blind to Z errors. You need both — which is exactly what Shor's 9-qubit code delivers by stacking them.
The bit-flip code (the previous chapter) catches X errors. It is beautiful, compact, and — if you take it as the complete solution to quantum error correction — completely wrong.
Real quantum noise is not just bit-flip. A qubit sitting in a superconducting circuit, a trapped ion, or a photonic mode suffers at least two kinds of damage with roughly comparable probability: X errors that swap |0\rangle \leftrightarrow |1\rangle, and Z errors that flip the sign of |1\rangle without touching |0\rangle. The bit-flip code handles the first; it is defenceless against the second. A Z error on any qubit of the encoded state \alpha|000\rangle + \beta|111\rangle slips straight past the syndrome measurements and corrupts the logical state — because the syndromes Z_1 Z_2, Z_2 Z_3 commute with every Z and cannot see them.
Symmetry suggests the fix. If there is a code that catches X errors by encoding in the Z-basis (|000\rangle, |111\rangle) and measuring Z-basis parities, there ought to be a mirror-image code that catches Z errors by encoding in the X-basis and measuring X-basis parities. The Hadamard gate — which swaps X and Z, and swaps the \{|0\rangle, |1\rangle\} basis with the \{|+\rangle, |-\rangle\} basis — is the bridge between the two pictures. Conjugating the bit-flip code by H^{\otimes 3} produces a second code, the phase-flip code, that does exactly the mirror job.
That is this chapter. You will build the phase-flip code from scratch, see why it works, see the explicit Hadamard duality with the bit-flip code, and meet the limitation that motivates Shor's 9-qubit construction in the next chapter.
The X basis — a 30-second recap
The X basis consists of the two states
They are the two eigenstates of the Pauli X operator, with eigenvalues +1 and -1 respectively:
Why these are X eigenstates: X|0\rangle = |1\rangle and X|1\rangle = |0\rangle, so X(|0\rangle + |1\rangle) = |1\rangle + |0\rangle = (|0\rangle + |1\rangle) — the state is returned to itself, eigenvalue +1. For |-\rangle the minus sign flips, giving eigenvalue -1.
The Hadamard gate converts between the two bases: H|0\rangle = |+\rangle and H|1\rangle = |-\rangle, and conversely H|+\rangle = |0\rangle, H|-\rangle = |1\rangle. And the single most useful algebraic identity of this chapter is
which says that conjugating X by H gives Z, and conjugating Z by H gives X. The Hadamard swaps the two Pauli operators.
Why HXH=Z: compute it in matrix form. H = \tfrac{1}{\sqrt 2}\begin{pmatrix}1 & 1\\1 & -1\end{pmatrix}, X = \begin{pmatrix}0 & 1\\1 & 0\end{pmatrix}. Then HX = \tfrac{1}{\sqrt 2}\begin{pmatrix}1 & 1\\-1 & 1\end{pmatrix}, and HXH = \tfrac{1}{2}\begin{pmatrix}1 & 1\\-1 & 1\end{pmatrix}\begin{pmatrix}1 & 1\\1 & -1\end{pmatrix} = \tfrac{1}{2}\begin{pmatrix}2 & 0\\0 & -2\end{pmatrix} = \begin{pmatrix}1 & 0\\0 & -1\end{pmatrix} = Z. The identity HZH=X follows from H^2 = I.
Keep HXH = Z in your head. It is the single equation driving this entire chapter.
The encoding
Start with a data qubit |\psi\rangle = \alpha|0\rangle + \beta|1\rangle and two ancillas in |0\rangle. The phase-flip code's encoding map is
The encoded state lives in a two-dimensional subspace of the eight-dimensional three-qubit Hilbert space — the subspace spanned by |+++\rangle and |---\rangle, the code space of this code. Everything outside that subspace is "error territory".
The encoding circuit
Two equivalent circuits build |\psi\rangle_L from (\alpha|0\rangle + \beta|1\rangle)|0\rangle|0\rangle:
Route A — "bit-flip then Hadamard". Run the bit-flip encoding first (two CNOTs from qubit 1 to qubits 2 and 3), then apply H to all three qubits.
Why the final step works: H^{\otimes 3}|000\rangle = (H|0\rangle)^{\otimes 3} = |+\rangle^{\otimes 3} = |{+}{+}{+}\rangle, and similarly H^{\otimes 3}|111\rangle = |{-}{-}{-}\rangle. The three Hadamards applied in parallel factor through the tensor product.
Route B — "Hadamard then CNOT then Hadamard". Apply H to qubit 1, then two CNOTs with qubit 1 as control, then three final Hadamards. This gives the same output — it is just Route A with an extra H^2 = I pair inserted at the start for free. Route A is cleaner; we use it from here on.
Either way, the encoded state is |\psi\rangle_L = \alpha|{+}{+}{+}\rangle + \beta|{-}{-}{-}\rangle.
Why Z errors are detectable — intuition first
A Z error on a single qubit does this:
Why Z flips |+\rangle and |-\rangle: Z|0\rangle = |0\rangle and Z|1\rangle = -|1\rangle, so Z(|0\rangle + |1\rangle) = |0\rangle - |1\rangle — the plus sign becomes minus. That is |+\rangle \to |-\rangle. By the same arithmetic |-\rangle \to |+\rangle.
So a Z error in the X basis is exactly what an X error is in the Z basis — it flips the basis element. The encoding \alpha|{+}{+}{+}\rangle + \beta|{-}{-}{-}\rangle suffers a Z error on qubit 2 (say), producing
One of the three +/- labels in each basis ket has flipped. In the X basis, that is exactly a bit-flip on qubit 2.
The picture is now the bit-flip picture, just with +/- instead of 0/1: three qubits, each of which "agrees" (all plus or all minus) in the encoded state, and a single-qubit error making one of them disagree. The detection mechanism must therefore be "measure whether adjacent qubits agree in the X basis". That is the syndrome.
Syndrome measurement — the X-basis parities
The two stabiliser observables of the phase-flip code are
Each of these is a tensor product of Pauli X's, acting on the specified pair of qubits and identity on the third. Their eigenstates in the X basis are exactly analogous to how Z_i Z_j has eigenstates in the Z basis:
- |{+}{+}\rangle is a +1 eigenstate of X_1 X_2 (both plus, parity +1).
- |{-}{-}\rangle is a +1 eigenstate (both minus, (-1)(-1) = +1).
- |{+}{-}\rangle and |{-}{+}\rangle are -1 eigenstates (one of each, parity -1).
Apply this to the code space. Every state in the code space is a linear combination of |{+}{+}{+}\rangle and |{-}{-}{-}\rangle. Both of these are +1 eigenstates of S_1 (first two qubits agree in the X basis) and +1 eigenstates of S_2 (last two qubits agree). So every code-space state satisfies
Measuring S_1 and S_2 on an uncorrupted encoded state returns (+1, +1) with certainty, and the measurement does not disturb the state.
Now a Z error takes the state out of the code space and changes the eigenvalues:
| Error | State after error | S_1 = X_1 X_2 | S_2 = X_2 X_3 |
|---|---|---|---|
| I | \alpha|{+}{+}{+}\rangle + \beta|{-}{-}{-}\rangle | +1 | +1 |
| Z_1 | \alpha|{-}{+}{+}\rangle + \beta|{+}{-}{-}\rangle | -1 | +1 |
| Z_2 | \alpha|{+}{-}{+}\rangle + \beta|{-}{+}{-}\rangle | -1 | -1 |
| Z_3 | \alpha|{+}{+}{-}\rangle + \beta|{-}{-}{+}\rangle | +1 | -1 |
Four distinct syndromes, four distinguishable error locations. The syndrome (s_1, s_2) uniquely pinpoints which qubit suffered the phase flip. Apply Z to that qubit and — because Z^2 = I — the error is exactly undone, and the state returns to the code space with amplitudes \alpha, \beta untouched.
The Hadamard duality — one code from the other
The phase-flip code is not a new invention. It is the bit-flip code with Hadamards stapled on.
Let E_{\text{bit}} be the bit-flip encoding unitary (|\psi\rangle|00\rangle \mapsto \alpha|000\rangle + \beta|111\rangle for the three-qubit case, zero-padded with ancillas), and let E_{\text{phase}} be the phase-flip encoding unitary. Then
Similarly, if S^{\text{bit}}_1 = Z_1 Z_2 and S^{\text{bit}}_2 = Z_2 Z_3 are the bit-flip stabilisers, and S^{\text{phase}}_1 = X_1 X_2 and S^{\text{phase}}_2 = X_2 X_3 are the phase-flip ones, then by H Z H = X:
And bit-flip errors X_i become phase-flip errors Z_i under the same conjugation:
Every operational piece of the bit-flip code has a phase-flip counterpart obtained by sandwiching with H^{\otimes 3}. Encoding, stabilisers, detectable errors, recovery — all mirror through the Hadamard layer.
This duality is not a trick of notation — it is a general principle. Under H^{\otimes n} conjugation, any code protecting against X-type errors becomes a code protecting against Z-type errors. When we meet CSS codes (ch.120) and stabiliser codes in full generality, this duality will reappear as part of the classification of quantum codes into X-type and Z-type stabilisers.
Worked examples
Example 1: detect and correct a Z error on |+⟩_L
Show end-to-end how the phase-flip code catches a Z error on one physical qubit of the encoded state |+\rangle_L.
Setup. The logical input is |+\rangle = \tfrac{1}{\sqrt 2}(|0\rangle + |1\rangle). Encode it:
Why: the encoding map sends |0\rangle \to |{+}{+}{+}\rangle and |1\rangle \to |{-}{-}{-}\rangle, and it is linear, so it sends \tfrac{1}{\sqrt 2}(|0\rangle + |1\rangle) to \tfrac{1}{\sqrt 2}(|{+}{+}{+}\rangle + |{-}{-}{-}\rangle). Linearity of the encoding is essential: it is a unitary map, and unitaries are linear.
Step 1. The error happens. A phase-flip hits qubit 1. Apply Z_1 = Z \otimes I \otimes I:
Why Z_1|{+}{+}{+}\rangle = |{-}{+}{+}\rangle: Z_1 acts as Z on qubit 1 and identity on qubits 2, 3. Z|+\rangle = |-\rangle, so qubit 1 flips + \to -; qubits 2, 3 unchanged.
The state is no longer in the code space.
Step 2. Measure S_1 = X_1 X_2. Evaluate on each basis ket:
- X_1 X_2 |{-}{+}{+}\rangle: X_1|{-}\rangle = -|{-}\rangle, X_2|{+}\rangle = +|{+}\rangle, |{+}\rangle on qubit 3 untouched. Total: (-1)(+1) = -1 times |{-}{+}{+}\rangle.
- X_1 X_2 |{+}{-}{-}\rangle: (+1)(-1) = -1 times |{+}{-}{-}\rangle.
Both terms give eigenvalue -1, so the state is a -1 eigenstate of S_1. The measurement returns s_1 = -1 deterministically, with no disturbance to the amplitudes.
Step 3. Measure S_2 = X_2 X_3.
- X_2 X_3 |{-}{+}{+}\rangle: (+1)(+1) = +1.
- X_2 X_3 |{+}{-}{-}\rangle: (-1)(-1) = +1.
s_2 = +1.
Step 4. Read the syndrome. (s_1, s_2) = (-1, +1) matches the Z_1 row of the syndrome table. The error is on qubit 1.
Step 5. Apply the correction Z_1. Since Z^2 = I:
Back to the code space, amplitudes preserved.
Result. |+\rangle_L is recovered exactly. The logical information — the equal superposition of |0\rangle_L and |1\rangle_L — survives a single-qubit Z error. Had two phase flips occurred in the same correction cycle, the syndrome would mis-identify them as a different single-qubit error and the recovery would actually introduce a logical flip, but at probability O(p^2) rather than O(p).
What this shows. The phase-flip code works by relabelling the bit-flip protocol. Everything the bit-flip code did in the \{|0\rangle, |1\rangle\} basis — encoding by tripling, syndrome by Z-parity, correction by X — has a dual here in the \{|+\rangle, |-\rangle\} basis with X and Z swapped.
Example 2: the phase-flip code is blind to X errors
The phase-flip code's mirror-image weakness: it does nothing to protect against X errors. Show this explicitly by applying an X error and watching the syndrome fail to detect it.
Setup. Take an encoded state |\psi\rangle_L = \alpha|{+}{+}{+}\rangle + \beta|{-}{-}{-}\rangle (arbitrary \alpha, \beta).
Step 1. What X does in the X basis. X|+\rangle = +|+\rangle and X|-\rangle = -|-\rangle — so X on a single X-basis state does not change it as a vector, but the second case contributes a sign. Why: |+\rangle and |-\rangle are eigenstates of X with eigenvalues +1 and -1. An X operator applied to its own eigenstate returns the eigenstate times the eigenvalue — so |+\rangle is returned unchanged and |-\rangle picks up a -1.
Step 2. Apply X_1.
The relative phase between the two logical basis kets has flipped: \beta \to -\beta. In the logical qubit's view, \alpha|0\rangle_L + \beta|1\rangle_L \to \alpha|0\rangle_L - \beta|1\rangle_L — this is a logical Z, not nothing.
Step 3. Measure the syndrome. Evaluate S_1 = X_1 X_2 on each term:
- X_1 X_2 |{+}{+}{+}\rangle = (+1)(+1)|{+}{+}{+}\rangle = +|{+}{+}{+}\rangle.
- X_1 X_2 |{-}{-}{-}\rangle = (-1)(-1)|{-}{-}{-}\rangle = +|{-}{-}{-}\rangle.
So the corrupted state is still a +1 eigenstate of S_1. Similarly a +1 eigenstate of S_2. The syndrome reads (+1, +1) — the same as "no error".
Step 4. Recovery attempt. The syndrome says "do nothing". You do nothing. The logical-Z error remains. The logical qubit is now in the state \alpha|0\rangle_L - \beta|1\rangle_L instead of \alpha|0\rangle_L + \beta|1\rangle_L — a different logical state, corrupted in a way the code cannot detect.
Result. A single X error on any physical qubit of the phase-flip code is invisible to the syndrome and produces a logical Z on the encoded qubit. The phase-flip code protects against Z errors at the cost of being completely exposed to X errors. In a real noise model where both X and Z happen with comparable probability, the phase-flip code alone does worse than the bare qubit, because every X error that would have been a visible bit-flip becomes a silent logical phase-flip.
What this shows. No three-qubit code can protect against both X and Z errors simultaneously — the information-theoretic capacity is not there. The bit-flip code picks X, the phase-flip code picks Z, and each fails against the other. Shor's 9-qubit code concatenates them (3 blocks of 3) to get both.
Why this matters. The phase-flip code is not a production-grade protection scheme. Its real job is pedagogical and conceptual: it establishes the Hadamard duality between X-type and Z-type codes, which is the skeleton of the CSS code construction (Calderbank-Shor-Steane, 1996). Every CSS code is built from two classical binary codes, one detecting X-type errors and one detecting Z-type errors, glued together by the same Hadamard-basis trick you just saw. The phase-flip code is the simplest non-trivial CSS code, and understanding it is the entry point to understanding every CSS-family code (Steane's 7-qubit code, surface codes, colour codes).
Common confusions
-
"The phase-flip code is a different code from the bit-flip code." Algebraically, no. The two codes are unitarily equivalent by the H^{\otimes 3} transformation: every operational element of one is obtained by sandwiching the corresponding element of the other with Hadamards. They are the same code in two different bases. Pragmatically, yes — they detect different error types, because "error type" is not basis-invariant in the way the code structure is.
-
"X errors do nothing, so the phase-flip code ignores them safely." Wrong. X|+\rangle = |+\rangle is true, but X|-\rangle = -|-\rangle. The relative sign on |-\rangle propagates into the encoded state as a logical Z — a real, observable error that corrupts the logical qubit. The code does not detect the error and the error is not harmless. That is the worst combination.
-
"Syndrome measurement needs Hadamards before and after." The stabiliser X_1 X_2 is measured with a standard ancilla-based circuit: Hadamard the ancilla, apply controlled-X on qubits 1 and 2 from the ancilla, Hadamard the ancilla again, measure. That looks like "Hadamards sandwiching the operation", because converting an X-basis measurement to a Z-basis measurement requires a Hadamard on each end. You are not Hadamarding the data qubits; you are Hadamarding the ancilla so the measurement reads out the X-parity as a standard computational-basis bit.
-
"The code works as long as errors are small." No — the code works against any single-qubit Z error, of any strength, regardless of whether it is a small rotation e^{-i\epsilon Z} or a full Z. Discretisation (see why QEC is hard) guarantees that a small rotation gets projected by the syndrome measurement onto either "no error" (with high probability) or "full Z on one qubit" (with small probability). Either outcome is then corrected exactly by the code. This is one of the most counterintuitive features of QEC: the protection is against an error channel, not against a specific error magnitude.
-
"Phase-flip is less important than bit-flip." False. In most physical noise models — superconducting qubits, trapped ions, photonic modes — T_2 (the phase-coherence time) is the limiting factor, not T_1 (the energy-relaxation time that causes bit-flips). Phase errors dominate. A real quantum device without phase-flip protection is like a leaky boat with a bucket labelled "bails out splashes" but no way to handle the slow seep through the hull.
Going deeper
If you came here to see the phase-flip code in action and understand how it dualises with the bit-flip code, you have it. This section sketches the formal CSS construction and the stabiliser-formalism picture for readers preparing for the rest of Part 14.
Both codes as [3, 1] CSS codes
A CSS code (Calderbank-Shor-Steane, 1996) is built from two classical linear binary codes C_1, C_2 \subseteq \mathbb{F}_2^n with C_2 \subseteq C_1. The quantum code's logical basis states are
i.e., equal superpositions over cosets of C_2 in C_1. The bit-flip code corresponds to C_1 = \{000, 111\} (the classical 3-bit repetition code) and C_2 = \{000\}: the logical bases are |000\rangle and |111\rangle directly. The phase-flip code is the dual CSS construction with the roles of C_1 and C_2 swapped (in the sense that the Hadamard-conjugated state is the coset superposition of the dual codes).
More concretely: the phase-flip encoding \alpha|{+}{+}{+}\rangle + \beta|{-}{-}{-}\rangle, when expanded in the computational basis, equals
where |x| is the Hamming weight of x. So
These are the two even-weight and odd-weight coset superpositions of the parity code \{x : |x| \text{ even}\}, exactly the CSS construction for this code pair.
Stabiliser generators
In the stabiliser formalism (ch.119), a code is defined by a set of commuting Pauli operators that fix every code-space state. For the phase-flip code, the generators are
Every state in the code space satisfies S_i |\psi\rangle_L = +|\psi\rangle_L for i = 1, 2. The logical operators are
(or any weight-odd product of X's, since any odd-weight X is equivalent modulo the stabiliser). Z_L distinguishes |0\rangle_L from |1\rangle_L (eigenvalues +1 and -1) and X_L flips between them.
Contrast with the bit-flip code:
The generators and the logical operators are swapped under X \leftrightarrow Z — the Hadamard duality in stabiliser language.
Toward Shor's 9-qubit code
The phase-flip code protects against Z errors and is blind to X. The bit-flip code is the opposite. Neither alone is useful for real noise, which contains both in roughly equal measure. Shor's 9-qubit code (ch.118) fixes this by concatenation: each of the three qubits in the outer phase-flip code is itself encoded into three physical qubits by the bit-flip code. The resulting 9-qubit state inherits the Z-error protection of the outer code and the X-error protection of the inner code — and, because Y = iXZ, it also corrects Y errors, and by the discretisation theorem it corrects any single-qubit error. Concatenation of the bit-flip and phase-flip codes is the simplest way to build a fully error-correcting quantum code, and Shor did it in the first QEC paper ever published. The next chapter walks through the construction.
Where this leads next
- Bit-flip code — the partner code. Reads this chapter's contents in the computational basis.
- Shor 9-qubit code — combines bit-flip and phase-flip codes. Corrects every single-qubit error.
- Why QEC is hard — no-cloning, continuous errors, measurement collapse, and the three insights that circumvent them.
- CSS codes — the general construction that bit-flip and phase-flip codes are the simplest instance of.
- Stabilizer formalism intro — the unifying language for every code in this part.
References
- Peter Shor, Scheme for reducing decoherence in quantum computer memory (1995), Phys. Rev. A 52, R2493 — arXiv:quant-ph/9508027. The original 9-qubit code paper; introduces the phase-flip code as an intermediate step.
- Andrew Steane, Multiple particle interference and quantum error correction (1996) — arXiv:quant-ph/9601029. The companion early QEC paper; introduces the 7-qubit code.
- John Preskill, Lecture Notes on Quantum Computation, Chapter 7 — theory.caltech.edu/~preskill/ph229. The pedagogical presentation of bit-flip, phase-flip, and Shor codes, with the Hadamard duality explicit.
- Nielsen and Chuang, Quantum Computation and Quantum Information (2010), §10.1 (three-qubit codes) — Cambridge University Press.
- Daniel Gottesman, Stabilizer codes and quantum error correction (PhD thesis, 1997) — arXiv:quant-ph/9705052. The stabiliser-formalism treatment.
- Wikipedia, Quantum error correction — overview with all three-qubit codes side by side.