An introduction to the surface code

This chapter provides an introduction to the surface code, discussing details about how physical qubits are connected and operated to create logical qubits, how errors are handled, and what performance metrics are needed to generate logical qubits with performance that exceeds their underlying physical qubit performance.


Overview
The surface codes, of which there are a number of variants, provide an appealing architecture for building a quantum computer [4,6]. This is primarily because the surface codes provide a simple architecture that promises, under certain assumptions, an exponential reduction in the error rate for logical qubits as one scales up the number of physical qubits that define each logical qubit. The surface codes originated in the toric codes, invented by Alexei Kitaev [1][2][3], which in turn evolved from Kitaev's models for supporting topological order, using qubits distributed on the surface of a toroid, a two-dimensional surface with periodic boundary conditions along both in-plane directions. Kitaev's toroidal geometry ultimately turned out to be unnecessary, and the planar versions, known as the surface codes, were primarily developed by Bravyi and Kitaev as well as Freedman and Meyer [4,5].
Error-protection schemes such as the surface codes rely on assemblies of physical qubits that are called logical qubits. The physical qubits that make up a logical qubit are operated in a cyclically-repeated quantum circuit that detects errors in the physical qubits as they occur. If the quantum gates in this circuit are sufficiently accurate, then the logical qubit will have a lower error rate than its underlying physical qubits. The error-protecting architecture has a certain threshold for the physical qubit error rate; for error rates above this threshold, the error-protection scheme fails (spectacularly). In general there is an assumption that local errors dominate, i.e. errors occurring in the individual physical qubits, uncorrelated with those occurring in other qubits; and errors in the CNOT entangling gates between pairs of physical qubits. Long-distance correlated errors, between pairs or larger groups of qubits, are in general assumed not to occur; numerical tests indicate that to some extent, such correlated errors are not disastrous, but the full panoply of possible long-range correlated errors has not been explored, and certainly some types of errors can cause propagation and failure of the surface code protection.
Note that in general errors are not corrected as they occur and are detected in the surface code cycle; instead, the overall control software accounts for these errors in measurement outcomes, propagating errors through the single qubit and two-qubit gates where possible.
Surface codes are unusually tolerant to local physical qubit errors. In one early estimate [7], the authors found that one type of surface code could handle error rates of almost 3% per cycle of the surface code circuit, although the authors assumed certain capabilities in the qubits that turn out to be difficult to implement in the currently available qubit technologies. Following this promising result, Raussendorf and co-workers discovered further that a logical CNOT operation, between logical qubits, could be implemented in an error-protected fashion by braiding transformations on a single surface, significantly simplifying the architecture for the surface code [8][9][10]. These authors also arrived at an error threshold of 0.75% per physical qubit operation, assuming only simple nearest-neighbor one-and two-qubit gates, lower than the threshold of Preskill et al., but with significantly simpler hardware requirements.
The tolerance of surface codes to errors, withstanding error rates of order 1% [11,12], is far higher than that of other error-protecting approaches. The Steane and Bacon-Shor codes implemented on two-dimensional lattices with nearest-neighbor coupling, for example, have thresholds of about 2 × 10 −5 [13,14], three orders of magnitude lower than the surface code (i.e. the qubits need to perform three orders of magnitude better to enable implementation using e.g. a Steane code). As a result, there has been significant interest in developing further the surface codes [11,12,15,16]. A number of authors are also working on improving the classical processing associated with the surface code [17][18][19][20][21], where error identification forms a significant software challenge. There are also a number of other two-dimensional topological codes that have appealing error thresholds and scaling, although these all tend to require physical qubits with additional capabilities [22][23][24][25].
Error protection always seems to involve large numbers of physical qubits [13,14]; the surface code is no exception. It takes a minimum of thirteen physical qubits to implement a single surface code logical qubit (even with zero physical qubit error rate). As the local error rate increases, the number of physical qubits needed to achieve a certain logical qubit error rate increases as well. Above an error rate threshold of around 1%, the surface code no longer provides protection; however, this threshold depends on the particular error source: For example, errors in the projective measurement of physical qubits can approach 50% without ruining the surface code protection, while errors in the physical qubit controlled-NOT must be kept below 1%. Given current physical qubit performance, it is likely that a reasonably faulttolerant logical qubit will require of order 10 3 to 10 4 physical qubits; however, each factor reduction in the error rate for the critical CNOT gate would reduce the number of required physical qubits by about the same factor (up to a limit). The number of physical qubits needed to factor a "interesting" number using Shor's algorithm [26] depends on a tradeoff between the quantum circuit's physical size and the amount of time considered acceptable for the computation: Larger circuits take less time, but are more expensive to build and operate. Estimates for surface-code circuit sizes are provided in Ref. [27], depending on the size of the number to be factored. For a 2,000 bit number (about 600 decimal digits), if we are willing to wait about one day for the result, the computational part of Shor's algorithm will need about 4,000 logical qubits; if we assume about 3,500 physical qubits per logical qubit, this corresponds to about 15 million physical qubits. However, Shor's algorithm has to perform modular exponentiation, which requires large numbers of what are known as |A〉 states, with |A〉 L = |0〉 L + e iπ/4 |1〉 L (the L subscript indicates these are logical qubit states). These states must be prepared very precisely, with a precision given by the overall surface code requirement. For a 2,000 bit number, this means precision of order 1 part in 10 16 or 10 17 . This can be achieved using a method called "magic state distillation" [28][29][30]. Producing these high-precision states at a sufficiently high rate and purity consumes the majority of a quantum computer's resources; here roughly 10 times the number of logical qubits, and thus 10 times the number of physical qubits, are required to produce these states at a rate sufficient to complete the computation in one day. Thus of order 2 × 10 8 physical qubits are needed to factor a 2,000 bit number, accounting for the need for |A〉 states.
Note that using the most powerful classical supercomputer and the best classical factoring algorithm, factoring such a number would require more time than the age of the universe and more electrical power than is available on the Earth. You can try: a company called RSA Laboratories (now called RSA Security LLC) published a 617 decimal digit number that is the product of two primes, and offered 200,000 USD to whoever could factor it. That number is called RSA-617 or RSA-2048 (it has 2048 binary digits), and is equal to Give it a try!

Experimental progress in implementing a surface code
Note: this section uses terminology not introduced until later in this chapter. For those not familiar with the surface code, the author recommends skimming this section on a first read, then returning after reading the rest of the chapter.
There are rapidly developing experimental efforts to build circuits that can test implementations of the surface code, with the most advanced efforts using superconducting qubit circuits. Evaluation of the performance of transmon-style qubits [31] in small test circuits that comprise sections of a surface code [32,33] showed that the physical qubit gate fidelities were very close to the break-even point for a surface code, perhaps slightly below. Note however, as discussed elsewhere, the error rate threshold is not a single number; instead the overall surface code performance has a different sensitivity to errors in each elementary physical qubit operation, with the highest sensitivity to the two-qubit entangling gate (e.g. a CNOT or CZ gate), which unfortunately is also the class of gates that has proven the most challenging to achieve the best fidelities. The size of a surface code logical qubit, i.e. the number of physical qubits required to implement a logical qubit, depends strongly on how close the physical qubit performance is to the surface code error threshold, with logical qubits growing rapidly in size as physical error rates approach the threshold from below. Keeping the circuit sizes manageable will probably require error rates, loosely speaking, one or two orders of magnitude below than the threshold, especially for the CNOT-class gates.
First steps towards an operational surface code using superconducting qubits include early experiments using linear chains of qubits to demonstrate joint measurement and bit-flip error detection [32,46], with first five and then nine qubits with nearest-neighbor coupling. More sophisticated later steps include building and measuring a superconducting qubit circuit with seven qubits that approximate two plaquettes of the surface code, with which measurements showed preliminary indications of the anyonic behavior of excitations in these circuits [39]. More recently, two separate experiments [40,43] explored measurements in circuits of the same size, with four data and three measure qubits, demonstrating error detection of a logical qubit based on the four physical qubits. In one experiment [40], the logical qubit had a post-selected lifetime about four times that of the best physical qubit in the circuit. The logical qubit could be probabilistically placed in the |0000〉 + |1111〉 state, with a measured success probability of 25 percent (using a technique that, if error-free, would yield this state 50 percent of the time). In the second experiment [43], the authors demonstrated the high fidelity preparation and measurement of the four data qubits in their four cardinal logical states |0〉 L = |0000〉, |1〉 L = |1111〉, |+〉 L and |−〉 L .
More recently two separate accounts of more complete surface code experiments have appeared, using a seventeen qubit circuit with nine data qubits and eight measure qubits, the latter providing four X and four Z stabilizers and creating an implementation of a distance d = 3 surface code circuit [44,45]. In one experiment [44], the researchers use an error correction scheme that preserves the four primary states of the logical qubit, comprising the nine data qubits. Repeated cycles allow detection of bit-and phase-flip error syndromes, with errors corrected in post-processing. When rejecting runs in which state leakage was detected, they find an error probability of about 3 percent per cycle. A very similar experiment on the same size circuit with the same surface code topology is described in [45]. By executing several consecutive error correction cycles, the experimenters found that the logical error could be significantly reduced from the native error after applying corrections by post-selecting errorfree states, yielding an error-corrected logical qubit with an improved fidelity over e.g. the best single physical data qubit. Both of these experiments had mean single-qubit gate fidelities of order 99.9 percent and two-qubit gate fidelities of order 99 percent, with measurement fidelities above 90 percent, roughly speaking above the large-circuit limit for the surface code error threshold.
There has also been work on larger circuits implementing the surface code. The Google AI team has published a number of papers in this regard, the most relevant being Refs. [41] and [42], using a circuit with 54 total superconducting qubits and conducting experiments using a subset of these. In [41], researchers implement one-dimensional repetition codes for a line of qubits embedded in a two-dimensional circuit, and demonstrate the exponential suppression of either bit-or phase-flip errors, reducing logical error rates by more than two orders of magnitude per measurement cycle when increasing the number of qubits from five to 21, a result that is stable over 50 rounds of error correction. In [42], in a second advance using a similar circuit, researchers prepared the ground state of the surface code Hamiltonian, and measured a topological entanglement entropy near the expected value of − ln 2, and further simulated anyon interferometry to explore the excitation braiding statistics. They also explored key aspects of the surface code, including logical state injection and the decay of the non-local order parameter.
These various results, which by no means include all experiments exploring the surface code implemented using superconducting qubits, not to mention other qubit hardware platforms, point to steadily advancing capabilities in experimental implementations of the surface code. As qubit energy and phase lifetimes improve, and correspondingly the single and twoqubit fidelities improve, the fundamental error rates will likely continue to improve beyond the surface code threshold, and researchers will then have to begin to deal with long-range errors, either due to circuit design or due to external influences [47][48][49].

Requirements for a quantum computer
A quantum computer requires a set of inter-connected physical qubits (quantum two-level systems, with basis states |0〉 and |1〉), and the ability to perform unitary operations (gates) on these qubits, as well as measure their quantum states with a projective measurement (one where after the measurement, the qubit state is an eigenstate of the measurement operator). The interconnectedness of the qubits is often not discussed, and many algorithms assume that the physical qubits have all-to-all coupling (every qubit connected to every other), which requires N (N − 1)/2 interconnects for N qubits. Architectures with less than all-to-all coupling require additional computational overhead. The surface code, as we will see, is a planar square array requiring only nearest-neighbor physical connections. However, as we will see, the surface code architecture places no constraints on the logical qubit interconnections: All-to-all coupling is easily achieved in the surface code, greatly simplifying the algorithm design.
We now begin with an introduction to quantum computing, sufficient to understand the surface code. We will write the physical qubit states as |0〉 and |1〉. These are the eigenstates of the PauliẐ operator with eigenvalues +1 and −1 respectively, are usually chosen to be the basis states for the vector space. These are thus assigned the column vectors Using this basis, the most important single qubit operators (called "gates" in quantum com-puting) are most easily represented by matrices, given here in the Z basis for completeness: (you can check that the column vectors for |0〉 and |1〉 are indeed the eigenvectors of theẐ operator with the correct eigenvalues). Note that the eigenstates of theX operator are and with eigenvalues +1 and −1 respectively. Note we also have and Note also that in some quantum computing texts the imaginary i is dropped in theŶ operator, which can cause some confusion. The Hadamard operator isĤ not to be confused with the Hamiltonian. TheŜ gate (also called the π/4 gate) is given bŷ and theT gate (also called the π/8 gate) iŝ Problem 1: Find the eigenvectors and eigenvalues for theĤ,Ŝ andT operators.
We will be dealing with multi-qubit states. A two-qubit product state for qubits a and b, where qubit a is in the state |u〉 and qubit b is in the state |v〉, can be written |u a 〉⊗|v b 〉 = |u a v b 〉 (here ⊗ means outer product). This is a state vector living in a 2 × 2 = 4 dimensional vector space. We will use the shorthand |uv〉 to represent this state, where the order of the letters corresponds to the order of the qubits; hence |vu〉 would represent the state |v a 〉 ⊗ |u b 〉. Most two-qubit states are not product states; for example (|uv〉+|vu〉)/ 2 is an entangled state that cannot be written as the product of two one-qubit states. The existence of these kinds of states was the subject of a famous and prolonged discussion between Neils Bohr and Einstein, and gave rise to the latter's involvement in the writing of the "EPR paradox" paper [50] as well as a series of letters between Einstein and Schrödinger (for an interesting discussion, see [51]).
We can now discuss the controlled-not (CNOT) gate, which operates on two qubits. For a CNOT, the first qubit is called the control qubit, and performs a conditionalX gate on the second qubit, which is called the target. TheX gate is performed if the control qubit is in its |1〉 state, otherwise nothing (anÎ gate) is done to the target qubit. As anX gate can be seen as a NOT gate, flipping the state of its target qubit, the controlled application of this gate provides the name for the CNOT gate.

