CSS code surgery as a universal construction

We define code maps between Calderbank-Shor-Steane (CSS) codes using maps between chain complexes, and describe code surgery between such codes using a specific colimit in the category of chain complexes. As well as describing a surgery operation, this gives a general recipe for new codes. As an application we describe how to `merge' and `split' along a shared $\overline{X}$ or $\overline{Z}$ operator between arbitrary CSS codes in a fault-tolerant manner, so long as certain technical conditions concerning gauge fixing and code distance are satisfied. We prove that such merges and splits on LDPC codes yield codes which are themselves LDPC.


Introduction
Quantum computers have become larger and more sophisticated in recent years [10,35], but faulttolerance is necessary to perform practically relevant quantum algorithms. Qubit stabiliser error-correction codes are a well-studied approach to fault-tolerant quantum computing [20] and are favourable both for their practicality and theoretical simplicity. Such codes store logical data using entangled states of physical qubits and repeated many-body measurements, and so long as the physical errors on the qubits stay below a certain threshold the logical data is protected.
The most well-known example of a qubit stabiliser code is the toric code, in which qubits are embedded on the surface of a torus, and properties of the logical space are determined by the topology of the surface [15,28]. This is a basic example of a qubit Calderbank-Shor-Steane (CSS) code; there are several equivalent ways of defining CSS codes, but for our purposes we shall describe them as codes which are all homological in a suitable sense [3].
This means that we can study CSS codes using the tools of homological algebra [38]. This approach has recently seen much success, for example in the construction of so-called good low-density parity check (LDPC) code families using a balanced product of chain complexes [33]. Such code families have an encoding rate k/n of logical to physical qubits which is constant in the code size, while maintaining a linear code distance d, a substantial asymptotic improvement over simpler examples such as the toric code. The main caveat is, informally, that the connectivity between physical qubits is non-local. This complicates the architecture of the system, and also complicates the protocols for performing logical gates.
There have been several recent works on protocols for logical gates in CSS codes [29,9,6,34,24], of varying generality. Here, we build on this work by defining surgery, in the abstract, using arbitrary 2. Constructing new codes.

Designing fault-tolerant logical operations.
We intend to expound on code maps in future work, but presently we focus on items 2 and 3.
We define CSS code merges as a colimit -specifically, a coequaliser/pushout -in the category of chain complexes. Not only does the construction describe a surgery operation, but it also gives a general recipe for new codes. An application of our treatment is the description of certain classes of code surgery whereby the codes are merged or split along a Z or X operator, closely related to the notion of 'welding' in [32]. We prove that merging two LDPC codes in such a manner still yields an LDPC code. We give a series of examples, including the specific case of lattice surgery between surface codes. Lastly, we discuss how to apply such protocols in practice, and prove that when a technical condition related to gauge fixing is satisfied then code surgery can be performed fault-tolerantly, allowing us to perform logical parity measurements on codes.
1.1 Guide to reading the paper Section 2 gives a bird's eye view of category theory and universal constructions, which will be useful later on. Section 3 describes the category of chain complexes with morphisms as matrices over F 2 . Category theorists may wish to skip past these sections.
We then give a rundown of CSS codes viewed as chain complexes in Section 4. Readers familiar with basic category theory and this perspective of CSS codes can safely skip to Section 4.3, where we introduce the notion of code maps, that is coherent transforms between codes.
We introduce surgery of codes as a colimit in Section 5. This is when the notion of 'gluing' codes together comes in, and we prove several results about these codes when the colimit uses logical Z or X-operators. Lastly, we introduce a protocol for performing logical Z ⊗ Z and X ⊗ X measurements fault-tolerantly in Section 6.

Universal constructions
In this section we provide a cartoon introduction to category theory and universal constructions. We avoid any weasel phrases like "in some sense", or even any further scare quotes. However, when we use actual precise language, ie. jargon words, we emphasise these with italic font.
A category is a collection of objects and morphisms. We will begin by drawing an object as a box with a decoration, such as .
Morphisms are arrows between objects, like this .
The arrow notation suggests that we can compose these.
. The product of two objects in a category is an object, together with two arrows, .
The product decoration combines the two decorations, . The product also must satisfy a universal property. This states that any other object that also combines the two decorations is already compatible with the product object in a unique way. In other words, for all test objects there exists a unique comparison morphism: .

Please define \titlerunning
The real product is the minimal object that projects down to the factors. Any other test object lives over the real product.
This universal property has the immediate consequence that any other object that satisfies all these requirements, will be isomorphic via a unique isomorphism that commutes with the other morphisms, .
A pullback is a product with constraints: . The resulting square should commute: if we compose any two paths of arrows with the same source object and the same target object then these paths should be equal.
. As with products, we also require the pullback to satisfy a universal property.
. All of these statements have dual statements, which we get by reversing all the arrows. When we do this we sometimes put a coprefix on the terminology. For example, a coproduct, which would normally be called a sum, looks like this .
Once again, we require any such candidate coproduct to satisfy a universal property .
We think of a coproduct as a way of gluing together objects. By adding constraints we can express where we wish to glue The answer to this question is called a pushout: it is an object together with two morphisms, that satisfies a universal property, We have purposefully avoided describing the decorations in these diagrams: how they work, what they mean. A more sensible introduction to category theory would describe these systematically, possibly mentioning the category of finite sets and functions. This is a story about how the objects are sets, with elements, and we can combine these in various ways to make other sets. Instead of telling this story, we skip to the punchline, which is that there are no elements, or rather, what you think of as an element of an object is really a morphism into that object: To push this idea home, and also move toward the goals of this paper, we consider the category Mat F 2 of finite dimensional matrices over the field F 2 . This has as objects the natural numbers and matrices over F 2 as morphisms. Composition of morphisms is matrix multiplication. We will show each object as a box with dots. For example, here is a composition of two morphisms in this category . The objects have very little going on inside them, serving only as anchors for the morphisms (matrices) where all the action is taking place. The vector elements of a vector space have vanished into the morphisms: A coproduct (sum) in this category is an object together with two morphisms satisfying the universal property of coproducts. Here is one candidate: . This coproduct will not be unique (except for some degenerate cases), but the universal property of the coproduct guarantees it is unique up to unique isomorphism. We have reinvented the direct sum of vector spaces.
For a pushout of vector spaces . we get . This is gluing of a two dimensional vector space and a three dimensional vector space along a one dimensional vector space.
But what about products? A curious thing happens in the category Mat F 2 ; we can get the dual universal construction by transposing matrices. For example, the above coproduct becomes the product: and similarly with pullbacks. The transpose duality of Mat F 2 will follow us throughout the rest of this paper.
Here we have been taking the objects of Mat F 2 to be just natural numbers. In the rest of the paper we will use a slightly different definition for the objects: each natural number n is replaced by a basis set of size n for an n-dimensional vector space.

Chain complexes
We now recap some elementary homological algebra. All of this section is known, but we fix notation and look explicitly at the particular category of interest.
Let Mat F 2 be the category which has as objects based finite-dimensional vector spaces over F 2 , so each vector space V has a specified basisṼ , and we have V ∼ = F |Ṽ |

