Graph-theoretic Simplification of Quantum Circuits with the ZX-calculus

We present the theoretical foundations for a new quantum circuit optimisation technique based on an equational theory called the ZX-calculus. We first interpret quantum circuits as ZX-diagrams, which provide a flexible, lower-level language for describing quantum computations graphically. Then, using the rules of the ZX-calculus, we derive a terminating simplification procedure for ZX-diagrams based on the two graph transformations of local complementation and pivoting. While little is known about extracting an equivalent quantum circuit from an arbitrary ZX-diagram, we show that our simplification procedure preserves a graph property called focused gFlow, and use this property to derive a deterministic strategy for circuit re-extraction. Hence, this approach enables us to temporarily break the rigid quantum circuit structure and 'see around' obstructions (namely non-Clifford quantum gates) to produce significant reductions, while still maintaining enough data to extract a reduced quantum circuit at the end.


Introduction
Quantum circuits provide a de facto 'assembly language' for quantum computation, in which computations are described as the composition of many simple unitary linear maps called quantum gates. An important topic in the study of quantum circuits is quantum circuit optimisation, whereby quantum circuits which realise a given computation are transformed into new circuits involving fewer or simpler gates. While some strides have already been made in this area, the field is still in its infancy. Most approaches to quantum circuit optimisation are based on only a handful of techniques: gate substitutions, computation of small (pseudo-)normal forms for special families of circuits [1,26,24], optimisation of phase polynomials [3,20], or some combination thereof [2,28]. This paper lays a theoretical foundation for a completely new quantum circuit optimisation technique based on the ZX-calculus [9]. A key point of departure of our technique is that we break the rigid structure of quantum circuits and perform reductions on a lower-level, stringdiagram based representation of a quantum circuit called a ZX-diagram. These diagrams are more flexible than circuits, in that they can be deformed arbitrarily, and are subject to a rich equational theory: the ZX-calculus. The core rules of the ZX-calculus give a sound and complete [5] theory for Clifford circuits, a well-known class of circuits that can be efficiently classically simulated. More surprisingly, it was shown in 2018 that modest extensions to the ZX-calculus suffice to give completeness for families of circuits that are approximately universal [21] and exactly universal [18,22,30] for quantum computation.
Since ZX-diagrams provide a more flexible representation of a quantum computation than a circuit, we can derive many new simplifications from the rules of the ZX-calculus that have no analogue for quantum circuits. However, this added flexibility comes at a price: while any quantum circuit can be interpreted as a ZX-diagram by decomposing gates into smaller pieces, the converse is not true. For a generic ZX-diagram corresponding to a unitary, there is no known general-purpose procedure for efficiently recovering a quantum circuit. Hence, an important part of our optimisation procedure is keeping enough information about the quantum circuit structure to get a circuit back at the end. Schematically: ZX-diagrams Quantum circuits simplification circuit extraction Until recently, this extraction step was poorly understood, and the only techniques for doing circuit-to-circuit translation with the ZX-calculus did so without departing from the overall structure of the original quantum circuit [17], avoiding the extraction problem altogether. In this paper we adopt a more ambitious approach. First, building on prior work of two of the authors [14,15,16], we use the rules of the ZX-calculus to derive a sound, terminating simplification procedure for ZX-diagrams. The key simplification steps involve the graph-theoretic transformations of local complementation and pivoting [25,7], which allow certain generators to be deleted from ZX-diagrams one-by-one or in pairs, respectively. When applied to Clifford circuits, the diagram resulting from the simplification procedure is represented in the form of a graph-state with local Cliffords (GS-LC) [19], a pseudo-normal form for representing Clifford circuits whose size is at most quadratic in the number of qubits. Hence, one side effect of our simplification procedure is that it gives a simple, graph-theoretic alternative to the normalisation of a ZX-diagram to GS-LC form proposed by Backens [5]. For non-Clifford circuits, the simplified ZX-diagram represents a 'skeleton' of the circuit we started with, consisting only of generators arising from non-Clifford phase gates and their nearest neighbours. Although this is no longer a canonical form, it can still be significantly smaller than the input circuit, especially when there is a relatively low proportion of non-Clifford gates. We then show that, even though this simplification breaks the circuit structure, it preserves a graph-theoretic invariant called focused gFlow [8,27], from which we can derive an efficient circuit extraction strategy. We demonstrate the full simplify-and-extract procedure by means of a running example, which is also available online as a Jupyter notebook. 1 In the case of Clifford circuits, this procedure will produce circuits comparable in size to those described by standard techniques [1,29]. In the non-Clifford case, this can already find more simplifications than naïvely 'cutting' the circuit and simplifying purely-Clifford sections. Additionally, this simplify-and-extract setup provides a new framework for implementing further simplifications on the 'skeleton', e.g. for T -count reduction. We will comment on the latter point-and its implementation in a tool called PyZX [23]-as a topic of future work.
The paper is organised as follows. After giving a brief introduction to the ZX-calculus in Section 2, we will introduce graph-like ZX-diagrams in Section 3. This mild restriction to the family of all ZX-diagrams will enable us to speak more readily about graph-theoretic properties of the diagram, like the existence of a focused gFlow. In Section 4 we will show that both local complementation and pivoting preserve focused gFlow. Then in Section 5 we will derive a simplification routine using these graph-theoretic notions. We will show how the diagrams produced by this routine can be extracted into a circuit in Section 6, and finally, in Section 7 we conclude and discuss future work.

