A Topological Proof That the Surface Code Has Distance $L$

From intuition to proof

If you've read an introduction to the toric code — say, Arthur Pesah's excellent interactive post — you've probably seen the claim that the toric code on an $L \times L$ lattice has distance $L$. It's worth examining the standard justification for this claim carefully, because one of its steps is doing more work than it might seem.

The standard argument and its gap

Recall that the distance of a quantum error-correcting code is the minimum weight of any non-trivial logical operator. To show the toric code has distance $L$, we need to verify two things:

  1. There exists a non-trivial logical operator of weight $L$ (so $d \leq L$).
  2. Every non-trivial logical operator has weight at least $L$ (so $d \geq L$).

The argument, as typically presented, has three steps:

  1. Logical operators are loops on the lattice
  2. There exist non-contractible loops on the lattice of length $L$, giving non-trivial logical operators of weight $L$.
  3. No non-contractible loop on the lattice has length less than $L$, so no non-trivial logical operator has weight less than $L$.

Steps 1 and 2 are straightforward: Step 1 follows directly from the stabilizer formalism, and Step 2 is proved by exhibiting a specific loop. Step 3 is more subtle: the claim that no non-contractible loop has length less than $L$ is geometrically intuitive but requires a careful argument. It feels obvious — of course you need at least $L$ edges to get around an $L \times L$ torus. But "wrapping all the way around" is doing a lot of informal work here. A non-contractible loop doesn't have to look like a straight horizontal line — it could zig-zag, include vertical segments, wander wildly across the lattice. Why can't some clever squiggly path wrap around the torus using fewer than $L$ edges? The geometric intuition is compelling, but it isn't a proof. The language of homology gives us the tools to make it one: it classifies loops by how they wrap around the torus, turning a geometric question into a linear algebra problem.

What you'll need

No prior knowledge of algebraic topology is required — we build up everything we need from scratch.

Roadmap

We proceed as follows. Sections 1–3 set up the lattice, define the chain complex, and introduce cycles, boundaries, and the first homology group $H_1$. Section 4 connects this formalism to the toric code, showing that non-trivial X-type logicals correspond to non-trivial elements of $H_1$, and introduces the dual lattice for Z-type logicals. Section 5 computes $\dim(H_1) = 2$ using rank-nullity. Section 6 defines the invariants $h$ and $v$ and establishes their key properties. Section 7 proves that these invariants give an isomorphism $H_1 \cong \mathbb{F}_2^2$. Finally, Section 8 combines everything to prove that the distance is exactly $L$.


1. The Lattice

We work with the surface code on a torus, sometimes called the toric code. The lattice is an $L \times L$ square grid, with all arithmetic on coordinates taken modulo $L$.

Diagram of the 4 by 4 toric code lattice with edge qubits and highlighted X and Z stabilizers.
The 4×4 toric code lattice. Qubits sit on edges; X-type stabilizers act on the four edges of a face (violet); Z-type stabilizers act on the four edges incident to a vertex (teal).

Each edge borders exactly two faces. Specifically:

Each vertex is incident to exactly four edges: $H(x-1,y)$, $H(x,y)$, $V(x,y-1)$, $V(x,y)$.

The toric code places one qubit on each of the $2L^2$ edges and defines:

Before diving into the algebra, it's worth getting a feel for the lattice. The interactive visualizer below lets you insert errors, apply stabilizers, and see the syndrome — play around before reading on.

Show interactive surface code visualizer
Review — The Lattice
In the toric code on an $L \times L$ lattice, where are the qubits placed?
On the edges of the lattice. There are $2L^2$ qubits total ($L^2$ horizontal edges and $L^2$ vertical edges).
Recalled?
What are the two types of stabilizers in the toric code?
X-type (plaquette) stabilizers, which apply $X$ to the four edges bounding a face, and Z-type (vertex) stabilizers, which apply $Z$ to the four edges incident to a vertex.
Recalled?
On the torus, how many faces does each edge border?
Exactly two, because of the periodic boundary conditions. (No edge is on a "boundary" of the lattice.)
Recalled?

2. The Chain Complex over $\mathbb{F}_2$

Our goal is to reason algebraically about subsets of edges — which ones form closed loops, which ones are the perimeter of a set of faces, and how to tell the difference. To do this, we represent edge-sets (and vertex-sets, and face-sets) as vectors over $\mathbb{F}_2$, and the geometric notion of "taking the boundary" becomes a linear map. This lets us use linear algebra to study the topology of the torus.

The lattice has three kinds of objects, which we organize by dimension: vertices (0-dimensional), edges (1-dimensional), and faces (2-dimensional). We call these 0-cells, 1-cells, and 2-cells respectively.

To reason about them algebraically, we represent subsets of each as vectors over $\mathbb{F}_2 = \{0, 1\}$ (arithmetic mod 2). A $k$-chain is a subset of $k$-cells, where addition corresponds to symmetric difference. The space of $k$-chains is:

The weight of a 1-chain $c \in C_1$, denoted $|c|$, is the number of edges it contains.

The key observation is that these three spaces are connected by a chain of linear maps, each going one dimension down: $\partial_2$ sends faces to edges (their perimeter), and $\partial_1$ sends edges to vertices (their endpoints). Together they form the chain complex

$$C_2 \xrightarrow{\partial_2} C_1 \xrightarrow{\partial_1} C_0.$$
Definition (Boundary maps)