. A morphism
Each V has a dual space V * . As V ∼ = V * , we may fix the duals such that V * = V , andṼ * =Ṽ . This has the benefit of forcing the dual of any matrix f : V → W , which is given by f * : W * → V * , to strictly be the transpose f : W → V .
Much of the following mathematics will work given any rigid Abelian category A as input, but we only need Mat F 2 for our purposes in Section 4.
Let Ch(Mat F 2 ) be the category of bounded chain complexes in Mat F 2 . We now recap some of the basic properties of this category. A chain complex C • looks like this: where each component C i is a based vector space and n ∈ Z is called the degree of the component in C • . C • has F 2 -matrices as differentials ∂ n : C n+1 → C n such that ∂ n • ∂ n+1 = 0 (mod 2), ∀n ∈ Z. To disambiguate differentials between chain complexes we will use ∂ C • n := ∂ n ∈ C • when necessary. All our chain complexes are bounded, meaning there is some k ∈ Z such that C n>k = 0 and l ∈ Z such that C n<l = 0, i.e. it is bounded above and below. We call k − l the length of C • for k and l the smallest and largest possible values respectively.
and call Z n , B n the n-cycles and n-boundaries. We also define a quotient H n (C • ) = Z n (C • )/B n (C • ), and call H n the nth homology space of C • .
Recall that dim(ker(∂ n−1 )) = null(∂ n−1 ) = dimC n − rank(∂ n−1 ). Note that throughout we sometimes use ker( f ) of a matrix f to mean the kernel object, i.e. subspace, and sometimes the kernel morphism, i.e. inclusion map. It should be clear from context which is meant.
Example 3.2. Let Γ be a finite simple undirected graph. We can form the incidence chain complex C • of . All other components are zero. The sole nonzero differential ∂ −1 is the incidence matrix of Γ, with (∂ −1 ) i j = 1 if the jth edge is attached to the ith vertex, and 0 otherwise.
has a basis set of vertices with no attached edges, and H 0 (C • ) is determined by the graph homology of Γ [38].
is called a chain map, and consists of a collection of matrices { f i : C i → D i } i∈Z such that each resultant square of maps commutes: As we specified bounded chain complexes only a finite number of the f i matrices will be non-zero. A chain map f • is an isomorphism in Ch(Mat F 2 ) iff all f i are invertible, in which case one can think of the isomorphism as being a 'change of basis' for all components, which thus transforms the differential matrices appropriately. Observe also that every pair of chain complexes has at least two chain maps, the zero chain maps, between them, given by a collection of entirely zero matrices either way. Proof. It is easy to check that f n induces matrices from Z n (C • ) → Z n (D • ) and the same for B n . This lemma is equivalent to saying that H n (−) is a functor from Ch(Mat F 2 ) → Mat F 2 . Ch(Mat F 2 ) has several known categorical properties which will be useful to us. One way to see a chain complex C • in Mat F 2 is as a Z-graded F 2 -vector space, with specified bases and a distinguished map ∂ : C • → C • with components ∂ i : C i+1 → C i , such that ∂ •∂ = 0. Many of the properties of Ch(Mat F 2 ) are inherited directly from those of Z-graded F 2 -vector spaces.
Lemma 3.5. Ch(Mat F 2 ) is an additive category, i.e. it has all finite biproducts.
Proof. Adding two chain maps obviously gives a chain map. Define the biproduct of C • and D • with shorthand (C ⊕ D) • of chain complexes C • , D • . It has components (C ⊕ D) n = C n ⊕ D n and the same for differentials. This is both a categorical product and coproduct. Lastly, the zero object is i.e. all 0 i are 0.
Lemma 3.6. Homology preserves direct sums (coproducts): given chain complexes C • and D • , This is obvious, considering the blocks of each differential in (C ⊕ D) • .
Definition 3.7. Let the dual chain complex C * • have components . Our choice of duals means that C * * • = C • on the nose. Lemma 3.8. For a chain complex C • , The dual of a chain map f is straightforward: it has matrices ( f Remark 3.9. These are categorical duals with respect to a tensor product of chain complexes. As we only need this tensor product for a very specific construction in Section 6 we relegate it to Appendix B.

Quantum codes
Here we introduce classes of both classical and quantum codes as chain complexes. We give easy examples such as the surface and toric codes. Up until Section 4.3, this part is also well-known, although we describe the relationship between Z and X operators in greater detail than we have found elsewhere.

Codes as chain complexes
Binary linear classical codes which encode k bits using n bits can be described by a k × n encoding F 2matrix G and a m × n parity check F 2 -matrix P. One can use G to take any length k bitstring b, viewed as a column vector, and obtain c = Gb, the codeword for b. The parity check matrix P, when applied to any codeword, gives Pc = 0; if the result is non-zero then an error has been detected, and under certain assumptions can be corrected. The distance d of a binary linear classical code is the minimum Hamming weight of its nonzero codewords, and one characterisation of codes is by their metrics [n, k, d]. We have that rank(P) = n − k, and the checks are independent when P is full rank, but we do not assume this in general. Given P, G is uniquely defined up to isomorphism as the matrix ker(P), so we use only P to define the code. We may trivially view a binary linear classical code as a length 1 chain complex in Ch(Mat F 2 ), with indices chosen for convenience: . d is then the minimum Hamming weight of vectors in Z 0 (C • ), which is the codespace. Note also that we can see H −1 (C • ) = C −1 /B −1 (C • ) as measuring the 'redundancy' of the parity checks. From Example 3.2 we know that each graph defines length 1 chain complex, and so every graph evidently specifies a classical code, with edges as physical bits and vertices parity checks. An easy case is for cycle graphs, whereby we have a repetition code with one redundant parity check, i.e. vertex. In general, the cycle graph C n with n vertices gives an [n, 1, n] repetition code.
We now move on to quantum codes. Qubit Calderbank-Shor-Steane (CSS) codes are a type of stabiliser quantum code. Let P n = P ⊗ n be the Pauli group over n qubits. Stabiliser codes start by specifying an Abelian subgroup S ⊂ P n , called a stabiliser subgroup, such that the codespace H is the mutual +1 eigenspace of all operators in S . That is, We then specify a generating set of S , of size m. For CSS codes, this generating set has as elements tensor product strings of either {I, X} or {I, Z} Pauli terms, with no scalars other than 1. One can define two parity check F 2 -matrices P X , P Z , for the Xs and Zs, which together define a particular code. Each column in P X and P Z represents a physical qubit, and each row a measurement/stabiliser generator. P X and P Z thus map Z and X operators on physical qubits respectively to sets of measurement outcomes, with a 1 outcome if the operators anticommute with a given stabiliser generator, and 0 otherwise; these outcomes are also called syndromes. P X is a m X × n matrix, and P Z is m Z × n, with m X , m Z marking the division of the generating set into Xs and Zs respectively, satisfying m = m X + m Z . We do not require the generating set to be minimal, and hence P X and P Z need not be full rank.
Definition 4.3. We say that w Z is the maximal weight of all Z-type generators and w X the same for the X-type generators. These are the highest weight rows of P Z and P X respectively. Similarly, we say that q Z , q X is the maximal number of Z, X generators sharing a single qubit. These are the highest weight columns of P Z and P X .
CSS codes are characterised by n, k, d , with k the number of encoded qubits and d the code distance, which we define presently.
That the stabilisers must commute is equivalent to the requirement that P X P Z = P Z P X = 0. We may therefore view these matrices as differentials in a length 2 chain complex: where ∂ 0 = P Z and ∂ −1 = P X , or the other way round (∂ 0 = P X , ∂ −1 = P Z ) if desired, but we start off with the former for consistency with the literature. The quantum code then has C 0 ∼ = F n 2 . We will typically fix C 0 = F n 2 for convenience. The code also has k = dim H 0 (C • ). To see this, observe first that C 0 represents the space of Z Paulis on the set of physical qubits, with a vector being a Pauli string e.g. v = 1 0 1 ; Z ⊗ I ⊗ Z. Each vector in H 0 (C • ) can be interpreted as an equivalence class [v] of Z operators on the set of physical qubits, modulo Z operators which arise as Z stabilisers. That this vector is in Z 0 (C • ) means that the Z operators commute with all X stabilisers, and when the vector is not in [0] = B 0 (C • ) it means that the Z operators act nontrivially on the logical space. A basis of H 0 (C • ) constitutes a choice of individual logical Paulis Z, that is a tensor product decomposition of the space of logical Z operators, and we set Z 1 = Z ⊗ I · · · ⊗ I on logical qubits, Z 2 = I ⊗ Z · · · ⊗ I etc. There is a logical qubit for every logical Z, hence k = dim H 0 (C • ).
To get the logical X operators, consider the dual C * • . The vectors in H 0 (C * • ) then correspond to X operators in the same manner. As a consequence of Lemma 3.8 there must be an X operator for every Z operator and vice versa.
Proof. First, recall that we have the nondegenerate bilinear form which is equivalent to · : C 0 × (C * ) 0 → F 2 ; computationally, this tells us whether a Z operator commutes or anticommutes with an X operator. Now, let u ∈ Z 0 (C • ) be a (possibly trivial) logical Z operator, and v ∈ B 0 (C * • ) be a product of X stabilisers. Then P X u = 0, and v = P X w for some w ∈ C −1 . Thus u · v = u v = u P X w = (P X u) w = 0, and so products of X stabilisers commute with logical Z operators. The same applies for Z stabilisers and logical X operators.
As a consequence, v · w = (v + s) · (w +t) for any The duality pairing of C 0 , (C * ) 0 thus lifts to H 0 (C • ), H 0 (C * • ), and a choice of basis The above lemma ensures that picking a tensor product decomposition of logical Z operators also entails the same tensor product decomposition of logical X operators, so that X i Z j = (−1) δ i, j Z j X i , for operators on the ith and jth logical qubits. Let where | · | is the Hamming weight of a vector, then the code distance d = min(d 1 , d 2 ). d 1 and d 2 are called the systolic and co-systolic distances, and represent the lowest weight nontrivial Z and X operators on the logical space respectively. As all the data required for a CSS code is contained within the chain complex C • -and potentially a choice of basis of H 0 (C • ) -then we could define a CSS code as just the single chain complex, but it will be convenient to have direct access to the dual complex as well.
Definition 4.5. A CSS code is a pair (C • ,C * • ) of a length 2 chain complex centred at degree 0 and its dual: A based CSS code additionally has a choice of basis for H 0 (C • ), and hence for H 0 (C * • ). We call the first of the pair the Z-type complex, as vectors in C 0 correspond to Z-operators, and the second the X-type complex.
Remark 4.6. As C * * • = C • , we see that given any CSS code (C • ,C * • ) we can exchange Z and X stabilisers (and operators) to obtain (C * • ,C • ). Employing the direct sum (C ⊕D) • of chain complexes we have the CSS code ((C ⊕D) • , (C * ⊕D * ) • ), which means the CSS codes (C • ,C * • ) and (D • , D * • ) perform in parallel on disjoint sets of qubits, without any interaction. The Z and X operators will then be the tensor product of operators in each.
In summary, there is a bijection between length 1 chain complexes in Ch(Mat F 2 ) and binary linear classical codes, and between length 2 chain complexes in Ch(Mat F 2 ) and CSS codes. That this is a bijection is because we have been quite careful: under our definitions, two classical codes with different matrices but which are the same up to, say, Gaussian elimination are counted as being different classical codes. Morally, these could be considered equivalent codes for some purposes, as they have the same codespaces, code distances and number of physical bits. A similar qualification applies to quantum CSS codes. Additionally, there is no guarantee that the chain complexes will give useful codes. For example, a length 2 chain complex could be an exact sequence and thus have a homology space of zero at all components, in which case there are no logical qubits in the code; even if there is nontrivial homology, the (co-)systolic distance could be very low and thus not practical as a code.
There are many classical and quantum codes which do not fit into this classification using Ch(Mat F 2 ), such as nonlinear classical codes and stabiliser quantum codes which are not CSS (although any n, k, d stabiliser code can be mapped to a 4n, 2k, 2d CSS code [5]). There are also fault-tolerant topological quantum systems which certainly don't live in Ch(Mat F 2 ), although they have similar homological properties [28,12]. Lastly, there are CSS codes for higher dimensional qudits, but for simplicity we stick to qubits.
Rather than just individual codes we tend to be interested in families of codes, where n, k, d scale with the size of code in the family. Of particular practical interest are quantum low density parity check (LDPC) CSS codes, which are families of codes where all w Z , w X , q Z and q X in the family are bounded from above by a constant. Equivalently, this means the Hamming weight of each column and row in each differential is bounded by a constant.

Basic quantum codes
, with a representative v = 1 1 1 1 1 1 1 1 1 . Similarly H 0 (C • ) * has the nonzero vector w = 1 1 1 1 1 1 1 1 1 , which is a representative of [w] ∈ H 0 (C • ). Hence, we have two logical operators Z = 8 i Z i , X = 8 i X i with Z i on the ith qubit and the same for X i . We equally have, say, Z = Z 1 ⊗ Z 4 ⊗ Z 7 and X = X 1 ⊗ X 2 ⊗ X 3 in the same equivalence classes as those above, [v] and [w].
We now consider two examples which come from square lattices. This can be done much more generally. In Appendix C we formalise categorically the procedure of acquiring chain complexes -and therefore CSS codes -from square lattices, which are a certain type of cell complex. Edges in the lattice are qubits, so n = 18, the 9 X-checks are associated with vertices and the 9 Z-checks are associated with faces, which are indicated by white circles. Grey vertices indicate periodic boundary conditions, so the lattice can be embedded on a torus. This is an instance of the standard toric code [28].
The abstracted categorical homology from before is now the homology of the tessellated torus, with cycles, boundaries etc. having their usual meanings. Letting the code be (C • ,C * • ), we have k = dim H 0 (C • ) = 2, and (co)systolic distances are the lengths of the essential cycles of the torus. This represents a patch of surface code (D • , D * • ), where we have two smooth sides, on the left and right, and two rough sides to the patch, on the top and bottom. Observe that we have 'dangling' edges at the top and bottom, which do not terminate at vertices. We have The systolic distance is 3, the length of the shortest path from the top to bottom boundary, and the cosystolic distance 3, the same but from left to right.

Code maps
One may wish to convert one code into another, making a series of changes to the set of stabiliser generators to be measured, and potentially also to the physical qubits. The motivation behind such protocols is typically to perform logical operations which are not available natively to the code; not only might the target code have other logical operations, but the protocol is itself a map between logical spaces when chosen carefully. An example of a change to the measurements and qubits is code deformation. We do not formalise code deformation here, as that has some specific connotations [37]. Instead we define a related notion, called a code map, which has some overlap. A code map is also related to, but not the same as, the 'homomorphic gadgets' from [24].
Note that the second chain map is strictly speaking obsolete, as all the data is contained in a single chain map f • , but as with chain complexes it will be handy to keep both around.
Let us unpack this definition. F Z first maps Z-operators in C 0 to Z-operators in D 0 , using f 0 . It may map a single Z on a qubit to a tensor product of Zs, or to I. It then has a map f 1 on Z generators, and another f −1 on X generators. Recalling Definition 3.3, we have:

I II
With two commuting squares labelled I and II. I stipulates that applying products of Z stabiliser generators on the code and then performing the code map should be equivalent to performing the code map and then applying products of Z stabiliser generators, i.e.
II stipulates that performing the X measurements and then mapping the code should be equivalent to mapping the code and then performing X measurements, so there is a consistent mapping between all measurement outcomes, i.e.
This has the component f 0 : D 0 → C 0 , which maps an X-operator in D 0 back to an X-operator in C 0 . Similarly for f −1 and f 1 , each of which come with commuting squares which are just the transposed conditions, e.g.
• f 0 , so they say nothing new. This is not surprising, as all the data for f * is given by f already.
We now show that this definition entails some elementary properties. For a start, Lemma 3.4 implies that a code map gives a map from a Z operator in H 0 (C • ) to Zs in H 0 (D • ); this can also map to a tensor product of logical Zs, and in particular map Z to zero i.e. I, but it must not map a Z to an operator which can be detected by the X stabiliser measurements. Hence ( f • , f * • ) preserves the fact that any Z is an undetectable operator on the codespace. A similar requirement holds for X operators, but this time the condition is inverted. Every X in H 0 (D * • ) must have a map only to logical operators in H 0 (C * • ), but the other way is not guaranteed.
Let n C and n D be the number of physical qubits in codes (C • ,C * • ) and (D • , D * • ) respectively. We may interpret F Z as a C-linear map M in FHilb, the category of Hilbert spaces. This C-linear map has the property that MU Z = U Z M, where U Z is a tensor product of Z Paulis on n C qubits and U Z is a tensor product of Z Paulis on n D qubits. In particular, given any U Z we have a specified U Z . The same is not true the other way round, as the map f 0 is not necessarily injective or surjective. Similarly, MU X = U X M. This time, however, given any unique U X on n D qubits we have a specified U X but vice versa is not guaranteed, depending on f 0 .
As a consequence, the linear map M is stabiliser, in the sense that it maps Paulis to Paulis, but not If M is not even an isometry, it cannot be performed deterministically, and the code map must include measurements on physical qubits. There will in general be Kraus operators corresponding to different measurement outcomes which will determine whether the code map has been implemented as desired; for now we assume that M is performed deterministically, and leave this complication for Section 6. Similarly, while the code map can be interpreted as a circuit between two codes, we do not claim that such a circuit can be performed fault-tolerantly in general.
Remark 4.11. For the following proposition, and at various points throughout the rest of the paper, we will use the ZX-calculus, a formal graphical language for reasoning about computation with qubits. We do not give a proper introduction to this calculus for brevity, but Sections 1-3 of [36] are sufficient for the interested reader. Our use of ZX diagrams is unsophisticated, and primarily for convenience. Proposition 4.12. Let F Z be a Z-preserving code map between codes (C • ,C * • ) and (D • , D * • ) with qubit counts n C and n D . The interpretation of F Z as a C-linear map M in FHilb has a presentation as a circuit with gates drawn from {CNOT, |+ , 0|}.
Proof. We start with the linear map M : By employing the partial transpose in the computational basis we convert it into the state i.e. inserting n C Bell pairs. By the definition of f 0 we know that this has an independent stabiliser, with one Z and n C − 1 Is followed by some n D -fold tensor product of Z and I, for each of the n C qubits. From f 0 it also has an independent stabiliser, with some n C -fold tensor product of X and I followed by n D − 1 Is and one X, for each of the n D qubits. |ψ is therefore a stabiliser state. Further, from Theorem 5.1 of [26] it has a presentation as a 'phase-free ZX diagram', of the form where the top n C qubits do not have a green spider. We perform the partial transpose again to convert the state |ψ back into the map M, which has the form Any ZX diagram of this form can be expressed as a matrix over F 2 , mapping X-basis states from (C 2 ) ⊗ n C to (C 2 ) ⊗ n D . The example above, ignoring the ellipses, has the matrix 1 0 1 1 1 1 which is equal to f 0 ; the point of the above rigmarole is thus to say that f 0 is precisely a linear map between X-basis states, which one can check easily. We can then perform Gaussian elimination on f 0 , performing row operations, which produce CNOTs on the r.h.s. of the diagram in the manner of [27], until the matrix is in reduced row echelon form. We then perform column operations producing CNOTs on the l.h.s. of the diagram, until the matrix has at most one 1 in each row and column. This can be performed using the leading coefficients to remove all other 1s in that row. The final matrix just represents a permutation of qubits with some states and effects. An empty column corresponds to a 0| effect, and an empty row a |+ state. We thus end up with a presentation of M in the form  Proof. When n C = 0 we see that M has exactly n D independent stabilisers with 1 X and n D − 1 Is, for each qubit to put X on. The flipped argument applies when n D = 0.
Definition 4.14. An X-preserving code map F X from a CSS code So F X is just mapping in the other direction to F Z from before, and we say that F X is opposite to F Z . In this case, when we interpret F X as a C-linear map L, it has the property that LU X = U X L and that any U X gives a specified U X , and LU Z = U Z L, but that any U Z gives a specified U Z but not vice versa.
By inspecting the stabilisers we see that, for F Z with interpretation M and F X with interpretation L,   While our definitions are for chain complexes of length 2, in principle one can map between any two codes with an arbitrary number of meta-checks, or between a classical code and quantum code, which could be interpreted as 'switching on/off' either X or Z stabiliser measurements.
While code maps are related to code deformations, we are aware of code deformation protocols which do not appear to fit in the model of chain maps described. For example, when moving defects around on the surface code for the purpose of, say, defect braiding [18], neither Z nor X operators are preserved in the sense we give here.

CSS code surgery
To understand code surgery we require some additional chain complex technology, namely colimits. Coproducts, pushouts and coequalisers are directly relevant for our applications. We have already covered coproducts in Lemma 3.5, so we describe pushouts and coequalisers here.
then for degrees n, n + 1 we have with [y] being the equivalence class in Q n having y as a representative, and the same for = 0, and one can additionally check that this is indeed a pushout in Ch(Mat F 2 ) by considering the universal property at each component. Doing some minor diagram chasing one can check that this is indeed a coequaliser in Ch(Mat F 2 ).
Remark 5.3. We can view the pushout The difference is that the pair of chain maps k • , l • have been replaced with the single map coeq • , so we have We can view coequalisers as instances of pushouts as well, doing a sort of reverse of the procedure above.
Remark 5.4. As with all colimits, those above are defined by the category theory only up to isomorphism. Any two chain complexes C • and D for all i ∈ Z, which one can check using rank-nullity. This is a very large isomorphism class, and we require more fine grained control over which chain complexes are chosen by the colimits. Thus we pick out the obvious objects from their isomorphism classes, which are those described in the definitions of the colimits above.
is an Abelian category, and thus is finitely complete and cocomplete, meaning that it has all finite limits and colimits.
While this is well-known, we sketch a proof of this lemma in Appendix A for completeness. There we also describe some additional limits, but we only use colimits in this work.

Generic code surgery
We now give a general set of definitions for surgery between arbitrary compatible CSS codes; the condition for compatibility is very weak here. Working at this level of generality means that we cannot prove very much about the output codes or relevant logical maps. As a consequence, we will then focus on particular surgeries which make use of 'gluing' or 'tearing' along logical Z or X operators in Section 5.3.
Recall from Remark 5.3 that we can view any pushout as a coequaliser. We thus have and we call coeq • the Z-merge chain map. We can bundle this up into a Z-merge code map: We then call coeq * • : Q * • → (C ⊕ D) * • an X-split chain map, and hence we have an X-split code map too: We have an X-merge chain map and thus X-merge code map using the coequaliser picture, so We also have a Z-split chain map and the Z-split code map E Z by taking the opposite.
This is rather abstract, so let's see a small concrete example.
Example 5.8. Consider the following pushout of cell complexes: We have not properly formalised pushouts of square lattices in the main body for brevity, but we do so in Appendix C. Informally, we are just 'gluing along' the graph in the top left corner, where the edges to be glued are coloured in blue. We can consider this pushout to be in Ch(Mat F 2 ) 1 , giving the pushout: One can see from the cell complexes that we have Rather than compute the pushout maps, let us instead give the coequaliser coeq • : We immediately see that coeq 1 = id. For the other two surjections we have Finally we interpret all the chain complexes in this pushout as being the Z-type complexes of CSS codes Thus we have a Z-merge code map F Z , with an interpretation M as a C-linear map, using coeq 0 and coeq 0 . We refrain from writing out the full 32-by-64 matrix, but as a ZX-diagram using gates from {CNOT, |+ , 0|} we have simply We know from Lemma 3.4 that this map must restrict to a map on logical qubits. However, easy calcu- there are no logical qubits -there are still operators which show up as errors and some which don't, but all of those which don't are products of Z or X stabiliser generators. By Corollary 4.13 and Corollary 4.17 the logical map in FHilb is then just |+ . This trivially preserves both Z and X operators, although its opposite code map F X does not preserve Z operators.
This example was very simple, but the idea extends in quite a general way. To give an idea of how general this notion of CSS code surgery is, consider the balanced product codes from [2,33]. The balanced product of codes is by definition a coequaliser in Ch(Mat F 2 ), and so we can convert it into a pushout using routine category theory. The coequaliser is where g • and f • represent left and right actions of A • respectively. Recall that we did not explicitly define the tensor product in the main body for brevity, but see Appendix B. Then to this coequaliser we can associate a pushout, where one can check that the universal property is the same in both cases. Thus we can think of a balanced product as a merge of tensor product codes, with the apex being two adjacent tensor product codes. As the maps in the span are evidently not monic, the merge is of a distinctly different sort from Example 5.8, and also the Zand X-merges we will describe in Section 5.3. It would be convenient if we could guarantee some properties of pushouts in general; for example, if the pushout of LDPC codes was also LDPC, or if the homologies were always preserved. Unfortunately, the definition is general enough that neither of these are true. We discuss this in slightly greater detail in Appendix D, but the gist is that we need to stipulate some additional conditions to guarantee bounds on these quantities.

Surgery along a logical operator
The procedure of merging here is closely related to that of 'welding' in [32]. Our focus is not just on the resultant codes, but the maps on physical and logical data. On codes generated from square lattices, the merges here will correspond to a pushout along a 'string' through the lattice.

Please define \titlerunning
be a length 2 chain complex. Let v ∈ C 0 be a vector such that v ∈ ker(∂ C • −1 )\im(∂ C • 0 ). We now construct the logical operator subcomplex V • . This has: where supp v is the set of basis vectors in the support of v, and ∂ i S is the restriction of a differential to a subset S of its domain. All other components and differentials of V • are zero.
There is a monic f • : V • → C • given by the inclusion maps of V 0 ⊆ C 0 etc.
Definition 5.10. Let V • be a logical operator subcomplex of two chain complexes C simultaneously, so there is some vector v ∈ C 0 and w ∈ D 0 such Then there is a monic span This monic span has the pushout The construction here is inspired by [9].
We are particularly interested in based CSS codes, i.e. when the logical spaces have bases.
Then we say that V • is a separated logical operator subcomplex. We also say that the corresponding Z-merged code of (C • ,C * • ) and The intuition here, following [9], is that it is convenient when the logical operators we glue along do not themselves contain any nontrivial logical operators belonging to a different logical qubit; if they do, the gluing procedure may yield a more complicated output code, as we could be merging along multiple logical operators simultaneously. In Appendix E we demonstrate that it is possible for this condition to not be satisfied, using a patch of octagonal surface code. We now prove some basic results.
Lemma 5.13. Let (Q • , Q * • ) be a separated Z-merged code with metrics n Q , k Q , d Q , and let n C , k C , d C , n D , k D , d D be the metrics of (C • ,C * • ) and (D • , D * • ) respectively. Let n V = dimV 0 be the Hamming weight of u and v. Then Further Proof. n Q is immediate by the definition. For the others, we start off by considering the code (C For the distance, we have d (C⊕D) = min(d C , d D ). Combining along a Z operator cannot decrease the distance. First, every vector v ∈ Z 0 (Q • )\B 0 (Q • ) has at least one corresponding vector v in Z 0 ((C ⊕ D) • )\B 0 ((C ⊕ D) • ) such that |v | = |v|. Now, we use the fact that any vector in B 0 ((C ⊕ D) * • ) is sent to a vector in B 0 (Q • ) by the quotient. For any vector a ∈ Z 0 (Q * • )\B 0 (Q * • ), i.e. a nontrivial X-operator, we can construct a vector a ∈ Z 0 ((C ⊕ D) * • )\B 0 ((C ⊕ D) * • ) by discarding all the 1s in a which are not in one of C * 0 or D * 0 , and so |a | ≤ |a|. However, there are cases in which there is no a such that |a | = |a|, which one can show with easy surface code merges, so the distance can be increased by the merge.
We would like to study not only the resultant code given some Z-merge, but also the map on the logical space. We will now switch from pushouts to coequalisers. Recall the Z-merge code map F Z from Equation 1. We call this a Z-merge code map when the merge is along a Z-operator as above, and from now on we assume that all merges are separated.
Lemma 5.14. Let the Z-merge code map These equivalences are from different quotients: y ∼ u and v ∼ x are homology quotients, while u ∼ v is a quotient from the pushout. Proof. M must have the following maps on Paulis on each pair of qubits being merged: which uniquely defines the matrix above. In other words we have |00 → |0 , |11 → |1 , |01 → 0, |10 → 0 etc, which has the convenient presentation as the ZX diagram on the left above.
This is obvious by considering the surjection in question and using Lemma 5.13. It essentially says that on each pair of logical operators in ((C ⊕ D) • , (C ⊕ D) * • ) which are both being quotiented, F Z acts as: where the map on Xs is inferred from the dual. We now show that, if we consider two codes to be merged as instances of LDPC families, their combined Z-merged code code is also LDPC. Recall Definition 4.3.
Similarly, letting the input codes have maximal number of shared generators on a single qubit q Z C , q X C and q Z D , q X D we have Proof. None of the Z-type generators are quotiented by a Z-merge map, so w Z Q = w Z (C⊕D) = max(w Z C , w Z D ). For the X-type generators, in the worst case the two generators which are made to be equivalent by the merge are the highest weight ones. For these generators to appear in V −1 they must have at least two qubits in each of their support which is in V 0 , and thus these qubits are merged together, so w X Q < w X C +w X D . Next, using again the fact that none of the Z-type generators are quotiented, a single qubit could in the worst case be the result of merging two qubits in (C • ,C * • ) and (D • , D * • ) which each have the maximal number of shared Z-type generators, so q Z Q ≤ q Z C + q Z D . For the X case, if a qubit is in V 0 then all X-type generators it is in the support of must appear in V −1 . Therefore, when any two qubits are merged all of their X-type generators are also merged. Thus q Z Q = q Z (C⊕D) = max(q X C , q X D ).
Note that as w Z , w X and q Z , q X are at worst additive in those of the input codes, the Z-merge of two LDPC codes is still LDPC, assuming the pushout is still well-defined using matching Z operators for each member of the code families; moreover by Lemma 5.13 the Z-merge of two good LDPC codes is still good. Next, we dualise everything, and talk about X-merges.
be a CSS code such that V * • is a logical operator subcomplex of C * • and D * • , and Q * • is the merged complex along V * • . Then the CSS code . In this case we glue along an X logical operator instead. The notion of separation, Lemma 5.13 and Lemma 5.16 carry over by transposing appropriately.
An X-merge map E X can be defined similarly, and a similar result as Lemma 5.14 applies to separated X-merged codes. Proof. This time, L must have the maps Similarly, the maps on logical operators are X ⊗ I → X; I ⊗ X → X; Z ⊗ Z → Z Having discussed Zand X-merged codes, we briefly mention splits. These are just the opposite code maps to F Z and E X . In both cases, all the mappings are determined entirely by Lemma 5.13 by taking transposes or adjoints when appropriate.
Remark 5.19. In practice, when the CSS codes in question hold multiple logical qubits it may be preferable to merge/split along multiple disjoint Z or X operators at the same time. Such a protocol is entirely viable within our framework, and requires only minor tweaks to the above results. The same is true should one wish to merge/split along operators within the same code.
We now look at a short series of examples.

Examples of surgery 5.4.1 Lattice surgery
Lattice surgery is the prototypical instance of CSS code surgery. It starts with patches of surface code and then employs separated splits and merges to perform non-unitary logical operations [23]. Here we discuss surgery on qubit lattices, although the protocol has recently been generalised significantly to qudits and arbitrary Kitaev models [11,13]. The presentation we give of lattice surgery is quite idiosyncratic, in the sense that we perform the merges on physical edges/qubits, whereas the standard method is to introduce additional edges between patches to join them together. We remedy this in Section 6.
Consider the pushout of cell complexes below: As before, we informally consider this to be 'gluing along' the graph in the top left, but for completeness it is formalised in Appendix C. By considering the pushout to be in Ch(Mat F 2 ), we have: Letting coeq • : (C ⊕ D) • → Q • be the relevant coequaliser map, we see that F Z = (coeq • , coeq * • ) constitutes a separated Z-merge map. In particular, observe that F Z sends the logical operators: as predicted by Lemma 5.15.
The first two give H 0 (coeq • ) = 1 1 and the last H 0 (coeq * • ) = 1 1 . F Z is evidently Z-preserving but not X-preserving, as X ⊗ I is taken to an operation which is detected by the Z stabilisers. Observe that we end up with a greater cosystolic distance of (Q • , Q * • ) than we started with in ((C ⊕ D) • , (C ⊕ D) * • ). If we instead consider the pair (coeq • , coeq * • ) as an X-preserving code map F X , then it is a separated X-split map. In terms of cell complexes we would have → We similarly have a separated X-merge map, by considering the pushout Then we also have a separated Z-split map with the obvious form.
Remark 5.20. While it is convenient to choose logical operators along patch boundaries to glue along, so that the complexes can all be embedded on the 2D plane, this is not necessary. One could intersect two patches along any matching operator.
Remark 5.21. We do not expound on this example, but the protocols for performing X and Z measurements by generalised lattice surgery in [9] can be seen as using separated Z-and X-merged codes, with the caveat that they don't perform the merge maps; instead they initialise fresh qubits in the ancillary hypergraph code and measure all stabiliser generators. The present work has overlap with their protocols, but we do not subsume them; for example their X ⊗ Z and Y measurement methods are outside of our formalism, as they lead to non-CSS codes.
Recall the toric code (C • ,C * • ) from Example 4.8. We can merge two copies of C • along a logical Z operator, which corresponds to an essential cycle of each torus. The resultant code will then look like two tori intersecting, depending somewhat on the choices of essential cycle: The Z-merge map on logical qubits will be the same as for patches.

Shor code surgery
Of course, the pushout we take does not have to come from square lattices. Let C • and D • be two copies of Shor codes from Example 4.7. 3 We can perform separated merges between them. We give two examples. First, for a Z-merge, we take the logical Z operator Z = 8 i Z i and apply Definition 5.9 to get the logical operator subcomplex: , and all other components zero. This is just C • from Example 4.7 truncated to be length 1, as this logical Z operator has support on all physical qubits. The monic chain map f • given by inclusion into the Shor code is just and the same for g • . The pushout of The Shor code can be constructed as a cellulation of the projective plane, so it is actually not wholly dissimilar from the lattice codes [19].
where ∂ Q • −1 = P X and ∂ Q • 0 = P Z |P Z . The map on logical data is fully determined by Lemma 5.15. We have ended up with virtually the same code as the Shor code, except that we have a duplicate for every Z-type generator, i.e. every measurement of Z stabilisers is performed twice and the result noted separately. While this example is very simple, it highlights that the result of a merge can have somewhat subtle features, such as duplicating measurements, which the two input codes do not. 4 For our second case, we use a different (but equivalent) logical operator, Z = Z 1 ⊗ Z 4 ⊗ Z 7 . We still glue two copies of the Shor code, but now we have V 0 = F 3 2 , V −1 = F 2 2 and ∂ V • −1 = 1 1 0 1 0 1 . That is, our logical operator subcomplex is just the repetition code from Example 4.1. We then have and the same for g 0 , forming again a monic span of chain complexes. The resultant Z-merged code is then

Fault-tolerant logical operations
We now describe how our abstract formalism leads to a general set of fault-tolerant logical operations for CSS codes. We consider this to be a good application of the homological algebraic formalism, as we suspect these logical operations would be challenging to derive without the machinery of Ch(Mat F 2 ). 5 So far in our description of code maps there are two main assumptions baked in: that one can perform linear maps between CSS codes (a) deterministically and (b) fault-tolerantly, both of which are desired for performing quantum computation.
For assumption (a), we can only implement code maps which are interpreted as an isometry deterministically. If they are not, instead we must perform measurements on physical qubits. Recall from Proposition 4.12 that every code map has an interpretation constructed from CNOTs and some additional states and effects taken from {|+ , 0|} for a Z-preserving code map or { +| , |0 } for an X-preserving code map. This means that in order to implement the code map non-deterministically, one need only apply CNOTs and measure some qubits in the Z-basis (for a Z-preserving code map) or the X-basis (Xpreserving code map). Of course, should we acquire the undesired measurement result, we induce errors in our code map. There is no protocol for correcting these errors in all generality. For assumption (b), there is no protocol for performing arbitrary CNOT circuits on physical qubits in a code fault-tolerantly. However, when performing CSS code surgery which is a separated Zor X-merge, we have a protocol which addresses both (a) and (b). For this we need an additional technical condition involving gauge fixing. For reasons of brevity we do not describe the connection between lattice surgery and gauge fixing, but refer the interested reader to [37]. In summary, we will consider the whole system to be a subsystem code, and fix the gauges of the Z operators we are gluing along. We will also make use of the tensor product of chain complexes, for which see Appendix B.
Definition 6.1. Let C • be a chain complex and u be a representative of the equivalence class [u] ∈ H 0 (C • ), which is a basis vector for H 0 (C • ). Let x be a vector in C 0 such that |x| = 1 and x · u = 1. We say that x is a qubit in the support of u. Recall from Lemma 4.4 that u has a unique paired basis vector It is possible to safely correct a qubit x when there is a vector v ∈ [v] such that x · v = 1 and y · v = 0 for all other qubits y in the support of u. We say that u is gauge fixable when it is possible to safely correct all qubits in the support of u. The same definition applies if we exchange X and Z appropriately. We now use this notion of gauge fixing to describe fault-tolerant CSS code surgery, generalising lattice surgery.
be length 1 chain complexes. Then we can make the tensor product chain complex W • = (P ⊗V ) • , see Appendix B. Explicitly, In the case where V • is a string along a patch of surface code, say of the form: Proof. Observe that ∂ V • −1 has maximum row weight w X C and column weight q X C . Then recall Definition 4.3 and inspect the matrices ∂ W • 0 and ∂ W • −1 . For dim H 0 (W • ), we use the Künneth formula [38] (or see Lemma B.3), which in this case says H 0 ((P ⊗V ) where the last comes from the fact that V −1 = im(∂ V • −1 ), using Definition 5.9. Note dim H 0 (V • ) ≥ 1 as there is always at least one nonzero vector which is mapped to zero by construction, and B 0 (V • ) = 0. dim H 0 (V • ) may be greater than 1, however, as there may be other nonzero vectors in Z 0 (V • ) which previously corresponded to products of Z stabilisers in C • and D • before taking the logical operator subcomplex.
where the middle term is W • = (P ⊗V ) • from Definition 6.3 above, and the two inclusion maps V • → W • map V 0 into each of the copies of V 0 in W 0 , and the same for V −1 .
Colloquially, we are gluing first one side of the code W • to C • , and then the other side to D • . 6 Lemma 6.6. The 'sandwiched code' (T • , T * • ) has n T = n C + n D + r; and Proof. For n T , just apply Lemma 5.13 twice. For k T , use Lemma 6.4 and observe that all elements of H 0 (W • ) are merged with stabilisers in B 0 (C • ) or B 0 (D • ), and thus do not contribute to k T , apart from the element corresponding to a logical operator in H 0 (C • ) and H 0 (D • ). Bearing this in mind one can also apply Lemma 5.13 twice. For d T , observe that all Z logical operators in W • have weight at least n V ≥ min(d C , d D ), and the only X logical operators in W • become part of logical operators in C • and D • , so the minimum distance cannot be lowered.
For w X T , the pushouts will glue each X type stabiliser generator in W • into those in C • and D • in such a way that they will have exactly one extra qubit in the support, by the product construction of W • ; we can see this from ∂ W • −1 in Definition 6.3, as there is exactly a single 1 which is not part of the ∂ V • −1 in any given row of the matrix.
For w Z T , q Z T and q X T we just use Lemma 6.4 and apply Lemma 5.16 twice.
The intuition here is that rather than gluing two codes (C • ,C * • ) and (D • , D * • ) together directly along a logical operator, we have made a small distance 1 hypergraph code (W • ,W * • ) and used that to sandwich the codes. A consequence of the above lemma is that this 'sandwiching' procedure maps LDPC codes to LDPC codes. Importantly, the two pushouts let us perform a code map on logical qubits fault-tolerantly. Proposition 6.7. Let (C • ,C * • ) and (D • , D * • ) be CSS codes which share a separated gauge-fixable Z operator on m physical qubits and r X-type stabiliser generators each; let the relevant logical qubits be i and j, and let V • be the logical operator subcomplex of C • and D • such that the codes admit a separated Z-merge. Further, let d be the code distance of ((C ⊕ D) • , (C ⊕ D) * • ). Then there is a fault-tolerant procedure with distance d for implementing a Z ⊗ Z measurement on the pair i, j of logical qubits, which gives the 'sandwiched code' (T • , T * • ). This procedure requires r auxiliary clean qubits and an additional m Z-type stabiliser generators.
Proof. We aim to go from the code The code map we apply to physical qubits is as follows. We call the physical qubits in the support of the logical operators to be glued together the participating qubits. We initialise a fresh qubit in the |+ state for each pairing of X-measurements on the two logical operators of qubits i and j, that is for each qubit in (W • ,W * • ) which is not glued to a qubit in (C • ,C * • ) or (D • , D * • ). We now modify the stabilisers to get to (T • , T * • ). To start, change the X stabiliser generators with support on the participating qubits to have one additional fresh qubit each, so that each pairing of Xmeasurements shares one fresh qubit. We add a new Z stabiliser generator with weight a + 2 for each participating qubit in one of the logical operators to be glued, where a is the number of X type generators of which that physical qubit is in the support. One can see this using Definition 6.3, as on the middle code (W • ,W * • ) we have We then measure d rounds of all stabilisers. All of the qubits in the domain of the last block of P Z above are those which were initialised to |+ . The only other qubits which contribute to the new Z stabiliser generators are those on either side of the sandwiched code, i.e. those along the Z logical operators of qubits i and j. Each of the physical qubits in the support of these logical operators is measured exactly once by the new Z stabiliser generators, and they are measured in pairs, one from each side; therefore performing these measurements and recording the total product is equivalent to measuring Z ⊗ Z. We will now check this, and verify that it is fault-tolerant. Let the outcome of a new Z-type measurement be c λ ∈ {1, −1}, and the overall outcome c L = ∏ λ ≤m c λ . Whenever c λ = −1 we apply the gauge fixing operator X λ = (i∈v | i=1) X i for the specified v ∈ C * 0 (or one could choose a gauge fixing operator using D * 0 instead). We let X c L = ∏ (λ | c λ =−1) X λ . On participating physical qubits, the merge is then where we abuse notation somewhat to let I and Z here refer to tensor products thereof. As each X λ belongs to the same equivalence class of logical X operators in H 0 (C • ), if c L = 1 then X c L acts as identity on the logical space; if c L = −1 then X c L acts as X on logical qubit i in the code before merging. One can then see that these two branches are precisely the branches of the logical Z ⊗ Z measurement. As the measurements were performed using d rounds of stabilisers, and the gauge fixing operators each have support on at least d qubits, the overall procedure is fault-tolerant with code distance d.
We also check that the procedure is insensitive to errors in the initialisation of fresh qubits. If a qubit is initialised instead to |− , or equivalently suffers a Z error, then the new Z stabiliser measurements are insensitive to this change, and it will just show up at the X measurements on either side of the fresh qubit. If it suffers some other error, say sending it to |1 , then each new stabiliser measurement with that qubit in its support may have its result flipped. By construction of V • , each fresh qubit is adjacent to an even number of new Z stabiliser measurements, and so initialising the fresh qubits incorrectly will not change c L .
As ZX diagrams, the branches are: ; π on logical qubits i and j, and all other logical qubits in the code are acted on as identity. We can freely choose which logical qubit may have the red π spider, as it will differ only up to a red π -i.e. a logical X -on the output logical qubit. In practice, depending on the code there will typically be cheaper ways of fixing the gauges than using an X logical operator for each −1 outcome, as there could be an X logical operator which has support on multiple of the qubits belonging to new stabilisers.
The protocol obviates the problem of performing the code map on physical qubits deterministically, as the only non-isometric transformations we perform are measurements of stabiliser generators. However, the code map on logical qubits is still not isometric, hence we have a logical measurement.
For the prototypical example of lattice surgery we then have:

→
We also look at a less obvious example, that of fault-tolerant surgery of the Shor code, in Appendix F.
By dualising appropriately one can perform an X-merge by sandwiching in a similar manner. We can also do the 'inverse' of the merge operation fault-tolerantly: be a CSS code formed by sandwiching codes (C • ,C * • ) and (D • , D * • ) together along a Z operator. Then there is a fault-tolerant procedure to implement a code map on logical qubits Proof. As the initial code is already a sandwiched code we can just take the opposite of sandwiching. We delete the qubits belonging to the intermediate code by measuring them out in the X-basis. The code map E X on participating logical qubits is by following precisely the same logic as for traditional lattice surgery [23].
Again, by dualising appropriately we get the last split operation. Given a procedure for making Z ⊗ Z and X ⊗ X logical measurements and the isometries from splits, one can easily construct a logical CNOT between suitable CSS codes following, say, [4] and observing that the same ZX diagrammatic arguments apply. Augmented with some Clifford single-qubit gates and non-stabiliser logical states one can then perform universal computation. As opposed to some other methods of performing entangling gates with CSS codes, e.g. transversal 2-qubit gates, the schemes above require only the m qubits from the respective Z or X operators to participate, and we expect m n for practical codes. Our method does not require the code to be 'self-ZX-dual' in the sense of [6]. Unlike that of [9], our method does not require a large ancillary hypergraph product code, which can have significantly worse encoding rate and code distance scaling than the LDPC codes holding data. Unlike [24], our method does not require the code to be defined on any kind of manifold, and is purely algebraic in description.

Conclusions and further work
We believe our constructions are flexible and conceptually quite simple. The immediate next step is to benchmark our CSS code surgery against other methods of performing entangling logical gates and characterise which CSS codes admit gauge fixable logical operators. It would also be interesting to test on various quantum LDPC codes with various noise models.
The pushouts we gave along logical operators are the most obvious cases. By taking pushouts of more interesting spans other maps on logical data can be obtained, although by Proposition 4.12 and Corollary 4.17 all code maps as we defined them are limited and do not allow for universal quantum computation; we also do not know whether other pushouts would allow the maps on logical data to be performed fault-tolerantly.
Herein we assumed that the two codes being 'glued' are different codes, but the same principles apply if we have only one code we would like to perform internal surgery on. In this case, the correct universal construction to use should be a coequaliser. There may be other uses of colimits in Ch(Mat F 2 ). The method of constructing good families of quantum LDPC codes in [33] uses a balanced product, which is also a coequaliser, and the instance used there could be generalised. The initial classical codes come from expander graphs, and the quotient is with respect to actions of a finite group G. This group could be changed to some other differential graded algebra, and the starting codes do not have to come from graphs. A generalised balanced product in this way cannot have asymptotically better n, k, d metrics than those of [33], up to constant factors, as their construction already saturates the relevant bounds. However, it would be interesting to see if one can obtain better metrics for concrete instances. Calculating the homologies of such codes is likely to require tools such as spectral sequences.
It should be possible to extend the definitions of Xand Z-merges straightforwardly to include metachecks [8], say by specifying that the logical operator subcomplex V • now runs from V 0 to V −2 , so it has X-checks and then metachecks on X-checks, but we have not proved how this affects metachecks in the merged code.
There are several ways in which our constructions could be generalised to other codes. The obvious generalisation is to qudit CSS codes. For qudits of prime dimension q, everything should generalise fairly straightforwardly using a different finite field F q but in this case the cell complexes will require additional data in the form of an orientation on edges, as is familiar for qudit surface codes. When q is not prime, one formalism for CSS codes with dimension q looks to be chain complexes in Z q -FFMod, the category of free finite modules over the ring Z q . As Z q is not generally a P.I.D. this may complicate the homological algebra.
Second, if we wish to upgrade to more general stabilisers we can no longer use chain complexes. The differential composition P X P Z is a special case of the symplectic product ω(M, N) = MωN for ω = 0 n I n −I n 0 n [21], but by generalising to such a product we lose the separation of Z and X stabilisers to form a pair of differentials.
For quantum codes which are not stabiliser but are based on cell complexes, such as the Kitaev model [28], there are no stabiliser generators, but the codes are still 'CSS-like', in the sense that vertices correspond to actions of the group algebra CG and faces actions of the function algebra C(G), with each measurement outcome corresponding to an irreducible representation of the quantum double D(G) = C(G)> CG. More generally we can replace CG and C(G) with H and H * for any semisimple Hopf algebra H [31,12]. Just as there are no stabiliser generators, there are no longer Z and X-operators, but there are ribbon operators. As special cases there are ribbon operators which correspond to actions of only CG or C(G). The first author recently generalised lattice surgery to Kitaev models [13], albeit with some caveats. In the same way that CSS codes generalise stabiliser codes based on cell complexes, we imagine there could be a general class of commuting projector models using the quantum double, which are not necessarily defined on a tessellated manifold. The details of such a class are not known to us, and generalising the notion of 'sites' on a lattice seems difficult. We speculate that the notion of 'gluing' along, say, a CG operator could work for such commuting projector models.
where ∂ K • n always exists and is uniquely defined, because and so by the universal property of ker( f n ) there is a unique matrix ∂ K • n : K n+1 → K n . These satisfy and then kernels are monic. K n = {v ∈ C n | f n (v) = 0} by the definition of kernels in Mat F 2 . Given the correct choice of basis, ∂ K • n is thus just ∂ C • n • ker( f n+1 ) as a matrix but without the all-zero rows which map into C n /K n .
That ker( f ) is a genuine kernel in Ch(Mat F 2 ) is straightforward to check but we do not give further details.
The reversed argument applies for cokernels, giving quotient complexes D • /im( f ) with components D n /im( f n ) etc.
Remark A.2. As Ch(Mat F 2 ) is additive, equalisers and coequalisers can be seen as special cases of kernels and cokernels by defining eq( f , g) = ker( f −g) and coeq( f , g) = coker( f −g), for f , g : C • → D • . For the chain complex part E • of an equaliser we have components E n = {c | f (c) = g(c)} ⊆ C n . For the chain complex part F • of a coequaliser, we have components F n = D n / f (c) ∼ g(c), for c ∈ C n .
We now sketch a proof of Lemma 5.5.
Proof. Recall that an Abelian category is an additive category such that: 1. Every morphism has a kernel and cokernel.
2. Every monomorphism is the kernel of its cokernel.
3. Every epimorphism is the cokernel of its kernel.
The first is just Lemma A.1, and the other two follow using the fact that they hold degree-wise in Mat F 2 .
We will now spell out pullbacks. While they can be defined using equalisers and products we construct them explicitly, as it is easy to do so.
One can check that (C ⊗ D) • is a F 2 -linear monoidal product ⊗ in Ch(Mat F 2 ), which follows from associativity and distributivity of ⊕ and ⊗ in Mat F 2 . For the unit, observe that Example B.2. Consider two chain complexes of length 1: In this case we have for nonzero components, and as the matrix partitions factor upon multiplication.
This example illustrates an interesting property of ⊗ in Ch(Mat F 2 ): both C • , D • have only one nonzero differential, but (C ⊗ D) • has two. It is easy to see that given two complexes of lengths s,t the tensor product will have length s + t.
That is, the homology subspaces factor through the tensor product conveniently. This is also called the Künneth formula. The manner in which the homology factors through does not make H n (−) a monoidal functor with respect to the tensor product.
The tensor product is used to build codes from other CSS codes [1].

C Graphs and cell complexes
In this appendix we give some categorical background on abstract cell complexes. This is not necessary to define CSS code surgery, but codes obtained from cell complexes are an important motivating example, as they include surface codes, toric codes [28], hyperbolic codes [7] and the balanced product codes from [33]. In general, if a CSS code comes from tessellating a manifold, it is likely to use cell complexes. Cell complexes are important in the study of topological spaces, and many of the constructions of CSS codes, such as balanced/lifted products, can also be phrased in the language of topology, but we stick to cell complexes for brevity. As a warm-up, we describe certain categories of graphs, and then move on to a specific kind of cell complex.
Let Γ be a finite simple undirected graph. Recall that as a simple graph, Γ has at most one edge between any two vertices and no self-loops on vertices. Γ can be defined as a pair of sets, V (Γ) and E(Γ), with E(Γ) ⊆ 2 V (Γ) , the powerset of vertices, where each e ∈ E(Γ) has 2 elements i.e. it can be expressed as e = {v 1 , v 2 }. An example of a graph is C n , the cycle graph with n vertices and edges. We will also use P n , the path graph with n edges and n + 1 vertices.
Definition C.1. Let Grph be the category of finite simple undirected graphs.
i.e. the function respects the incidence of edges.
Grph has several different products and other categorical features. We are particularly interested in colimits. Grph has a coproduct Γ + ∆ being the disjoint union, with V (Γ + ∆) = V (Γ) V (∆) and E(Γ + ∆) = E(Γ) E(∆). It also has an initial object I given by the empty graph. However, Grph is not cocomplete, as it does not have all pushouts.
Example C.2. As a counterexample [30], given the diagram no cocone exists, as the graphs are not allowed self-loops. Therefore, no pushout exists.
One can easily see that there are diagrams for which pushouts do exist, though. More than just graphs, we would like to allow for open graphs, i.e. graphs which may have edges which connect to only one vertex, but are not self-loops.
This restriction prevents internal vertices from being 'deleted' by a graph morphism by converting them to boundary vertices, although we do not prevent the reverse from occurring. OGrph has very similar properties to Grph. Its initial object is the empty open graph. OGrph has a coproduct, where V (Γ + ∆) = V (Γ) V (∆) and B(Γ + ∆) = B(Γ) B(∆). Like Grph, OGrph is not cocomplete, as Example C.2 also works in the setting of open graphs. It is obvious that Grph is a subcategory of OGrph.
We now move on to cell complexes, in particular abstract cubical complexes. These are abstract cell complexes which are 'square', unlike their 'triangular' relatives simplicial complexes.
Definition C.6. [17] Let S be a finite set and let Ω be a collection of nonempty subsets of S such that: • Ω covers S.
• For each X ∈ Ω, there is a bijection from X to the abstract d-cube for some choice of d, such that any Y ⊂ X is in Ω iff it is mapped to a face of the d-cube.
Then Ω is an abstract cubical complex.
Abstract cubical complexes are combinatorial versions of cubical complexes, meaning they are stripped of their associated geometry. The elements in Ω are still called faces. We can consider Ω to be a graded poset, with subset inclusion as the partial order, and the grading dim(X) = log 2 |X|. We also call this grading the dimension d of X, and we call X a d-face. The set of d-faces in Ω is called Ω d . There is a relation Ω d → Ω d−1 taking a d-face to its (d − 1)-face subsets.
We call the vertex set V (Ω) = S = Ω 0 , and also define the dimension of a cubical complex The d-skeleton of Ω is the maximal subcomplex ϒ ⊆ Ω such that dim(ϒ) = d. The 1-skeleton of an abstract cubical complex is a finite simple undirected graph. The 2-skeleton of an abstract cubical complex is 'like' a square lattice, in that it has 2-faces which each have 4 0-faces as subsets and 4 1-faces.
Definition C.7. Let ACC be the category of abstract cubical complexes. A morphism f : incidence is preserved at each dimension.
Similar to Grph, ACC has coproduct given by (Ω + ϒ) i = Ω i ϒ i and an initial object I = / 0, and does not generally have pushouts, where we can reuse the same counterexample as Grph. Another categorical property we highlight here is that ACC has a monoidal product called the box product.
Definition C.8. Let ϒ 2 Ω be the box product of abstract cubical complexes. Then We now check that ϒ 2 Ω is indeed an abstract cubical complex.
Proof. First, it has a vertex set V (ϒ 2 Ω) = V (ϒ) × V (Ω), and thus trivially covers 0. Third, if X and Y each have a bijection to an i-cube and j-cube respectively, then X × Y has a bijection to an (i + j)-cube. Any W ⊂ X × Y can be expressed as T × U, for T ⊂ X and U ⊂ Y . Then W is in Ω 2 ϒ iff T is mapped to a face of the i-cube and U to a face of the j-cube, thus W to a face of the (i + j)-cube.
Let us compile this into a more digestible form for the case when ϒ and Ω are both graphs. Given vertices (u, u ) and . The 1-skeleton of ϒ 2 Ω is just the normal box product of graphs [25].
Example C.9. Let C m and C n be cycle graphs with m and n vertices respectively, considered as abstract cubical complexes. Then T = C m 2 C n admits an embedding as a square lattice on the torus, and has dim(C m 2 C n ) = 2. Setting m = n = 3 we have where the grey dots indicate periodic boundary conditions and the white circles specify 2-faces. This example will come up in the form of the toric code in Section 4.
Obviously, Grph is a subcategory of ACC. We are also interested in open abstract cubical complexes.
As in our previous examples, OACC has the obvious coproduct and initial object, and does not have pushouts in general.
Example C.12. Let ϒ be a 'patch', a square lattice with two rough and two smooth boundaries: This patch has 6 2-faces, 13 1-faces and 6 0-faces.
Example C. 13. We can perform the pushout of two smaller open abstract cubical complexes to acquire a patch: where the apex is P 1 , the blue edge indicates where the apex is mapped to, and the bottom right open abstract cubical complex is the object of the pushout.
Example C.14. Let G 3 be the open path graph, and let Ω be a patch. Then we have a pushout This example comes up in the context of lattice surgery on surface codes. Evidently, both OGrph and ACC are subcategories of OACC, and one can define a box product for OACC in the same way as we did for ACC in Definition C.8.
One can define quantum codes using abstract cell complexes more generally, but abstract cubical complexes are the specific type which we make use of in examples in Section 4 and onwards. We now relate the above cell complexes to chain complexes by way of functors.
Definition C. 15. Given an abstract cubical complex Ω we can define the incidence chain complex C • in Ch(Mat F 2 ), where each nonzero component has a basisC n−1 = Ω n , say, and each nonzero differential ∂ C • n−1 takes an n + 1-face to its n-dimensional subsets. 7 The differential is thus a matrix with a 1 where an n-face is contained within an (n + 1)-face, and 0 elsewhere. It is an elementary fact that every (d − 2)face in a d-face is the intersection of exactly 2 (d − 1)-faces, thus ∂ C • n−1 • ∂ C • n = 0 mod 2. Clearly, the incidence chain complex of a dimension 1 abstract cubical complex is just the incidence matrix of a simple undirected graph.
We can do essentially the same thing given an open abstract cubical complex ϒ. In this case, each nonzero component has a basisC n−1 = {X ∈ Ω n | X ⊆ B(Ω)}, that is we ignore all faces which are made up only of boundary vertices, and differentials are the same matrices as above, with a 1 where an n-face which is not a subset of B(Ω) (and therefore would be 'invisible') is contained in an (n + 1)-face. It is easy to see that we still have ∂ C • n−1 • ∂ C • n = 0 mod 2. The incidence chain complex of a dimension 1 open abstract cubical complex is the incidence matrix of an open graph.
Definition C. 16. Let C • and D • be the incidence chain complexes of two abstract cubical complexes Ω and ϒ with a morphism f : Ω → ϒ, and setC −1 ,D −1 as V (Ω),V (ϒ) respectively. This induces a chain map g • : C • → D • , with the matrix g −1 given by f , and all matrices on higher components generated inductively. Degrees i < −1 are assumed to be zero.
As a consequence, we can define a functor ϕ : ACC → Ch(Mat F 2 ), sending each abstract cell complex to its free chain complex as described in Definition C. 15. ϕ( f ) ∈ Hom(ϕ(Ω), ϕ(ϒ)) for any morphism f : Ω → ϒ between abstract cubical complexes, as the function on vertices is already F 2 -linear and the matrices at higher degrees are uniquely determined. ϕ is faithful but not full, as there exist morphisms, such as the zero morphism, which are not in the image of ϕ.
Definition C.17. There is also a functor ϑ : OACC → Ch(Mat F 2 ). On objects, this again follows Definition C.15. On morphisms this is the same as ϕ except it must obviously ignore maps between boundary vertices everywhere. Thus ϑ is not faithful.
Proof. We give a proof sketch here. We know already that ϕ preserves coproducts so it is sufficient to check that it preserves pushouts. Let
The same checks apply if we take ϑ : OACC → Ch(Mat F 2 ) instead. Observe that in this case f and g may have images only in B(Ω) and B(ϒ), in which case Ξ must have empty V (Ξ). Then the pushout in Ch(Mat F 2 ) will just be a direct sum, i.e. the pushout with ϑ (Ξ) = 0 • as the apex.
Recall that ACC and OACC do not themselves have all pushouts, and therefore all colimits, but ϕ and ϑ preserve those which they do have.
Remark C.20. For any chain complex C • we have also the pth translation C[p] • , where all indices are shifted down by p, i.e. C[p] n = C n+p and ∂ C[p] • n = ∂ C • n+p . This extends to an invertible endofunctor p : Ch(Mat F 2 ) → Ch(Mat F 2 ) in the obvious way.

D Pushouts and properties of codes
Here we describe a few problems with using general pushouts to construct new quantum codes. First, in a certain sense the pushout of LDPC codes is not necessarily LDPC. To illustrate this, consider the following pushout of graphs: As ϑ is cocontinuous this pushout exists also in Ch(Mat F 2 ). There, it represents a merge of two binary classical codes, although we can consider a binary linear code to just be a CSS code without any Z measurements. As a consequence, we have two initial codes with P X having maximal weights 1 each, and the merged code has maximal weight 4. Evidently, one can scale this with the size of the input graphs: here, the input graphs each have 3 edges, but if there are graphs with m edges each (and weight 1) and the apex with m vertices (and weight 0) then the pushout graph will have maximal weight m + 1. As a consequence the family of pushout graphs as m scales is not bounded above by a constant, and so the corresponding family of codes is not LDPC.
be a monic span in Ch(Mat F 2 ), and let Q • be the pushout chain complex of this monic span. Further, let the monic span be a representative of a family of monic spans which are parameterised by some n ∈ N, and let A • , C • and D • be the Z-type complexes of quantum LDPC codes. Then (Q • , Q * • ) is also LDPC.
Formulating this conjecture properly requires specifying what it means for a monic span to be parameterised. The above conjecture is clearly not an if and only if, as balanced products are not pushouts of monic spans.
Lastly, taking pushouts evidently preserves neither homologies nor code distances, as easy examples with lattice surgery demonstrate. Moreover, we do not know of a way of giving bounds on these quantities for general pushouts, although again we suspect it should be easier for monic spans.

E Octagonal surface code patch
Consider the following patch of surface code: where the bristled edges are rough boundaries, and the diagonal edges are smooth boundaries. We have abstracted away from the actual cell complex as the tessellation is not important. Z-type logical operators take the form of strings extending from one rough boundary to another, e.g.
Two strings belong to the same equivalence class iff they are isotopic on the surface, allowing for the endpoints to slide up and down a rough boundary. There are exactly 3 nontrivial such classes out of which all other strings can be composed. As a consequence, this patch of surface code has logical space V with dimV = 2 3 = 8. 8 We can choose a basis for this logical space, which has logical Z operators with representatives: where the middle operator can be smoothly deformed to a vertical line from top to bottom if desired.
Recall that on the surface code an X operator anticommutes with a Z operator iff the strings cross an odd number of times. Thus, given the basis above, the duality pairing of Lemma 4.4 forces a similar basis of X operators, with representatives: We see that Z 1 is contained entirely within Z 2 on physical qubits. Thus it is possible to construct a Z merge which is not separated, in the parlance of Definition 5.12. If we choose a different representative, by deforming Z 2 to be a vertical line, then we can also perform a separated Z merge.

F Fault tolerant Z-merge with the Shor code
In this appendix we work through an example explicitly, using the techniques of Section 6 to perform a distance 3 fault-tolerant Z ⊗ Z measurement between two copies of the Shor code, for which see Example 4.7. Let us say the two copies are labelled (C • ,C * • ) and (D • , D * • ), with We will use the Z operator Z 1 ⊗ Z 4 ⊗ Z 7 , denoted u = 1 0 0 1 0 0 1 0 0 , with u ∈ C 0 and u ∈ D 0 , to glue along.
The logical operator subcomplex V • is then For T • we take the two pushouts from Definition 6.5. First, we have with R 1 = W 1 ⊕C 1 , as V 1 = 0. The other components of R • require taking quotients, identifying elements of W 0 and C 0 , and the same for W −1 and C −1 . One can then use Definition 5.1 to show that For the second pushout, that is we then have The differentials are somewhat unwieldy, but we include them for completeness:  For the fault-tolerant Z ⊗ Z measurement, we therefore start with the code ((C ⊕ D) • , (C ⊕ D) * • ). Recall that this has d = 3. We then initialise the 2 new qubits in the |+ state and measure 3 rounds of the stabilisers specified by ∂ T • 0 and ∂ T • −1 . As the 2 new qubits each participate in 2 of the new Zmeasurements, the product of the outcomes is insensitive to initialisation errors. We apply the gaugefixing operators from Example 6.2 to correct for measurements of the 3 new Z-measurements which output the -1 measurement outcome. We end up with the code (T • , T * • ).