Skip to content

Phase flips and the Shor code

The repetition code ended with a problem. It catches a bit flip on any one qubit, but a phase flip, the ZZ gate, passes straight through it. This lesson fixes that, and the result is the first code ever found that protects a qubit against every kind of single-qubit error. Peter Shor published it in 1995.

A phase flip is a bit flip in another basis

Section titled “A phase flip is a bit flip in another basis”

Recall what ZZ does to the states ∣+⟩\ket{+} and ∣−⟩\ket{-}:

Z∣+⟩=∣−⟩,Z∣−⟩=∣+⟩.Z\ket{+} = \ket{-}, \qquad Z\ket{-} = \ket{+}.

In the basis {∣+⟩,∣−⟩}\{\ket{+}, \ket{-}\} a phase flip is a bit flip. That is the identity HZH=XHZH = X from the gates lesson seen from the other side. So we can reuse the whole repetition code, with ∣+⟩\ket{+} and ∣−⟩\ket{-} in place of ∣0⟩\ket{0} and ∣1⟩\ket{1}:

α∣0⟩+β∣1⟩  ⟼  α∣+++⟩+β∣−−−⟩.\alpha\ket{0} + \beta\ket{1} \;\longmapsto\; \alpha\ket{{+}{+}{+}} + \beta\ket{{-}{-}{-}}.

This is the three-qubit phase-flip code. To build it, encode with the two CNOTs you already know, then change basis with an HH on every qubit.

PuzzleEncode against phase flips

Build the encoder for the phase-flip code. The input on the top wire is ∣0⟩\ket{0}, which must become ∣+++⟩\ket{{+}{+}{+}}, an equal superposition of all eight bit strings. Your circuit is also tested on the input ∣1⟩\ket{1}, which must become ∣−−−⟩\ket{{-}{-}{-}}.

This puzzle needs JavaScript.

Hint

Three HH gates alone make ∣+++⟩\ket{{+}{+}{+}}, but they would send the input ∣1⟩\ket{1} to ∣−++⟩\ket{{-}{+}{+}}. Spread the top qubit's value with two CNOTs first.

For bit flips we asked whether neighbouring qubits agree in the ∣0⟩,∣1⟩\ket{0}, \ket{1} basis. For phase flips we ask whether they agree in the ∣+⟩,∣−⟩\ket{+}, \ket{-} basis:

  • Do qubits 0 and 1 have the same sign?
  • Do qubits 1 and 2 have the same sign?

Both parts of the encoded state, ∣+++⟩\ket{{+}{+}{+}} and ∣−−−⟩\ket{{-}{-}{-}}, answer yes to both questions. After a phase flip on the middle qubit they become ∣+−+⟩\ket{{+}{-}{+}} and ∣−+−⟩\ket{{-}{+}{-}}, and both answer no to both. As before, the answers locate the error and say nothing about α\alpha and β\beta.

How do we ask the question? “Do these two qubits have the same sign?” is a measurement of the operator X⊗XX \otimes X. A pair with the same sign is unchanged by it. A pair with different signs picks up a factor of −1-1:

(X⊗X)∣+−⟩=(X∣+⟩)(X∣−⟩)=∣+⟩⋅(−∣−⟩)=−∣+−⟩.(X\otimes X)\ket{{+}{-}} = (X\ket{+})(X\ket{-}) = \ket{+}\cdot\bigl(-\ket{-}\bigr) = -\ket{{+}{-}}.

To read that sign out, use phase kickback, the trick behind Deutsch’s algorithm. Put an ancilla in ∣+⟩\ket{+} and let it control an XX on each data qubit. If the data picks up −1-1 under X⊗XX\otimes X, that sign lands on the ∣1⟩\ket{1} part of the ancilla and turns ∣+⟩\ket{+} into ∣−⟩\ket{-}. A final HH converts this to a 0 or a 1.

PuzzleCompare two signs