We define two linear maps.

$\partial_2 : C_2 \to C_1$ sends a single face $F(x,y)$ to the sum of its four boundary edges: $$\partial_2(F(x,y)) = H(x,y) + H(x,y+1) + V(x,y) + V(x+1,y)$$

$\partial_1 : C_1 \to C_0$ sends a single edge to the sum of its two endpoints:

Both maps extend linearly. For a subset of faces $\mathcal{F} \subseteq C_2$, we have $\partial_2(\mathcal{F}) = \sum_{f \in \mathcal{F}} \partial_2(f)$ (sum mod 2), so an edge $e$ is in $\partial_2(\mathcal{F})$ if and only if an odd number of faces in $\mathcal{F}$ contain $e$ on their boundary. Since each edge borders exactly two faces, this is equivalent to: $e \in \partial_2(\mathcal{F})$ if and only if exactly one of the two bordering faces is in $\mathcal{F}$.

For a subset of edges $E$, a vertex $v$ is in $\partial_1(E)$ if and only if an odd number of edges in $E$ are incident to $v$.

Example. Let's compute some concrete boundary maps on a 3×3 torus.

First, $\partial_1$ applied to a single edge: $\partial_1(H(0,0)) = (0,0) + (1,0)$. The two endpoints are the only vertices in the boundary.

Next, $\partial_2$ applied to a single face $F(0,0)$: $$\partial_2(F(0,0)) = H(0,0) + H(0,1) + V(0,0) + V(1,0)$$ This is the set of four edges forming the perimeter of the face.

Now consider $\partial_2$ applied to two adjacent faces $F(0,0)$ and $F(1,0)$ (side by side horizontally). Their shared boundary edge is $V(1,0)$. Over $\mathbb{F}_2$: $$\partial_2(F(0,0)) + \partial_2(F(1,0)) = H(0,0) + H(0,1) + V(0,0) + V(1,0) + H(1,0) + H(1,1) + V(1,0) + V(2,0)$$ The edge $V(1,0)$ appears twice and cancels, leaving six edges forming the outer perimeter of the two-face rectangle. This illustrates the general principle: shared (interior) edges always cancel, so $\partial_2(\mathcal{F})$ is the outer boundary of $\mathcal{F}$.

Before defining cycles and boundaries, we establish a key property: applying $\partial_1$ after $\partial_2$ always gives zero. This is the algebraic expression of the geometric fact that the boundary of a boundary is empty — the perimeter of any set of faces has no dangling endpoints.

Lemma 2.1
$\partial_1 \circ \partial_2 = 0$.
Proof. It suffices to check on a single face $F(x,y)$: $$\begin{aligned} \partial_1(\partial_2(F(x,y))) &= \partial_1(H(x,y)) + \partial_1(H(x,y+1)) + \partial_1(V(x,y)) + \partial_1(V(x+1,y)) \\ &= [(x,y)+(x+1,y)] + [(x,y+1)+(x+1,y+1)] \\ &\quad + [(x,y)+(x,y+1)] + [(x+1,y)+(x+1,y+1)]. \end{aligned}$$ Each of the four corner vertices $(x,y)$, $(x+1,y)$, $(x,y+1)$, $(x+1,y+1)$ appears exactly twice, so each cancels over $\mathbb{F}_2$, giving $0$. $\square$
Review — The Chain Complex
What does the boundary map $\partial_2$ do to a single face $F(x,y)$?
It returns the sum (over $\mathbb{F}_2$) of the four edges bounding that face: $H(x,y) + H(x,y+1) + V(x,y) + V(x+1,y)$.
Recalled?
For a set of faces $\mathcal{F}$, when is an edge $e$ in $\partial_2(\mathcal{F})$?
When exactly one of the two faces bordering $e$ is in $\mathcal{F}$.
Recalled?
What does the property $\partial_1 \circ \partial_2 = 0$ mean geometrically?
The boundary of a boundary is empty — the perimeter of any set of faces has no dangling endpoints (every vertex appears an even number of times).
Recalled?

3. Cycles, Boundaries, and Homology

