Course Home
⚠ Math rendering unavailable — KaTeX library could not be loaded. Formulas will appear as raw LaTeX.

The Toric Code

An interactive introduction to topological quantum error correction

1. What is the Toric Code?

The toric code is a topological quantum error-correcting code introduced by A. Kitaev in 1997. It is one of the most important models in quantum information, because it demonstrates how topology can protect quantum information from local errors.

The key idea: place qubits on the edges of a square lattice that wraps around on a torus (periodic boundary conditions). Then define two types of local check operators — star operators ($A_v$) at vertices and plaquette operators ($B_p$) at faces.

The code encodes 2 logical qubits in an $L \times L$ lattice using $2L^2$ physical qubits. Errors are detected as excitations of these operators, and logical information is stored in non-local, topological degrees of freedom.

Key properties:
  • Qubits live on edges of a square lattice
  • Star operators $A_v$ = product of $X$ on edges around a vertex
  • Plaquette operators $B_p$ = product of $Z$ on edges around a face
  • All stabilizers commute: $[A_v, B_p] = 0$
  • Code distance $= L$ (the lattice size)
  • Logical operators are non-contractible loops on the torus
$$A_v = \prod_{e \in \text{star}(v)} X_e \qquad B_p = \prod_{e \in \partial p} Z_e$$

2. Visualizing the Torus

The toric code lives on a lattice wrapped around a torus. The left edge is identified with the right, and the top with the bottom. Drag to rotate the 3D torus below — the red and blue loops show the two topologically distinct non-contractible cycles.

Drag to rotate. The two colored loops cannot be shrunk to a point — they represent the two independent logical operators of the code.

3. Interactive Lattice Explorer

Click or drag on edges (qubits) to apply error chains and see how the syndrome changes. Use the tools below to switch between X errors, Z errors, and to visualize stabilizer operators.

Qubit (edge)
Vertex / star
X error
Z error
e-anyon
m-anyon
Click or drag across edges to place X errors. Syndromes appear at adjacent plaquettes.
X X Error   Z Z Error   S Star   P Plaquette   R Reset

4. Stabilizer Operators

Star Operator $A_v$

The star operator at vertex $v$ acts with Pauli $X$ on all edges touching that vertex. On a square lattice, each vertex has 4 adjacent edges:

$$A_v = X_{e_1} X_{e_2} X_{e_3} X_{e_4}$$

$A_v$ detects Z errors: if a $Z$ error occurs on an edge, the two star operators at its endpoints will be excited (eigenvalue $-1$). These excitations are called e-anyons (electric charges).

Key insight: Z errors always create e-anyons in pairs at the endpoints of error chains. A single isolated excitation is impossible!

Plaquette Operator $B_p$

The plaquette operator at face $p$ acts with Pauli $Z$ on all edges bounding that face:

$$B_p = Z_{e_1} Z_{e_2} Z_{e_3} Z_{e_4}$$

$B_p$ detects X errors: if an $X$ error occurs on an edge, the two plaquette operators sharing that edge will be excited. These excitations are called m-anyons (magnetic fluxes).

Key insight: X errors always create m-anyons in pairs at the faces on either side. Anyons are always born in pairs!

Why do all stabilizers commute?

Any star operator (all $X$) and any plaquette operator (all $Z$) share either 0 or exactly 2 edges. Since $X$ and $Z$ anticommute on a single qubit, sharing 2 qubits gives $(-1)^2 = +1$. Therefore $[A_v, B_p] = 0$ for all $v, p$.

5. Interactive Stabilizer Measurement

In the toric code, we detect errors by measuring all stabilizer operators. Each measurement returns $+1$ (no error detected) or $-1$ (anyon present). Click on vertices or face centers below to measure them and watch eigenvalues change as you add errors.

Click a vertex or face center to measure its stabilizer. Green ($+1$) = no excitation, Red ($-1$) = anyon detected!
Try this: Place a Z error on one edge, then click the two vertices at its endpoints. You'll see both flip from $+1$ to $-1$ — the error creates an e-anyon pair. Now place another Z error on an adjacent edge to "move" the anyon.

6. Counting the Code Space

Why does the toric code encode exactly 2 logical qubits? Let's count carefully for an $L \times L$ lattice.

