|q⟩ Bad Qubits

intermediate · Programming · Quantum Error Detection (Intro)

The Three-Qubit Phase-Flip Code

The three-qubit bit-flip code (module 14, lessons 3–5) protected against XX errors by encoding each logical qubit as three physical qubits in the ZZ basis. A phase-flip error is the ZZ gate, which does nothing to 0|0\rangle but flips the sign of 1|1\rangle:

Z0=0,Z1=1.Z|0\rangle = |0\rangle, \qquad Z|1\rangle = -|1\rangle.

A ZZ error on the logical +=12(0+1)|+\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle + |1\rangle) state turns it into =12(01)|-\rangle = \tfrac{1}{\sqrt{2}}(|0\rangle - |1\rangle), flipping the relative phase. Since ZZ commutes with the computational basis it is invisible to the bit-flip code. We need a different approach.

Basis change: from Z errors to X errors

The key insight is that ZZ and XX are related by the Hadamard:

HZH=X.H Z H = X.

This means a ZZ error in the ZZ basis looks like an XX error in the XX basis. If we first rotate every physical qubit from the ZZ basis into the XX basis by applying HH, a phase-flip ZZ becomes a bit-flip XX — and the bit-flip code already handles those.

Encoding procedure

Starting from a logical qubit in state ψL=α0+β1|\psi_L\rangle = \alpha|0\rangle + \beta|1\rangle:

  1. Fan out using two CNOTs so qubits 1 and 2 mirror qubit 0: (α0+β1)00CNOT01,CNOT02α000+β111.(\alpha|0\rangle + \beta|1\rangle)|00\rangle \xrightarrow{CNOT_{01},\,CNOT_{02}} \alpha|000\rangle + \beta|111\rangle.

  2. Rotate into the X basis by applying HH to every qubit: α000+β111H3α++++β.\alpha|000\rangle + \beta|111\rangle \xrightarrow{H^{\otimes 3}} \alpha|{+}{+}{+}\rangle + \beta|{-}{-}{-}\rangle.

Here +=H0|{+}\rangle = H|0\rangle and =H1|{-}\rangle = H|1\rangle. The logical codewords are therefore

0L=+++,1L=.|0_L\rangle = |{+}{+}{+}\rangle, \qquad |1_L\rangle = |{-}{-}{-}\rangle.

A phase-flip ZZ on any single physical qubit maps +|{+}\rangle \leftrightarrow |{-}\rangle on that qubit — exactly a bit flip in the XX basis. Majority-vote decoding (in the XX basis) then corrects any single-qubit ZZ error.

Why this works

After encoding, a ZZ error on physical qubit kk sends +Z+=|{+}\rangle \to Z|{+}\rangle = |{-}\rangle on that qubit. Applying H3H^{\otimes 3} before measuring converts this to a plain XX error in the computational basis, and the two-ancilla parity checks Z0Z1Z_0 Z_1 and Z1Z2Z_1 Z_2 (syndrome measurement, lesson 4) locate and correct it. The original logical state is recovered by the inverse process.

The three-qubit phase-flip code corrects any single-qubit phase-flip error with only three physical qubits and two ancilla parity checks, at the cost of being unable to correct bit-flip errors — a trade-off that motivates the nine-qubit Shor code, which combines both codes.

Try it

Build the encoding circuit for the logical +=H0|+\rangle = H|0\rangle state. The grader checks the full three-qubit statevector, which must equal α++++β\alpha|{+}{+}{+}\rangle + \beta|{-}{-}{-}\rangle for α=β=12\alpha = \beta = \tfrac{1}{\sqrt{2}}.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.