2
The ZX-calculus and quantum circuits We will provide a brief overview of the ZX-calculus. For an in-depth reference see Ref. [10]. The ZX-calculus is a diagrammatic language similar to the familiar quantum circuit notation. A ZX-diagram consists of wires and spiders. Wires entering the diagram from the left are inputs; wires exiting to the right are outputs. Given two diagrams we can compose them by joining the outputs of the first to the inputs of the second, or form their tensor product by simply stacking the two diagrams.
Spiders are linear maps which can have any number of input or output wires. There are two varieties: Z spiders depicted as green dots and X spiders depicted as red dots. 2 Written explicitly in Dirac 'bra-ket' notation, these linear maps are: A ZX-diagram with m intput wires and n output wires then represents a linear map (C 2 ) ⊗m → (C 2 ) ⊗n built from spiders (and permutations of qubits) by composition and tensor product of linear maps. As a special case, diagrams with no inputs and n outputs represent vectors in (C 2 ) ⊗n , i.e. (unnormalised) n-qubit states.
Example 2.1. We can immediately write down some simple state preparations and unitaries in the ZX-calculus: In particular we have the Pauli matrices: It will be convenient to introduce a symbol -a yellow square -for the Hadamard gate. This is defined by the equation: = π 2 π 2 π 2 (1) We will often use an alternative notation to simplify the diagrams, and replace a Hadamard between two spiders by a blue dashed edge, as illustrated below. ...

Figure 1
A convenient presentation for the ZX-calculus. These rules hold for all α, β ∈ [0, 2π), and due to (h) and (i2) all rules also hold with the colours interchanged.
Both the blue edge notation and the Hadamard box can always be translated back into spiders when necessary. We will refer to the blue edge as a Hadamard edge. Two diagrams are considered equal when one can be deformed to the other by moving the vertices around in the plane, bending, unbending, crossing, and uncrossing wires, so long as the connectivity and the order of the inputs and outputs is maintained. Furthermore, there is an additional set of equations that we call the rules of the ZX-calculus; these are shown in Figure 1. Equal diagrams correspond to equal linear maps 3 .
Remark 2.2. The ZX-calculus is universal in the sense that any linear map can be expressed as a ZX-diagram. Furthermore, when restricted to Clifford ZX-diagrams, i.e. diagrams whose phases are all multiples of π/2, the version we present in Figure 1 is complete. That is, for any two Clifford ZX-diagrams that describe the same linear map, there exists a series of rewrites transforming one into the other. Recent extensions to the calculus have been introduced which are complete for the larger Clifford+T family of ZX-diagrams [21], where phases are multiples of π/4, and for all ZX-diagrams [18,22,30].
Quantum circuits can be translated into ZX-diagrams in a straightforward manner. We will take as our starting point circuits constructed from the following universal set of gates: We fix this gate set because they admit a convenient representation in terms of spiders: Note that the green spider is the first (i.e. control) qubit of the CNOT and the red spider is the second (i.e. target) qubit. Other common gates can easily be expressed in terms of these gates. In particular, S := Z π 2 , T := Z π 4 and: For our purposes, we will define quantum circuits as a special case of ZX-diagrams. (2). Important subclasses of circuits are Clifford circuits, sometimes called stabilizer circuits, which are obtained from compositions of only CNOT, H, and S gates. They are efficiently classically simulable, thanks to the Gottesman-Knill theorem [1]. Local Clifford circuits are Clifford circuits on a single qubit, i.e. circuits consisting only of H and S gates.