QuantityCountReason
Physical qubits (edges)$2L^2$$L^2$ horizontal + $L^2$ vertical
Star operators $A_v$$L^2$One per vertex
Independent star constraints$L^2 - 1$$\prod_v A_v = I$ (one relation)
Plaquette operators $B_p$$L^2$One per face
Independent plaquette constraints$L^2 - 1$$\prod_p B_p = I$ (one relation)
Total independent stabilizers$2L^2 - 2$Sum of above
Logical qubits $k$$2$$k = n - s = 2L^2 - (2L^2 - 2) = 2$
The formula: For a stabilizer code with $n$ physical qubits and $s$ independent stabilizer generators, the number of encoded logical qubits is $k = n - s$. For the toric code: $k = 2L^2 - (2L^2 - 2) = 2$.

The key subtlety: there is one global constraint among all star operators ($\prod_v A_v = I$, because every edge touches exactly two vertices and $X^2 = I$), and similarly one constraint among all plaquette operators. This removes 2 from the stabilizer count, giving us exactly 2 logical qubits — a topological invariant of the torus!

7. Code Parameter Explorer

Adjust the lattice size $L$ below to see how all the code parameters scale. The toric code is a $[[n, k, d]]$ quantum error-correcting code.

L = 5
215
Physical Qubits $n$
50
$2L^2$
Logical Qubits $k$
2
always $2$
Code Distance $d$
5
$= L$
Stabilizers
48
$2L^2 - 2$
Encoding Rate $k/n$
4.0%
$2 / 2L^2$
Correctable Errors
2
$\lfloor (d-1)/2 \rfloor$
Trade-off: Increasing $L$ gives better protection (higher $d$), but at the cost of more physical qubits ($\propto L^2$). The encoding rate $k/n \to 0$ as $L \to \infty$ — a constant overhead for topological protection.

8. Logical Operators & Topology

Logical operators correspond to non-contractible loops on the torus — paths that wrap all the way around.

$\bar{X}_1$: a horizontal chain of $X$ operators wrapping around the torus. Commutes with all stabilizers but is not itself a stabilizer product.
Logical qubit 1:
  • $\bar{X}_1$ = chain of $X$ along a horizontal non-contractible loop
  • $\bar{Z}_1$ = chain of $Z$ along a vertical non-contractible loop of the dual lattice
Logical qubit 2:
  • $\bar{X}_2$ = chain of $X$ along a vertical non-contractible loop
  • $\bar{Z}_2$ = chain of $Z$ along a horizontal non-contractible loop of the dual lattice

These loops commute with all stabilizers (they cross each stabilizer an even number of times) but are logically distinct from the identity. The code distance is $L$ — an error must corrupt at least $L$ physical qubits to create a logical error.

9. Error Correction — Step by Step

Error correction on the toric code works by measuring syndromes and pairing anyons. Follow the walkthrough below, or experiment freely.

Add errors, then click Correct to see minimum-weight matching pair the anyons.
Step 1 of 5

Prepare the code state. All stabilizers have eigenvalue $+1$. There are no anyons — the lattice is "clean."

Step 2 of 5

Errors occur. Physical noise applies random Pauli errors on qubits. Each error excites adjacent stabilizers, creating anyons in pairs.

Step 3 of 5

Measure syndromes. We measure all $A_v$ and $B_p$ to find which ones are excited ($-1$ eigenvalue). This tells us the locations of the anyons but not the specific error that created them.

Step 4 of 5

Pair anyons using MWPM (Minimum-Weight Perfect Matching). The decoder finds the minimum-weight set of paths that connects all anyons in pairs. This is the most likely error consistent with the observed syndrome.

Step 5 of 5

Apply corrections. Apply correction operators along the pairing paths. If the combined error + correction forms a contractible loop, we succeed! If it forms a non-contractible loop, we've introduced a logical error.

MWPM decoder: Minimum-Weight Perfect Matching finds the lowest-cost way to pair all anyons. On a torus, “cost” = the shortest path distance between each pair, respecting periodic boundaries. It runs in polynomial time and is the standard decoder for surface codes.
Threshold theorem: The toric code has an error threshold of $\sim 10.3\%$ for independent depolarizing noise. Below this rate, increasing $L$ suppresses the logical error rate exponentially.

10. Challenge Mode

Can you be the decoder? Hidden errors have been placed on the lattice. Your job: click edges to apply corrections that eliminate all anyons without creating a logical error (non-contractible loop).

Click on edges to place correction operators. Try to pair all anyons with the shortest paths!

11. Check Your Understanding

0 / 5

Question 1

Where do qubits live in the toric code?

Question 2

An $X$ error on a single qubit excites which stabilizers?

Question 3

How many logical qubits does the toric code encode on a torus?

Question 4

Why does the product of all star operators $\prod_v A_v$ equal the identity?

Question 5

What is the code distance of an $L \times L$ toric code?