With the boundary maps in hand, we can now name the two kinds of edge-sets that matter for the toric code. A cycle is an edge-set with no dangling ends — every vertex has even degree. This includes single closed loops, disjoint unions of loops, and even the empty set. A boundary is the perimeter of some set of faces — a cycle that can be built by combining face boundaries. Every boundary is automatically a cycle (perimeters don't have dangling ends), but the converse fails: some cycles wrap around the torus without being the boundary of any set of faces. These non-boundary cycles are exactly the non-trivial logical operators of the code, and measuring "how many" of them there are is the purpose of the first homology group.

Definition

Example. We illustrate these definitions on a 3×3 torus.

An edge-set that is not a cycle: Take the single edge $H(0,0)$. The vertices $(0,0)$ and $(1,0)$ each have degree 1 in this edge-set — odd — so the cycle condition fails. This edge-set is not in $Z_1$.

A cycle that is a boundary: Take the four edges forming the perimeter of face $F(0,0)$: $\{H(0,0), H(0,1), V(0,0), V(1,0)\}$. Every vertex has degree 0 or 2 (the four corners each have degree 2), so this is a cycle. It equals $\partial_2(F(0,0))$, so it is also a boundary: it lies in $B_1$.

A cycle that is not a boundary: Take the horizontal loop $c_h = \{H(0,0), H(1,0), H(2,0)\}$, the full row of horizontal edges at $y=0$. Every vertex on row $y=0$ has degree 2, and all other vertices have degree 0, so $c_h \in Z_1$. But $c_h \notin B_1$.

Three lattice fragments showing a non-cycle, a cycle that is a boundary, and a cycle that is not a boundary.
Three edge-sets on a 3×3 torus. Left: a single edge is not a cycle (vertices have odd degree). Middle: the boundary of a face is both a cycle and a boundary. Right: the horizontal loop $c_h$ is a cycle but not a boundary — it wraps around the torus without enclosing any set of faces.
Lemma 3.1
$B_1 \subseteq Z_1$. Every boundary is a cycle.
Proof. If $c = \partial_2(\mathcal{F})$, then $\partial_1(c) = \partial_1(\partial_2(\mathcal{F})) = 0$ by Lemma 2.1. $\square$

We can also see this geometrically: $\partial_2(\mathcal{F})$ consists of all edges bordering exactly one face of $\mathcal{F}$. At any vertex $v$, consider the (up to four) faces meeting at $v$. Each face in $\mathcal{F}$ that meets $v$ contributes exactly 2 of its boundary edges incident to $v$. Over $\mathbb{F}_2$, each such face contributes an even number of incident boundary-edges in $\partial_2(\mathcal{F})$. The total count of edges of $\partial_2(\mathcal{F})$ incident to $v$ is a sum of even numbers, hence even.

Since two cycles that differ by a boundary represent the same logical operator, we want to group them together. The right algebraic structure for this is the quotient $Z_1 / B_1$, which we call the first homology group.

Definition (First homology group)
$$H_1 = Z_1 / B_1$$

Two cycles $c, c'$ are homologous ($c \sim c'$) if $c + c' \in B_1$. They represent the same coset of $H_1$.

Review — Cycles, Boundaries, Homology
What is a 1-cycle?
An edge-set where every vertex has even degree — 0, 2, or 4 edges of the set meet at each vertex.
Recalled?
What is a 1-boundary?
An edge-set that equals $\partial_2(\mathcal{F})$ for some set of faces $\mathcal{F}$ — the perimeter of a set of faces.
Recalled?
Why is every boundary automatically a cycle?
Because $\partial_1 \circ \partial_2 = 0$: applying $\partial_1$ to any boundary gives 0, meaning every vertex has even degree.
Recalled?
When are two cycles $c$ and $c'$ homologous?
When they differ by a boundary: $c + c' \in B_1$. Equivalently, they represent the same element of $H_1 = Z_1 / B_1$.
Recalled?

4. Connection to the Toric Code