Problem 2:
Find the eigenvectors and eigenvalues of the CNOT operator.
Which set of gates one actually needs depends on what is most easily implemented with the physical qubits. The Solovay-Kitaev theorem [2,52] implies that a complete set of gates to build a quantum computer comprises the single-qubit operatorsÎ,X ,Ẑ, the HadamardĤ, theŜ andŜ † phase gates, and theT andT † gates. A two-qubit entangling gate is also needed, which is usually chosen to be the CNOT gate (any two-qubit gate from which a CNOT can be constructed is of course acceptable). Note that as we have the identitiesT 2 =Ŝ,T 4 =Ẑ, HẐĤ =X ,ẐŜ =Ŝ † , andT 7 =T † , the minimal set of gates is {T, H, C N OT }.
Note: From hereon in, we will drop theX notation in favor of the simpler X notation, where the context will hopefully always be clear.
In the surface code, we must be able to swap the states of two qubits, an operation that is imaginatively called a SWAP operation. A SWAP operation, taking the two qubit state |uv〉 to |vu〉, can be performed with three CNOT operations. This is shown (and discussed) later; see Fig. 11.
All of the gates we have discussed so far are unitary. In case your quantum mechanics is rusty, a unitary operator U is one where U times its adjoint U † is the identity operator I, i.e.
To form the adjoint of an operator, you take the complex conjugate transpose of the operator's matrix representation. As you can see, except for the S and T gates, the operators we have introduced so far are all their own adjoints, and their squares are the identity matrix. These are thus by definition Hermitian. Products of operators appear frequently. The order of a product matters, as in general AB = BA when dealing with operators (i.e. matrices). If AB = BA, then A and B are said to commute: where [A, B] is the commutator of A and B.
Qubit operators in general do not commute. The identity I commutes with all operators. However, you can easily show by multiplying the matrices for X and Z that so that the commutator as you can verify by multiplying the matrices out. This means that operating on a qubit first with Z then with X gives the negative of operating first with X and then with Z. However, a very curious commutation feature occurs with two-qubit states. If we have qubit a and b, with their respective X and Z operators X a , X b , Z a and Z b , as we have seen, X a and Z a do not commute, and similarly for X b and Z b . However, operators on different qubits always commute, so X a and Z b for example commute. A surprising fact is that the product of the operators X a X b commutes with the product of operators Z a Z b , as you can show: using the fact that operators on different qubits always commute. Hence Note that when we write Z X without subscripts, the Z and X operators act on the same qubit, so Z X is represented by a 2×2 matrix resulting from the matrix product of the two 2×2 matrices representing Z and X . Thus By contrast, when we write Z a X b , which more properly we should write as Z a ⊗ X b , these operators act on different qubits, so the representation of this outer product operator is a 4 × 4 matrix acting on a two-qubit vector space. The "canonical" basis for this two-qubit space is the same as what we used for the CNOT operator, namely {|0 a 0 b 〉, |0 a 1 b 〉, |1 a 0 b 〉, |1 a 1 b 〉}. The representation of the outer product operator Z a X b in this basis is Problem 3: Find the eigenvectors and eigenvalues of Z a X b in the canonical basis. Problem 4: Write the matrix representation of X a Z b in the canonical basis.

Measurement
We haven't talked about measurement. In the surface code, all measurements are projective; you should recall from your quantum mechanics classes that this means that a measurement of an operator projects the qubit onto an eigenstate of the measurement operator, and the measurement value that is returned is the corresponding operator eigenvalue. For the surface code, you only need to be able to perform single-qubit Z measurements, projecting the qubit onto the |0〉 or |1〉 state with a measurement result of +1 and −1, respectively. It is useful (but not necessary) to also be able to perform single-qubit X measurements, returning +1 and leaving the qubit in the |+〉 = (|0〉 + |1〉) / 2 state, or returning −1 and leaving the qubit in the |−〉 = (|0〉 − |1〉) / 2 state. In fact, however, an X measurement capability is not needed because the Hadamard H operator very conveniently maps |+〉 ↔ |0〉 and |−〉 ↔ |1〉 (a second applications of H gives the identity). Hence, if an X measurement is needed, you simply apply H to the qubit, do a Z measurement, and perform H on the state resulting from the measurement. The final state is then an X eigenstate and the measurement value corresponds to that eigenstate. If two operators commute, then quantum mechanics guarantees that there is a basis that comprises eigenvectors for both operators. Consider our two-qubit commuting operators X a X b and Z a Z b . The first pair has an eigenstate | + +〉 and the second an eigenstate |11〉 (dropping the subscripts a and b and relying on order only), but these are not eigenstates for the other pair. However, as you can easily show, the Bell states {|00〉+|11〉, |01〉+|10〉, |01〉−|10〉, |00〉−|11〉} (divide by 2 to normalize) are all eigenstates of both operators, with eigenvalues {+1, −1, −1, +1} for Z a Z b as can easily be seen, and eigenvalues {+1, +1, −1, −1} for X a X b . The eigenvalues for X a X b are most easily seen by re-writing the eigenstates as by 2). Note that the two eigenvalues (for Z a Z b and X a X b ) identify the precise Bell state.

Problem 5:
Show that the Bell states can be written as claimed using the {|+〉, |−〉} basis vectors.

Quantum Circuits
A quantum circuit comprises a sequence of gates that operate on a population of qubits. They are the method by which a quantum algorithm is implemented, and provide a kind of "machine language" for a quantum computer. They are usually represented graphically. Figure 1 displays the operation |u 〉 = H|u〉. The qubit evolution is represented by a horizontal time-line, where time moves forward from left (starting with |u〉) to the right through one or more boxes (here, the Hadamard H) that represent the gates applied to each qubit, ending in the final qubit state (|u 〉). A one-qubit gate has one qubit time-line entering and one emerging; a two-qubit gate has two such lines.  In Fig. 2 we add a second gate after the Hadamard, representing the operation |u 〉 = X H|u〉. Note that as H is applied first to the qubit, it appears to the left of (earlier than) the second operation X .
For multiple qubits you run lines in parallel, one for each qubit. The circuit in Fig. 3 has two qubits, giving for the upper qubit time-line |u 1 〉 = X H|u 1 〉 and for the lower |u 2 〉 = H X |u 2 〉.
Next we look at a two-qubit CNOT, which is a single box interrupting the timelines of both qubits. The quantum circuit graphical representation of a CNOT is shown in Fig. 4. This circuit represents the operation |u 〉 = C N OT (|u 1 〉, |u 2 〉), where |u 〉 is the two-qubit (bipartite)  The operator representation for the CNOT gate, as mentioned earlier, can be written where I 2 and X 2 operate on the second, target qubit. It is often useful to combine multiple CNOT gates, involving one control qubit and multiple targets, into a single operation as shown in Fig. 5. This is called a single-control, multi-target CNOT. Do not confuse this circuit with the Toffoli gate, which has two controls and a single target, which we show in Fig. 6. This is a straightforward extension of the CNOT gate, with two control qubits and one target qubit.  The operator representation for the Toffoli is where I j is the identity for the jth qubit and X 3 the X operator for the target qubit. The Toffoli can be constructed from single qubit gates and a few CNOTs; the minimal circuit to implement the Toffoli involves six CNOTs and a number of Hadamard and T and T † gates, shown in Fig. 7. Hence in principle the Toffoli gate itself is not needed, i.e. it is not a u u 2 3 u 1 Figure 6: Quantum circuit for a Toffoli gate; the first two qubits |u 1 〉 and |u 2 〉 are the combined controls, and the third qubit |u 3 〉 the target. "fundamental" gate; however, it is very useful, and "expensive" to implement with more fundamental gates, so a hardware system that supports the Toffoli can afford some computational efficiency. A general two-qubit unitary gate is drawn as shown in Fig. 8, where the circuit is drawn assuming the two qubits entering the gate are in a product state, although this is not necessary, while the output state |u 〉 would in general be a two-qubit entangled state. Next in Fig. 9 we show a controlled-U (cU) gate, where the unitary U operation is performed when the control qubit is in the |1〉 state, but if the control is in |0〉, the identity I is applied. The operator representation for this gate is We're now ready to do some examples. Consider the circuit shown in Fig. 10. Both qubits start in some state |x y〉 (where x and y are each either 0 or 1), a Hadamard is applied to the first qubit, and then a CNOT is performed with the first qubit as control and the second as target. What does this circuit do? Obviously from the caption it generates Bell states; how does this work?
x u y H Figure 10: Bell-state generating circuit.
First consider what the circuit does for two possible choices of x and y, with the arrows indicating the outcome after each gate (first after applying the Hadamard, then after applying the CNOT): Can you figure out what the outcomes are for the other values of x and y? A certain pattern should emerge. Another way to analyze this circuit is using operator representations, combining the Hadamard on the first qubit with nothing on the second (U HI = H ⊗ I), followed by a CNOT on the second qubit, U CNOT = |0〉〈0| ⊗ I + |1〉〈1| ⊗ X : You can check that gives the correct results for different input states. Note to get this result, we operated with the Hadamard from the right on the bras on the left. What the notation means is that if you apply this operator to the input state |x y〉, the first part of the operator projects out the |+〉 component of |x〉, and that component is output as |0 y〉, while the second part projects out the |−〉 component of |x〉 and outputs |1, X y〉, using this shorthand to indicate applying an X operator to the | y〉 state. Finally, we look at the circuit that implements a SWAP gate, a necessary operation for the surface code, using only two-qubit CNOT gates. This is shown in Fig. 11. This will take a product state |uv〉 and generate the state |vu〉, and works in general, i.e. will also successfully swap the qubits in an entangled state. a b Figure 11: Two-qubit swap circuit.

Problem 6:
Show that the circuit in Fig. 11 indeed performs a SWAP operation as claimed.

Measurement
Measurement is a process we have discussed earlier. We previously stated that, for the surface code, a measurement of a particle's (or qubit's) quantum state is projective, and depending on the measurement result the qubit ends up in one of two corresponding eigenstates. As a result, the qubit no longer has any useful (unknown) information in it, and from the point of view of a quantum algorithm, the qubit has effectively been destroyed. While the physical qubit of course can be re-used, the quantum circuit representation of a qubit measurement is a qubit time-line entering from the left, which then ends in the circuit element that represents measurement. This is shown in Fig. 12, where the classical information that emerges from the measurement is represented by two light-weight lines, in contrast to the single heavy line used for the qubit. u Figure 12: Measurement circuit. A quantum state |u〉 (single line) enters from left, and classical information (two lines) emerges from the right. Note this process is irreversible, unlike almost all the other quantum gates.

Errors and a qubit named Angelina
If all the gates and measurements could be performed exactly, without any errors, we wouldn't need the surface code. However, qubits are subject to many things that can cause errors. The most trivial are "clumsiness" errors, where you have mis-calibrated your equipment and for example your X gate includes some admixture of another gate, maybe a tiny piece of a Z gate, so maybe the gate is actually X +c Z where c is small but not known, generating a superposition output state, |0〉 → |1〉 + c|0〉 (note we're being sloppy with normalization).
More pernicious errors are unwanted interaction errors. When we listed the gates, we explicitly included the identity gate I; this is a very important gate, because whenever you are not applying some intentional gate to a qubit, you are implicitly acting on the qubit with the I gate. It is very hard to do the I gate precisely, even if this involves doing nothing to the qubit: Just as "idle hands are the devil's workshop" [53], so idle qubits are the targets of all kinds of bad deeds. Maybe the idle qubit interacts with its neighboring qubits, because of stray coupling; maybe it absorbs a photon wandering in from the environment; maybe it emits a photon into the environment. All of these non-idle processes will generate entanglement, with another qubit, or with the environment. These errors can cause serious problems with a computation.
To see how to handle errors, we first review the density matrix representation of a qubit state. For this, let's introduce a qubit we'll call Angelina. In the simplest case, Angelina is in a pure superposition state, where a and b are complex amplitudes whose square magnitudes reflect the probability of finding Angelina in the |0〉 or |1〉 states, with If we prefer the Bloch sphere representation, we can write a = |a|e iα = cos(θ /2) e iα and b = |b|e i(α+φ) = sin(θ /2)e i(α+φ) , so that Angelina's state, dropping the overall un-measurable angle α, is This is a vector of unit magnitude pointing somewhere on the Bloch sphere, as in Fig. 13. This describes Angelina when she is in a pure state |A〉, i.e. when we know exactly what state |A〉 she is in. However, we may not have enough information about Angelina to know what her exact state is; instead, we may know that she is in state |A 1 〉 with probability p 1 , state |A 2 〉 with probability p 2 , etc., in other words in a mixed state. In this situation we represent Angelina's quantum state via the density operator ρ, given by involving the weighted sum of the kets |A j 〉 in outer product with the bras 〈A j |, with weights given by the probabilities p j . Don't confuse the states |A j 〉 with the basis states: The mixed state can involve more than the two states it takes to span the Hilbert space, and correspondingly the states do not have to be mutually orthogonal. The density matrix representation, introduced by von Neumann, is useful in part because the expectation value for measuring some operator B is simply given by taking the matrix product of the representations for ρ and B and summing its diagonal elements (that's the meaning of the trace t r). For the pure state |A〉, Angelina's density operator is simply Note this is a 2 × 2 = 4 density matrix. This has the standard representation For a general mixed state, the representation of the density matrix is Note that the trace of ρ is unity for both the pure and the mixed states, as you can easily show, but the trace of ρ 2 is unity only for a pure state, and is less than 1 for a mixed state.