The two data qubits are in ∣+−⟩\ket{{+}{-}}: their signs differ. Make the bottom wire end in ∣1⟩\ket{1} to record that, leaving the data exactly as it was. Your circuit is also tested on data with equal signs, where the bottom wire must end in ∣0⟩\ket{0}, and on two entangled states.

This puzzle needs JavaScript.

Hint

HH on the ancilla, a CNOT from the ancilla onto each data qubit, then HH on the ancilla again.

Compare this with the parity check of the last lesson. There the data qubits were the controls and the ancilla was the target. Here the CNOTs are turned around and wrapped in a pair of HH gates. One circuit measures Z⊗ZZ\otimes Z, the other measures X⊗XX\otimes X.

With two such measurements, on qubits 0 and 1 and on qubits 1 and 2, the syndrome table is the same as before with ZZ in place of XX:

Syndrome Meaning Correction
00 No phase flip Nothing
10 Qubit 0 has the odd sign ZZ on qubit 0
11 Qubit 1 has the odd sign ZZ on qubit 1
01 Qubit 2 has the odd sign ZZ on qubit 2
PuzzleRepair a phase flip

The encoded state ∣+++⟩\ket{{+}{+}{+}} has suffered a phase flip on one qubit. Read the signs, find it and repair it.

This puzzle needs JavaScript.

Hint

A ZZ on a qubit puts a minus sign on every term in which that qubit is 1. Look for the bit that all the negative terms share.

We have gained protection against ZZ and lost it against XX. Since X∣+⟩=∣+⟩X\ket{+} = \ket{+} and X∣−⟩=−∣−⟩X\ket{-} = -\ket{-}, a bit flip on any one qubit gives

α∣+++⟩+β∣−−−⟩  ⟶  α∣+++⟩−β∣−−−⟩.\alpha\ket{{+}{+}{+}} + \beta\ket{{-}{-}{-}} \;\longrightarrow\; \alpha\ket{{+}{+}{+}} - \beta\ket{{-}{-}{-}}.

No two qubits disagree in sign, so the syndrome is 00, and yet the encoded state has changed. The two codes fail in mirror-image ways.

Check

The phase-flip code corrects one phase flip. What happens if two of its qubits suffer phase flips?

Shor’s idea was to use both codes at once, one inside the other. This is called concatenation.

  1. Encode the qubit with the phase-flip code, giving three qubits.
  2. Encode each of those three with the bit-flip code, giving nine.

Under the first step ∣0⟩→∣+++⟩\ket{0} \to \ket{{+}{+}{+}}. Under the second, each ∣+⟩=12(∣0⟩+∣1⟩)\ket{+} = \tfrac{1}{\sqrt2}(\ket{0} + \ket{1}) becomes 12(∣000⟩+∣111⟩)\tfrac{1}{\sqrt2}(\ket{000} + \ket{111}), and each ∣−⟩\ket{-} becomes the same with a minus sign. So the two encoded basis states are

∣0L⟩=(∣000⟩+∣111⟩) (∣000⟩+∣111⟩) (∣000⟩+∣111⟩)22,\ket{0_L} = \frac{(\ket{000} + \ket{111})\,(\ket{000} + \ket{111})\,(\ket{000} + \ket{111})}{2\sqrt2}, ∣1L⟩=(∣000⟩−∣111⟩) (∣000⟩−∣111⟩) (∣000⟩−∣111⟩)22.\ket{1_L} = \frac{(\ket{000} - \ket{111})\,(\ket{000} - \ket{111})\,(\ket{000} - \ket{111})}{2\sqrt2}.

The subscript LL stands for logical: these are the states of the protected qubit, as opposed to the nine physical qubits that carry it. The nine fall into three blocks of three: qubits 0 to 2, 3 to 5, and 6 to 8.

PuzzleThe nine-qubit encoder

Build the encoder for the Shor code. The input on the top wire is ∣0⟩\ket{0}, which must become ∣0L⟩\ket{0_L}: eight terms, one for each way of choosing 000 or 111 in each block. Your circuit is also tested on the input ∣1⟩\ket{1}, which must become ∣1L⟩\ket{1_L}. Eleven gates do it.

This puzzle needs JavaScript.