We now connect the homological machinery of Sections 2–3 to the toric code defined in Section 1. The punchline: non-trivial X-type logicals correspond exactly to non-trivial elements of $H_1$ (cycles that aren't boundaries), and the same holds for Z-type logicals on the dual lattice. This means the distance question — "what is the minimum-weight non-trivial logical?" — becomes a question about homology: "what is the minimum-weight non-trivial cycle?"

4.1 X-type logicals as non-trivial cycles

An X-type Pauli operator is specified by an edge-subset $c$ (apply $X$ to edges in $c$ and $I$ elsewhere). We now show that the X-type logical operators correspond exactly to non-trivial elements of $H_1$.

Claim 4.1
An X-type operator on edge-set $c$ commutes with all Z-type stabilizers if and only if $c \in Z_1$.
Proof. Consider the vertex stabilizer at $v$, which applies $Z$ to the four edges incident to $v$. The X-type operator on $c$ applies $X$ to the edges in $c$. On each qubit (edge), the single-qubit operators are either $X$ and $Z$ (if the edge is in both $c$ and the stabilizer's support), or $X$ and $I$, or $I$ and $Z$, or $I$ and $I$. Since $X$ and $Z$ anticommute on a single qubit while all other pairs commute, the full operators commute if and only if they overlap on an even number of qubits. (Each anticommuting pair contributes a factor of $-1$; the total sign is $(-1)^{|c \cap \operatorname{supp}(v)|}$.)

The overlap $|c \cap \operatorname{supp}(v)|$ is exactly the number of edges in $c$ incident to $v$. So the X-type operator on $c$ commutes with the vertex stabilizer at $v$ if and only if $v$ has even degree in $c$. This holds for all $v$ if and only if $c \in Z_1$. $\square$

Claim 4.2
An X-type operator on edge-set $c$ is a product of plaquette stabilizers if and only if $c \in B_1$.
Proof. Each plaquette stabilizer for face $f$ is the X-type operator on $\partial_2(f)$ (the four boundary edges of $f$). Since the toric code is defined over $\mathbb{F}_2$ (applying the same Pauli twice gives the identity), a product of plaquette stabilizers for a face-set $\mathcal{F}$ is the X-type operator on $\sum_{f \in \mathcal{F}} \partial_2(f) = \partial_2(\mathcal{F})$. Conversely, every element of $B_1 = \operatorname{im}(\partial_2)$ is $\partial_2(\mathcal{F})$ for some $\mathcal{F}$, and the corresponding X-type operator is the product of those plaquettes. $\square$

Combining these:

Since the toric code is a CSS code, we can define the X-distance $d_X$ and Z-distance $d_Z$ separately: $d_X$ is the minimum weight of a non-trivial X-type logical operator, and $d_Z$ is the minimum weight of a non-trivial Z-type logical operator. (We will show in Section 8.3 that the full distance satisfies $d = \min(d_X, d_Z)$.) By the correspondence above, the X-distance is: $$d_X = \min\{|c| : c \in Z_1 \setminus B_1\}$$ i.e., the minimum weight of a non-trivial cycle (one that is not a boundary).

4.2 Z-type logicals and the dual lattice

By a completely analogous argument, Z-type logical operators correspond to non-trivial cycles of a dual chain complex. We now make this precise.

Definition (Dual lattice)
The dual lattice of our $L \times L$ torus is constructed by:

The dual lattice is again an $L \times L$ square lattice on a torus: $L^2$ dual vertices, $2L^2$ dual edges, and $L^2$ dual faces, with the same local structure (every dual vertex has degree 4, every dual face has 4 boundary edges).

The key correspondence. A Z-type operator on primal edge-set $c$ applies $Z$ to each edge in $c$. It commutes with all X-type (plaquette) stabilizers if and only if, for every face $f$, the number of edges of $c$ on the boundary of $f$ is even. Under the primal-to-dual correspondence $e \leftrightarrow e^*$, this is exactly the condition that the dual edge-set $c^* = \{e^* : e \in c\}$ has even degree at every dual vertex — i.e., $c^*$ is a cycle in the dual lattice.

Similarly, the Z-type operator on $c$ is a product of vertex stabilizers if and only if $c^*$ is a boundary in the dual lattice (since each primal vertex stabilizer corresponds to the boundary of a dual face).

Therefore, $d_Z$ equals the minimum weight of a non-trivial cycle in the dual lattice. We will return to this in Section 8.3. For now, we focus on proving $d_X = L$, which occupies Sections 5–8.2.

Review — Connection to the Toric Code
An X-type operator on edge-set $c$ commutes with all Z-type stabilizers if and only if $c$ satisfies what condition?
$c$ is a cycle — every vertex has even degree in $c$.
Recalled?
A non-trivial X-type logical operator corresponds to what kind of cycle?
A cycle that is not a boundary — a non-trivial element of $H_1 = Z_1 / B_1$.
Recalled?
What is the dual lattice of the $L \times L$ torus, and why is it useful?
It places a vertex at the center of each primal face, connects them by dual edges (one per primal edge), and turns primal vertices into dual faces. Z-type logicals on the primal lattice correspond to cycles on the dual lattice, so we can reuse the same homological framework for both X-type and Z-type distance.
Recalled?

5. Dimension of $H_1$

How many independent non-contractible loops does the torus have? Geometrically, the answer is clearly 2: one wrapping horizontally and one wrapping vertically. We prove this rigorously by computing $\dim(H_1) = 2$. This tells us the toric code encodes $k = 2$ logical qubits, and $H_1$ has exactly $2^2 = 4$ cosets to classify.

Since $H_1 = Z_1/B_1$ is a quotient of finite-dimensional vector spaces and $B_1 \subseteq Z_1$, we have $\dim(H_1) = \dim(Z_1) - \dim(B_1)$. So we need $\dim(Z_1)$ and $\dim(B_1)$ separately. Our main tool is rank-nullity.

5.1 Background: rank-nullity

Theorem (Rank-nullity)
For a linear map $T: V \to W$ between finite-dimensional vector spaces: $\;\dim(V) = \dim(\ker T) + \operatorname{rank}(T)$, where $\operatorname{rank}(T) = \dim(\operatorname{im}(T))$.

In other words: the dimension of the domain splits into two pieces — the dimension of what $T$ kills (the kernel) and the dimension of what survives in the image.

We also use one fact about transpose maps over $\mathbb{F}_2$. A linear map $T: V \to W$ between finite-dimensional $\mathbb{F}_2$-vector spaces is represented by a matrix $M$; the transpose $T^T: W \to V$ is represented by $M^T$. The key property we need is:

Fact
$\operatorname{rank}(T) = \operatorname{rank}(T^T)$. (Both equal the size of the largest nonsingular square submatrix of $M$, over any field.)

5.2 Computing $\dim(Z_1)$

$Z_1 = \ker(\partial_1)$. By rank-nullity applied to $\partial_1$: $2L^2 = \dim(Z_1) + \operatorname{rank}(\partial_1)$. Rather than computing $\operatorname{rank}(\partial_1)$ directly, it is easier to compute $\operatorname{rank}(\partial_1^T)$ (which is equal) and then find $\dim(\ker(\partial_1^T))$ using rank-nullity.

What does $\partial_1^T$ do? Since $\partial_1: C_1 \to C_0$ maps edge-space to vertex-space, its transpose $\partial_1^T: C_0 \to C_1$ maps vertex-space to edge-space. Concretely, $\partial_1$ is represented by a matrix $M$ with $M_{v,e} = 1$ iff $v$ is an endpoint of $e$; the transpose $M^T$ has $M^T_{e,v} = 1$ iff $v$ is an endpoint of $e$. So $\partial_1^T$ maps a subset of vertices $S$ to the set of edges with an odd number of endpoints in $S$. Since every edge has exactly two endpoints, this is the set of edges with exactly one endpoint in $S$ — the edge cut (or coboundary) of $S$.

Lemma 5.1
$\dim(\ker(\partial_1^T)) = 1$.
Proof. $S \in \ker(\partial_1^T)$ iff no edge has exactly one endpoint in $S$, i.e., every edge has both or neither endpoints in $S$. Since the lattice is connected, $S$ must be $\emptyset$ or all vertices. $\square$
Corollary 5.2
$\operatorname{rank}(\partial_1) = L^2 - 1$.
Proof. By rank-nullity on $\partial_1^T: C_0 \to C_1$: $\operatorname{rank}(\partial_1^T) = L^2 - 1$. Since $\operatorname{rank}(\partial_1) = \operatorname{rank}(\partial_1^T)$, done. $\square$
Corollary 5.3
$\dim(Z_1) = L^2 + 1$.
Proof. $\dim(Z_1) = 2L^2 - (L^2-1) = L^2+1$. $\square$

5.3 Computing $\dim(B_1)$

$B_1 = \operatorname{im}(\partial_2)$, so $\dim(B_1) = \operatorname{rank}(\partial_2)$. By rank-nullity: $L^2 = \dim(\ker(\partial_2)) + \dim(B_1)$.

Lemma 5.4
$\dim(\ker(\partial_2)) = 1$.
Proof. $\mathcal{F} \in \ker(\partial_2)$ iff every edge borders an even number of faces in $\mathcal{F}$. Since each edge borders exactly two faces, adjacent faces must both be in $\mathcal{F}$ or both absent. By connectedness of the face-adjacency graph, $\mathcal{F}$ is $\emptyset$ or all faces. One can verify directly that $\partial_2(\text{all faces}) = 0$: each edge borders exactly two faces, both in $\mathcal{F}$, so each edge appears $2 \equiv 0 \pmod 2$ times. $\square$
Corollary 5.5
$\dim(B_1) = L^2 - 1$.
Proof. $\dim(B_1) = L^2 - 1$. $\square$

5.4 The dimension of $H_1$

Theorem 5.6
$\dim(H_1) = 2$.
Proof. $$\begin{aligned} \dim(H_1) &= \dim(Z_1) - \dim(B_1) \\ &= (L^2+1) - (L^2-1) \\ &= 2. \end{aligned} \quad \square$$

Since $H_1$ is a 2-dimensional vector space over $\mathbb{F}_2$, it has exactly $2^2 = 4$ elements (cosets).

Review — Dimension of $H_1$
What is $\dim(H_1)$ for the $L \times L$ torus over $\mathbb{F}_2$?
2. This means the toric code encodes 2 logical qubits and $H_1$ has $2^2 = 4$ cosets.
Recalled?
In the computation of $\dim(Z_1)$, why do we analyze the transpose $\partial_1^T$ instead of $\partial_1$ directly?
Because $\ker(\partial_1^T)$ is easy to characterize: it consists of vertex-labelings where adjacent vertices always agree, which forces the labeling to be constant (by connectedness). This gives $\dim(\ker(\partial_1^T)) = 1$, so $\operatorname{rank}(\partial_1) = L^2 - 1$.
Recalled?
Why is $\dim(\ker(\partial_2)) = 1$?
A face-set $\mathcal{F}$ is in $\ker(\partial_2)$ iff every edge borders an even number of faces in $\mathcal{F}$. Since each edge borders exactly two faces, adjacent faces must have the same membership. Connectedness forces $\mathcal{F}$ to be either empty or all faces.
Recalled?

6. The Invariants $h$ and $v$

We know from Theorem 5.6 that $H_1$ is a 2-dimensional vector space over $\mathbb{F}_2$, so it has exactly 4 elements (cosets). But the dimension count alone doesn't tell us how to distinguish these cosets — given a cycle, how can we tell which coset it belongs to?

To answer this, we need concrete invariants: quantities that we can compute from a cycle $c$ that (a) depend only on the coset of $c$ (i.e., don't change when we add a boundary), and (b) take different values on different cosets.

The idea is geometric. On the torus, a non-trivial cycle must "wrap around" in at least one direction. We can detect this wrapping by counting how many times a cycle crosses a given "slice" of the torus. Concretely, we define two parity-valued invariants — $h(c)$ and $v(c)$ — that detect horizontal and vertical wrapping respectively. Together, they give a complete classification of cosets and, crucially, a tool for proving the distance lower bound.

6.1 Definitions

Definition
For $c \in C_1$ and $x_0 \in \{0,\ldots,L-1\}$: $$h_{x_0}(c) = \bigl|\{y : H(x_0, y) \in c\}\bigr| \bmod 2$$ the parity of horizontal edges of $c$ at $x$-coordinate $x_0$. Similarly: $$v_{y_0}(c) = \bigl|\{x : V(x, y_0) \in c\}\bigr| \bmod 2$$ the parity of vertical edges of $c$ at $y$-coordinate $y_0$.

We use the notation $[P]$ for the Iverson bracket: $[P] = 1$ if the statement $P$ is true, and $[P] = 0$ otherwise. For example, $[H(x_0, y) \in c]$ is 1 if the edge $H(x_0, y)$ belongs to $c$, and 0 otherwise.

Example. We compute $h$ and $v$ for three cycles on a 3×3 torus.

The horizontal loop $c_h = \{H(0,0), H(1,0), H(2,0)\}$: At each $x$-coordinate $x_0 \in \{0,1,2\}$, there is exactly one horizontal edge of $c_h$ — namely $H(x_0, 0)$. So $h_{x_0}(c_h) = 1$ for all $x_0$, giving $h(c_h) = 1$. Since $c_h$ contains no vertical edges, $v(c_h) = 0$.

A squiggly homologous cycle $c' = c_h + \partial_2(F(0,0))$: Adding the boundary of $F(0,0)$ to $c_h$ gives $c' = \{H(0,1), H(1,0), H(2,0), V(0,0), V(1,0)\}$ — a cycle with a detour around the face $F(0,0)$. Despite its different shape, $h(c') = 1$ and $v(c') = 0$: the same as $c_h$. This illustrates that $h$ and $v$ depend only on the homology class, not the specific representative.

The boundary $\partial_2(F(0,0)) = \{H(0,0), H(0,1), V(0,0), V(1,0)\}$: At $x_0 = 0$, there are two horizontal edges: $H(0,0)$ and $H(0,1)$. Two is even, so $h_0(\partial_2(F(0,0))) = 0$. At all other $x$-coordinates, there are no horizontal edges. So $h(\partial_2(F(0,0))) = 0$, and similarly $v(\partial_2(F(0,0))) = 0$, consistent with Lemma 6.3.

Lemma 6.1
The maps $h_{x_0}, v_{y_0}: C_1 \to \mathbb{F}_2$ are linear.
Proof. We show linearity of $h_{x_0}$; the argument for $v_{y_0}$ is identical. For 1-chains $c, c' \in C_1$, the chain $c + c'$ is their symmetric difference. A horizontal edge $H(x_0, y)$ is in $c + c'$ if and only if it is in exactly one of $c$ and $c'$. Therefore: $$\begin{aligned} h_{x_0}(c + c') &= \sum_{y} [H(x_0,y) \in c + c'] \\ &= \sum_{y} \bigl([H(x_0,y) \in c] + [H(x_0,y) \in c']\bigr) \pmod{2}. \end{aligned}$$ where the second equality holds because over $\mathbb{F}_2$, the indicator of "exactly one" equals the sum of the two indicators (since $1 \oplus 1 = 0 = 1 + 1 \pmod 2$). Hence $$h_{x_0}(c + c') = h_{x_0}(c) + h_{x_0}(c').$$ $\square$

6.2 Independence of coordinate choice

Lemma 6.2
If $c \in Z_1$, then $h_{x_0}(c)$ is the same for all $x_0$, and $v_{y_0}(c)$ is the same for all $y_0$.
Proof. We show $h_{x_0}(c) = h_{x_0+1}(c)$; the result for all $x_0$ follows by induction. Sum the even-degree condition over the $L$ vertices in column $x = x_0 + 1$: $$\sum_y \deg_c(x_0+1,y) \equiv 0 \pmod{2}$$ The edges incident to vertex $(x_0+1, y)$ are: Summing over all $y$, the two vertical sums run over the same $L$ edges (indices mod $L$) and cancel mod 2: $$\sum_y [H(x_0,y) \in c] + \sum_y [H(x_0{+}1,y) \in c] \equiv 0 \pmod 2$$ So $h_{x_0}(c) = h_{x_0+1}(c)$. The proof that $v_{y_0}(c) = v_{y_0+1}(c)$ is analogous. $\square$

For cycles we write simply $h(c)$ and $v(c)$ since the values are coordinate-independent.

6.3 Invariance under adding boundaries

Lemma 6.3
If $b \in B_1$, then $h(b) = 0$ and $v(b) = 0$.
Proof. Let $b = \partial_2(\mathcal{F})$. Write $a_y = [F(x_0,y) \in \mathcal{F}]$. Then $[H(x_0,y) \in b] = a_y \oplus a_{y-1}$. Summing over $y$: $$\begin{aligned} h_{x_0}(b) &= \sum_y (a_y \oplus a_{y-1}) \\ &= \sum_y a_y + \sum_y a_{y-1} \\ &= 2\sum_y a_y \equiv 0 \pmod 2. \end{aligned}$$ since both sums run over the same values (indices mod $L$). $\square$
Corollary 6.4 & Lemma 6.5
The map $\varphi: H_1 \to \mathbb{F}_2^2$ defined by $\varphi([c]) = (h(c), v(c))$ is well-defined and linear.
Proof. Well-defined: if $c' = c + b$ with $b \in B_1$, then $$\begin{aligned} h(c') &= h(c) + h(b) \\ &= h(c), \end{aligned}$$ by Lemmas 6.1 and 6.3 (and similarly for $v$). Linear: $$\begin{aligned} \varphi([c_1]+[c_2]) &= (h(c_1+c_2), v(c_1+c_2)) \\ &= (h(c_1)+h(c_2), v(c_1)+v(c_2)) \\ &= \varphi([c_1])+\varphi([c_2]). \end{aligned}$$ $\square$
Review — The Invariants $h$ and $v$
What does $h(c)$ measure?
The parity (mod 2) of the number of horizontal edges of $c$ at any fixed $x$-coordinate.
Recalled?
Why is $h(c)$ the same at every $x$-coordinate (for a cycle $c$)?
Summing the even-degree condition over a column of vertices at $x = x_0 + 1$: the two vertical edge sums cancel (they run over the same set mod $L$), leaving $h_{x_0}(c) + h_{x_0+1}(c) \equiv 0$.
Recalled?
What are $h(b)$ and $v(b)$ for any boundary $b$?
Both are 0. This is why $\varphi([c]) = (h(c), v(c))$ is well-defined on homology classes: adding a boundary doesn't change $h$ or $v$.
Recalled?

7. The Isomorphism $H_1 \cong \mathbb{F}_2^2$

We have established two things so far: $H_1$ is a 2-dimensional $\mathbb{F}_2$-vector space (Section 5), and we have a well-defined linear map $\varphi: H_1 \to \mathbb{F}_2^2$ that assigns to each cycle its $(h, v)$ invariants (Section 6). We now show that $\varphi$ is an isomorphism. This means the invariants $h$ and $v$ completely classify the cosets of $H_1$: two cycles are homologous if and only if they have the same $(h, v)$ values. This is the key fact that enables the distance proof in Section 8, because it guarantees that every non-trivial cycle has $h = 1$ or $v = 1$ (or both), and we will show that either condition forces the cycle to have weight at least $L$.

Theorem 7.1
$\varphi: H_1 \to \mathbb{F}_2^2$ is an isomorphism.
Proof. By Theorem 5.6, $$\dim(H_1) = 2 = \dim(\mathbb{F}_2^2).$$ Since $\varphi$ is linear (Lemma 6.5) and maps between spaces of the same finite dimension, it suffices to show surjectivity (surjectivity + equal dimensions $\Rightarrow$ injectivity).

Representative for $(1,0)$: Let $c_h = \{H(x,0) : x=0,\ldots,L-1\}$, the horizontal loop along row $y=0$.

Therefore $\varphi([c_h]) = (1,0)$.

Representative for $(0,1)$: Let $c_v = \{V(0,y): y=0,\ldots,L-1\}$, the vertical loop along column $x=0$. By an analogous argument, $c_v$ is a cycle with $\varphi([c_v]) = (0,1)$, and $c_v \notin B_1$ since $v(c_v) = 1$ while $v(b) = 0$ for all boundaries.

Representative for $(1,1)$: $c_{hv} = c_h + c_v$ (symmetric difference). By linearity of $\varphi$: $$\begin{aligned} \varphi([c_{hv}]) &= (1,0)+(0,1) \\ &= (1,1). \end{aligned}$$ Also, $c_{hv} \notin B_1$ since $\varphi([c_{hv}]) \neq (0,0)$ while $\varphi$ sends all boundaries to $(0,0)$.

Thus $\varphi$ is surjective, hence an isomorphism. $\square$
Review — The Isomorphism $H_1 \cong \mathbb{F}_2^2$
What are the four cosets of $H_1$, and what $(h, v)$ values do they have?
$(0,0)$: boundaries (trivial). $(1,0)$: horizontal loops. $(0,1)$: vertical loops. $(1,1)$: both.
Recalled?
How do we know the horizontal loop $c_h$ is not a boundary?
$h(c_h) = 1$, but every boundary has $h = 0$ (Lemma 6.3). So $c_h$ cannot be a boundary.
Recalled?
Why does surjectivity of $\varphi: H_1 \to \mathbb{F}_2^2$ imply that $\varphi$ is an isomorphism?
$\varphi$ is a linear map between vector spaces of the same dimension (both 2). A surjective linear map between equal-dimensional spaces is automatically injective.
Recalled?

8. The Distance Theorem

We now have all the tools. The isomorphism $\varphi: H_1 \to \mathbb{F}_2^2$ tells us every non-trivial cycle has $h(c)=1$ or $v(c)=1$ (or both). Lemma 6.2 says this parity is the same across all $L$ slices. Together, these force every non-trivial cycle to contribute at least one edge to each of $L$ disjoint sets.

Theorem 8.1
The X-distance of the toric code on an $L \times L$ lattice is $d_X = L$.

8.1 Upper bound: $d_X \leq L$

The horizontal loop $c_h = \{H(x,0): x=0,\ldots,L-1\}$ is non-trivial with $|c_h| = L$, so $d_X \leq L$.

8.2 Lower bound: $d_X \geq L$

Let $c \in Z_1 \setminus B_1$. Since $\varphi$ is injective and $c \notin B_1$, we have $\varphi([c]) \neq (0,0)$, so $h(c)=1$ or $v(c)=1$.

Case 1: $h(c) = 1$. Define $S_x = \{H(x,y): y = 0,\ldots,L-1\}$ for each $x$. These $L$ sets are pairwise disjoint. By Lemma 6.2, the parity of $|c \cap S_x|$ equals $h(c) = 1$ for every $x$, so $|c \cap S_x| \geq 1$ for each $x$. Therefore: $$|c| \geq \sum_{x=0}^{L-1} |c \cap S_x| \geq L$$

Case 2: $v(c) = 1$. The same argument with $T_y = \{V(x,y): x=0,\ldots,L-1\}$ gives $|c| \geq L$. $\square$

8.3 Z-type logicals and the full distance

Claim 8.2
For a CSS code, $d = \min(d_X, d_Z)$.
Proof.

Decomposition. Any $n$-qubit Pauli $P$ can be written (up to a global phase $\pm 1, \pm i$) as $$P = X^a Z^b,$$ where $a, b \in \mathbb{F}_2^n$ and $$X^a = X_1^{a_1} \otimes \cdots \otimes X_n^{a_n}$$ (similarly for $Z^b$). Write $\operatorname{supp}(a) = \{i : a_i = 1\}$ for the support of $a$. The weight of $P$ is $|\operatorname{supp}(a) \cup \operatorname{supp}(b)|$, since $P$ acts as identity on qubit $i$ only when $$a_i = b_i = 0.$$

Commutation. In a CSS code, every stabilizer generator is either purely X-type or purely Z-type. We use two facts:

Therefore, $$\begin{aligned} P = X^a Z^b \text{ commutes with all stabilizers} \iff&\ a \text{ commutes with all Z-type stabilizers} \\ &\text{and } b \text{ commutes with all X-type stabilizers}. \end{aligned}$$ Equivalently, $a \in Z_1$ and $b \in Z_1^*$ (the cycle space of the dual lattice). This decoupling is specific to CSS codes.

Non-triviality. In a CSS code, the stabilizer group factors as products of X-type and Z-type stabilizers. So $$\begin{aligned} P = X^a Z^b \text{ is a stabilizer} \iff&\ X^a \text{ is a product of X-type stabilizers } (a \in B_1) \\ &\text{and } Z^b \text{ is a product of Z-type stabilizers } (b \in B_1^*). \end{aligned}$$ Taking the contrapositive: $P$ is a non-trivial logical iff it commutes with all stabilizers and at least one of $a \in Z_1 \setminus B_1$ or $b \in Z_1^* \setminus B_1^*$ holds.

Lower bound: $$\begin{aligned} a \in Z_1\setminus B_1 &\implies \operatorname{wt}(P) \ge |a| \ge d_X, \\ b \in Z_1^*\setminus B_1^* &\implies \operatorname{wt}(P) \ge |b| \ge d_Z. \end{aligned}$$ Since at least one holds, $\operatorname{wt}(P) \ge \min(d_X,d_Z)$.

Upper bound: a minimum-weight non-trivial X-type logical has weight $d_X$, so $d \le d_X$; similarly $d \le d_Z$. Therefore $$d = \min(d_X, d_Z).$$ $\square$

As shown in Section 4.2, Z-type logical operators correspond to non-trivial cycles in the dual lattice, and the dual lattice of the $L \times L$ torus is again an $L \times L$ torus with the same combinatorial structure. Therefore the entire argument of Sections 5–8.2 applies verbatim, yielding $d_Z = L$. Thus:

$$\begin{aligned} d &= \min(d_X, d_Z) \\ &= \min(L, L) \\ &= L. \end{aligned}$$

The toric code is a $[[2L^2, 2, L]]$ code: $n = 2L^2$ physical qubits, $k = 2$ logical qubits, $d = L$. $\quad\blacksquare$

Review — The Distance Theorem
What is the upper bound argument for $d_X \leq L$?
The horizontal loop $c_h$ has exactly $L$ edges and is a non-trivial cycle (since $h(c_h) = 1 \neq 0$). So $d_X \leq L$.
Recalled?
Why does $h(c) = 1$ imply that $|c| \geq L$?
$h(c) = 1$ means $c$ has an odd number of horizontal edges at each of the $L$ distinct $x$-coordinates. Odd $\geq 1$, and these $L$ sets are pairwise disjoint, so $|c| \geq L$.
Recalled?
Why does $d = \min(d_X, d_Z)$ hold for a CSS code?
Any non-trivial logical $P = X^a Z^b$ has at least one non-trivial part ($a$ or $b$). Its weight $|\operatorname{supp}(a) \cup \operatorname{supp}(b)| \geq \max(|a|, |b|)$, which is $\geq d_X$ or $\geq d_Z$. So every non-trivial logical has weight $\geq \min(d_X, d_Z)$.
Recalled?

Conclusion

The proof rested on a single key idea: defining invariants $h$ and $v$ that detect whether a cycle wraps around the torus horizontally or vertically. These invariants are well-defined on homology classes, completely classify the four cosets of $H_1$, and force any non-trivial cycle to contain at least $L$ edges, because the regular structure of the square lattice guarantees $L$ disjoint "slices" each contributing at least one edge.

The deeper lesson is that the distance of the toric code is a topological fact, not a combinatorial coincidence. The number of logical qubits comes from $\dim(H_1)$, and the distance comes from the minimum weight of a non-trivial homology class. This is why the proof naturally lives in the language of homology rather than, say, case analysis on paths.

This perspective generalizes far beyond the square lattice. The entire framework — qubits on $k$-cells, stabilizers from boundary maps, logical operators from non-trivial homology classes — works for any cellulation of any manifold in any dimension. The distance question becomes a question about the systole: the shortest non-trivial cycle. This is the foundation for hyperbolic codes, higher-dimensional toric codes, and the broader family of homological codes.

That said, the specific argument we used for the lower bound — partitioning edges into $L$ disjoint slices with odd parity — exploits the regular structure of the $L \times L$ square lattice. For other manifolds or irregular cellulations, computing the distance can be significantly harder, and is in general an NP-hard problem. Even for close relatives of the toric code, modifications are needed: the planar and rotated surface codes have different boundary conditions that change the homology (from $H_1 \cong \mathbb{F}_2^2$ to $H_1 \cong \mathbb{F}_2$), and the distance argument must account for paths that terminate at boundaries rather than wrapping around.

Further reading