Problem 7:
Prove that a mixed state with density matrix ρ has t r(ρ 2 ) < 1.
How does Angelina's state become mixed, if we start with a pure state? Certainly this won't happen if all we do is operate on Angelina with unitary operators U, as unitary operators always take pure states to pure states.
So how does Angelina's state become mixed? One way this could happen is if we turn our backs, and our "friend" comes and, based on some coin flips, either does or does not do some unitary operations on Angelina. If the probability to perform U j is p j , where U j is a unitary, then Angelina's initially pure state density operator ρ = |A〉〈A| will be transformed to where |A j 〉 = U j |A〉 and 〈A j | = 〈A|U † j . Hence, for example, say there is a 50% chance our frenemy did nothing, and a 50% chance he did an X gate. After this probabilistic manipulation, Angelina's state is given by the density matrix ρ , where (note X = X † ). The trace of ρ is still unity, but the trace of ρ 2 is 1/2 (do you see why?), so this is now a mixed state. We of course do not have to rely on a friend to come to transform Angelina's state to a mixed one. Angelina will be interacting with her environment, which will involve many small interactions with a very complex system, very much like a classical Brownian particle interacting with its surrounding fluid. In the latter case, the Brownian particle is visibly affected by the thousands of fluid molecules that interact with it; however the accompanying particle's reaction on the fluid is not easily detected. The resulting motion, to first order, is the random walk and thermalization of the Brownian particle. Here, we similarly need to understand how Angelina interacts with the environment that surrounds her, and how she becomes entangled with that environment. How can we hope to get a handle on something we know so little about?
Let's begin by looking at something simpler. Let's introduce a second qubit, which we'll call Brad. Let's assume Angelina and Brad interact through a simple exchange Hamiltonian: where X A,B are the X operators on Angelina and Brad, respectively, and g is the interaction strength in frequency units. If we start in the state |01〉, where Angelina is in |0〉 and Brad in |1〉, then an application of the Hamiltonian generates the result swapping Angelina and Brad's states. Of course, if we start in the reverse situation, we get the reverse outcome. Note that the eigenstates of this Hamiltonian are the entangled Bell states |B x y 〉, , , , The H int eigenvalue for the eigenstate |B x y 〉 is (−1) x ħ hg, as was discussed earlier.
Consider now a general state |M 〉 of Angelina and Brad; this evolves in time under the interaction Hamiltonian according to Schrödinger's equation, The four entangled Bell states are stationary states, with If we start say with the state |10〉 = (|B 01 〉 − B 11 〉)/ 2 at time t = 0, the state will become entangled, with a time dependence Problem 8: Show that this is indeed the solution to Schrödinger's equation, Eq. 41.
The state transforms periodically to the state |01〉 = (|B 01 〉+B 11 〉)/ 2 (with a phase factor), every t n = (n + 1/2)π/g, and returns to the initial state |10〉 every t n = nπ/g: The "swap time" is T s = π/g. Other than at these precise times, however, Angelina and Brad are in an entangled state. For times ∆t very short compared to the swap time, the entanglement is weak, with a state approximately given by |10〉 + 2i g∆t|01〉 (needing normalization, of course), with 2g∆t = 2π∆t/T s 1. From the point of view of Angelina, this is a small X A error; measuring her state (ignoring that of Brad) after the time ∆t will return the (erroneous) state |0〉 A with probability p = |2π∆t/T s | 2 1, and will otherwise return the original state |1〉 A with probability 1 − p ≈ 1. The evolution we have seen holds for a general state, regardless of the initial states of Angelina and Brad: After a short interaction time ∆t T s , the probability that Angelina's state suffers an X A error is p = |2π∆t/T s | 2 1, while the probability the original state is instead recovered on measurement is 1 − p.
Problem 10: Demonstrate that the probability of an X A error is indeed p = |2π∆t/T s | 2 .
For this example, we chose the interaction between Angelina and Brad to be an X X interaction, and the interaction was deterministic: We know exactly the Hamiltonian through which they interact. We could instead have chosen for example a Z Z or Y Y interaction, each of which would have given a comparable result, generateing small Z A or Y A errors, respectively.

Problem 11:
Demonstrate that a Z Z interaction will give a Z A error, of similar form to the X X interaction.
Another assumption we made is that Brad was in a pure state when we started the interaction. Can we inject some uncertainty by instead saying that we don't know Brad's state, that instead Brad is in a mixed state? In that case, we have to use a density matrix ρ B to describe Brad, but it turns out this doesn't really change things in a substantial way. We can use a procedure known as purification to handle this situation.

