The toric code of Module 7 lives on a torus. A real chip is flat. You cannot wrap a 2D grid of qubits around a doughnut without long wires. The fix is to cut the torus open and give the lattice boundaries. Each boundary type lets one kind of anyon disappear into it. A logical operator is then a string that runs from one boundary to the opposite one, rather than a loop.
This module builds the two standard flat versions: the planar code
Learning goals. After this module you should be able to
- explain rough and smooth boundaries and why a string ending on a boundary has a single defect;
- derive the parameters of the planar and rotated surface codes, and write down the
rotated code with its logical operators; - draw the syndrome extraction circuits for X-type and Z-type checks and explain them with Pauli propagation through CNOTs;
- explain hook errors and choose a CNOT order that keeps the full distance.
1. Boundaries
Start from the toric code’s chain picture. A Z error string flips the stars (X-checks) at its two endpoints. On the torus there is nowhere for an endpoint to go, so defects are always created in pairs. Now remove the periodic identification and stop the lattice at an edge. There are two natural ways to do it.
- Smooth boundary. The lattice ends in a row of vertices. Stars on that row lose one edge and become weight-3 X-checks. Plaquettes stay complete.
- Rough boundary. The lattice ends in a row of dangling edges that stick out with no vertex at the far end. Plaquettes along that side lose an edge and become weight-3 Z-checks. Stars stay complete.
The key effect: a Z string can end on a rough boundary without creating a defect there. The dangling edge has only one endpoint inside the lattice, so a Z error on it flips only one star. Dually, an X string, which lives on the dual lattice, can end on a smooth boundary without a defect. Following Fowler et al., we often name boundaries by which checks are cut: the rough boundary carries the truncated Z-type checks, so we call it a Z-boundary. The smooth boundary carries truncated X-type checks, so it is an X-boundary.
Convention check
Which boundary is “rough” depends on whether X-checks sit on vertices (this course, Kitaev) or on faces (Pesah, for example). The statement that does not depend on the picture is:
runs between the two boundaries that carry the cut Z-type checks, and runs between the two boundaries that carry the cut X-type checks.
2. The planar code
Definition 8.1 (Planar surface code).Take a
grid of sites , .
- Data qubits sit at sites with
even. - X-checks (stars) sit at sites with
even and odd. - Z-checks (plaquettes) sit at sites with
odd and even. Each check acts on the data qubits among its four grid neighbours
, that lie inside the grid.
In the edge picture of Module 7, X-check sites are vertices. Data qubits at (even, even) sites are horizontal edges and those at (odd, odd) are vertical edges. The left and right columns of horizontal edges dangle. So the left and right sides are rough and the top and bottom are smooth.
Theorem 8.1 (Parameters of the planar code).The planar code is an
code. It has X-checks and Z-checks, all independent. With one ancilla per check, it uses physical qubits.
Proof
Counting. Sites with
and both even number , and those with both odd number , so . X-checks have choices of even and of odd . Z-checks are the same with roles swapped. So there are checks. Independence. Suppose a set
of X-checks multiplies to . Every data qubit must then be covered an even number of times by . A dangling edge on the left or right boundary touches only one star, so that star cannot be in . Along any edge, both endpoints are in or neither, and the vertex graph is connected. Hence . The same argument on the dual lattice works for Z-checks: the smooth boundaries play the role of the dangling edges. So and . Distance.
on the top row of horizontal edges, , has weight and commutes with every star: each star in row 0 touches two of these edges, all others touch none. on the left column of dangling edges, , has weight , commutes with every plaquette, and meets in one qubit. A nontrivial Z-logical has to anticommute with and with each of its translates (the column of horizontal edges at ). So it contains a horizontal edge in each of the columns and has weight at least . The same holds for X-logicals with rows. So .
We checked this by computer. For
- At
, a Z error on the left-boundary qubit flips one star, while a Z error on the bulk qubit flips two. Single defects are possible only at the matching boundary. - The
planar code has data qubits and checks, so qubits with ancillas. This is Fowler et al.’s standard example.
Compared with the toric code we lost one logical qubit:
3. The rotated surface code
The planar code spends many qubits along its boundaries. Rotate the lattice by 45 degrees, keep only the central diamond, and add weight-2 checks along the new boundaries. The result is the rotated surface code. It has the same distance with about half as many data qubits (Bombin and Martin-Delgado, 2007). In this picture, data qubits sit on the vertices of a
Definition 8.2 (Rotated surface code).Place
data qubits on a grid, row and column from to , with index . Each unit square with top-left qubit , , carries a weight-4 check: X-type if is even, Z-type if is odd. Along the boundary, add weight-2 checks on every second pair of neighbouring boundary qubits: X-type on the top and bottom edges and Z-type on the left and right edges, continuing the checkerboard pattern.
For
You can check commutation by eye. Every X-type and Z-type check overlap on 0 or 2 qubits. For example,
Proposition 8.1 (Parameters of the rotated code).The rotated surface code is a
code for odd . It has X-type and Z-type checks, all independent. With one ancilla per check it uses physical qubits. The logical operators can be chosen as
Proof
Counting. There are
bulk squares, half of each type (for odd the number is even), plus weight-2 checks on each of the four sides. That gives checks of each type. A rank computation (or an argument like the one for the planar code) shows they are independent, so . Distance.
on any full column commutes with every Z-check: a bulk Z-square covering columns and , or and , meets the column in two qubits, and the weight-2 Z-checks on the left and right sides meet it in zero or two. So the columns are equivalent copies of . A nontrivial Z-logical anticommutes with all of them, so it has a qubit in every column and weight . Rows play the same role for X-logicals, and the top row and left column attain weight .
Numerically: for
| distance | planar data / total | rotated data / total |
|---|---|---|
| 3 | 13 / 25 | 9 / 17 |
| 5 | 41 / 81 | 25 / 49 |
| 7 | 85 / 169 | 49 / 97 |
The 17-qubit
Example 8.1 (Syndrome table of the d = 3 rotated code).Order the Z-checks as
. They detect X errors. Order the X-checks as . They detect Z errors. Single-qubit errors give (computed):
qubit 0 1 2 3 4 5 6 7 8 0010 1000 1000 0110 1100 1001 0100 0100 0001 1000 1010 0010 1000 1100 0100 0001 0101 0100 Some syndromes repeat:
and both give . This is not a failure of the code. is itself a stabilizer (the top boundary check), so correcting with leaves , which does nothing. The same holds for , for and for . This is degeneracy (Module 3). Since every repeated syndrome belongs to such a pair, a lookup table that maps each syndrome to one of its errors corrects every single-qubit X, Y or Z error.
4. Measuring a check: syndrome extraction circuits
A check like
To see why this works, we need the propagation rules for Paulis through a CNOT (from Module 5):
CNOT copies X forward from control to target, and Z backward from target to control. We also need
Proposition 8.2 (What the circuits measure).With the ancilla prepared in
, the Z-check circuit (four CNOTs with data as controls, ancilla as target, then measure the ancilla in the Z basis) measures . The X-check circuit ( on the ancilla, four CNOTs with the ancilla as control, , measure) measures . In both cases the data qubits are left in the corresponding eigenstate.
Proof
Work in the Heisenberg picture. Measuring
at the end of a Clifford circuit is the same as measuring at the start. For the Z-check circuit, pull back through each CNOT (ancilla = target). Each step attaches a on that CNOT’s control, giving . The ancilla starts in , so and the outcome is the eigenvalue of . For the X-check, the final turns into . Pulling back through CNOTs where the ancilla is the control attaches on each target, and the first turns back into . The measured operator is . A Stim tableau computation of confirms both: it returns +ZZZZZand+XXXXZ, with the ancilla written last.
One full syndrome extraction round has four steps: reset all ancillas, apply Hadamards on the X-type ancillas, run four CNOT layers, then apply Hadamards again and measure. The Z-type ancillas idle during the Hadamard steps, so both kinds stay in lockstep. Fowler et al. count this as an 8-step “surface code cycle”.
5. Scheduling the CNOTs and hook errors
Each data qubit belongs to up to four checks, and each check needs four CNOTs. To finish in four layers, every layer must use each qubit at most once. Let the corners of a check be TL, TR, BL, BR (top-left, top-right, bottom-left, bottom-right). Two orders are natural:
- Z order: TL, TR, BL, BR (the path traces a “Z”);
- N order: TL, BL, TR, BR (the path traces an “N”).
The schedule must also measure the stabilizers correctly when X-checks and Z-checks share qubits, and it must not create harmful hook errors. For the layout in Definition 8.2, the two standard choices pass the first test. Using the same order for all X-checks and the same order for all Z-checks, we tried all four combinations in Stim. Each is collision-free and gives deterministic detectors. The second test is where they differ.
Definition 8.3 (Hook error).A hook error is a single fault on an ancilla, partway through its CNOT sequence, that spreads to two or more data qubits. If an X error hits an X-check ancilla (the control) after the second CNOT, it is copied onto the targets of CNOTs 3 and 4. A single fault thus becomes
on the last two data qubits the check touches. Likewise, a Z error on a Z-check ancilla (the target) after the second CNOT is copied backward onto the last two controls as .
Stim propagation confirms this. An X on the X-check ancilla after CNOT 2 becomes
A weight-2 error from one fault is harmless as long as it does not line up with a logical operator. It is dangerous when it does.
Remark 8.1 (The hook rule).To keep the full circuit-level distance, the last two qubits touched by each check must form a pair perpendicular to the logical operator built from that check’s error type. In the layout of Definition 8.2 (
vertical, horizontal), X-checks should use the Z order (the last pair is horizontal) and Z-checks the N order (the last pair is vertical).
This is a design rule, not a theorem. We checked it with a Stim simulation of a
| X-check order / Z-check order | memory-Z distance | memory-X distance | |
|---|---|---|---|
| 3 | Z / N (correct) | 3 | 3 |
| 3 | Z / Z | 3 | 2 |
| 3 | N / N | 2 | 3 |
| 3 | N / Z | 2 | 2 |
| 5 | Z / N (correct) | 5 | 5 |
| 5 | Z / Z | 5 | 3 |
| 5 | N / N | 3 | 5 |
| 5 | N / Z | 3 | 3 |
| 7 | Z / N (correct) | 7 | 7 |
| 7 | Z / Z | 7 | 4 |
| 7 | N / N | 4 | 7 |
| 7 | N / Z | 4 | 4 |
A wrong order reduces the distance in the affected basis to
These absolute rates depend on the noise model, so do not compare them directly with Module 10. Stim’s built-in surface_code:rotated_memory_z generator with the same
A silent bug
Every ordering in the table is a valid, collision-free circuit with deterministic detectors. Nothing crashes. If you only ever simulate memory-Z, the Z/Z ordering looks perfect. Always test both bases when you write a surface code circuit generator.
6. Running the code: initialization and readout
A memory experiment is the basic benchmark: store a logical state for
- Prepare
. Reset every data qubit to . All Z-checks then start at for sure. The X-checks give random outcomes in the first round, and these outcomes fix the state’s X-check signs, which are tracked from then on. The result is a valid eigenstate. To prepare , reset to instead. - Run
rounds of syndrome extraction. - Read out
. Measure every data qubit in the Z basis and take the parity of the outcomes along a string, for example qubits . One data qubit is not enough. Unlike the repetition code, the logical value is a parity of qubits. The same data outcomes also give one final value of every Z-check, which is used for decoding.
Turning these measurement records into something a decoder can use is the subject of Module 9.
7. Beyond memory: computing with patches
A surface code patch stores one qubit. For computation the standard approach is lattice surgery (Horsman, Fowler, Devitt and Van Meter, 2012). Two patches placed side by side are temporarily merged by measuring new checks along their shared boundary. The product of the new check outcomes gives a joint logical measurement such as
Summary
- Cutting the torus open creates boundaries. A rough (Z-)boundary absorbs Z strings and a smooth (X-)boundary absorbs X strings, so a single defect can appear next to a boundary.
- The planar code (Definition 8.1) is
, with independent checks and qubits including ancillas (Theorem 8.1). - The rotated code (Definition 8.2) is
with checks and qubits (Proposition 8.1). joins the Z-type boundaries and joins the X-type boundaries. At every single-qubit error can be corrected from the syndrome (Example 8.1). - Checks are measured by copying parities onto ancillas with CNOTs (Proposition 8.2). CNOTs copy X forward and Z backward.
- A fault on an ancilla can spread to two data qubits (Definition 8.3). Ordering the CNOTs so that the last pair is perpendicular to the matching logical (Remark 8.1) preserves distance
. The wrong order gives .
Further reading
- S. B. Bravyi, A. Yu. Kitaev, Quantum codes on a lattice with boundary, arXiv:quant-ph/9811052. The planar code.
- A. G. Fowler, M. Mariantoni, J. M. Martinis, A. N. Cleland, Surface codes: Towards practical large-scale quantum computation, Phys. Rev. A 86, 032324 (2012), arXiv:1208.0928. Circuits, the surface code cycle, X/Z boundaries.
- H. Bombin, M. A. Martin-Delgado, Optimal resources for topological two-dimensional stabilizer codes: Comparative study, Phys. Rev. A 76, 012305 (2007), arXiv:quant-ph/0703272. The rotated layout.
- Y. Tomita, K. M. Svore, Low-distance surface codes under realistic quantum noise, Phys. Rev. A 90, 062320 (2014), arXiv:1404.3747. The 17-qubit code and CNOT ordering.
- C. Horsman, A. G. Fowler, S. Devitt, R. Van Meter, Surface code quantum computing by lattice surgery, New J. Phys. 14, 123011 (2012), arXiv:1111.4022.
- D. Litinski, A Game of Surface Codes: Large-Scale Quantum Computing with Lattice Surgery, Quantum 3, 128 (2019), arXiv:1808.02892.
- Google Quantum AI, Quantum error correction below the surface code threshold, Nature 638, 920 (2025), arXiv:2408.13687.
- Google Quantum AI / Coursera, Quantum error correction IV slides (rotated code, CNOT order and hook errors) and the Surface code in Stim lab.