Definition 2.3. A circuit is a ZX-diagram generated by compositions and tensor products of the ZX-diagrams in equation
The addition of T gates yields Clifford+T circuits, which are capable of approximating any n-qubit unitary to arbitrary precision, whereas the inclusion of Z α gates for all α enables one to construct any unitary exactly.

Graph-like ZX-diagrams
Before starting our simplification procedure, we transform ZX-diagrams into the following, convenient form.

Every input or output is connected to a Z-spider and every Z-spider is connected to at most one input or output.
A ZX-diagram is called a graph state if it is graph-like, every spider is connected to an output, and there are no non-zero phases. This corresponds to the standard notion of graph state from the literature [19]. Hence, graph-like ZX-diagrams are a generalisation of graph states to maps where we allow arbitrary phases and some interior spiders, i.e. spiders not connected to an input or output.

Lemma 3.2. Every ZX-diagram is equal to a graph-like ZX-diagram.
Proof. Starting with an arbitrary ZX-diagram, we apply (h) to turn all red spiders into green spiders surrounded by Hadamard gates. We then remove excess Hadamards via (i2). Any non-Hadamard edge is then removed by fusing the adjacent spiders with (f ). Any parallel Hadamard edges or self-loops can be removed via the following 3 rules: which we prove in Appendix B.1. Finally, we can apply the rules (i1) and (i2) from right-to-left until the last condition is satisfied.
A useful feature of graph-like ZX-diagrams is that much of its structure is captured by its underlying open graph.  Note that, because of Definition 3.1, the sets I and O in G(D) will always be disjoint. When discussing open graphs we will use the notation I := V \ I and O := V \ O for the non-inputs and non-outputs respectively. The set of neighbours of a vertex v ∈ V will be denoted by N (v). We are now ready to define a graph-theoretic condition that will be instrumental in our circuit extraction procedure.
Remark 3.5. Generalised flow (gFlow) techniques were introduced in the context of measurement-based quantum computing [12,13]. Focused gFlow is a special case of the standard notion of gFlow [8], where the latter allows other vertices v in the set Odd G (g(u))∩O, provided they are in the future of u (i.e. u ≺ v). However, as a property of graphs, the two notions are equivalent: an open graph has a gFlow if and only if it has a focused gFlow. We will rely on this equivalence in Appendix B.2 to prove the following Lemma.

Local complementation and pivoting
Local complementation is a graph transformation introduced by Kotzig [25].
A related graph-theoretic operation is pivoting, which takes place at a pair of adjacent vertices.

