An interactive introduction to topological quantum error correction
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.
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.
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.
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).
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).
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$.
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.
Why does the toric code encode exactly 2 logical qubits? Let's count carefully for an $L \times L$ lattice.
| Quantity | Count | Reason |
|---|---|---|
| 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 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!
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.
Logical operators correspond to non-contractible loops on the torus — paths that wrap all the way around.
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.
Error correction on the toric code works by measuring syndromes and pairing anyons. Follow the walkthrough below, or experiment freely.
Prepare the code state. All stabilizers have eigenvalue $+1$. There are no anyons — the lattice is "clean."
Errors occur. Physical noise applies random Pauli errors on qubits. Each error excites adjacent stabilizers, creating anyons in pairs.
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.
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.
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.
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).
Where do qubits live in the toric code?
An $X$ error on a single qubit excites which stabilizers?
How many logical qubits does the toric code encode on a torus?
Why does the product of all star operators $\prod_v A_v$ equal the identity?
What is the code distance of an $L \times L$ toric code?