Sidebar: Two-state purification
The way purification works is as follows: Say in our original basis {|p〉, |r〉}, Brad's density matrix is where |a| 2 + |c| 2 = 1 and the density matrix is Hermitian (note this is not meant to be the density matrix for a pure state). The Hermitian property of ρ B means we can find another basis {|s〉, |t〉} in which the density matrix is diagonal: with | f | 2 +|g| 2 = 1. Note the diagonal elements are real and non-negative. Written in outer product form, this is We extend the vector space {|s〉, |t〉} to a four-dimensional one, adding two additional basis vectors |u〉 and |v〉, such that the set {|s〉, |t〉, |u〉, |v〉} is an orthonormal spanning set. We define the pure state |B 〉 for Brad in this extended space as Note we only pair up |s〉 with |u〉 and |t〉 with |v〉, and we have taken the square roots of the 2 × 2 density matrix elements. The corresponding pure state density matrix is If we trace out the two imaginary degrees of freedom |u〉 and |v〉, we recover the original mixed state density matrix: as desired.
How does Brad's extended state interact with Angelina? If we consider the X A X B interaction, we've done two things that affect the X B part of this: We've changed basis vectors to make Brad's density matrix diagonal, and we've extended Brad's Hilbert space from two to four dimensions. Specifically, the new basis vectors are where U is the unitary change-of-basis operator. The components of U in the {|p〉, |r〉} basis are As we have the identity we can write showing that the components of |s〉 in the {|p〉, |r〉} basis are the first column of the matrix representation of U in that same basis. A similar calculation shows that the components of |t〉 are the second column of that matrix representation.
The representation of X B in the new basis will no longer look like an X Pauli matrix, but instead will be some weighted sum of the four Pauli matrices I B , X B , Y B and Z B (now in the new basis), because these span the space of Hermitian 2 × 2 matrices.
To deal with the extension of Brad's vector space to four dimensions, we define in the extended space the extended X B operator where I is the identity in the extension space, and in the expression on the right, the non-zero sub-matrices are the representation of X B in the new {|s〉, |t〉} basis that diagonalizes the density matrix. The interaction X A X B ⇒ X A X B , and for example the expectation value 〈X A X B 〉 would be calculated as tracing over Angelina's two-dimensional space as well as Brad's four-dimensional purified space. Just tracing over the extension of Brad's space will return the mixed density matrix ρ B for Brad. In any case, the result is the same as for a pure state.
What about more generally? What if Angelina interacted with two two-level systems at the same time, say with Brad and Charlie, through a similarly deterministic Hamiltonian interaction? Or, more likely, what if she interacts with a set of harmonic oscillators, at different frequencies and with different types of interactions? Clearly these questions can go on indefinitely, building more and more complex models for an environment that in fact we know very little about. What we really want is a generic method to treat Angelina's interaction with her environment, without having to invoke specific microscopic models.
Interaction of the worst (entangling) kind (worst in the sense that these will tend to reduce the off-diagonal elements in Anglina's density matrix, which is where all of her "quantum-ness" resides) happen all the time, between qubits and the environment in which they are embedded: Energy exchange, change of phase, all lead to errors in the qubit evolution. The most general way this kind of error occurs can be understood in the following simple way: We prepare the qubit of interest (Angelina) in a pure state |A〉. We assume we can write the environment (the universe outside the qubit) as a quantum state |e〉, initially not entangled with the qubit. If you object that the environment should be described by a density matrix, because we don't know what state it is in, we can use purification of the environment to generate a pure-state description of the environment. See the detailed description that follows if you are interested.

Sidebar: Purification of the environment
The way purification for the environment works is very similar to that for the two-state system discussed earlier. We place the environment in a (for simplicity, at most countably infinite) vector space, spanned by the basis vectors {|u j 〉, j = 1, . . . , N }. The density matrix is a Hermitian matrix, so we can always find another basis {|v j 〉, j = 1, . . . , N } in which the density matrix is diagonal, We double the size of the Hilbert space, we span the increased space by adding N additional orthonormal basis vectors {|w j 〉, j = 1, . . . , N }, then define the extended state of the environment as which is clearly a pure state. The extended density matrix is then To recover the original density matrix, we trace out the additional imaginary states we created to make the state pure, as desired.
Let's take the outer product of the initial environment state |e〉 with the qubit state. Let's first assume the qubit starts in the state |0〉: This gives us the product state |e〉⊗|0〉 = |e0〉 (note we're writing the environment state first). Next, we allow these to interact for a short time. The initial environment evolves in this time to |e 1 〉, and the interaction causes an admixture of the orthogonal qubit state |1〉, with the environment in a different state |e 2 〉. Hence we can write The interaction not only changes the qubit, but modifies the environment as well. For a qubit starting instead in the |1〉 state, we have We usually assume the environmental changes are small, so the corresponding error-associated amplitudes are small, i.e. we assume that |b|, |c| 1 and that |a|, |d| are just slightly smaller than 1.
We can generalize by using the qubit projection operators P 0 and P 1 , written in our {|0〉, |1〉}, basis as written on the right in terms of the Pauli operators, and Using these projection operators we can write the evolution for a general state |u〉 = x|0〉+ y|1〉 where the Pauli operators I and X on the qubit state. The operators P 0 , P 1 pull out the appropriate components of |u〉, and I and X then either leave these as they are, or flip them, |0〉 ↔ |1〉. Note that the notation here is somewhat sloppy, the terms should be written more precisely as e.g. b |e 2 〉 ⊗ X P 0 |u〉 and so on. The meaning however should be clear. Equation (67) can be written Now we can write so we can write this as Note that the amplitudes b and c in the terms with the operators X and Y are assumed small. Furthermore, a and d should be nearly equal, and the environmental states |e 1 〉 and |e 4 〉 should be very nearly equal, so the term with operator Z should also be small. We can schematically rewrite this as where the amplitudes |ε x |, |ε y |, and |ε z |, which correspond to X , Y and Z errors in the qubit are all small, and |a 0 | ≈ 1 is very close to unity, which represents no error in the qubit. We see that without many assumptions, and some hand-waving, a general interaction of a qubit with its environment can be written as a no-error outcome, with large amplitude, superposed with small-amplitude environmental interactions that cause X , Z and Y errors. These are known as bit-flip, phase-flip and combined bit-and-phase-flip errors, respectively. Note that further, the amplitude of the errors will scale with the interaction time that the qubit and the environment are interacting, as we saw for the Angelina-Brad interaction: The longer the qubit and environment interacts, the larger the amplitudes for the X , Y and Z errors, thus the more likely that such errors will occur. If the error amplitudes become too large, then our simple model will break down, because the qubit will begin to suffer multiple errors that generate more complex states. We now have a handle on what a qubit does when interacting with its environment: We can model short, weak interactions as leaving the qubit state unchanged, superposed with states where the qubit suffered single X , Y and Z errors. These errors leave the qubit entangled with its environment. In principle, if we know what state the qubit is supposed to be in (here |u〉), we could build a projective measurement operator that projects onto |u〉, P u = |u〉〈u|, which returns the value 1. This can be combined with its counterpart that projects onto the "not |u〉" space, P not u = I − P u , which returns the value −1. With a probability |a 0 | 2 ≈ 1 we would return the value 1 and the qubit would be returned to its desired state, dis-entangled from the environment; with a probability 1 − |a 0 | 2 1 we would instead get the value −1, and we would know we have to correct the state of the qubit. However, this is only useful if we know |u〉, and also only if the qubit is not entangled with other computational qubits. How do we deal more generally with these errors, in a way that doesn't require knowing anything about the state of the qubit, and avoiding having to dis-entangle the qubit from its computational partners?

Error correction
Error correction is a fascinating topic in quantum computation. Peter Shor's discovery of a ninequbit encoding that allows the detection and correction of errors in the encoded qubit state, without knowing the state of the qubit, is a landmark in the evolution of quantum computation. We will introduce this topic by first describing the most simple quantum error-correcting code, a circuit that can detect and correct bit-flip errors without knowing the encoded state.

Three-bit encoding
We have two parties, Angelina and Brad (these are people now, not qubits) that are going to communicate with one another. Let's assume Angelina and Brad communicate through a classical communication channel, sending zeroes and ones to one another using some kind of binary encoding. However, the communication channel is "noisy" in the sense that a zero can be flipped during transmission to a one, and a one can be flipped to a zero, in some random, unpredictable way. We make the simplifying assumption that this is a binary symmetric communication channel, which means that the probability that an error causing 1 → 0 is as likely as an error that causes 0 → 1, both occurring with probability p; real hardware probably won't respect this simplification, of course. The probability of no error is 1 − p. We won't consider any other kinds of errors (for example, complete loss of a communication bit, or scrambling the order of a multi-bit communication stream).
Say Angelina and Brad want to lower the probability of a communication error. The simplest way for Angelina to do this is to use a repetition code, which just means Angelina will send the information more than once. Brad, upon receiving Angelina's message, can then "vote" the results against one another, with the majority winning. For this to work, Angelina of course needs to send the information an odd number of times. The simplest method is for Angelina to use a 3-bit repetition code: Angelina encodes each bit of information into three transmission bits, so 0 → 000 and 1 → 111.
When transmitting this three-bit repetition code, there are a number of things that can happen. One outcome is that none of the bits suffers an error; another possibility is that one bit suffers an error; or two suffer errors, or all three do. We can write the probabilities for each of these outcomes, assuming the errors are all independent, in other words that there is no correlation between errors. If the probability for one bit to suffer an error is p, then the probability that no bit suffers no error is (1− p) 3 . The probability that all three bits have errors is p 3 ; and the probability for one bit or two bits to have errors is 3(1 − p) 2 p and 3(1 − p)p 2 , respectively (the factor of 3 because there are three places the error or non-error can occur among the three bits). Note of course these probabilities are just the binomial expansion of (a + b) 3 where a = 1 − p and b = p.
When Bob takes the bits he's received and votes them against one another, a communication error only occurs when there are two-or three-bit errors, so the probability of a communication error goes from p when sending a single bit, to 3(1 − p)p 2 + p 3 when sending three bits. This probability is less than p, if p < 1/2; if p is very small, then this error is of order 3p 2 p. If for example the error probability p is one percent (0.01), then the probabilities of the various errors are shown in the table below. The rate of communication errors falls from one percent to roughly 0.03 percent by using this simple code, a reduction by a factor of 30 at the cost of having to send three times as many bits. Using more bits in the same manner reduces the rate further, in an exponential fashion.
Problem 12: Find the communication error rate using a majority voting scheme for a 5-bit encoding, given a one-bit error rate p.

Bit-flip qubit encoding
Let's now turn to the more interesting problem where Angelina and Brad want to communicate using quantum bits. Let's assume that we can have only bit-flip errors, so we have the error processes |0〉 → |1〉 and |1〉 → |0〉, each occurring with equal probability p. We are thus considering random X gates, occurring with probability p. If the unwanted X gate does not act, then the identity operator I acts instead, with probability 1 − p. Say Angelina wants to send a quantum state |u〉 = a|0〉 + b|1〉. If she sends this state, then there is a probability 1 − p that Brad will receive the state |u〉, but there is a probability p that Brad will instead receive the state X |u〉 = b|0〉 + a|1〉, due to the erroneous action of the X operator. How does Angelina protect against this? She cannot create the state |u〉|u〉|u〉 = |uuu〉, unless she knows the amplitudes a and b. To create the state |uuu〉, not knowing a and b, Angelina would have to copy the state and thus violate the no-cloning theorem (see sidebar). However, Angelina can instead create and send a kind of state called a GHZ state, of the form (for three qubits) (a|000〉 + b|111〉)/ 2. Here GHZ stands for Greenberger-Horne-Zeilinger, the names of the researchers that explored the utility of such states. Hence Angelina performs this transformation: where |0〉 = |000〉, a three-qubit encoding.

Sidebar: No-cloning theorem
A very important theorem in quantum information, the no-cloning theorem, says that it is not possible to copy a quantum state. This means there is no "photocopier", no black-box that can copy quantum states. The proof is by negation: Assume there is such a box, so |u〉|s〉 → |u〉|u〉 through a unitary transformation (see Fig. 14): Now copy another state |v〉 (the unitary U should work for any state): Taking the inner product of these two expressions, using the identity U † U = I and 〈s|s〉 = 1 and remembering that (AB) = B A , we have Then either |v〉 = |u〉, or |v〉 and |u〉 are orthogonal. Hence a proposed transformation U that copies a general quantum state is impossible; a general U can at most copy one state and its orthogonal partner (but not a superposition of the two).
How would Angelina generate a GHZ state from the state |u〉? The circuit to do this shown in Fig. 15: Angelina starts with the first qubit in the state |u〉 = a|0〉 + b|1〉, with the accompanying second and third qubits each in the state |0〉. Alice then does a controlled-NOT using the first qubit as the control and the second qubit as the target, yielding the two-qubit state a|00〉 + b|11〉 as the output. She then does a second controlled-NOT with the third qubit, generating the desired three-qubit GHZ state a|000〉 + b|111〉. 0 0 ψ Figure 15: Circuit to extend one-qubit encoding to three-qubit encoding.
Brad receives the state sent by Angelina, but subject to random bit-flip errors on each qubit. He wants to check if there were any errors in transmission. To do this, Brad uses two additional qubits, known as ancilla qubits, to "vote" the communication qubits against one another, using the circuit in Fig. 16. This kind of circuit performs what is known as a "parity check".
The way this circuit works is that the first and second qubits control sequential CNOTs on the first ancilla, which effectively makes these two qubits "vote" against one another; if they are not in the same state, the result is that the first ancilla is flipped. Then the second and third qubits do the same to the second ancilla, flipping that ancilla if these two qubits differ.  Figure 16: Circuit to generate an error syndrome in two ancilla qubits, using three-qubit encoding. The three communication qubits (q 1−3 , in the state |ψ〉) are effectively "voted" against one another using the two ancilla qubits (a 1,2 , both set to |0〉) at the bottom of the circuit. This kind of circuit does what is called a "parity check." Finally, the two ancillas are compared, thus effectively comparing one and two against two and three, which is the same as one voting against three. The result is that the two ancillas report not only an error, but indicate in binary form where the error occurred; note the circuit only works if at most one qubit suffered an error.
Let's work through this circuit, following its operation step by step. We assume that a single X error has occurred to the second qubit during transmission, so the state Brad receives is a|010〉 + b|101〉 instead of the error-free a|000〉 + b|111〉. The states following each of the gates in the circuit are then: At the end of the process, the ancilla qubits encode the binary number 10, which indicates an error on the second qubit; the measurement of the two ancillas at the end of this process would identify this error to Brad, and he could then apply a corrective X gate to the second qubit, recovering the state Angelina had sent. You can work through the other possible errors and see how the circuit works. Table 1 summarizes the outcomes, where q 1 through q 3 are the communicated qubits and A 1 and A 2 are the ancillas; an X in the column for q j means that that qubit suffered an X error. You can see that, as mentioned above, A 1 A 2 is a binary-encoded indicator of where the error occurred. If two or three qubits suffer errors, the circuit does not work, so we didn't tabulate those errors. The ancillas A 1 and A 2 are called an error syndrome, indicating what error occurred. Table 1: Three-qubit error encoding ancilla results, for single bit-flip errors.
You can work out how to extend this table for what happens with the ancillas for multiple qubit errors.
We can now add to the circuit in Fig. 16 so that we use the error syndromes from the two ancillas to automatically correct any single-qubit errors, as shown in Fig. 17, which now includes the error-correcting circuit as well as the error-syndrome circuit.  Figure 17: Circuit to detect and correct errors in the three-qubit bit-flip error code. Here we introduce a new set of gates, double-control CNOT gates (these are versions of the Toffoli gate). When the two controls are filled circles, the CNOT is applied when both controls are in the |1〉 state, while an empty circle means that control qubit must be in the |0〉 state for the CNOT to act.
This quite interesting circuit unfortunately only corrects bit-flip (X ) errors. As we have seen with quantum bits, you can also suffer from Y and Z errors, and as you can check, this circuit does not protect against these. Hence this is not sufficient to correct all single qubit errors.

Problem 13:
Work out what happens if a Z error occurs on one of the three qubits in the code. Does the error detection circuit signal an error?
We can build a circuit that detects phase-flip instead of bit-flip errors, by instead encoding the qubits in a GHZ state in the |±〉 basis: This could easily be implemented by adding a Hadamard to each of the three qubits after the double-CNOT circuit has created the three-qubit GHZ encoding. However, as you can verify for yourself, this circuit no longer protects against bit-flip errors!

The Shor 9-qubit code
It turns out that (of course) there is a way to protect against both bit-flip and phase-flip errors; this was another of Peter Shor's major discoveries. His code, which was the first example of a fully-protected error code, uses nine qubits in what is called a concatenated code: This is called, quite imaginatively, Shor's nine-qubit code. The encoding is done using the circuit shown in Fig. 18. Shor's code has since been improved with a handful of smaller, more efficient codes, and the error-syndrome analysis has been worked out in much greater detail. The way the Shor code works is that each set of three initial qubits in Fig. 18 are first encoded in the bit-flip code a|000〉 + b|111〉 that we already looked at. Each qubit in this code is then the basis for a three-qubit block, where a qubit in |0〉 is encoded as | + ++〉 and a qubit in |1〉 becomes | − −−〉. This allows detection and correction of both bit-and phase-flips. In fact, this code can protect against up to one bit-flip error in each block, and any number of phase-flips in one block. A circuit similar to that shown in Fig. 17 is used to detect X errors in any of the three-qubit blocks; the circuit shown in Fig. 19 is used to detect phase-flip errors in the blocks. The first set of six CNOTs compares the signs of the qubits in blocks 1 and 2, and the second set compares those in blocks 2 and 3. A phase-flip error on any qubit in one of the blocks gives the same outcome, thus the Shor code is known as a degenerate code: Different errors can lead to the same outcome, while in a non-degenerate code each error can be uniquely identified. If the Shor code suffers a Y error, both the X -correcting and Z-correcting codes will work to fix that error, so this is a fully error-protected code. Figure 19: Circuit to detect phase-flip (Z) errors in the Shor 9-qubit codeword.
A number of more sophisticated and compact encoding schemes have been developed since the 9-qubit Shor code. These include the seven-qubit Steane code and a class of five-qubit codes discovered by Ray Laflamme and co-workers. These have been generalized as the CSS codes, named after their inventors, A. Robert Calderbank, Peter Shor and Andrew Steane. There are also a class of stabilizer codes, discovered by Daniel Gottesman and separately by A. Robert Calderbank, Eric Rains, Peter Shor, and Neil Sloane. We now turn to the surface code, which is a kind of stabilizer code.

Introduction to the surface code
The surface code is an error-detecting code that embeds some concepts from topology. There are two main parts to the surface code: A geometric layout for the individual physical qubits, and a sequence of operations that are carried out in a continuous cycle with these qubits. The combination of these two parts leads to an error-protected "fabric" in which errors can still occur, but are corrected with exponentially growing probability as the number of physical qubits is increased (as long as the operations that make up the continuous cycle are performed above a certain level of fidelity). Logical qubits can then be defined in this fabric, with size of the logical qubits (the area of the fabric, or equivalently the number of physical qubits involved) determined by the rate at which errors occur in the physical qubits and the rate of errors that can be tolerated at the logical level. The rate at which logical qubit errors can be tolerated depends on the computational problem. If a problem involves say 100 logical operations, and the probability of a logical error is 1%, meaning 99% chance of no error on any operation, then the chance that you can perform all 100 operations without a logical error scales as 0.99 100 = 0.37: You have a roughly one-third chance of getting the right answer, which may or may not be tolerable. If instead the calculation involves 1000 operations, then the probability of getting the right answer is 0.99 1000 ≈ 4×10 −5 . To recover a one-third chance of no error, the logical error rate would need to be improved by a factor of 10, to 0.1%.
The threshold above which the physical qubit operations need to be performed to obtain the exponential improvement promised by the surface code is around 1%. If the physical qubit operations can be performed with say a 10 −4 = 0.01% error rate, then it turns out that a logical qubit with an error rate of order 10 −17 would need about a thousand physical qubits.
The physical layout of the qubits in the surface code is shown in Fig. 20a; the qubits can all be identical, but are used in three different ways, as determined by the cycle of operations. Half of the qubits are "data qubits," represented by open circles in Fig. 20a, storing the quantum state for the computation. The other half are error-detecting qubits, represented by filled circles, which we call "measure qubits." The measurement qubits are used to stabilize and manipulate the quantum state of the data qubits. There are two types of measurement qubits, "measure-Z" qubits, colored green in Fig. 20a, and "measure-X" qubits, colored yellow; these are also called Z syndrome and X syndrome qubits in the surface code literature; see e.g. Ref. [15]. The cycles for each are shown in Fig. 20b and c. Each data qubit is coupled to two measure-Z and to two measure-X qubits, and each measurement qubit is coupled to four data qubits. In some situations, such as in a logical Hadamard operation, the data qubits are exchanged with the measurement qubits, so every physical qubit must be able to serve in both data and measure roles.
A measure-Z qubit, when performing the measurement cycle shown in Fig. 20b, forces its neighboring data qubits a, b, c and d into an eigenstate of the operator product Z a Z b Z c Z d : Each measure-Z qubit is thus said to measure a Z stabilizer. A measure-X qubit, cycling through the process in Fig. 20c, forces its neighboring data qubits into an eigenstate of X a X b X c X d , and therefore measures an X stabilizer. Away from the boundaries, each data qubit contacts four measure qubits, and each measure qubit contacts four data qubits; the measure qubits perform four-terminal measurements. On the boundaries, the measure qubits contact only three data qubits and perform three-terminal measurements, and the data qubits contact either two or three measure qubits. The solid line surrounding the array indicates the array boundary. (b) Geometric sequence of operations (left), and quantum circuit (right) for one surface code cycle for a measure-Z qubit, which stabilizes Z a Z b Z c Z d . (c) Geometry and quantum circuit for a measure-X qubit, which stabilizes X a X b X c X d . The two identity I operators for the measure-Z process, which are performed by simply waiting, ensure that the timing on the measure-X qubit matches that of the measure-Z qubit, the former undergoing two Hadamard H operations. The identity operators come at the beginning and end of the sequence, reducing the impact of any errors during these steps.

Quiescent state of the surface code
The measure-Z and measure-X qubits are operated in the cycle shown in Fig. 20b and c. After initializing each measure qubit in its ground state |g〉, the sequence comprises four CNOTs followed by a projective measurement. For the measure-Z qubit, the CNOTs target the measure qubit with the four nearest-neighbor data qubits as the controls, with the projective measurement yielding an eigenstate of the Z a Z b Z c Z d product operator. For the measure-X qubit, after the ground state initialization, a Hadamard is applied to the measurement qubit, putting it in |+〉, the four CNOTs then target the nearest-neighbor data qubits using the measure qubit as the control, and a Hadamard follows this operation; the projective measurement yields an eigenstate of the X a X b X c X d product operator.

Problem 14:
Show that the surface code cycle shown in Fig. 20b for a measure-Z qubit indeed forces the four data qubits into an eigenstate of Z a Z b Z c Z d .
After the projective measurement of all the measure qubits in the array, the state |ψ〉 of all the data qubits simultaneously satisfies Z a Z b Z c Z d |ψ〉 = Z abcd |ψ〉, with eigenvalues Z a bcd = ±1, and X a X b X c X d |ψ〉 = X a bcd |ψ〉 with eigenvalues X abcd = ±1. This holds for every four-qubit collection a bcd of data qubits, for the entire surface code array. Following each measurement of the measure-Z and measure-X qubits, the cycle of resets, CNOTs and measurement is repeated. The measure qubits in Fig. 20b and c all operate in lock-step, so that every step in the cycle shown in the figure is completed over the entire 2D array before the next step begins. We note that the particular zig-zag sequence a bcd for each of the measure qubits is quite particular, and cannot easily be modified while preserving the stabilizer outcomes. It ensures, in part, that no measure-X is interacting at the same time on a particular data qubit as any measure-Z or other measure-X, and similarly for all the measure-Zs. Also, the timing in the surface code sequence shows that measurements of the measure-X and measure-Z qubits happen more or less at the same time, certainly these should all be completed before starting the next surface code cycle, but they do not need to be simultaneous across the array.
The Z Z Z Z and X X X X measurements, starting with the very first complete measurement cycle when starting up a surface code, take whatever initial state the data qubits are in, and project from that state a complex entangled state |ψ〉 that is a simultaneous eigenstate of all the measurements across the array; we call this entangled state the quiescent state. The particular quiescent state |ψ〉 that results is selected at random from the enormous number of possible outcomes. Given the measurement outcomes, depending on the detailed structure of the overall array (which we discuss later), there may or may not be more than one possible state for the data qubits; this depends on the number of degrees of freedom left by the set of measurement outcomes.
Once selected, in the absence of errors, the same state |ψ〉 will be maintained by each subsequent cycle of the sequence, with each measure qubit yielding the same measurement outcome as it measured in the previous cycle, because all the X and Z stabilizer measurements commute with one another. This is trivial for stabilizers that do not have any qubits in common, as single-qubit operators on different qubits always commute. Stabilizers that have qubits in common will always share two such qubits, so every X stabilizer shares two data qubits with each neighboring Z stabilizer and vice versa. As we discussed earlier, X and Z measurements that share two qubits in common still commute, even though the individual X and Z operators on one qubit do not commute.
In Table 2 we show the Z Z Z Z and X X X X eigenstates and their corresponding stabilizer eigenvalues. For any set of four data qubits, measured by say a measure-Z qubit, the eigenvalue returned by the measure-Z stabilizer restricts the four data qubits to some linear superposition of the eight four-qubit states with the corresponding eigenvalue. The same holds for the four data qubits stabilized by a measure-X qubit. If we consider a set of six adjoining data qubits, two measured only by a measure-Z, two measured by both a measure-Z and a measure-X, and two measured only by a measure-X, we know that the two that are measured by both measure qubits will be in a linear superposition of Bell states, entangled with the other qubits, as the Bell states can be simultaneous eigenstates of both Z Z and X X . The Bell state is not specified, because the nature of the four-fold measurement constrains only the product of the four qubits, not of any pair. How constrained would such a set of qubits be? Each data qubit has two degrees of freedom, the relative amplitude and the phase of its |0〉 and |1〉 basis states (i.e. the two angles on the Bloch sphere). With six qubits there are 2 × 6 = 12 degrees of freedom. We only have two measurements, which impose four constraints (fixing the real and imaginary parts of the corresponding operator eigenvalue), so we still have eight degrees of freedom. Table 2: Eigenstates for the four-qubit stabilizers Z a Z b Z c Z d and X a X b X c X d . Eigenvalue Consider now a larger piece of the surface code, such as that in Fig. 21. This has been drawn with two types of boundaries, terminating with measure-X qubits on the right and left, which we call X boundaries, and measure-Z qubits on the top and bottom, which we call Z boundaries. The X boundaries are also called smooth boundaries and the Z boundaries are called rough boundaries [15].
If we count the number of data and measure qubits in the array, we find there are 41 data qubits and 40 measure qubits, so there are 2 × 41 degrees of freedom (with 2 41 distinct states) in the data qubits, with 2 × 40 constraints from the stabilizer measurements, which are all linearly independent. To see this, you need to realize that each stabilizer measurement picks one of two possible outcomes (±1) so reduces the number of degrees of freedom by a factor of two. Hence there are net two unconstrained degrees of freedom in this piece of surface code array: This is equivalent to a qubit, and in fact, this patch of the surface code forms a logical qubit, as we will see later. If we had instead drawn this array with only X or only Z boundaries, instead of half X and half Z boundaries, then it turns out that one of the Z or X stabilizers can be written as a product of all the other Z or X stabilizers. As the stabilizers all commute, this means that this stabilizer does not impose an additional constraint, so with N measure qubits you only have 2N − 2 constraints. However, in these arrays (as you can easily convince yourself) there are only N − 1 data qubits, so in these arrays there are no additional degrees of freedom: The stabilizer measurements completely determine the quiescent state, there is no freedom given a set of measurement outcomes, so such patch of the surface code array could not serve as a logical qubit.

Manipulating a logical qubit
As we have just discussed, the segment of surface code array shown in Fig. 21 has two unconstrained degrees of freedom. Thus we can recast the state |ψ〉 of the array into the outer product of a logical qubit state |u〉 L with the fully stabilized state |v〉 of the rest of the array.
In other words, we can write |ψ〉 = |u〉 L ⊗ |v〉 , where |v〉 lives in a 2 40 dimensional vector space and is constrained to a single state by the stabilizer measurements. We now want to find logical operators X L and Z L that can manipulate |u〉 L without affecting |v〉.
Consider the operators X L = X 1 X 2 X 3 X 4 X 5 and Z L = Z 6 Z 7 Z 3 Z 8 Z 9 , shown in Fig. 21. Note that X L operates on a chain of qubits that connect the two X boundaries of the array, and Z L operates on a chain that connects the two Z boundaries, which is important. As drawn, these operators commute with every stabilizer in the figure: X L trivially commutes with all the X stabilizers, and because it bit-flips (applies X j to) either none, or two, of the four qubits stabilized by each Z stabilizer, it also commutes with all the Z stabilizers. Applying X L to the array thus does not change any of the X or Z stabilizer measurements. The same holds for Z L , which phase-flips (applies Z j to) none or two of the four qubits in each X stabilizer, so commutes with all the X stabilizers, and trivially commutes with all the Z stabilizers. However, these operators clearly change the state |ψ〉 of the array, because X L bit-flips five qubits, and Z L phase-flips five qubits. Furthermore, we can easily show that X L Z L = −Z L X L , so these obey the commutator relations for X and Z operators on a qubit. We thus have defined two operators that manipulate the independent degrees of freedom of our logical qubit |u〉 L , without affecting the stabilized state |v〉.

Problem 15:
Prove that X L Z L = −Z L X L by commuting the individual qubit operators that make up these logical operators.

Problem 16:
Try defining X L and Z L as chains of X j and Z j operators that link the Z and X boundaries respectively (i.e. perpendicular to the chains we defined for X L and Z L ). Why doesn't this work?
It may seem that we could choose other chains of single qubit operator products to define different X L or Z L operators. Consider X L = X 1 X 10 X 11 X 12 X 3 X 4 X 5 , shown in Fig. 21. This satisfies the same conditions as X L , as the X j data qubit operators are paired so as to bracket each measure-Z qubit, so X L commutes with all the stabilizers, and will generate a quiescent state |ψ X 〉 = X L |ψ〉 with the same measurement outcomes as |ψ〉 and |ψ X 〉. However the state |ψ X 〉 is actually trivially related to |ψ X 〉: First, note that we can manipulate the set of operators appearing in X L , as where we have used X 2 2 = I. The operator in parentheses in the third line of Eq. (87) is the stabilizer outlined in black in Fig. 21: We have shown that X L is just X L multiplied by an operator product that is stabilized to one of its ±1 eigenvalues by the surface code stabilizers. If we operate on a quiescent state |ψ〉 with X L , we can simply replace this operator product by its eigenvalue: X L |ψ〉 = X 2,10,11,12 X L |ψ〉 = ±X L |ψ〉 .
Hence X L is trivially related to X L . This holds for any operator X L that can be written as a stabilized operator product times X L . Any X L chain that crosses the array in Fig. 21 can be written as X L multiplied by a product of X X X X stabilizers: There is only one non-trivially distinct X L operator for this array. Figure 21: A square 2D array of data qubits, with X boundaries on the left and right, and Z boundaries on the top and bottom. The array has 41 data qubits, but only 40 X X X X and Z Z Z Z stabilizers. A product chain X L = X 1 X 2 X 3 X 4 X 5 of X j operators connects the two X boundaries and commutes with all the array stabilizers . The product chain Z L = Z 6 Z 7 Z 3 Z 8 Z 9 connects the two Z boundaries and also commutes with the array stabilizers. A modification of the X L chain to the chain X L = X 1 X 10 X 11 X 12 X 3 X 4 X 5 generates a quiescent state |ψ X 〉 = X 2,10,11,12 |ψ X 〉, equal to ±1 times |ψ X 〉, as determined by the result of the measurement X 2,10,11,12 = ±1 of the encircled measure-X qubit (outlined in black). This result may seem odd: If we operate with a loop of X X X X operators corresponding to a stabilizer, we are bit-flipping four data qubits, so the state X X X X |ψ〉 should be different from |ψ〉. However, the fact that X X X X is equal to a stabilizer means that |ψ〉 already includes superposition states of the un-bit-flipped and the bit-flipped states of the data qubits involved in X X X X , so the loop operators is essentially just swapping states already in |ψ〉, with no net effect.
The same argument applies to alternative Z L operators: Any such operator is equal to the original Z L multiplied by a product of Z Z Z Z stabilizers, and thus any Z L |ψ〉 differs from Z L |ψ〉 by ±1. Hence there is also only one linearly independent Z L operator for this array.
Our array only gives us two degrees of freedom unconstrained by the stabilizers, and only defines a single logical qubit. Even if we make the array larger, while maintaining the half-X and half-Z boundaries, the array will only have two degrees of freedom, so won't allow us to define more logical qubits. Later, we will show how to make larger numbers of logical qubits within an array, by adding more degrees of freedom within the array by turning off stabilizer measurements. Each logical qubit increases the size of the logical Hilbert space by a factor of two, and concomitantly reduces the size of the stabilizer Hilbert space of by a factor of two.

Error detection
We now consider errors in the surface code. Possible errors include erroneous single qubit gates, measurement errors, initialization errors, and CNOT errors. These can comprise concatenated errors, which involve by chance multiple neighboring data qubits; such concatenated errors are called error chains. We do not consider here long-range correlated errors, i.e. errors which link distant physical qubits. These are tolerated to some extent in the surface code, meaning in some cases they can be corrected, but depending on the details, these can also lead to serious logical errors. Single qubit errors, and local chains of errors, as long as they can be identified correctly, can be corrected for by tracking them and correcting any subsequent measurement outcomes using classical control software. Edmonds' minimum-weight perfect-matching algorithm [54,55] provides an automated method for doing this, and works perfectly for sufficiently sparse errors, but begins to fail as the error density increases, and as the length of the error chains increases. Numerical simulations provide estimates of the tolerance of the surface code to different types of errors, such as shown in Fig. 22.
The simulations used to generate Fig. 22 include the following types of errors, occurring during the surface code cycle shown in Fig. 20: 1. Attempting to perform a data qubit identity I, but instead performing a single qubit operation X , Y , or Z; the probability for any one of these is p/3.
3. Attempting to perform a measure qubit Hadamard H, but performing in addition one of X , Y , or Z, each with probability p/3.

4.
Performing a measure qubit Z measurement, but reporting the wrong value and projecting to the wrong state with probability p.

5.
Attempting to perform a measure qubit-data qubit CNOT, but instead performing any one of the fifteen two-qubit operations I ⊗ X , . . . , Z ⊗ Z, each with probability p/15 (only fifteen because I × I can be ignored).
The errors occur randomly during the simulation. The probability p is per step in the surface code cycle (there are eight steps in the cycle), so the overall rate per cycle of the surface code is approximately 8p. Edmonds' matching algorithm maps changes detected in the stabilizer outcomes to physical qubit errors; the rate at which the algorithm makes mistakes, meaning it misidentifies the source of a particular error report, is displayed as the logical error rate P L , defined as the number of logical X L errors appearing anywhere in the array, per surface code cycle. There are of course also Z L errors, which occur at about the same rate as X L errors, and for which the scaling with p is very similar; here we only look at X L errors. The relation between P L and p depends very strongly on the array size d, termed the distance of the array: d is the minimum number of physical qubit bit-flips or phase-flips needed to define an X L or Z L operator. In Fig. 21, for example, we need a minimum of five physical operators to define a logical operator, so that array has a distance d = 5.
For small physical error rates p, the logical error rate P L is small, and gets smaller as the array distance d increases. For large p, P L is larger, and gets larger as d increases. The cross-over between these two regimes occurs when p crosses the threshold error rate p th : For p < p th , the logical error rate falls approximately exponentially with d, while for p > p th , P L increases with d. In Fig. 22, the threshold rate is p th = 0.57%.
For error rates p < p th , the simulations show that the logical error rate scales with p according to the power law P L ∼ p d e , where we define the error dimension d e for odd d as Logical X error rate P For even d, we round down, so d e = d/2. Using this, the error rate P L is approximately

Statistical model for the logical error rate
The error rate scaling can be understood using a simple model. In Fig. 23a we show two measure-Z qubits reporting errors, marked by the letter E. Two possible sets of data qubit errors that could generate this error are shown in Fig. 23b and c: Either two data qubits had X errors (b), or the other three data qubits had X errors (c). These events look identical to the measure qubits. In general, for a size d × d array, n actual errors can look like d − n errors and be misidentified as such; the error-identifying algorithm will make this mistake if d − n is smaller than n, as d − n errors are more likely. For an array with distance d, with d odd, the most identification mistakes of this type occur when d e ≡ (d + 1)/2 errors occur and are mistakenly identified as (d − 1)/2 errors. The rate this occurs scales as p d e . This explains the general scaling seen in Fig. 22 and Eq. (90). This underlines the primary result that in the Figure 23: (a) Two measure-Z qubits report errors (E) in a row of a 2D array. This could be generated by (b) two X errors appearing in one surface code cycle, or (c) three X errors appearing in the other three data qubits. surface code, misidentification of qubit errors causes logical errors, and that large d arrays are thus less error-prone than small arrays (when operating below the error rate threshold).
The logical error rate can be estimated using statistical arguments. Chains of errors will give the same measurement outcomes if they are complementary, as in Fig. 23: Two chains of errors are complementary if their product is an array-crossing chain that commutes with all the stabilizers (in other words, the complementary chains' product is a logical operator, or equivalent to a logical operator). Since shorter error chains are more likely, we only consider minimum-length chains that cross the array. These have d operators, and can cross the array in any one of the d data qubit rows. The most likely mistakes occur for error chains with length cycle. The total X L error rate P L from these statistical arguments is then where the factor of d accounts for the d independent rows in the array. A plot of this prediction is shown in Fig. 22b, and scales in the same way as the more complete simulations in Fig. 22a. We can estimate the number of qubits needed to obtain a desired logical error rate. Using Eq. (90) for the error probability, we plot in Fig. 24 the total number n q of data and measurement qubits, n q = (2d − 1) 2 , versus the per-step error rate normalized to the threshold p/p th < 1, for three values of the logical error rate P L . We find that n q increases rapidly as p approaches the threshold p th . For a uniform gate fidelity of 99.9% (p = 10 −3 ), we have p/p th = 0.18. In this case, a logical qubit will need to contain 10 3 − 10 4 physical qubits in order to achieve logical error rates below 10 −14 −10 −15 , just barely sufficient to perform Shor's algorithm for a 500-digit number with a reasonable chance of success.
A similar discussion applies to errors occurring in time instead of the spatially-correlated errors we have discussed so far. A particular pattern of errors in time will suffer misidentifications with the same scaling as in Eq. (91), so we need roughly the same distance d in time (measured in complete surface code cycles) as we do in space to obtain the same logical error rate. Hence a d = 5 array needs to execute the surface code cycle five times to achieve the same error distance in time. See Ref. [56].

Logical error rate for different error classes
Logical errors occur more frequently for some types of errors than for others. We can classify errors by their approximate impact on the logical error rate. Errors on the data qubits, primarily identity ("idle") operations disturbed by erroneous X , Y or Z operations, are called "class-0" errors. There are four idling steps per surface code cycle. Errors that occur on the measure qubits, namely initialization, measurement, and Hadamard operations, are called "class-1" errors. Each measure-X qubit has four possible class-1 errors per cycle, and each measure-Z two possibilities. There is thus an average of three opportunities per surface code cycle for a class-1 error. Finally, errors in the measure qubit-data qubit CNOT operations are called "class-2" errors. There are four class-2 error opportunities per surface code cycle.
Simulations showing the logical error rate P L calculated for each error class separately (assuming the other error classes do not occur) are shown in Fig. 25. The logical error rate is most sensitive to class-2 errors, which have a per-step threshold of 1.25% (per-cycle threshold of ∼ 5%): CNOTs need to be performed accurately.
The dependence of the overall logical error rate on the three classes of errors occurring concurrently is shown in Fig. 26, where we display the contours of error rates (p 0 , p 1 , p 2 ) that give a total logical error rate P L = 0.02, approximately the value of P L at threshold. This figure shows that class 0 and 2 errors have roughly equal impact on P L , while P L is roughly five times less sensitive to class 1 errors.
Returning to the general error rate discussion, we see that the surface code is able to handle a per-step physical qubit error rate up to the threshold p th = 0.57%, while still preserving the integrity of logical states in the array.

Creating logical qubits
We know how to create a logical qubit from a d × d "swatch" of the surface code, and we have shown how to build the distance d logical X L and Z L operators as array-crossing chains (products) of individual qubit X and Z operators, with X L and Z L commuting with all the Physical qubit error rate p  stabilizers. However, this construct only gives us a single logical qubit. To add more logical qubits, we need to recognize that the qubit degrees of freedom come from the boundaries in the surface code; for our "swatch," the outer boundary provides the needed freedom. However, we can create more boundaries interior to the swatch, in other words we can create holes within the fabric of the surface code. The holes, known as defects in the published literature, are created by simply turning off one or more of the internal measure-X and measure-Z qubits -here, "turning off" means that the measure qubits inside a hole no longer perform their stabilizer measurement cycles; the qubits are not physically removed or otherwise disturbed. As long as we preserve the required error-protecting distance d, we can create any number of logical qubits in this way, each with their respective logical operators. We will see later that these "internal" qubits further offer a way to create a logical controlled-NOT gate that is itself protected by the surface code stabilizer cycle.
In Fig. 27 we show one way to do this, where we turn off a single measure-Z qubit, thereby creating a hole. As the Z-cut stabilizer no longer performs its surface code cycle, releasing the constraint on the product Z a Z b Z c Z d of its neighboring data qubits, we have created two additional degrees of freedom in the surface code array. These degrees of freedom can be manipulated in a way similar to the way we manipulate our previous array qubit, by defining logical operators X L and Z L that anti-commute. The hole we call a "Z-cut hole", and the qubit that corresponds to it is called a "Z-cut qubit".
In the figure, the Z-cut hole is near an X boundary of the array. The hole is surrounded by measure-X qubits, in other words making the Z-cut hole has created an internal X boundary. We define a logical X operator as X L = X 1 X 2 X 3 , which connects the array's outer X boundary with the internal X boundary of the hole. This logical operator is a chain of operators that are paired across each Z stabilizer, so it commutes with all the Z stabilizers in the array, and trivially commutes as well with the X stabilizers. We also define a logical Z operator as Z L = Z 3 Z 4 Z 5 Z 6 , which comprises a loop of Z operators surrounding the Z-cut hole. These Z operators are paired across each X stabilizer, so commutes with each of these, and trivially with all the Z stabilizers as well. This operator loop is not constrained to an eigenstate of a Z stabilizer as it would be Figure 27: A single Z-cut qubit. The array boundary is indicated by a solid black line (top), while the other three sides of the array continue outwards indefinitely. Two logical operators, X L = X 1 X 2 X 3 from the array's outer X boundary to the internal X boundary of the Z-cut hole, and Z L = Z 3 Z 4 Z 5 Z 6 surrounding the Z-cut hole, are shown. Note that the operator chains for X L and Z L have one physical data qubit in common (data qubit 3). without the Z-cut hole, so this operator can change the quiescent state |ψ〉 of the array in a non-trivial way.
The X L chain and the Z L loop share one data qubit, number 3 in Fig. 27. These two operators therefore anti-commute, as you can easily prove to yourself. You can also easily show that X 2 L = Z 2 L = I, and we can define Y L = Z L X L . We have thus created a set of anti-commuting single-qubit Bloch operators associated with this Z-cut hole, so the Z-cut hole is a logical qubit. We note that if the outer perimeter of the swatch of surface code were a single Z boundary, there would be no way to build an appropriate X L chain for the Z-cut hole. Equivalently, the product of all the Z stabilizers in the array would fix the value of Z L , so creating a Z-cut hole in a Z-boundary swatch would not create any additional degrees of freedom: At least one X boundary is needed. Note the distance d of this qubit is d = 3, limited by the length of the X L chain from the hole to the swatch boundary. The distance could be increased to d = 4 by moving the hole one stabilizer cell further away from the array boundary. However d would then be limited to 4 by the size of the Z-cut hole, which limits the Z L operator to a product of four operators. Turning off more Z stabilizers in the hole would make it possible to have a larger distance qubit. A single Z-cut qubit is called a smooth defect or a dual defect in the literature.
An X-cut qubit can be formed in an analogous way, by turning off a single measure-X qubit in an array that has at least one Z boundary; this logical qubit's Z L operator is then a chain of Z operators from the array Z boundary to the internal Z boundary created by turning off the measure-X qubit, and the X L operator is the loop of X bit-flips surrounding the X-cut hole. An X-cut qubit is called a rough defect or a primal defect in the published literature.
This concept can be made even more general by creating the qubits in pairs, i.e. creating two Z-cut or two X-cut qubits. The logical operators for these paired qubits then are created by a chain of operators linking the qubits (an X chain for Z-cut qubits, a Z chain for X-cut qubits), combined with a loop of operators surrounding the hole (a Z loop for a Z-cut qubit, and an X loop for an X-cut qubit). An example of a "double Z-cut qubit," achieved by turning off two measure-Z qubits in a swatch, is shown in Fig. 28. Making these two Z-cut holes adds Figure 28: A double Z-cut qubit. Each Z-cut hole has the logical operators X L1 and Z L1 , X L2 and Z L2 . We can replace X L1 (dashed blue line) by X L1 X L2 , which with X L2 is equivalent to the two separate X operators. However, X L1 X L2 can be multiplied by all the outlined X stabilizers, which at most give a sign change to the operator, to yield the equivalent operator X L (solid blue line) that links the two qubit holes. four additional degrees of freedom to the array. We define X L1 and Z L1 for the upper (first) Z-cut hole, and X L2 and Z L2 for the lower (second) Z-cut hole, as shown in the figure. These four linearly independent operators manipulate all four degrees of freedom, with each logical operator pair (X L j , Z L j ) commuting with the other pair but anti-commuting with each other.
We can manipulate the two qubit holes together by replacing X L1 by the product X L1 X L2 ; this anti-commutes with both Z L1 and Z L2 , and together with X L2 provides the same functionality as X L1 and X L2 . Now we define a new X L operator linking the two qubit holes, shown in Fig. 28; this is the product of three data qubit X operators, and links the internal X boundary of the upper qubit to the internal X boundary of the lower qubit. Note that X L X L1 X L2 is equal to the product of all the stabilizers enclosed by these operators (these stabilizers have black outlines in Fig. 28). Hence, if we multiply X L1 X L2 by all these stabilizers, the result is X L , so X L is equivalent to X L1 X L2 to within ±1 (the sign is equal to the product of all the enclosed stabilizers). Hence we can replace X L1 X L2 by X L .
We now have the operator set {X L , X L2 , Z L1 , Z L2 }, where the logical X operators commute with one another, as do the logical Z operators, but X L anti-commutes with both Z L1 and Z L2 ; X L2 anti-commutes only with Z L2 .
However, we are only interested in using two of the degrees of freedom in the double qubit, the two that can be manipulated with "local" operator chains (ones that do not extend to the swatch edge). We therefore use X L along with either Z L1 or Z L2 . If we choose to use Z L2 ≡ Z L , and define Y L = Z L X L , we have a complete set {X L , Z L , Y L } of logical qubit operators. By restricting ourselves to this set of logical operators, we are manipulating half of the degrees of freedom provided by the two qubit holes. Hence effectively we are manipulating a single logical qubit. We write the state of this logical qubit in the usual way, as e.g. α|g L 〉 + β|e L 〉. The advantage this concept provides is that now our double Z-cut qubit can be manipulated using only local operator chains: We are not longer required to build chains that link the qubit to the edge of the surface code patch. This will be very significant when we look at braiding operations between pairs of qubits. Figure 29: (a) A double Z-cut and (b) a double X-cut qubit, formed by turning off two measure-Z and two measure-X qubits, respectively. For the double Z-cut qubit the logical operators are X L = X 1 X 2 X 3 , a chain that links one Z-cut hole's internal X boundary to the other, and a Z L = Z 3 Z 4 Z 5 Z 6 loop that encloses the lower Z-cut hole. For the X-cut qubit, Z L = Z 1 Z 2 Z 3 , a chain that links the two internal X-cut holes' Z boundaries, and X L = X 3 X 4 X 5 X 6 loop that encloses the lower X-cut hole. X L and X L share one data qubit (qubit 3), so the operators anti-commute. Note that the loop operators can be defined to surround either of the two holes in the qubit. We can create a double X-cut qubit in a similar fashion, by turning off two measure-X qubits. Both the double Z-cut and double X-cut qubit types are shown in Fig. 29. The logical operators for the double X-cut qubit are defined in a similar way: Z L is a chain linking the two X-cut holes, and either X L = X L1 or X L = X L2 , the operator loops that surround one or the other of the X-cut holes. It is easy to show that these anti-commute.
As we will see later, the two different types of logical qubits (X-cut and Z-cut) are both needed to perform the topological braid transformation that provide the logical CNOT operation in the surface code: Only braids between mixed qubit types give the needed functionality. However, as we discuss below, a topological CNOT can be performed between two Z-cut qubits, by using an X-cut qubit as an intermediary; similarly, a CNOT between two X-cut qubits can be performed using a Z-cut as an intermediary. An arbitrary quantum computation can therefore be completed using mostly one flavor of logical qubit, with the other qubit type only making appearances in a supporting role.
The error-protection distance for the logical qubits we've been discussing is only d = 3, meaning these are relatively fault-intolerant. Moving the two holes further apart by one surface code unit cell increases the distance to d = 4, limited by the length of the Z L or X L loops; increasing the size of the holes in parallel with the hole distance will increase the distance d further, as long as measurements are repeated d times in time in tandem with the increased physical distance. The repetition in time provides protection from errors that occur in the measure-Z and measure-X qubit measurement cycles, where e.g. an isolated error in the measure-Z surface code cycle would be detected by repeated measurement cycles, providing a distance d in time equivalent to the distance d in space.

Logical qubit initialization and measurement
How does one initialize and measure a logical qubit? We describe two methods to initialize the logical state, and then two methods to measure the state.

Initialization.
There are two distinct ways to initialize a logical qubit state: The "easy" way and the "difficult" way. The easy method is to initialize the qubit in an eigenstate of the qubit cut, so for an X-cut qubit, easy initialization is in the |+ L 〉 or |− L 〉 eigenstates of X L , while for a Z-cut qubit, easy initialization is in the |0 L 〉 or |1 L 〉 eigenstates. This is illustrated for an X-cut qubit in Fig. 30, starting with a 2D array with no cuts, and stepping immediately to one with a double-X cut qubit, achieved by turning off two measure-X qubits to create two X-cut holes. If we define X L as the loop surrounding the upper qubit hole, the initial state of the qubit is the measurement outcome of the upper measure-X qubit just before it was turned off (sampled and voted d times to provide error protection). If the initial state is not the desired one, we can apply a Z L phase-flip to get the desired state. The alternative is to perform a "difficult" initialization of the X-cut qubit in the Z L basis, i.e. to initialize the qubit in either |0 L 〉 or |1 L 〉. This can be done using a logical Hadamard after initializing in the X L basis. Logical Hadamards are however somewhat complicated, as we will see later; it is actually simpler to initialize in the Z L basis directly. This is shown in Fig. 31. Starting with a fully-stabilized array, a strip of X stabilizers is turned off, where each end of the strip becomes one of the two qubit holes. The Z stabilizers that border the strip are also switched from measuring four adjacent data qubits to measuring three qubits. In Fig. 31b, there are six Z stabilizers that are switched from four-to three-terminal operation. Note that the stabilizer measurements still all commute, as you can check. The three isolated data qubits (numbered 1, 2 and 3 in Fig. 31) are measured once along Z to maintain error correction, and then the data qubits are set to the ground |0〉 state as in Fig. 31c. Setting these three qubits to their ground state ensures that the quiescent state |ψ〉 that results will be in the logical ground state of the Z L = Z 1 Z 2 Z 3 operator. Finally the two X stabilizers internal to the strip are turned back on, and the three-terminal Z stabilizers switched back to four-terminal measurements, completing the process. If instead a |1 L 〉 is desired, an X L is applied (in software) to bit-flip the logical qubit. Note that the projective measurements of the two X stabilizers that were turned back on at the end of the initialization will leave the three data qubits in a +1 eigenstate of Z L , in other words a |0 L 〉 eigenstate, even though the data qubits themselves will no longer be in their individual ground states |0〉. We can see this as follows: After the ground state re-set, the quiescent state |ψ〉 is transformed to |ψ 〉 = |000〉⊗|φ〉, where |000〉 is the state of the three data qubits, and |φ〉 the state of all the other data qubits in the array. The state |ψ 〉 is a +1 eigenstate of Z L , but is not an eigenstate of all the X stabilizers, as you can easily verify. However, we know that Z L commutes with all the X stabilizers, so there are common eigenstates of both Z L and these stabilizers. The state |ψ 〉 can be written as a superposition of these X stabilizer eigenstates, which are all +1 eigenstates eigenstates of Z L , so the logical qubit remains in |0 L 〉. The X stabilizer measurements will project |ψ 〉 onto one of these eigenstates, leaving us with a state |ψ 〉 that is still a +1 eigenstate of Z L but is also an eigenstate of all the X (and Z) stabilizers in the array.
Initializing a Z-cut qubit is completely analogous to the initialization of an X-cut qubit.

Measurement
Measuring a logical qubit is almost the inverse of initialization. As with initializing, logical measurements can be classified as "easy" or "difficult". For an easy measurement, the measure qubits in the qubit holes are simply turned on, with the stabilizer measurement projecting the logical qubit onto a stabilizer eigenstate, with the stabilizer eigenvalue equal to the logical qubit measurement outcome (after completing d surface code cycles and voting). For the X-cut qubit shown in Fig. 32a, we turn on the two measure-X qubits in the holes, whose measurement projects the data qubits adjacent to them into an X a X b X c X d product eigenstate. This constitutes an X L measurement of the logical qubit, with the X L eigenvalue equal to the (time-stable) value of X a bcd . For a difficult measurement, used to e.g. measure the X-cut qubit in the Z L basis, we use the measurement process illustrated in Fig. 32.
A completely analogous process is used to perform an "easy" measurement of Z L or a "difficult" measurement of X L for a Z-cut qubit.

Moving qubits
We turn now to the very interesting method used to entangle logical qubits. This is done by physically moving the qubit holes in the 2D array, providing a central and unique functionality of the surface code. By passing one hole of a two-hole qubit between the two holes of a second qubit, "braiding" the logical qubits together, you perform a logical CNOT of the two qubits! We first cover how to move a logical qubit hole while preserving the surface code error protection. The corresponding transformations of the logical operators associated with the moving qubit are described in the Heisenberg representation. We briefly review the Heisenberg representation of quantum mechanics (see e.g. [57] for a more complete introduction, and [58] in relation to quantum computing).
Many processes comprise unitary transformations U, with U U † = U † U = I. These include changes of basis, changes of representation, and temporal evolution. In the Schrödinger representation, these transformations are applied to the wavefunction, so that |ψ〉 ⇒ U|ψ〉, with the operators kept static. Hence for example wavefunctions are time-dependent, arising from the application of the evolution operator U(t), while operators are time-independent. Inner products are invariant under a unitary transformation.
In the Schrödinger picture, the matrix elements of an operator A transform according to As in general A = U † AU, the matrix elements can change under a unitary transformation, even though the operators do not. The second line in Eq. (92) provides the basis for the Heisenberg representation: We can equivalently assume the wavefunctions |ψ〉 do not change under the unitary transformation U, and instead we modify the operator A ⇒ A = U † AU. Hence in the Heisenberg picture, operators rather than wavefunctions are modified under a change of basis, and operators are time-dependent. The evolution of any measurable quantity will be the same in the Heisenberg representation as in the Schrödinger representation, so there is no detectable difference between the two pictures: They are thus completely equivalent from the point of view of measurement outcomes. In the surface code, moving and braiding qubits are transformations that affect both the physical array of qubits as well as the logical operators. The physical transformations are not unitary, as these involve projective measurements of the data qubits, but the transformations of the logical operators are unitary, and are most easily described using the Heisenberg representation.

One-cell logical qubit move
In Fig. 33 we show how to move a Z-cut qubit hole downwards by one cell in the surface code array. The logical qubit has the logical operators X L = X 1 X 2 X 3 and Z L = Z 3 Z 4 Z 5 Z 6 , where Z L is the loop of Z operators surrounding the lower Z-cut hole.
We first describe the physical operations involved in the move, which takes two complete surface code cycles. First, we stop measuring the Z stabilizer just below the qubit hole that we are moving. This means that data qubit 6 will not be measured by a Z stabilizer (see Fig. 33). The two measure-X qubits adjacent to data qubit 6 are converted from four-to three-terminal measurements, excluding data qubit 6 in their measurement cycles. The next surface code cycle is then executed, during which data qubit 6 is measured separately with the now-idle Z stabilizer in X , yielding in its eigenvalue X 6 ; this measurement is used for tracking errors, and for monitoring the sign of the redefined X L operator. After completing this cycle, the measure-Z qubit in the original lower Z-cut hole is turned on, so it begins to perform a normal measurement cycle. The two measure-X qubits that monitor data qubit 6 are also switched back to four-terminal measurements. The next surface code cycle is executed, which completes the physical operation for the move. To establish all stabilizer values in time takes an additional d − 1 surface code cycles. Altogether this one-cell move takes 1 + d surface code cycles.
While performing the physical operations, the two logical operators Z L and X L need to be redefined to preserve their functionality. We redefine Z L so that it encloses two stabilizer cells, its original cell plus the one below it, by multiplying Z L by the four Z operators, Z 6 Z 7 Z 8 Z 9 , that make up the Z stabilizer below the qubit hole. We term the new extended operator Z e L , given by as shown in Fig. 33a. We complete the first surface code cycle of the move, turning off the lower Z stabilizer and measuring data qubit 6. We redefine the X L operator by multiplying it by X 6 , giving see Fig. 33b. The original qubit hole stabilizer is turned back on, and the second surface code cycle of the move completed. The Z e L operator is redefined by multiplying it by the four Z operators that make up the stabilizer that was just turned on, Z 3 Z 4 Z 5 Z 6 , giving The two new logical operators X L and Z L are as drawn in Fig. 33c. Note that, as required, they still share a single data qubit and commute with all the stabilizers.

Byproduct operators
When performing a move transformation, the extension of X L by multiplying by X 6 can yield an extended X L that differs in sign from X L ; this occurs if the measurement outcome X 6 = −1.
Similarly the final Z L can differ in sign from Z L , depending on the product of the two Z stabilizer measurement outcomes involved in the move. Given the initial state |ψ〉 prior to the transformation, this results in where |ψ 〉 is the desired state, which would result if there were no sign changes in the logical operators. Hence we must operate on the resulting state with the operator product Z p X L X p Z L to regain the desired state |ψ 〉. These additional bit-and phase-flip operators are called "byproduct operators". The powers p X and p Z are determined by whether a sign change occurred in X L during the move. The byproduct operator Z L corrects the sign of the transformed X L through their anti-commutation relation, and the byproduct operator X L similarly corrects Z L . Note that these operators are not actually applied, but are tracked in the control software. These corrective operators are thus only applied when the logical qubit is measured, reversing the sign of the measurement if needed. Two corrective X L s or two corrective Z L s of course cancel.
We can also look at what happens to our description of the 2D array wavefunction |ψ〉 in terms of the stabilizer and logical qubit subspaces. Prior to the move, we have |ψ〉 = |Q〉|q L 〉; after the move, we have X Figure 33: Moving a Z-cut logical qubit hole down by one cell. (a) Logical qubit with X L = X 1 X 2 X 3 and Z L = Z 3 Z 4 Z 5 Z 6 . (b) Z L extended by multiplying it by the Z stabilizer just below the lower qubit hole: This Z stabilizer is turned off along with converting the surrounding four-terminal X stabilizers into three-terminal stabilizers, leaving data qubit 6 un-stabilized. An X measurement of data qubit 6 is performed. (c) Extend operator X L by multiplying X L by X 6 : X L = X 6 X L = X 1 X 2 X 3 X 6 . The Z stabilizer just above qubit 6 is turned on, we wait d surface code cycles. Define Z L as the product of this Z stabilizer and Z e L : Z L = Z 6 Z 7 Z 8 Z 9 . The byproduct operators only affect the logical state, so we can write the equivalent to Eq. (96) as where |q L 〉 is the desired logical state.

Multi-cell logical qubit move
We can easily generalize the one-cell move to a multi-cell move, using the same number of surface code clock cycles as a one-cell move, translating the logical qubit hole over an unlimited number of cells. Multi-cell moves are performed by extending the one-cell move to a contiguous strip of cells. A multi-cell move is shown in Fig. 34 for a Z-cut qubit hole. Details for the multi-cell move are given in the figure caption. Briefly, Z L is extended by multiplying it by all the stabilizers through which the logical qubit is to be moved, giving the extended Z e L : Here each stabilizer operator Z s j represents the product of the four Z operators on the four data qubits surrounding the jth stabilizer cell. These stabilizers are then turned off, and the fourterminal X stabilizers adjacent to the strip switched to three-terminal measurements. The unstabilized data qubits in the strip are all measured along X , and X L is extended by multiplying it by all the X operators on these data qubits, We complete the move for Z L by then multiplying Z e L by all the stabilizers except the one that is in the new shifted qubit hole, where we use the fact that Z 2 s j = I and that Z L = Zs1, i.e. the original Z L is just the set of four Z data qubit operators that define Z s1 . After the move, we wait an additional d − 1 surface code cycles to establish all stabilizer values in time.
A completely analogous process is used to move X-cut logical qubit holes, exchanging the roles of the X and Z stabilizers and measurements.
We note that multi-cell moves can be done very quickly, as a very long cut can be made in just one step of the surface code cycle, and the qubit holes moved in d + 1 surface code cycles, the same as for a one-cell move. This therefore enables long-distance interaction and communication between logical qubits, a very powerful capability.

Errors during move transformations
Previously we discussed handling of errors when the surface code stabilizers are being manipulated. A very similar discussion applies to errors that occur during the move transformations, with some differences in the details. The data qubits that are left isolated during the move can suffer from both Z and X errors; X errors have no impact, as they are erased by the single data qubit X measurements during the move. Z errors will be detected by computing the product of each data qubit X measurement with its corresponding three-terminal X stabilizer measurement, and comparing this product with that stabilizer's prior four-terminal X measurement. Z errors on the data qubits bordering the cut are detected and localized by the two X stabilizers that monitor each of these data qubits. An X error on one of these data qubits changes the sign of the solitary Z stabilizer that monitors that data qubit, but this error can be distinguished from a stabilizer measurement error by waiting d surface code cycles.

The braiding transformation and the logical CNOT
A "braid transformation" combines a pair of multi-cell moves involving one of the two holes of a logical qubit. The mobile hole moves along a closed path, with the first move taking the hole partway around the loop, and the second move completing the loop. As with the move transformations, braids are described in terms of their effect on the logical qubit operators. A braid transformation can entangle two logical qubits, in a way that makes the braid transformation equivalent to a logical CNOT; this is its most important function in the surface code.

Braid transformation of one logical qubit
We first describe the braid transformation of a single logical qubit, using a Z-cut qubit. In Fig. 35 we show the transformation of the X L operator and in Fig. 36 that of the Z L operator. For this example, the braid transformation moves the qubit hole along a path that encloses only fully stabilized cells. Later, we discuss what happens when the path encloses another logical qubit hole.
In Fig. 36, the first step shifts the qubit hole eight cells along the path (this can be any number of cells between one and the total number of cells in the braid path less the array distance d; moving the hole so that after the first move it ends up less than d cells from the start point would effectively reduce the array distance). This move is completed before the second move is started, meaning that all the stabilizers that were switched off during the move have been turned back on (except for the last cell). After waiting d surface code cycles, to catch measurement errors during the first move, the second multi-cell move is then performed, shifting the qubit hole the remaining four cells in the path, and returning the hole to where it started. The braid cannot be completed in a single multi-cell move, as this would isolate the part of the surface code array enclosed by the braid.
As can be seen in Figs. 35 and 36, the braid transforms the two logical operators X L and Z L in different ways. The X L operator is a chain of three X operators linking the two holes (Fig. 35a). As shown in Fig. 35b-e, this operator is extended in each of the moves, such that after the second move, the X L operator in Fig. 35e includes the original chain of three data qubit operators in addition to the loop X loop = X 1 . . . X 12 . This closed loop encloses only fully stabilized cells, with stabilizers X s1 , X s2 . . . X s9 , so X loop = X s1 . . . X s9 . The quiescent state |ψ〉 is necessarily an eigenstate of X loop , with eigenvalue given by the measurement outcomes of the enclosed stabilizers, X loop = X s1 . . . X s9 . Another way to understand this is that the closed loop of operators X loop can be deformed through all the enclosed X stabilizers, leaving only a product of measurement outcomes. The X L operator (blue, dark) is extended to X L = X 1 . . . X 8 X L . (d) The second move again involves measuring isolated data qubits in X . (e) The X L operator is extended to X L and now includes the original chain linking the two qubit holes, and in addition a closed loop of operators. (f) The closed loop is completely stabilized by the nine X stabilizers X s1 . . . X s9 (blue (dark) squares). We can thus reduce the operator chain X L to a chain X L that is identical to the original X L (other than sign difference). The signs are captured by defining the power p X through (−1) p X ≡ X The Z L operator is the loop of qubit Z operators that encloses the lower qubit hole. As shown in Fig. 36, the braid transformation alternately extends and then collapses the operator loop that surrounds the Z-cut hole, once for each of the two moves; other than sign changes from Z stabilizers that the loop passes over, the final Z L loop is the same as the initial loop. The difference between the transformation of X L and Z L is the key to how the braid acts as a CNOT.
Details of the braid transformation involving just one logical qubit are given in the captions for Figs. 35 and 36. Note that in Figs. 35 and 36, while we separate the actions that affect the X L operator from those that affect Z L , all X measurements of the isolated data qubits and measurements of the Z stabilizers are performed as part of the braid.

Braiding two qubits
What if the braid path encloses another logical qubit hole? The easiest way to understand what happens is to examine how operators on the two qubits are transformed by the braid operation. The two-qubit operators will be outer products of the single-qubit operators X L , Z L and I L .
As a review of two-qubit operators, consider X L ⊗ Z L , operating on the two-qubit logical state |a L b L 〉. Using the eigenstates |0 L 〉 and |1 L 〉, then X L ⊗ Z L can be represented in the standard two-qubit basis {|0 L 0 L 〉, |0 L 1 L 〉, |1 L 0 L 〉, |1 L 1 L 〉} by the matrix The braid transforms each two-qubit operator into some other two-qubit operator, with Z L2 is then multiplied by all the stabilizers shown in the dashed boxes (Fig. 39d), leaving behind a loop of Z operators surrounding the first qubit's hole, a loop that is exactly a Z L1 operation on the first Z-cut qubit, as well as the original Z L2 on qubit 2, unchanged from prior to the braid (other than possible sign changes). The braid therefore performs the transformation This transformation involves braiding the first qubit hole through the second qubit, which drags the loop of Z operators along as it did for the empty loop braid, but as the loop preserves its closed form during the two moves, it does not generate a chain or loop of operators that can act on the second qubit, so the braid transformation does nothing (other than sign changes). Hence we find In general, a braid transformation leaves logical operators that are built from closed loops of data qubit operators unchanged, and there is no braid-induced interaction with the other qubit hole. By contrast, logical operators that are built from open chains of data qubit operators linking the two qubit holes end up leaving a loop of operators surrounding the other qubit hole. The different braid outcomes arise because the first logical qubit is a Z-cut qubit, for which the X L operator is an open chain that interacts with the second qubit, while the second logical qubit is an X-cut qubit, for which the Z L operator is an open chain that interacts with the first qubit.
The braid is made of two move transformations that induce sign changes in the first qubit's logical operators, which appear as byproduct logical operators X L1 and Z L1 acting on the array wavefunction, as was discussed for the one-cell and multi-cell moves. The braid transformation also generates sign changes in the second qubit's logical operators, even though that qubit is not displaced during the braid. These sign changes generate the byproduct logical operators X L2 and Z L2 acting on the array wavefunction.
We now turn to a discussion of the CNOT gate, and make clear why the transformations we have detailed actually identify the braid as a CNOT between two logical qubits.

The CNOT gate
The CNOT gate is a fundamental gate for quantum computation. One of the two qubits in the CNOT serves as the control, and the other as the target. In the standard two-qubit basis |00〉, |01〉, |10〉, |11〉, the CNOT is represented by the 4 × 4 real matrix If the control qubit is in |0〉, the target qubit state is unchanged, while if the control qubit is in |1〉, the target qubit undergoes an X bit-flip, exchanging |0〉 and |1〉. Note that C is Hermitian, C † = C, and unitary, C C † = C † C = C C = I. One way to test a potential CNOT operation is to perform the operation on each of the four two-qubit basis states, and then do projective measurements of the result onto each of the four basis states. These sixteen experiments can be compared to the matrix in Eq. (106) to verify that the CNOT has been implemented correctly. This is essentially a Schrödinger picture test of the CNOT.
An equivalent method is to use the Heisenberg picture, by examining the transformation of the various two-qubit operators due to the action of the CNOT. This would seem to involve showing that the CNOT performs the correct transformation for all sixteen outer products of pairs of the four single qubit operators, I, X , Y and Z. It turns out you only need to show that the CNOT transforms four of these outer products correctly: The other twelve relations are either trivial (I ⊗ I is unchanged), or can be written in terms of these four transformations. Hence C † (X ⊗ X )C = X ⊗ I is the same as Eq. (108), as can be seen by multiplying both sides of Eq. (108) by C † on the left and by C on the right, and using the fact that C is unitary. Combinations such as X ⊗ Z transform according to Other combinations involving Y can be worked out using the identity Y = Z X ; hence We can therefore validate a CNOT implementation by verifying that it satisfies the four relations given by Eqs. (107-110). However, these are precisely the four transformations that we worked out for the braid, so indeed a braid is a CNOT.
Note that the full braid transformation, including all the operations on the physical data qubits, involves a number of projective measurements and is therefore not unitary. However, if we consider the effect of the braid on the product |Q〉|q L 〉, while the transformation of the stabilized state |Q〉 is not unitary, the transformation of the logical state |q L 〉 is indeed a unitary one.

CNOT between two Z-cut qubits
We have only shown how to perform CNOTs using a braid with a Z-cut qubit as the control and an X-cut qubit as the target. We can extend this to a CNOT between two Z-cut qubits using the circuit shown in Fig. 40a-c. The circuit performs a CNOT of the logical Z-cut "target-in" qubit using the logical Z-cut control qubit, with the Z-cut "target-out" qubit carrying the result. An ancillary X-cut qubit is the target for the three logical CNOTs in the circuit, so all the CNOTs are braid transformations between a Z-cut and an X-cut qubit. The circuit includes two logical measurements, with outcomes M Z and M X . If the target-in qubit is measured to be in |+ L 〉 (|− L 〉), with M X = +1(−1), then the target-out qubit does not (does) have a Z L applied to it prior to the CNOT. If the X-cut qubit is measured to be in |g L 〉 (|e L 〉), with M Z = +1(−1), then the target does not (does) have an X L applied to it after the CNOT.
You can verify that this circuit works properly by using operator transformations, or alternatively by testing it with the four input basis states |00〉, |01〉, |10〉 and |11〉 for the control and target-in Z-cut qubits (dropping the logical subscript L for now). There is an analogous circuit for performing a CNOT between two X-cut qubits, using a Z-cut qubit as an intermediate ancillary to perform the braid.
Problem 17: Verify that the circuit in Fig. 40(a)-(c) works as promised.

Single-control, multi-target CNOTs
It is frequently necessary to implement single-control, multi-target CNOTs; these appear for example in the distillation circuits used to purify imperfect states, as we shall see when we discuss the S and T gates. It turns out that these kinds of CNOTs are actually no more complicated than a single-control, single-target CNOT, and in fact can be implemented in the same number of surface code cycles as a CNOT between two Z-cut or between two X-cut qubits. We don't show the details here, but refer the reader to the literature, such as Ref. [27].

The Hadamard transformation
The Hadamard transformation is a single-qubit gate that in the standard qubit |0〉, |1〉 basis is represented by the 2 × 2 matrix In the Heisenberg representation, the Hadamard takes X to Z and vice versa, i.e.  [59]. We start in Fig. 41 with an array of d = 7 logical qubits. Next we isolate the  logical qubit in a separate patch of the 2D array, shown in Fig. 42a. The Z L operator loop is transformed to a patch-crossing chain by multiplying it by a number of stabilizers in the patch. By widening the ring that isolates the logical qubit into a "moat", so that the moat engulfs the two qubit holes, the logical qubit is transformed to a simple "patch" qubit as shown in Fig. 42b-d, similar to the d = 5 array qubit we discussed earlier.
The key to the logical Hadamard is now implemented, by performing physical Hadamards on all the data qubits in the patch; this exchanges the X L operator for Z L and vice versa, as well as swapping the identities of the X and Z stabilizers (Fig. 42e). This however results in a misalignment of the stabilizers in the patch with those in the larger 2D array, so we then perform two swaps, from data qubit to measure qubit, then from measure qubit to data qubit, shifting the patch by one stabilizer cell and realigning the stabilizers (Fig. 42f). The two Z-cut holes are then recreated (Fig. 43g), positioned so that the Hadamard-transformed X L chain ends on the internal boundary of each hole, and the Z L chain is multiplied by a set of stabilizers that returns it to a loop around one of the qubit holes. The qubit holes are then moved to realign them with their original positions ( Fig. 43h-j), a move that is split into two steps to preserve the array distance d. In the final step, the qubits are rejoined with the main array (as in Fig. 41).

Single qubit S L and T L operators
Finally, we need to implement the S L and T L operators and their adjoints. These operators are represented in the standard basis by the matrices and T L = 1 0 0 e iπ/4 .
The S L gate is also called the P or phase gate, and T L the π/8 gate. As we will see, the circuit used to implement T L is probabilistic, and half the time generates T † L . As you can easily check using Eq. (115), when this occurs T † L can be converted to T L by application of an S L gate. T L can be similarly converted to T † L by application of S L , where S L T L |ψ L 〉 = Z L T † L |ψ L 〉, and the Z L byproduct operator is handled in software. A high-fidelity logical implementation of these two gates involves special ancilla states. Implementing S L relies on the |Y L 〉 ancilla state while implementing T L relies on the |A L 〉 ancilla state The |Y L 〉 and |A L 〉 ancilla states are created in a special "short qubit," which can be put in an arbitrary but concomitantly imperfect state, a process known as "state injection." Once the state has been injected, the "short qubit" is increased to the standard distance d to make it less error-prone, and the imperfect logical state of the standard-distance qubit is then purified by a high-fidelity process known as "magic distillation", a process that can be found in the The Z L operator is multiplied by all the black outline Z stabilizers, transforming Z L to the chain going from left to right. (c) In the next cycle, all X and Z stabilizers outside the dashed box are turned off, creating a "moat" as in (d), eliminating the two qubit holes. In the same cycle, all isolated data qubits are measured in Z, except those colored blue (light), which are measured in the X basis to preserve error tracking. The other un-stabilized data qubits do not need to be measured, as changes in their quantum states will be accounted for later. (e) Before the next surface code cycle starts, a physical Hadamard is performed on all data qubits, swapping X L and Z L . (f) Following the Hadamard, a pair of swaps is performed between each patch data qubit and its neighboring measure qubits, first between each data qubit and the measure qubit above it, then between each measure qubit and the data qubit to its left. This shifts the patch by one stabilizer cell in the array, aligning the measure qubits. The dashed box shows the location of the patch prior to these swaps. This sequence continues in Fig. 43.  literature. The S L and T L gates are then implemented with circuits using logical CNOTs and Hadamards involving these ancilla states, as shown in Fig. 44 and Fig. 45, respectively. The S L gate implementation shown in Fig. 44 involves two logical CNOTs and two logical Hadamard operations. An input state |ψ L 〉 in one logical qubit is deterministically transformed into S L |ψ L 〉 by interacting with the ancilla qubit in the |Y L 〉 state. This can be most easily seen by testing the circuit with the input state |ψ L 〉 = α|g〉 + β|e〉.
To instead apply S † L , we use the identity S † L = Z L S L , which means we use the circuit shown in Fig. 44 and have a byproduct Z L appear on the output, in other words |ψ L 〉 transforms to Z L (S † L |ψ L 〉).

Problem 19:
Show that the circuit in Fig. 44 performs an S operation as claimed. Figure 44: Logic circuit that implements the S L gate. The circuit uses the ancilla state |Y L 〉 = (|g L 〉 + i|e L 〉)/ 2, on which two controlled logical CNOTs and two logical Hadamards are performed, resulting in the input state |ψ L 〉 being transformed to S L |ψ L 〉. Note that as S † L = Z L S L , the same circuit transforms |ψ L 〉 to Z L S † L |ψ L 〉 where Z L is a byproduct operator. The T L gate is implemented with the non-deterministic circuit shown in Fig. 45. Given the input ancilla state |θ L 〉 = |g L 〉 + e iθ |e L 〉, the output |φ L 〉 of this circuit is The first operator is a byproduct operator, whose power p Z is equal to 0(1) if the Z L measurement M Z of the logical qubit state is +1(−1). The second operator is a rotation by the angle θ , For the T L gate, the rotation angle is θ = +π/4(−π/4) depending on the sign +1(−1) of the Z L measurement. Approximately half of the times we run the circuit we will succeed in generating T L |ψ L 〉, heralded by the measurement M Z = +1. However, the other times we run the circuit the measurement M Z = −1 will herald the output state |φ L 〉 = X L R Z (−π/4)|ψ L 〉 = X L T † L |ψ L 〉 .
In this case we fix the output using S L T † L = T L . There is a slight complication if there are byproduct X L or Z L operators; here we assume there are no byproduct operators. The correction to the output is achieved by applying S L to |φ L 〉: where we use the identity S L X L = i X L S † L and drop the unimportant phase factor of i. Hence passing the output state |φ L 〉 through the S L circuit gives the result (X L Z L )T L |ψ L 〉, where X L Z L are byproduct operators that are handled by the control software.
If we need to perform T † L , we use the circuit in Fig. 45, and about half the time we will get the measurement outcome M Z = −1, which indicates the circuit produced X L T † L , which is the desired output (with a byproduct operator X L ). The other half of the time we get the measurement outcome M Z = +1, and we can correct the circuit output T L |ψ L 〉 by applying S L , as S L T L = Z L T † L ; this is done using the S L circuit, with the output including the byproduct operator Z L that is handled by the control software.
Problem 20: Show that the T circuit in Fig. 45 works as advertised.
A L S L T L I L X L Z L Figure 45: Logic circuit for the T L gate. The circuit uses the ancilla state |A L 〉 = (|g L 〉 + e iπ/4 |e L 〉)/ 2, which is used to control a CNOT on the target state |ψ L 〉, transforming the ancilla to the output state |φ L 〉. The CNOT target state is measured along Z L , resulting in a projective, probabilistic outcome. Following this, the CNOT control qubit is processed by a conditional S L gate: If the Z L measurement M Z yields a +1 outcome, the output state is the desired one, |φ L 〉 = T L |ψ L 〉, and the S L gate is not applied. If however the measurement yields a −1 outcome, the output is |φ L 〉 = X L T † L and the S L gate is applied, resulting in the output X L Z L T L |ψ L 〉. Note the double lines represent the classical measurement data with a probabilistic outcome, with these data controlling the S L gate. The bracketed operators in the output correspond to M Z = +1 (I L ) and M Z = −1 (X L Z L ). We leave some important topics for the interested reader to learn about in the literature, including the methods in the surface code for generating high-precision |A L 〉 and |Y L 〉 states using magic distillation circuits, to the level of precision as needed for whatever problem is at hand.

Conclusion
We have now covered all the basic aspects of the surface code approach to quantum computing. We have described all the gates that are required to implement e.g. Shor's algorithm or Grover's search algorithm. The discussion has been mostly theoretical, while the motivation for developing the surface code is of course to find a realistic and practical physical implementation for a quantum computer. There are a number of physical systems in which this scheme could in principle be implemented, ranging from cold atoms [60,61] and ions [62][63][64], to semiconductor-based approaches [65], to superconducting integrated circuits [66][67][68][69][70][71]. Each of these systems has certain advantages and certain disadvantages. For any system to be a candidate for a surface code implementation, it must of course meet the requirements for singlequbit and two-qubit gate and measurement fidelities, which is not true for any system to date, although a number of systems are close to these requirements. The surface code clearly also requires a very large number of physical qubits (of order 10 8 is probably the smallest number needed for a practical factoring computer), so a separate requirement is the ability to assemble and integrate a large number of nominally identical qubits. Furthermore, the operation and error detection of the surface code assumes classical logic support, with the classical logic operating significantly faster than the qubits, in order that state preparation, qubit interactions, and error tracking can be maintained with a high level of fidelity.
An outstanding challenge for experimentalists, at this point, is to show the ability to assemble large (of order 100) numbers of high-fidelity qubits, and then to demonstrate that one or two cycles of error detection of the type used in the surface code can actually be implemented in a way that improves the effective qubit lifetimes. If this can be achieved, then one can begin to consider scaling up; it may be possible, using of order 1000 qubits, to demonstrate significant extensions of the lifetime of the logical qubits defined in this array compared to the lifetime of the underlying physical qubits. The lifetime of the logical qubits can then hopefully be demonstrated to scale as predicted for the distance d of the surface code.
This would then set the stage for expanding to larger arrays, demonstrating the hoped-for exponential improvement in qubit lifetimes with the size of the logical qubits. One can then begin braiding operations, and demonstrate the implementation of a logical CNOT with the improved lifetimes due to the surface code. These goals can be met, assuming fidelities can be maintained, with of order a few thousand qubits.
Scaling up then to a full quantum computer would be mostly an engineering challenge, of how to make and then operate larger numbers (millions) of qubits. While clearly an incredibly daunting challenge, successfully meeting this challenge would make a fundamental demonstration of a complex engineered quantum system, and might usher in an era of a new computing paradigm.