Hint

Wires 0, 3 and 6 lead the three blocks. First run the phase-flip encoder on those three wires: two CNOTs from wire 0, then an HH on each. Then run the bit-flip encoder inside each block: two CNOTs from each leader. Gates on different wires can share a column.

A bit flip on any qubit is caught inside its own block. Each block is a little bit-flip code, so we measure the two Z⊗ZZ\otimes Z parities in each block, six checks in all, exactly as in the last lesson. The signs between the terms play no part in this.

A phase flip on any qubit changes the sign in its block: ∣000⟩+∣111⟩\ket{000} + \ket{111} becomes ∣000⟩−∣111⟩\ket{000} - \ket{111}. To find out which block, compare the signs of neighbouring blocks. That takes two more checks, each a product of six XX operators:

X0X1X2X3X4X5andX3X4X5X6X7X8.X_0X_1X_2X_3X_4X_5 \qquad\text{and}\qquad X_3X_4X_5X_6X_7X_8.

A subscript names the qubit a gate acts on. Three XX gates on a block leave ∣000⟩+∣111⟩\ket{000} + \ket{111} alone and put a minus sign on ∣000⟩−∣111⟩\ket{000} - \ket{111}, so each of these checks asks “do these two blocks have the same sign?”

Both at once, the gate Y=iXZY = iXZ, sets off both kinds of check, and the two corrections are made independently.

That is eight checks on nine qubits. Each check that is measured halves the space of allowed states, and 9−8=19 - 8 = 1: exactly one qubit’s worth of freedom is left over to hold the data. The next lesson turns this counting into a method.

Check

A phase flip hits qubit 4, in the middle block. Which checks report something?

Notice that the phase-flip syndrome names a block, not a qubit. That is enough. A ZZ on qubit 3, on qubit 4 or on qubit 5 has exactly the same effect on every encoded state, because all three turn ∣000⟩+∣111⟩\ket{000} + \ket{111} into ∣000⟩−∣111⟩\ket{000} - \ket{111}. We may undo the flip with a ZZ on any qubit of the block.

A code in which different errors act identically, and so need not be told apart, is called degenerate. Classical codes have nothing like it. It will matter again for the surface code.

Check

The real error was a phase flip on qubit 5. The correction applied a ZZ to qubit 3 instead. What is the result?

We have dealt with XX, ZZ and YY. Real noise is none of these exactly: it is a small unwanted rotation, or a leak of energy, or a stray measurement. Two facts from earlier lessons show that this does not matter.

First, from the lesson on quantum channels: the matrices II, XX, YY and ZZ form a basis for all 2×22\times2 matrices. So whatever happens to one qubit can be written as

E=a I+b X+c Y+d ZE = a\,I + b\,X + c\,Y + d\,Z

for some complex numbers a,b,c,da, b, c, d. Applied to an encoded state ∣ψL⟩\ket{\psi_L}, it gives a superposition of four things: the undamaged state, the state with a bit flip, with both flips, and with a phase flip.

Second, the digitisation of errors from the last lesson: those four states give four different syndromes, so measuring the syndrome collapses the superposition onto one of them. The continuous error becomes one definite error, chosen at random, and the syndrome says which.

So a code that corrects XX, ZZ and YY on a qubit corrects every possible error on that qubit. The Shor code does this for any one of its nine qubits. You could replace one qubit with a completely different one and the data would survive.

  • Nine physical qubits for one logical qubit. The next lesson brings this down to five, which turns out to be the minimum, and the one after it introduces a seven-qubit code that is easier to compute with.
  • One error only. Two errors in the wrong places produce a syndrome that points to the wrong correction.
  • Perfect checks assumed. We have taken for granted that the encoding and the syndrome measurements themselves work flawlessly. Removing that assumption is the subject of the lesson on fault tolerance.

Describing the code took a page of state vectors. The next lesson introduces a notation in which the Shor code is just a list of eight short strings, and in which finding a syndrome needs no state vectors at all.

RecallAnswer from memory, then check
LessonFinish this lesson