Definition 4.3. Let G be a graph, and let u and v be a pair of connected vertices in G.
The pivot of G along the edge uv is the graph G ∧ uv := G u v u.
On a graph, pivoting consists in exchanging u and v, and complementing the edges between three particular subsets of the vertices: the common neighbourhood of u and v (i.e. Our primary interest in local complementation and pivoting is that each corresponds to a transformation of ZX-diagrams. In the special case where a ZX-diagram D is a graph-state, it is possible to obtain a diagram D where G(D ) = G(D) u by applying an X −π/2 gate on the spider corresponding to u and Z π/2 on all of the spiders in N (v): This is a well-known property of graph states, and a derivation using the rules of the ZXcalculus was given in [14]. Similarly, it was shown in [16] that a pivot G(D ) = G(D) ∧ uv can be introduced by applying Hadamard gates on u and v and Z π gates on N (u) ∩ N (v): Note that the swap on the RHS comes from the vertices u and v being interchanged by the pivot.
The following theorem will prove pivotal in our extraction routine. It shows that the existence of a focused gflow is preserved by local complementation (resp. pivoting) followed by the deletion of the vertex (resp. the pair of vertices) on which the transformation is applied: be an open graph that admits a focused gFlow,

the graph obtained by deleting the vertices in W and any adjacent edges.
Proof. The two cases are proved in Appendix B.3, Lemmas B.9 and B.12 respectively.

A simplification strategy for circuits
We now have all the necessary machinery to introduce our simplification routine. The general idea is to use local complementation and pivoting based rewrite rules to remove as many interior spiders as possible. A spider is called interior when it is not connected to an input or an output, otherwise it is called a boundary spider.

Definition 5.1. We call a spider Pauli if its phase is a multiple of π, and
Clifford if its phase is a multiple of π 2 . If the phase of a Clifford spider is an odd multiple of π 2 (and hence non-Pauli), we call this a proper Clifford spider.
The graph-like ZX-diagram resulting from the translation of a Clifford circuit contains only Clifford spiders, since the only time the phase changes on a spider is during a spider-fusion step, in which case the phases are added together.
We will show that our procedure is able to eliminate all interior proper Clifford spiders and all Pauli spiders adjacent either to a boundary spider or any interior Pauli spider. In particular, for Clifford circuits, we will obtain a pseudo-normal form which contains no interior spiders (cf. Section 6).
The main rewrite rules we use are based on local complementation and pivoting. The first one allows us to remove any interior proper Clifford spider while the second one removes any two connected interior Pauli spiders.

Lemma 5.2. The following rule holds in the ZX-calculus:
where the RHS is obtained from the LHS by performing a local complementation at the marked vertex, removing it, and updating the phases as shown. where the RHS is obtained from the LHS by performing a pivot at the marked vertices, removing them, and updating the phases as shown.
The proof of these lemmas can be found in Appendix B.4.
We can additionally apply (8) to remove an interior Pauli spider that is adjacent to a boundary spider. To do this, we first turn the boundary spider into a (phaseless) interior spider via the following transformation, which follows from the rules (f ), (i1), and (i2): After this transformation, we can perform (8) on the two spiders labelled {u, v} to remove them. For our purposes, we can then treat w as a boundary spider, and save the single-qubit unitaries to the right of w separately.
Theorem 5.4. There exists a terminating procedure which turns any graph-like ZX-diagram D into a graph-like ZX-diagram D (up to single-qubit unitaries on inputs/outputs) which does not contain:

interior proper Clifford spiders 2. adjacent pairs of interior Pauli spiders 3. interior Pauli spiders adjacent to a boundary spider
In particular, if D only contains Clifford spiders, then D contains no interior spiders.
Proof. Starting with a graph-like ZX-diagram, mark every interior Clifford spider for simplification. Then repeat the steps below (followed by (4) to remove parallel edges), until no rule matches: apply (7) to remove a marked proper Clifford spider apply (8) to remove an adjacent pair of marked Pauli spiders apply (9) to a boundary adjacent to a marked Pauli spider and immediately apply (8) All 3 rules remove at least 1 marked node, so the procedure terminates. Since none of these rules will introduce non-Clifford spiders, if D contains only Clifford spiders so too does D . Since the only possibilities for interior spiders in D are covered by cases 1-3, D contains no interior spiders. Proof. It was shown in Theorem 4.4 that the transformations (7) and (8) preserve focused gFlow. The transformation (9) acts as an input/output extension on G(D). That is, for X ∈ {I, O}, v ∈ X, it adds a new vertex w and an edge vw, and sets X := (X\{v}) ∪ {w}. It was shown in [27] that these transformations preserve focused gflow.
This simplification procedure is very effective, especially for circuits with few non-Clifford gates. We can see this in the following example:  (10) Note the non-Clifford phases now clearly appear on the 4 interior spiders. In general, the interior spiders will either be non-Clifford or be Pauli with all non-Clifford neighbours. Any other possibilities will be removed.

Circuit extraction
As described in the previous section, when a Clifford circuit is fed into our simplification routine, no interior spiders will be left, and hence the diagram is in GS-LC form (graph-state with local Cliffords). It is straightforward to extract a Clifford circuit from a GS-LC form. First, we unfuse spiders to create CZ gates and local Cliffords on the inputs and outputs, then apply the (h) rule to the spiders on the right side of the resulting bipartite graph: The part of the diagram marked P now has the form of a classical parity circuit. That is, it is a permutation of computational basis states where the output basis is given in terms of parities of the inputs, e.g. |x 1 , Such a unitary can always be realised via CNOT gates, using standard techniques based on Gaussian elimination over F 2 (see e.g. [26]). Hence, we obtain a Clifford circuit with 6 layers of gates: Local Cliffords + CZs + CNOTs + Hadamards + CZs + Local Cliffords (11) Remark 6.1. This is similar to the 11-layer pseudo-normal form for Clifford circuits given in [1] and the slightly more compact version in [29]. The reason we obtain fewer layers is we allow CZ gates in the decomposition, rather than expanding them in terms of CNOT and local Cliffords.
If a Clifford+T circuit -or a more general circuit containing gates Z α or X α for α = k π 2 -is fed into our simplification routine, there might still be interior spiders left, and hence the diagram will not be in GS-LC form.
Our general strategy for extracting a circuit from a diagram produced by our simplification routine is to progress through the diagram from right-to-left, consuming vertices to the left and introducing quantum gates on the right. For an n-qubit circuit, we maintain a layer of n vertices called the frontier between these two parts. The goal is therefore to progress the frontier from the outputs all the way to the inputs, at which point we are left with an extracted circuit. We will see that the existence of a focused gFlow guarantees we can always progress the frontier leftward in this manner.
Much like in the Clifford case, our extraction procedure relies on applying (a variant of) Gaussian elimination on the adjacency matrix of a graph. Hence, we first establish a correspondence between primitive row operations and CNOT gates. We can now start describing our extraction procedure. To start, let the frontier F consist of all the output vertices. If a vertex of the frontier has a non-zero phase, unfuse this phase in the direction of the output. Whenever two vertices of the frontier are connected, unfuse this connection into a CZ gate. If the diagram started out having a gFlow, then the resulting diagram still has a gFlow since we only affected output vertices. Let g be the gFlow of the diagram. We push the frontier onto new vertices in the following way: 1. Find an unextracted vertex v such that g(v) ⊆ F . Such a vertex always exists: take for instance any non-frontier vertex that is maximal in the gFlow order. 2. We know that Odd(g(v)) = {v} hence we can pick a designated vertex w ∈ F such that w is connected to v. 3. Write the biadjency matrix of g(v) ⊆ F and their neighbours to the left of the frontier ('the past'). Because Odd(g(v)) = {v} we know that the sum of all the rows in this matrix modulo 2 is an all-zero vector except for a 1 corresponding to v. Hence, if we apply row operations with w as the target from every other vertex in g(v) we know that w will have only a single vertex connected to it in its past, namely v. Do these row operations using Proposition 6.2. As a result, CNOT gates will be pushed onto the extracted circuit. By that same proposition, the diagram will still have a gFlow. 4. At this point it might be that v is still connected to vertices of F that are not w. For each of these vertices w ∈ F , do a row operation from w to w by adding the corresponding CNOT to the extracted diagram in order to disconnect w from v. At this point v's only connection to F is via w. We can therefore conclude that the gFlow of the resulting diagram is such that the only vertex u that has w ∈ g(u) is v. 5. The vertex w has arity 2 and since it is phaseless we can remove it from the diagram by (i1). Remove w from the frontier, and replace it by v. By the remarks in the previous point the resulting diagram still has a gFlow. Unfuse the phase of v as a phase gate to the extracted part. 6. If there is any vertex in F that is not connected to an input, go back to step 1, otherwise we are done. After all vertices have been extracted by the above procedure, we will have some permutation of the frontier vertices to the inputs. The extraction is finished by realising this permutation as a series of SWAP gates.
For a pseudocode description of the extraction algorithm, see Appendix C in the Appendix. For a fully worked-out example we refer to Appendix A. Remark 6.3. An interesting thing to note is we do not actually need a pre-calculated focused gFlow for this algorithm: knowing that one exists is enough. In step 3, it suffices to apply primitive row operations until a row with a single 1 appears in the biadjacency matrix. If this is possible by some set of row operations (i.e. if there exists a focused gFlow), such a row will always appear in the reduced echelon form, so we can simply do Gauss-Jordan elimination. This is how we describe this step in the pseudocode presented in Appendix C.
Picking up our example from the previous section, we can apply this procedure to the skeleton of the circuit in equation (10) and we obtain a new, extracted circuit: The circuit has been reduced from 195 gates to 41 gates, going via an intermediate representation as a ZX-diagram with 12 spiders. As mentioned in the introduction, this example is available as a Jupyter notebook 'demos/example-gtsimp.ipynb' in the PyZX [23] repository on GitHub.

Conclusions and Future Work
We have introduced a terminating rewrite strategy for ZX-diagrams that simplifies Clifford circuits to GS-LC form using the graph-theoretic notions of local complementation and pivoting. We have shown how the diagrams produced by the rewrite strategy applied to non-Clifford circuits can be turned back into a circuit using the fact that our rewrites preserve focused gFlow. The question of how a circuit can be extracted from a general ZX-diagram is still open. We speculate that this problem is not tractable in the general case, as it seems to be related to the problem of finding an ancilla-free unitary from a circuit containing ancillae. This observation does however not preclude finding larger classes of rewrites and ZX-diagrams such that we can efficiently extract a circuit. The next step for optimising quantum circuits using ZX-diagrams is to find additional rewrite rules that allows one to produce smaller diagrams, while still being able to extract the diagrams into a circuit. An approach based on replacing non-Clifford phase gates with certain ZX-diagrams called phase gadgets during the simplification procedure has shown some promise. These phase gadgets enable phases to propegate non-locally through the circuit, much like they do in phase-polynomial approaches to quantum circuit optimisation [20,4,3]. However, unlike those approaches, the framework we described in this paper (with the addition of phase gadgets) applies to arbitrary circuits, rather than only {CNOT, Z α } circuits. Various heuristics involving phase gadgets have been implemented in the circuit optimisation tool PyZX, however a detailed description of these techniques and analysis of their performance is a topic of future work.
A second avenue of future work is to adapt the circuit extraction procedure to work in constrained qubit topologies. Many near-term quantum devices only allow interactions between certain pairs of qubits (e.g. nearest neighbours in some fixed planar graph). A notable feature of the circuit extraction procedure described in Section 6 is that we have some freedom in choosing which CNOTs to apply in reducing the bi-adjacency matrix of the ZX-diagram. We are currently exploring techniques by which this extraction can be constrained to only apply CNOTs in 'allowed' locations to produce circuits suitable for hardware with topological constraints.

A Example derivation
In this section we will work through a complete example of our simplification algorithm. Let us start with the following circuit: We see that we have multiple interior Pauli and proper Clifford phases and hence we must do local complementations and pivots. We start by doing a sequence of local complementations. The locations of these operations are marked by a red star. We now have two internal Pauli vertices left, but they are not connected. Hence we must use equation (9), to be able to get rid of them: Here the frontier is marked by the box. We first unfuse the phases onto gates. Then we want to extract new vertices. These are marked by a red star. Since the three marked vertices have no other connections to the frontier, and the frontier vertices have only these single neighbours, we can extract these by simply putting them onto the frontier. The next step is to extract the bottomleft vertex, but in this case there is a connection too many which we must extract as a CNOT: We now simply repeat this procedure until the entire circuit is extracted. The extracted circuit has 4 two-qubit gates and 18 single-qubit gates. Applying some final cancellations between Hadamards on the inputs and outputs gets the number of single-qubit gates down to 14: is derivable in the ZX-calculus.
Proof. To derive the (a) rule we take advantage of the freedom to deform the diagram as shown below.
The final equation is simply dropping the scalar.  Proof. To derive the (πc) rule we use the (f ) rule first to split the π-labelled vertex, and the (π) and (c) rules do the bulk of the work as shown below: ..

B.2 Circuits and focused gFlow
To show that graph-like ZX-diagrams arising from circuits admit a focused gFlow, we introduce a simpler sufficient condition.
Definition B.5. [11] Given an open graph G, a causal flow (f, ≺) on G consists of a function f : O → I and a partial order ≺ on the set V satisfying the following properties: where v ∼ u means that u and v are adjacent in the graph.
Like focused gFlow, causal flow is a special case of a more general property called gFlow: Definition B. 6. Given an open graph G, a generalised flow, or gFlow consists of a pair (g : O → 2 I , ≺) such that for any u ∈ O: In the case where all of the sets g(u) are singletons, the conditions above are equivalent to the causal flow conditions. Hence, if a graph admits a causal flow, it also admits a gFlow by letting g(u) := {f (u)}. It was furthermore show in [27] that any gFlow can be systematically transformed into a focused gFlow. Combining these two facts, we obtain the following theorem. The following proof of Lemma 3.6 shows that the diagrams produced by making a circuit graph-like according to the procedure outlined in Lemma 3.2 have causal flow and hence a focused gFlow.
Proof of Lemma 3.6. Let D denote the circuit, and let D denote the diagram produced by applying the procedure of Lemma 3.2 to D.
In order to prove that D has a focused gFlow, it suffices to show that it admits a causal flow, by Theorem B.7.
With every spider v of the circuit D we associate a number q v specifying on which 'qubit-line' it appears. We also associate a 'row-number' r v ≥ 0 specifying how 'deep' in the circuit it appears. Suppose now that v ∼ w in D. If they are on the same qubit, so q v = q w , then necessarily r v = r w . Conversely, if they are on different qubits, q v = q w , then they must be part of a CZ or CNOT gate, and hence r v = r w .
In the diagram resulting from Lemma 3.2, every spider arises from fusing together adjacent spiders on the same qubit line from the original diagram. For a spider v in D we can thus associate two numbers s v , and t v , where s v is the lowest row-number of a spider fused into v, and t v is the highest. Spider fusion from D only happens for spiders on the same qubit-line, and hence v also inherits a q v from all the spiders that got fused into it. Any two spiders v and w in D with q v = q w must have been produced by fusing together adjacent spiders on the same qubit-line, and hence we must have t v < s w or t w < s v , depending on which of the spiders is most to the left. If instead v ∼ w and q v = q w , then their connection must have arisen from some CNOT or CZ gate in D, and hence the intervals [s v , t v ] and [s w , t w ] must have overlap, so that necessarily s w ≤ t v and s v ≤ t w . Now we let O be the set of spiders connected to an output, and I the set of spiders connected to an input in D . For all v ∈ O we let f (v) be the unique connected spider to the right of v on the same qubit-line. We define the partial order as follows: v ≺ w if and only if v = w or t v < t w . It is straightforward to check that this is indeed a partial order.
By construction f (v) ∼ v and the property v ≺ f (v), follows from t v < s f (v) discussed above, so let us look at the third property of causal flow.
Suppose w ∼ f (v). We need to show that v ≺ w. If v = w this is trivial so suppose v = w. First suppose that q w = q f (v) (which is also equal to q v ). f (v) has a maximum of two neighbours on the same qubit-line, and since one of them is v, this can only be if w = f (f (v)) and hence v ≺ f (v) ≺ w, and we are done. So suppose that q w = q f (v) . By the discussion above, we must then have s w ≤ t f (v) and s f (v) ≤ t w . Since we also have

B.3 Preservation of focused gFlow
Throughout this section, we will rely extensively on the symmetric difference of sets: A∆B := (A∪B)\(A∩B). This extends to an n-ary operation ∆ i∈I A i in the obvious way. By convention, we assume these binds are far to the right as possible, i.e.

B.3.1 Local complementation
The following lemma will be needed in the other proofs, it shows how the odd neighbourhood of a set evolves under local complementation: The local complementation is acting as follows on the neighbourhoods: As a consequence, Proof. If (g, ≺) is a focused gFlow for (G, I, O) then (g , ≺) is a focused gFlow for (G u \ u, I, O) where g : O \ {u} → 2 I\{u} is inductively defined as First of all, g can indeed be defined inductively by starting at the maximal elements in the partial order, since for any t ∈ R w , t ∈ g(w) so that w ≺ t, and similarly when u ∈ g(w), w ≺ u so t ∈ R u implies u ≺ t hence w ≺ t. That g preserves the order, i.e. v ∈ g (w) =⇒ w ≺ v, follows similarly. Moreover, it is easy to show that ∀w ∈ O \ {u}, First notice that for any w ∈ O , u / ∈ Odd G (g(w)), so, according to Lemma B.8, and using the linearity of Odd(.) and the distributivity of ∩ over ∆, As a consequence, Now, we prove that ∀w ∈ O , Odd G u (g (w)) ∩ O = {w} by induction on w starting at maximal elements in the order.
Let w 0 ∈ O be maximal for ≺ i.e. ∀w ∈ O , ¬(w 0 ≺ w). By maximality we must have By induction, for any w ∈ O , If u / ∈ g(w), If u ∈ g(w), Notice that (g , ≺) is not necessarily a focused gFlow.

B.3.2 Pivoting
The action of the pivoting on the odd-neighbourhood can be described as follows: Proof. The pivoting acts as follows on neighbourhoods: As a consequence, B.4 ZX reduction rules Lemma (5.2).
First of all, we will need the following equation: We pull out all of the phases via (f ) then apply the local complementation rule (5): Thanks to equation (14), the topmost spider in the RHS above becomes an X-spider, with phase ∓π/2, which is canceled out by X ±π/2 gate directly below it. The resulting (phaseless) X-spider copies and fuses with the neighbours: ...
..      We then apply the colour-change rule to turn the Z-spiders with phases jπ and kπ into X-spiders, which copy and fuse with the neighbours of the original two vertices:  Here M describes the biadjacency matrix of the relevant vertices, and M is the matrix produced by starting with M and then adding row 2 to row 1, taking sums modulo 2. Furthermore, if the diagram on the lefthandside has a focused gFlow, then so does the righthandside.
Proof. We will show how to transform the first diagram into the second in such a way that gFlow and equality is preserved at every step. For clarity we will not draw the entire diagram, but instead we focus on the relevant part. First of all we note that we can add CNOTs in the following way while preserving equality: . . . As we are only adding vertices at the outputs, it should be clear how the gFlow can be extended to incorporate these new vertices. Now let A denote the set of vertices connected to the top vertex, but not to the vertex beneath it, B the set of vertices connected to both, and C the vertices connected only to the bottom one. Further restricting our view of the diagram to just these two lines, we see that we can apply a pivot rewrite as in (8):  for v ∈ F do 6: if v connected to output by Hadamard then 7: C ← Hadamard(Qubit(v)) 8: Remove Hadamard from v to output 9: if v has non-zero phase then 10: C ← Phase-gate(P hase(v), Qubit(v)) 11: for edge between v and w in F do

12:
C ← CZ(Qubit(v), Qubit(w)) 13: Remove edge between v and w 14: while ∃v ∈ D\F do there are still vertices to be processed Remove v from F

42:
Add w to F 43: for edge between w1 and w2 of ws do