1 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 ZXcalculus [9]. A key point of departure of our technique is that we break the rigid structure of quantum circuits and perform reductions on a lowerlevel, stringdiagram based representation of a quantum circuit called a ZXdiagram. These diagrams are more flexible than circuits, in that they can be deformed arbitrarily, and are subject to a rich equational theory: the ZXcalculus. The core rules of the ZXcalculus give a sound and complete [5] theory for Clifford circuits, a wellknown class of circuits that can be efficiently classically simulated. More surprisingly, it was shown in 2018 that modest extensions to the ZXcalculus suffice to give completeness for families of circuits that are approximately universal [21] and exactly universal [18, 22, 30] for quantum computation.
Since ZXdiagrams provide a more flexible representation of a quantum computation than a circuit, we can derive many new simplifications from the rules of the ZXcalculus that have no analogue for quantum circuits. However, this added flexibility comes at a price: while any quantum circuit can be interpreted as a ZXdiagram by decomposing gates into smaller pieces, the converse is not true. For a generic ZXdiagram corresponding to a unitary, there is no known generalpurpose 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:
schematic 
Until recently, this extraction step was poorly understood, and the only techniques for doing circuittocircuit translation with the ZXcalculus 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 ZXcalculus to derive a sound, terminating simplification procedure for ZXdiagrams. The key simplification steps involve the graphtheoretic transformations of local complementation and pivoting [25, 7], which allow certain generators to be deleted from ZXdiagrams onebyone or in pairs, respectively. When applied to Clifford circuits, the diagram resulting from the simplification procedure is represented in the form of a graphstate with local Cliffords (GSLC) [19]
, a pseudonormal 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, graphtheoretic alternative to the normalisation of a ZXdiagram to GSLC form proposed by Backens
[5]. For nonClifford circuits, the simplified ZXdiagram represents a ‘skeleton’ of the circuit we started with, consisting only of generators arising from nonClifford 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 nonClifford gates. We then show that, even though this simplification breaks the circuit structure, it preserves a graphtheoretic invariant called focused gFlow [8, 27], from which we can derive an efficient circuit extraction strategy. We demonstrate the full simplifyandextract procedure by means of a running example, which is also available online as a Jupyter notebook.^{1}^{1}1nbviewer.jupyter.org/github/Quantomatic/pyzx/blob/906f6db3/demos/examplegtsimp.ipynbIn the case of Clifford circuits, this procedure will produce circuits comparable in size to those described by standard techniques [1, 29]. In the nonClifford case, this can already find more simplifications than naïvely ‘cutting’ the circuit and simplifying purelyClifford sections. Additionally, this simplifyandextract setup provides a new framework for implementing further simplifications on the ‘skeleton’, e.g. for 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 ZXcalculus in Section 2, we will introduce graphlike ZXdiagrams in Section 3. This mild restriction to the family of all ZXdiagrams will enable us to speak more readily about graphtheoretic 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 graphtheoretic 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 ZXcalculus and quantum circuits
We will provide a brief overview of the ZXcalculus. For an indepth reference see Ref. [10].
The ZXcalculus is a diagrammatic language similar to the familiar quantum circuit notation. A ZXdiagram 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: spiders depicted as green dots and spiders depicted as red dots.^{2}^{2}2If you are reading this document in monochrome or otherwise have difficulty distinguishing green and red, spiders will appear lightlyshaded and darklyshaded. Written explicitly in Dirac ‘braket’ notation, these linear maps are:
A ZXdiagram with intput wires and output wires then represents a linear map built from spiders (and permutations of qubits) by composition and tensor product of linear maps. As a special case, diagrams with no inputs and
outputs represent vectors in
, i.e. (unnormalised) qubit states.We can immediately write down some simple state preparations and unitaries in the ZXcalculus:
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:
(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. blueedgedef 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 ZXcalculus; these are shown in Figure 1. Equal diagrams correspond to equal linear maps^{3}^{3}3We will consider linear maps and to be “equal” if they satisfy for some nonzero . Backens [6] presents a version of the ZXcalculus where equality is “on the nose”..
The ZXcalculus is universal in the sense that any linear map can be expressed as a ZXdiagram. Furthermore, when restricted to Clifford ZXdiagrams, i.e. diagrams whose phases are all multiples of , the version we present in Figure 1 is complete. That is, for any two Clifford ZXdiagrams 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 ZXdiagrams [21], where phases are multiples of , and for all ZXdiagrams [18, 22, 30].
Quantum circuits can be translated into ZXdiagrams 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:
CNOT  (2) 
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, , and:
CZ  (3) 
For our purposes, we will define quantum circuits as a special case of ZXdiagrams.
A circuit is a ZXdiagram generated by compositions and tensor products of the ZXdiagrams in equation (2).
Important subclasses of circuits are Clifford circuits, sometimes called stabilizer circuits, which are obtained from compositions of only CNOT, , and gates. They are efficiently classically simulable, thanks to the GottesmanKnill theorem [1]. Local Clifford circuits are Clifford circuits on a single qubit, i.e. circuits consisting only of and gates.
The addition of gates yields Clifford+T circuits, which are capable of approximating any qubit unitary to arbitrary precision, whereas the inclusion of gates for all enables one to construct any unitary exactly.
3 Graphlike ZXdiagrams
Before starting our simplification procedure, we transform ZXdiagrams into the following, convenient form.
A ZXdiagram is graphlike when:

All spiders are Zspiders.

Zspiders are only connected via Hadamard edges.

There are no parallel Hadamard edges or selfloops.

Every input or output is connected to a Zspider and every Zspider is connected to at most one input or output.
A ZXdiagram is called a graph state if it is graphlike, every spider is connected to an output, and there are no nonzero phases. This corresponds to the standard notion of graph state from the literature [19]. Hence, graphlike ZXdiagrams 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.
Every ZXdiagram is equal to a graphlike ZXdiagram.
Proof.
Starting with an arbitrary ZXdiagram, we apply to turn all red spiders into green spiders surrounded by Hadamard gates. We then remove excess Hadamards via . Any nonHadamard edge is then removed by fusing the adjacent spiders with . Any parallel Hadamard edges or selfloops can be removed via the following 3 rules:
(4) 
which we prove in Appendix B.1. Finally, we can apply the rules and from righttoleft until the last condition is satisfied. ∎
A useful feature of graphlike ZXdiagrams is that much of its structure is captured by its underlying open graph.
An open graph is a triple where is an undirected graph, and is a set of inputs and a set of outputs. For a ZXdiagram , the underlying open graph is the open graph whose vertices are the spiders of , whose edges correspond to Hadamard edges, and whose sets and are the subsets of spiders connected to the inputs and outputs of , respectively.
Note that, because of Definition 3, the sets and in will always be disjoint. When discussing open graphs we will use the notation and for the noninputs and nonoutputs respectively. The set of neighbours of a vertex will be denoted by . We are now ready to define a graphtheoretic condition that will be instrumental in our circuit extraction procedure.
[27] Given an open graph , a focused gFlow on consists of a function and a partial order on the vertices of such that for all ,
where is the odd neighbourhood of .
Generalised flow (gFlow) techniques were introduced in the context of measurementbased quantum computing [12, 13]. Focused gFlow is a special case of the standard notion of gFlow [8], where the latter allows other vertices in the set , provided they are in the future of (i.e. ). 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.
If is a graphlike ZXdiagram obtained from a circuit by the procedure of Lemma 3, then admits a focused gFlow.
Proof.
See Appendix B.2. ∎
4 Local complementation and pivoting
Local complementation is a graph transformation introduced by Kotzig [25]. Let be a graph and let be a vertex of . The local complementation of according to , written as , is a graph which has the same vertices as , but all the neighbours of are connected in if and only if they are not connected in . All other edges are unchanged.
Local complementations according to the vertices and are given by:
A related graphtheoretic operation is pivoting, which takes place at a pair of adjacent vertices. Let be a graph, and let and be a pair of connected vertices in . The pivot of along the edge is the graph . On a graph, pivoting consists in exchanging and , and complementing the edges between three particular subsets of the vertices: the common neighbourhood of and (i.e. ), the exclusive neighbourhood of (i.e. ), and exclusive neighbourhood of (i.e. ):
Our primary interest in local complementation and pivoting is that each corresponds to a transformation of ZXdiagrams. In the special case where a ZXdiagram is a graphstate, it is possible to obtain a diagram where by applying an gate on the spider corresponding to and on all of the spiders in :
(5) 
This is a wellknown 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 can be introduced by applying Hadamard gates on and and gates on :
(6) 
Note that the swap on the RHS comes from the vertices and 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:
Let be an open graph that admits a focused gFlow,

for , setting

for , setting
where is the graph obtained by deleting the vertices in and any adjacent edges.
5 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.
We call a spider Pauli if its phase is a multiple of , and Clifford if its phase is a multiple of . If the phase of a Clifford spider is an odd multiple of (and hence nonPauli), we call this a proper Clifford spider.
The graphlike ZXdiagram 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 spiderfusion 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 pseudonormal 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. The following rule holds in the ZXcalculus:
(7) 
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. The following rule holds in the ZXcalculus:
(8) 
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 , , and :
(9) 
After this transformation, we can perform (8) on the two spiders labelled to remove them. For our purposes, we can then treat as a boundary spider, and save the singlequbit unitaries to the right of separately.
There exists a terminating procedure which turns any graphlike ZXdiagram into a graphlike ZXdiagram (up to singlequbit unitaries on inputs/outputs) which does not contain:

interior proper Clifford spiders

adjacent pairs of interior Pauli spiders

interior Pauli spiders adjacent to a boundary spider
In particular, if only contains Clifford spiders, then contains no interior spiders.
Proof.
Starting with a graphlike ZXdiagram, 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
All 3 rules remove at least 1 marked node, so the procedure terminates. Since none of these rules will introduce nonClifford spiders, if contains only Clifford spiders so too does . Since the only possibilities for interior spiders in are covered by cases 13, contains no interior spiders. ∎
For and the ZXdiagrams from Theorem 5, if admits a focused gFlow, then so too does .
Proof.
This simplification procedure is very effective, especially for circuits with few nonClifford gates. We can see this in the following example:
bigexample4rows 
The circuit above has 195 gates, with 4 nonClifford gates. If we apply the simplification procedure, we obtain a small ‘skeleton’ of the circuit, containing 12 spiders:
(10) 
Note the nonClifford phases now clearly appear on the 4 interior spiders. In general, the interior spiders will either be nonClifford or be Pauli with all nonClifford neighbours. Any other possibilities will be removed.
6 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 GSLC form (graphstate with local Cliffords). It is straightforward to extract a Clifford circuit from a GSLC form. First, we unfuse spiders to create CZ gates and local Cliffords on the inputs and outputs, then apply the rule to the spiders on the right side of the resulting bipartite graph:
gslcextract1prime 
The part of the diagram marked 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. Such a unitary can always be realised via CNOT gates, using standard techniques based on Gaussian elimination over (see e.g. [26]). Hence, we obtain a Clifford circuit with 6 layers of gates:
Local Cliffords + CZs + CNOTs + Hadamards + CZs + Local Cliffords  (11) 
This is similar to the 11layer pseudonormal 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 or for – is fed into our simplification routine, there might still be interior spiders left, and hence the diagram will not be in GSLC form.
Our general strategy for extracting a circuit from a diagram produced by our simplification routine is to progress through the diagram from righttoleft, consuming vertices to the left and introducing quantum gates on the right. For an qubit circuit, we maintain a layer of 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.
For any ZXdiagram , the following equation holds:
cnotpivot 
(12) 
where describes the biadjacency matrix of the relevant vertices, and is the matrix produced by starting with and then adding row 2 to row 1, taking sums modulo 2. Furthermore, if the diagram on the LHS has a focused gFlow, then so does the RHS.
Proof.
See Appendix B.4. ∎
We can now start describing our extraction procedure. To start, let the frontier consist of all the output vertices. If a vertex of the frontier has a nonzero 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 be the gFlow of the diagram. We push the frontier onto new vertices in the following way:

Find an unextracted vertex such that . Such a vertex always exists: take for instance any nonfrontier vertex that is maximal in the gFlow order.

We know that Odd hence we can pick a designated vertex such that is connected to .

Write the biadjency matrix of and their neighbours to the left of the frontier (‘the past’). Because Odd we know that the sum of all the rows in this matrix modulo 2 is an allzero vector except for a 1 corresponding to . Hence, if we apply row operations with as the target from every other vertex in we know that will have only a single vertex connected to it in its past, namely . Do these row operations using Proposition 6. As a result, CNOT gates will be pushed onto the extracted circuit. By that same proposition, the diagram will still have a gFlow.

At this point it might be that is still connected to vertices of that are not . For each of these vertices , do a row operation from to by adding the corresponding CNOT to the extracted diagram in order to disconnect from . At this point ’s only connection to is via . We can therefore conclude that the gFlow of the resulting diagram is such that the only vertex that has is .

The vertex has arity 2 and since it is phaseless we can remove it from the diagram by . Remove from the frontier, and replace it by . By the remarks in the previous point the resulting diagram still has a gFlow. Unfuse the phase of as a phase gate to the extracted part.

If there is any vertex in 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 workedout example we refer to Appendix A.
An interesting thing to note is we do not actually need a precalculated 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 GaussJordan 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: bigexampleextract2 The circuit has been reduced from 195 gates to 41 gates, going via an intermediate representation as a ZXdiagram with 12 spiders. As mentioned in the introduction, this example is available as a Jupyter notebook ‘ demos/examplegtsimp.ipynb’ in the PyZX [23] repository on GitHub.
7 Conclusions and Future Work
We have introduced a terminating rewrite strategy for ZXdiagrams that simplifies Clifford circuits to GSLC form using the graphtheoretic notions of local complementation and pivoting. We have shown how the diagrams produced by the rewrite strategy applied to nonClifford 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 ZXdiagram 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 ancillafree unitary from a circuit containing ancillae. This observation does however not preclude finding larger classes of rewrites and ZXdiagrams such that we can efficiently extract a circuit.
The next step for optimising quantum circuits using ZXdiagrams 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 nonClifford phase gates with certain ZXdiagrams called phase gadgets during the simplification procedure has shown some promise. These phase gadgets enable phases to propegate nonlocally through the circuit, much like they do in phasepolynomial 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
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 nearterm 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 biadjacency matrix of the ZXdiagram. 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.
Acknowledgements. SP acknowledges support from the projects ANR17CE250009 SoftQPro, ANR17CE240035 VanQuTe, PIAGDN/Quantex, and LUE / UOQ. AK and JvdW are supported in part by AFOSR grant FA23861814028.
References
 [1] Scott Aaronson and Daniel Gottesman. Improved simulation of stabilizer circuits. Physical Review A, 70(5):052328, 2004.
 [2] Nabila Abdessaied, Mathias Soeken, and Rolf Drechsler. Quantum circuit optimization by hadamard gate reduction. In International Conference on Reversible Computation, pages 149–162. Springer, 2014.
 [3] Matthew Amy, Dmitri Maslov, and Michele Mosca. Polynomialtime Tdepth optimization of Clifford+ T circuits via matroid partitioning. IEEE Transactions on ComputerAided Design of Integrated Circuits and Systems, 33(10):1476–1489, 2014.
 [4] Matthew Amy and Michele Mosca. Tcount optimization and ReedMuller codes. 2016. arXiv:1601.07363.
 [5] Miriam Backens. The zxcalculus is complete for stabilizer quantum mechanics. New Journal of Physics, 16(9):093021, 2014.
 [6] Miriam Backens. Making the stabilizer zxcalculus complete for scalars. In Chris Heunen, Peter Selinger, and Jamie Vicary, editors, Proceedings of the 12th International Workshop on Quantum Physics and Logic (QPL 2015), volume 195 of Electronic Proceedings in Theoretical Computer Science, pages 17–32, 2015. arXiv:1507.03854, doi:10.4204/EPTCS.195.2.
 [7] André Bouchet. Graphic presentations of isotropic systems. J. Comb. Theory Ser. A, 45:58–76, July 1987. URL: http://dl.acm.org/citation.cfm?id=51355.51360.
 [8] Daniel E Browne, Elham Kashefi, Mehdi Mhalla, and Simon Perdrix. Generalized flow and determinism in measurementbased quantum computation. New Journal of Physics, 9(8):250, 2007. URL: http://stacks.iop.org/13672630/9/i=8/a=250.
 [9] B. Coecke and R. Duncan. Interacting quantum observables: categorical algebra and diagrammatics. New Journal of Physics, 13:043016, 2011. arXiv:quantph/09064725.
 [10] B. Coecke and A. Kissinger. Picturing Quantum Processes. Cambridge University Press, 2014.
 [11] V. Danos and E. Kashefi. Determinism in the oneway model. Phys. Rev. A, 74(052310), 2006. doi:10.1103/PhysRevA.74.052310.
 [12] V. Danos, E. Kashefi, and P. Panangaden. The Measurement Calculus. April 2007. arXiv:0704.1263.
 [13] Vincent Danos, Elham Kashefi, Prakash Panangaden, and Simon Perdrix. Extended Measurement Calculus. Semantic Techniques in Quantum Computation, 2010.
 [14] R. Duncan and S. Perdrix. Graph states and the necessity of Euler decomposition. Mathematical Theory and Computational Practice, pages 167–177, 2009.
 [15] R. Duncan and S. Perdrix. Rewriting measurementbased quantum computations with generalised flow. In Proceedings of ICALP, Lecture Notes in Computer Science, pages 285–296. Springer, 2010.
 [16] Ross Duncan and Simon Perdrix. Pivoting makes the ZXcalculus complete for real stabilizers. In Proceedings of the 10th International Workshop on Quantum Physics and Logic (QPL), volume 171 of Electronic Proceedings in Theoretical Computer Science, pages 50–62. Open Publishing Association, 2014. doi:10.4204/EPTCS.171.5.
 [17] Andrew Fagan and Ross Duncan. Optimising Clifford Circuits with Quantomatic. In Proceedings of the 15th International Conference on Quantum Physics and Logic (QPL), volume 287 of Electronic Proceedings in Theoretical Computer Science, pages 85–105. Open Publishing Association, 2019. doi:10.4204/EPTCS.287.5.
 [18] Amar Hadzihasanovic, Kang Feng Ng, and Quanlong Wang. Two complete axiomatisations of purestate qubit quantum computing. In Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science, LICS ’18, pages 502–511, New York, NY, USA, 2018. ACM. doi:10.1145/3209108.3209128.
 [19] Marc Hein, Wolfgang Dür, Jens Eisert, Robert Raussendorf, M Nest, and HJ Briegel. Entanglement in graph states and its applications. 2006. arXiv:quantph/0602096.
 [20] Luke E Heyfron and Earl T Campbell. An efficient quantum compiler that reduces T count. Quantum Science and Technology, 4(015004), 2018. doi:10.1088/20589565/aad604.
 [21] Emmanuel Jeandel, Simon Perdrix, and Renaud Vilmart. A Complete Axiomatisation of the ZXCalculus for Clifford+T Quantum Mechanics. In Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science, LICS ’18, pages 559–568, New York, NY, USA, 2018. ACM. doi:10.1145/3209108.3209131.
 [22] Emmanuel Jeandel, Simon Perdrix, and Renaud Vilmart. Diagrammatic Reasoning Beyond Clifford+T Quantum Mechanics. In Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science, LICS ’18, pages 569–578, New York, NY, USA, 2018. ACM. doi:10.1145/3209108.3209139.
 [23] Aleks Kissinger and John van de Wetering. PyZX: A circuit optimisation tool based on the ZXcaculus. http://github.com/Quantomatic/pyzx.
 [24] Vadym Kliuchnikov and Dmitri Maslov. Optimization of Clifford circuits. Phys. Rev. A, 88:052307, Nov 2013. doi:10.1103/PhysRevA.88.052307.
 [25] Anton Kotzig. Eulerian lines in finite 4valent graphs and their transformations. In Colloqium on Graph Theory Tihany 1966, pages 219–230. Academic Press, 1968.
 [26] Ketan Markov, Igor Patel, and John Hayes. Optimal synthesis of linear reversible circuits. Quantum Information and Computation, 8(3&4):0282–0294, 2008.
 [27] Mehdi Mhalla, Mio Murao, Simon Perdrix, Masato Someya, and Peter S Turner. Which graph states are useful for quantum information processing? In Conference on Quantum Computation, Communication, and Cryptography, pages 174–187. Springer, 2011.
 [28] Yunseong Nam, Neil J Ross, Yuan Su, Andrew M Childs, and Dmitri Maslov. Automated optimization of large quantum circuits with continuous parameters. npj Quantum Information, 4(1):23, 2018.
 [29] Maarten Van Den Nest. Classical simulation of quantum computation, the gottesmanknill theorem, and slightly beyond. Quantum Information & Computation, 10(3):258–271, 2010.
 [30] Renaud Vilmart. A NearOptimal Axiomatisation of ZXCalculus for Pure Qubit Quantum Mechanics. 2018. arXiv:1812.09114.
Appendix A Example derivation
In this section we will work through a complete example of our simplification algorithm.
Let us start with the following circuit: complexample1 This has 5 twoqubit gates and 19 singlequbit gates. Using the procedure of Lemma 3 to get it in graphlike form we get: complexample2
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. complexample3 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: complexample4 Applying the pivots to the designated vertices gives us: complexample5 Note that in the bottom qubit we end up where we started before doing the pivots, but now we have marked the vertex as a local Clifford, and hence the vertex now counts as a boundary vertex. This means the simplification procedure has ended. The final step is extracting a circuit: complexample6a 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: complexample6b We now simply repeat this procedure until the entire circuit is extracted. complexample6c
The extracted circuit has 4 twoqubit gates and 18 singlequbit gates. Applying some final cancellations between Hadamards on the inputs and outputs gets the number of singlequbit gates down to 14: complexample7
Appendix B Proofs
b.1 Derived Rules in the ZXcalculus
The antipode rule, hopfrule is derivable in the ZXcalculus.
Proof.
To derive the rule we take advantage of the freedom to deform the diagram as shown below. hopfproof The final equation is simply dropping the scalar. ∎
The copy rule, picopyrule is derivable in the ZXcalculus.
Proof.
To derive the rule we use the rule first to split the labelled vertex, and the and rules do the bulk of the work as shown below: picopyproof ∎
Selfloops can be removed in the ZXcalculus:
Proof.
The first one follows by applying from right to left and then using . For the second one we do:
The first step is simply the definition of the Hadamard box, and in the last step we implicitly drop the scalar X spider that we are left with. ∎
Parallel Hadamard edges can be removed in the ZXcalculus: paredgerem
Proof.
∎
b.2 Circuits and focused gFlow
To show that graphlike ZXdiagrams arising from circuits admit a focused gFlow, we introduce a simpler sufficient condition.
[11] Given an open graph , a causal flow on consists of a function and a partial order on the set satisfying the following properties:



if then
where means that and are adjacent in the graph.
Like focused gFlow, causal flow is a special case of a more general property called gFlow:
Given an open graph , a generalised flow, or gFlow consists of a pair such that for any :


if , then

if and , then
In the case where all of the sets 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 . 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.
If an open graph admits a causal flow , then it also admits a focused gFlow.
The following proof of Lemma 3 shows that the diagrams produced by making a circuit graphlike according to the procedure outlined in Lemma 3 have causal flow and hence a focused gFlow.
Proof of Lemma 3.
Let denote the circuit, and let denote the diagram produced by applying the procedure of Lemma 3 to .
In order to prove that has a focused gFlow, it suffices to show that it admits a causal flow, by Theorem B.2.
With every spider of the circuit we associate a number specifying on which ‘qubitline’ it appears. We also associate a ‘rownumber’ specifying how ‘deep’ in the circuit it appears. Suppose now that in . If they are on the same qubit, so , then necessarily . Conversely, if they are on different qubits, , then they must be part of a CZ or CNOT gate, and hence .
In the diagram resulting from Lemma 3, every spider arises from fusing together adjacent spiders on the same qubit line from the original diagram. For a spider in we can thus associate two numbers , and , where is the lowest rownumber of a spider fused into , and is the highest. Spider fusion from only happens for spiders on the same qubitline, and hence also inherits a from all the spiders that got fused into it. Any two spiders and in with must have been produced by fusing together adjacent spiders on the same qubitline, and hence we must have or , depending on which of the spiders is most to the left. If instead and , then their connection must have arisen from some CNOT or CZ gate in , and hence the intervals and must have overlap, so that necessarily and .
Now we let be the set of spiders connected to an output, and the set of spiders connected to an input in . For all we let be the unique connected spider to the right of on the same qubitline. We define the partial order as follows: if and only if or . It is straightforward to check that this is indeed a partial order.
By construction and the property , follows from discussed above, so let us look at the third property of causal flow.
Suppose . We need to show that . If this is trivial so suppose . First suppose that (which is also equal to ). has a maximum of two neighbours on the same qubitline, and since one of them is , this can only be if and hence , and we are done. So suppose that . By the discussion above, we must then have and . Since we also have we get so that indeed . ∎
b.3 Preservation of focused gFlow
Throughout this section, we will rely extensively on the symmetric difference of sets: . This extends to an ary operation 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: Given a graph , and ,
Proof.
First notice that is linear: , . Moreover , the neighbourhood of in . As a consequence, .
The local complementation is acting as follows on the neighbourhoods:
As a consequence,
Notice that iff . Hence, if , . Otherwise, if , .
∎
If is a focused gFlow for then is a focused gFlow for where is inductively defined as
where .
Proof.
If is a focused gFlow for then is a focused gFlow for where is inductively defined as
where .
First of all, can indeed be defined inductively by starting at the maximal elements in the partial order, since for any , so that , and similarly when , so implies hence . That preserves the order, i.e. , follows similarly. Moreover, it is easy to show that , . It remains to show that , or equivalently, where .
First notice that for any , , so, according to Lemma B.3.1, and using the linearity of and the distributivity of over ,
Moreover, , so
(Lemma B.3.1)  
As a consequence,
Now, we prove that by induction on starting at maximal elements in the order.
Let be maximal for i.e. , . By maximality we must have and hence . If , it implies that . If , then , so is maximal, so and , as a consequence is also equal to in this case.
By induction, for any ,

If ,

If ,
∎
If is a focused gFlow for then is a gFlow for where is defined as
Notice that is not necessarily a focused gFlow.
b.3.2 Pivoting
The action of the pivoting on the oddneighbourhood can be described as follows: Given a graph , , , and ,
(13) 
where and are the closed neighbourhood and the closed odd neighbourhood respectively.
Proof.
The pivoting acts as follows on neighbourhoods:
As a consequence,
Notice that iff , and similarly for . Hence we obtain (13) by case distinction.∎
If is a focused gFlow for then is a focused gFlow for where , .
Proof.
If is a focused gFlow for then is a focused gFlow for where is defined as follows:
It remains to show that