There and back again: A circuit extraction tale

Translations between the quantum circuit model and the measurement-based one-way model are useful for verification and optimisation of quantum computations. They make crucial use of a property known as gflow. While gflow is defined for one-way computations allowing measurements in three different planes of the Bloch sphere, most research so far has focused on computations containing only measurements in the XY-plane. Here, we give the first circuit-extraction algorithm to work for one-way computations containing measurements in all three planes and having gflow. The algorithm is efficient and the resulting circuits do not contain ancillae. One-way computations are represented using the ZX-calculus, hence the algorithm also represents the most general known procedure for extracting circuits from ZX-diagrams. In developing this algorithm, we generalise several concepts and results previously known for computations containing only XY-plane measurements. We bring together several known rewrite rules for measurement patterns and formalise them in a unified notation using the ZX-calculus. These rules are used to simplify measurement patterns by reducing the number of qubits while preserving both the semantics and the existence of gflow. The results can be applied to circuit optimisation by translating circuits to patterns and back again.


Introduction
The gate-based model and the measurement-based model are two fundamentally different approaches to implementing quantum computations. In the gate-based model [42], the bulk of the computation is performed via unitary (i.e. reversible) one-and two-qubit gates. Measurements serve mainly to read out data and may be postponed to the end of the computation. Conversely, in the measurement-based model, the bulk of the computation is performed via measurements on some general resource state, which is independent of the specific computation. We focus here on the one-way model [44], where the resource states are graph states (see Section 2.2). In this paper, we study ways of converting between these two different approaches to quantum computation with a view towards optimizing the implementation of both. While computations in the gate-based model are represented as quantum circuits, computations in the one-way model are usually represented by measurement patterns, which describe both the graph state and the measurements performed on it [16,17]. Measurement patterns in the one-way model generally do not allow arbitrary single-qubit measurements. Instead, measurements are often restricted to the 'planes' of the Bloch sphere that are orthogonal to the principal axes, labelled the XY-, XZ-, and YZ-planes. In fact, most research has focused on measurements in just the XY-plane, which alone are sufficient for universal quantum computation [16]. Similarly, measurements in the XZ-plane are also universal [38], although this model has been explored less in the literature. In this paper, we will consider measurements in all three of the planes, since this usually leads to patterns involving fewer qubits, and allows for more non-trivial transformations of the graph state.
Due to the non-deterministic nature of quantum measurements, a one-way computation needs to be adaptive, with later measurement angles depending on the outcomes of earlier measurements [44]. While the ability to correct undesired measurement outcomes is necessary for obtaining a deterministic computation, not all sequences of measurements support such corrections. The ability to perform a deterministic computation depends on the underlying graph state and the choice of measurement planes, which together form an object called a labelled open graph. If all measurements are in the XY-plane, causal flow (sometimes simply called 'flow') is a sufficient condition for the labelled open graph to support deterministically implementable patterns [15]. Yet causal flow is not necessary for determinism. Instead, the condition of generalized flow (or gflow) [9] is both sufficient and necessary for deterministic implementability. Gflow can be defined for labelled open graphs containing measurements in all three planes, in which case it is sometimes called extended gflow [9,Theorems 2 & 3].
A given representation of some computation can be transformed into a different representation of the same computation using local rewriting. The new representation may be chosen to have more desirable properties. For quantum circuits, such desirable properties include low depth [2], small total gate count [36], or small counts of some particular type of gate, such as the T-gate [3]. For measurement patterns, desirable properties include a small number of qubits [24,29] or a particularly simple underlying graph state [38].
Local processes can also be used to translate a pattern into a circuit. This is used, for example, to verify that the pattern represents the desired operation [13,15,18,21,40]. Conversely, a translation of a circuit into a pattern can be used to implement known algorithms in the one-way model, or it can be combined with a translation back to a circuit to trade depth against width, to parallelise Clifford operations, or to reduce the number of T gates [8,14,28]. No complete set of rewrite rules is known for quantum circuits or for measurement patterns, although a completeness result does exist for 2-qubit circuits over the Clifford+T gate set [7].
Rewriting of patterns or circuits, as well as translations between the two models, can be performed using the ZX-calculus, a graphical language for quantum computation [10]. This language is more flexible than quantum circuit notation and also has multiple complete sets of graphical rewrite rules [25,30,31,46]. While translating a measurement pattern to a quantum circuit can be difficult, the translation between patterns and ZX-diagrams is straightforward [21,23,33].

Our contributions
In this paper, we give an algorithm that extracts a quantum circuit from any measurement pattern whose underlying labelled open graph has extended gflow. Our algorithm does not use ancillae. This is the first circuit extraction algorithm for extended gflow, i.e. where patterns may contain measurements in more than one plane. The algorithm works by translating the pattern into the ZX-calculus and transforming the resulting ZX-diagram into a circuit-like form. It generalises a similar algorithm, which works only for patterns where all measurements are in the XY-plane [23]. The circuit extraction algorithm employs the ZX-calculus, so it can be used not only on diagrams arising from measurement patterns but on any ZX-diagram satisfying certain properties. Thus, this procedure is not only the most general circuit extraction algorithm for measurement patterns but also the most general known circuit extraction algorithm for ZX-calculus diagrams.
In developing the circuit extraction algorithm, we derive a number of explicit rewrite rules for ZX-diagrams representing measurement patterns, in particular rewrites that involve graph transformations on the underlying resource state. We show how the gflow changes for each of these rewrite rules, i.e. how the rewrites affect the instructions for correcting undesired measurement outcomes. These rewrite rules unify and formalise several rules that were previously employed in the literature in a more ad-hoc manner, e.g. the pivot-minor transformation in Ref. [38] or the elimination of Clifford measurements first derived in a different context in Ref. [27]. The rewrite rules serve not only to prove the correctness of the algorithm, but also to simplify the measurement patterns by reducing the number of qubits involved. Combining the different rules allows us to remove any qubit measured in a Pauli basis, while maintaining deterministic implementability. This shows that the number of qubits needed to perform a measurement-based computation is directly related to the number of non-Clifford operations required for the computation.
We also generalise several concepts originally developed for patterns containing only XY-plane measurements to patterns with measurements in multiple planes. In particular, we adapt the definitions of focused gflow [39] and maximally delayed gflow [37] to the extended gflow case. Our generalisation of focused gflow differs from the three generalisations suggested by Hamrit and Perdrix [26]; indeed, the desired applications naturally lead us to one unique generalisation (see Section 3.3).
Combined with the known procedure for transforming a quantum circuit into a measurement pattern using the ZX-calculus [23], our pattern simplification and circuit extraction procedure can be used to reduce the T-gate count of quantum circuits. This involves translating the circuit into a ZX-calculus diagram, transforming to a diagram which corresponds to a measurement pattern, simplifying the pattern, and then re-extracting a circuit. A rough overview of the different translation and optimisation procedures is given in Figure 1.
The remainder of this paper is structured as follows. Known definitions and results relating to ZX-calculus, measurement patterns and gflow are presented in Section 2. We derive the rewrite rules for extended measurement patterns and the corresponding gflow transformations in Section 3. These results are used in Section 4 to simplify measurement patterns in various ways. Then in Section 5, we demonstrate the algorithm for extracting a circuit from a measurement pattern whose underlying labelled open graph has extended gflow. The conclusions are given in Section 6.

Preliminaries
We give a quick overview over the ZX-calculus in Section 2.1 and introduce the one-way model of measurement-based quantum computing in Section 2.2. The graph-theoretic operations of local complementation and pivoting (on which the rewrite rules for measurement patterns are based) and their representation in the ZX-calculus are presented in Section 2.3. Section 2.4 contains the definitions and properties of different notions of flow.

The ZX-calculus
The ZX-calculus is a diagrammatic language similar to the widely-used quantum circuit notation. We will provide only a brief overview here, for an in-depth reference see [11].
A ZX-diagram consists of wires and spiders. Wires entering the diagram from the left are called input wires; wires exiting to the right are called output wires. Given two diagrams we can compose them horizontally (denoted •) by joining the output wires of the first to the input wires of the second, or form their tensor product (denoted ⊗) by simply stacking the two diagrams vertically.
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. 1 Written explicitly in Dirac 'bra-ket' notation, these linear maps are: It will be convenient to introduce a symbol -a yellow square -for the Hadamard gate. This is defined by the equation: 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.
:= 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.
Definition 2.2. The interpretation of a ZX-diagram D is the linear map that such a diagram represents and is written D . For a full treatment of the interpretation of a ZX diagram see, e.g. Ref. [31]. We say two ZX-diagrams D 1 and D 2 are equivalent when D 1 = z D 2 for some non-zero complex number z. 1 If you are reading this document in monochrome or otherwise have difficulty distinguishing green and red, Z spiders will appear lightly-shaded and X spiders will appear darkly-shaded.
We define equivalence up to a global scalar, as those scalars will not be important for the class of diagrams we study in this paper.

Remark 2.3.
There are many different sets of rules for the ZX-calculus. The version we present only preserves equality up to a global scalar. Versions of the ZX-calculus where equality is 'on the nose' can be found in Ref. [5] for the stabiliser fragment, in Ref. [31] for the Clifford+T fragment, and in Ref. [30,41] for the full language.
The interpretation of a ZX-diagram is invariant under 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 2. Two diagrams are equivalent if one can be transformed into the other using the rules of the ZX-calculus. ...
... Figure 2: 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. Note the ellipsis should be read as '0 or more', hence the spiders on the LHS of (f ) are connected by one or more wires.

Remark 2.4.
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 2 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 [31], where phases are multiples of π/4, and for all ZX-diagrams [25,30,46].
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 choose this gate set because it admits a convenient representation in terms of spiders: These gates have the following interpretations: For the CNOT gate, the green spider is the first (i.e. control) qubit 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: Remark 2.5. Note that the directions of the wires in the depictions of the CNOT and CZ gates are irrelevant, hence we can draw them vertically without ambiguity. Undirected wires are a general feature of ZX-diagrams, and from hence forth we will ignore wire directions without further comment. We will also freely draw inputs/outputs entering or exiting the diagram from arbitrary directions if the meaning is clear from context (as for example in Example 2.43).
For our purposes, we will define quantum circuits as a special case of ZX-diagrams.

Definition 2.6.
A circuit is a ZX-diagram generated by composition and tensor products of the ZX-diagrams in equations (2) and (3). The interpretation of such a circuit is given by the composition and tensor products of the interpretations of the generating diagrams given in equation (2), in accordance with: 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]. A unitary is local Clifford if it arises from a single-qubit Clifford circuit, i.e. a composition of H and S gates. The addition of T gates to Clifford circuits 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 [42].

Measurement-based quantum computation
Measurement-based quantum computing (MBQC) is a universal model for quantum computation, underlying the one-way quantum computing scheme [43]. The basic resource for MBQC is a graph state, built from a register of qubits by applying CZ-gates pairwise to obtain an entangled quantum state. 2 The graph state is then gradually consumed by measuring single qubits. By choosing an appropriate resource state and appropriate measurements, any quantum computation can be performed. The difficulty is the nondeterminism inherent in quantum measurements, which means computations need to be adaptive to implement deterministic operations.
Measurement-based quantum computations are usually formalised in terms of measurement patterns, a syntax describing both the construction of graph states and their processing via successive measurements. In the following, we first present measurement patterns, and then introduce other -and, in our opinion, simpler -formalisms for reasoning about these computations. Instead of allowing arbitrary single-qubit measurements, measurements are usually restricted to three planes of the Bloch sphere, labelled XY, XZ, and YZ. For each measurement, the state denoted '+' is taken to be the desired result of the measurement and the state denoted '−' is the undesired result, which will need to be adaptively corrected. The allowed measurements are ( [17, p. 292]): where 0 ≤ α ≤ 2π. Usually, the desired measurement outcome is labelled 0 and the undesired measurement outcome is labelled 1.

Definition 2.7.
A measurement pattern consists of an n-qubit register V with distinguished sets I, O ⊆ V of input and output qubits and a sequence of commands consisting of the following operations: • Preparations N i , which initialise a qubit i / ∈ I in the state |+ .
• Entangling operators E ij , which apply a CZ-gate to two distinct qubits i and j.
• Corrections [X i ] s , which depend on a measurement outcome (or a linear combination of measurement outcomes) s ∈ {0, 1} and act as the Pauli-X operator on qubit i if s is 1 and as the identity otherwise, • Corrections [Z j ] t , which depend on a measurement outcome (or a linear combination of measurement outcomes) t ∈ {0, 1} and act as the Pauli-Z operator on qubit j if t is 1 and as the identity otherwise.
We will only consider runnable patterns, which satisfy certain conditions ensuring they are implementable in practice. Definition 2.8 ([9, p. 4]). A measurement pattern is runnable if the following conditions hold.
• No correction depends on an outcome not yet measured.
• All non-input qubits are prepared.
• All non-output qubits are measured.
• A command C acts on a qubit i only if i has not already been measured, and one of (1)-(3) holds: (1) i is an input and C is not a preparation, (2) i has been prepared and C is not a preparation, (3) i has not been prepared, i is not an input, and C is a preparation.
Runnable measurement patterns can be standardized [9, p. 4], so that all preparations N i appear first, then all the entangling operators E ij , then the measurements M λ,α i and finally the corrections. The entangling operators E ij in a pattern determine the resource graph state. In fact, they correspond to the edges of the underlying labelled open graph. This is formalised in the following definitions and remark, which will be used throughout the paper. Given the measurement pattern, the corresponding labelled open graph (G, I, O, λ) may be determined as follows: the vertices of the graph G are given by the set of qubits V . The edges of G are given by the set and E ij appears in the pattern}.
The sets I and O are the ones given in Definition 2.7. The functions λ and α are determined by the measurement operators M λ,α i ; Definition 2.8 guarantees that both are well-defined. Given a labelled open graph we can apply this process in reverse to construct a standardised runnable measurement pattern without corrections. (In the absence of corrections, the order of the individual preparation commands, entangling commands, and measurement commands in the pattern does not matter since all commands of the same type commute. ) A labelled open graph and an assignment of measurement angles can also be determined from a measurement pattern including corrections by simply ignoring the latter (i.e. by assuming that all measurements yield the desired result). In Section 2.4, we discuss necessary and sufficient conditions under which appropriate corrections commands can be determined when constructing a measurement pattern from a labelled open graph; these corrections then put constraints on the order of the measurements.
In general, a single measurement pattern can result in a variety of measurement instructions, with each instruction depending on earlier measurement outcomes and the resulting corrections that must be applied. We are, however, interested in the subset of measurement patterns that always implement the same linear map, regardless of the measurement outcomes. For these patterns, we can identify the unique linear map implemented by the pattern with the set of measurement instructions obtained when all the measurement outcomes are 0 (and thus no corrections are necessary). This leads us to the following definition: where E G := i∼j E ij and N I := i∈I N i .   Table 1. The elements are composed in the obvious way and any sets of adjacent phase-free green spiders other than measurement effects are merged. In other words, merging affects green spiders which come from the translation of a preparation or entangling command. Proof. For each operator M in Table 1 and its corresponding diagram D M , it is straightforward to check that D M = M . The result thus follows by the compositional properties of the interpretation · and the fact that rewriting ZX-diagrams preserves semantics.
In order to specify a converse direction to this result, we will define a special class of ZX-diagrams. Before we do that, we recall which ZX-diagrams correspond to graph states. The graph corresponding to a graph state diagram is the graph whose vertices are the green spiders of the ZX-diagram and whose edges are given by the Hadmard edges of the ZX-diagram.

Definition 2.18.
A ZX-diagram is in MBQC form if it consists of a graph state diagram in which each vertex of the graph may also be connected to: • an input (in addition to its output), and • a measurement effect (in one of the three measurement planes) instead of the output. Proof. Consider performing the translation described in Definition 2.14 in two steps. The first step involves translating the preparation and entangling commands of the linear map M Γ,α according to Table 1 and then merging any sets of adjacent green spiders. This yields a graph state diagram with some additional inputs. (The underlying graph is G.) The second step is the translation of the measurement projections of M Γ,α . This yields measurement effects on some of the outputs of the graph state diagram. Thus, the resulting ZX-diagram is in MBQC form by Definition 2.18.
The converse of Lemma 2.20 also holds.  It will turn out to be useful to consider a 'relaxed' version of the MBQC form for ZX-diagrams. Definition 2.23. We say a ZX-diagram is in MBQC+LC form when it is in MBQC form (see Definition 2.18) up to arbitrary single-qubit Clifford unitaries on the input and output wires (LC stands for 'local Clifford'). When considering the underlying graph of a ZX-diagram in MBQC+LC form, we ignore these single qubit Clifford unitaries.
Note that an MBQC form diagram is an MBQC+LC form diagram with trivial singlequbit unitaries on its inputs and outputs. An example diagram in MBQC+LC form is given in Figure 3.

Graph-theoretic rewriting
The rewrites we will use are based on the graph-theoretic notions of local complementation and pivoting. We present these operations (and their effects) in Definitions 2.24 and 2.25 as they appear in Ref. [22]. Our interest is in the effect these operations have on a measurement pattern. In particular, we consider whether a ZX-diagram in MBQC form will remain in MBQC form after applying a local complementation or pivot (or remain close enough to MBQC form to still be useful). In other words, G u is a graph that has the same vertices as G. Two neighbours b and c of u are connected in G u if and only if they are not connected in G. All other edges are the same as in G. Definition 2.25. Let G = (V, E) be a graph and u, v ∈ V two vertices connected by an edge. The pivot of G about the edge u ∼ v is the operation resulting in the graph G ∧ uv := G u v u.
If we denote the set of vertices connected to both u and v by A, the set of vertices connected to u but not to v by B and the set of vertices connected to v but not to u by C, then pivoting consists of interchanging u and v and complementing the edges between each pair of sets A, B and C. That is, a vertex in A is connected to a vertex in B after pivoting if and only if the two vertices are not connected before pivoting; and similarly for the two other pairs. All the remaining edges are unchanged, including the edges internal to A, B and C. We illustrate this by the following picture, where crossing lines between two sets indicate complementing the edges.
Remark 2.26. From the above characterisation it follows that pivoting is symmetric in the (neighbouring) vertices, that is, G ∧ uv = G ∧ vu.
In the ZX-calculus, a spider with a zero phase and exactly two incident wires is equivalent to a plain wire (representing the identity operation) by rule (i1) in Figure 2. The following definition represents the corresponding graph operation, which will be used to remove such vertices. Definition 2.27. Let G = (V, E) be a graph and let u, v, w ∈ V be vertices such that N G (v) = {u, w} and u / ∈ N G (w), that is, the neighbours of v are precisely u and w, and u is not connected to w. We then define identity removal as Since v has exactly two neighbours, one of which is w, the choice of u is implicit in the notation. We think of this as 'dragging u along v to merge with w'.
The effect of the identity removal is to remove the middle vertex v and to fuse the vertices u and w into one, as illustrated in the picture below. Thus the operation is symmetric in u and w, in the sense that G v u and G v w are equal up to a relabelling of one vertex. Note that u and w are allowed to have common neighbours (which will disconnect from the fused vertex w as a result of identity removal).
Example 2.28. Consider the following graph: Note that the vertices u, v and w satisfy the condition for identity removal: u and w are not connected and are precisely the neighbours of v. Hence identity removal results in the graph Remark 2.29. If we have vertices u, v and w with N G (v) = {u, w} but, unlike in the definition above, u ∈ N G (w), we can first perform a local complementation on v, so that u and w become disconnected, and then remove the identity vertex v. In symbols: The abstract application of a local complementation to a graph corresponds to the application of a specific set of local Clifford gates on the corresponding graph state: ([45], in the manner of [20,Theorem 2]). Let G = (V, E) be a graph with adjacency matrix θ and let u ∈ V , then This result can be represented graphically in the ZX-calculus: ([20,Theorem 3]). The following equality involving graph state diagrams and Clifford phase shifts follows from the graphical rewrite rules: Here, the underlying graph on the LHS is G u and the underlying graph on the RHS is G. Any vertices not adjacent to u are unaffected and are not shown in the above diagram.
Combining this result with the definition of pivoting in terms of local complementations (cf. Definition 2.25) we also get: Here u and v are a connected pair of vertices, and the underlying graph on the LHS is G ∧ uv while the RHS is G. Any vertices not adjacent to u or v are unaffected and are not shown in the above diagram.

Generalised flow
The notion of flow or causal flow on open graphs was introduced by Danos and Kashefi [15] as a sufficient condition to distinguish those open graphs capable of supporting a deterministic MBQC pattern with measurements in the XY-plane. Causal flow, however, is not a necessary condition for determinism. That is there are graphs that implement a deterministic pattern even though they do not have causal flow [9,21]. Browne et al. [9] adapted the notion of flow to what they called generalised flow (gflow), which is both a necessary and sufficient condition for 'uniformly and strongly stepwise deterministic' measurement patterns (defined below). Unlike causal flow, gflow can also be applied to arbitrary labelled open graphs, i.e. it supports measurement patterns with measurements in all three planes. This even more general case is sometimes called extended gflow. The existence of gflow implies the uniform, strong and stepwise determinism of any pattern on the open graph (cf. Theorem 2.39 below). Hence, by applying transformations to an open graph that preserve the existence of a gflow, we ensure that the modified open graph still supports a uniformly, strongly and stepwise deterministic pattern. Note that the condition of interest is preservation of the existence of gflow, not preservation of the specific gflow itself.
We will now give the formal definitions of (causal) flow and (extended) gflow. • u ∼ f (u) When vertices are smaller than a vertex v in the order ≺, they are referred to as being 'behind' or 'in the past of' of v.
The notion of gflow differs from the above definition of causal flow in two ways. The value of f (u) is allowed to be a set of vertices instead of a single vertex, so that corrections can be applied to more than one vertex at a time. As a result of this change, the third condition of causal flow is now too strong: requiring that no element of f (u) is adjacent to any vertex 'in the past' of u would be too restrictive. Since corrections are applied to sets of vertices at a time, it is possible to make use of the following idea: if corrections are simultaneously applied to an even number of neighbours of v, then there is no net effect on v. Thus, the second change takes the form of a parity condition: all vertices in the neighbourhood of f (u) that lie 'in the past' of u are required to be in the even neighbourhood of f (u). As a result, net effects of corrections do not propagate into 'the past'.
Allowing measurements in more than one measurement plane requires further careful adjustment of the parity conditions depending on the measurement plane of the vertex being measured.
i.e. the set of vertices having an odd number of neighbours in K. If the graph G is clear from context, we simply write Odd (K). The even neighbourhood of K in G, Even G (K), is defined in a similar way; The set g(v) is called the correction set of v.

Remark 2.38.
In the original definition of gflow in Ref. [9], condition (g2) is given as: In other publications, such as Ref. [17], the definition is changed to the version we give as (g2), yet this is usually done without comment. For completeness, we provide an example which demonstrates that the condition (4) is insufficient for determinism. Consider the following open graph: Here, the set of inputs is {i 1 , i 2 }, the set of outputs is {o 1 , o 2 }, and the non-outputs are measured in the planes λ(i 1 ) = λ(i 2 ) = XY. If we choose both measurement angles to be 0, it is straightforwardly checked that this diagram implements the linear map: This has rank 2 and thus is not invertible, and in particular not unitary. It therefore cannot be deterministically implementable and hence it should not have a gflow. However, it is considered to have a gflow under condition (4) instead of (g2): pick the partial order It is then easily checked that (g, ≺) satisfies conditions (g1), (4), and (g3)-(g5). Yet (g, ≺) does not satisfy condition (g2) and hence is not a gflow under the revised definition.
To demonstrate that, with condition (g2), the presence of a gflow indeed guarantees determinism of a pattern, we give a detailed proof of the following sufficiency theorem, which was first stated in Ref. [9] as Theorem 2 with a sketch proof. The precise statement of the theorem requires some additional notation and the proof is quite lengthy and technical, so we state a coarse version of the theorem here and refer the reader to Appendix A (and more specifically to Theorem A.5) for the details. Proof. We present the proof sketch here, a complete treatment of the proof can be found in Ref. [17,Theorem 7.9.7]. The proof is by induction on the number of measurements. If the number of measurements is n = 0, then the pattern trivially has a gflow. Suppose the pattern has n + 1 qubits to be measured. Since the pattern is assumed to be stepwise deterministic, after performing the first measurement, the remaining pattern is still stepwise, uniformly and strongly deterministic. Hence it has a gflow by the induction hypothesis, where the partial order is given by the order in which the measurements are performed.
It remains to extend this gflow to include the first qubit to be measured. The essential part of this is to find a subset S ⊆ I that can act as the correction set of the first measurement (cf. Definition 2.36). Given such a subset S, we define the full gflow as: where g is the gflow of the smaller pattern.
It is not straightforward to actually find a concrete gflow from the procedure in this proof. A constructive algorithm has since been given in Ref. [39], which finds gflow on labelled open graphs where all measurements are in the XY plane. In Section 3.2, we extend this algorithm to find extended gflow on labelled open graphs with measurements in all three planes.
Gflow is a property that applies to labelled open graphs. For convenience we also define it for ZX-diagrams. The following result from Ref. [23] shows that any unitary circuit can be converted into a deterministic measurement pattern. Proposition 2.42 ([23, Lemma 3.7]). Given a circuit there is a procedure for converting it into an equivalent measurement pattern. Furthermore, this measurement pattern only contains XY-plane measurements and has causal flow, so it also has gflow.
Note that Ref. [23] does not explicitly talk about measurement patterns. What they call graph-like diagrams however correspond in a straightforward manner to diagrams in MBQC form with every measured vertex being measured in the XY-plane. (We also note that the procedure of Proposition 2.42 takes O(n 2 ) operations, where n is the number of vertices.) Below, we present a concrete example illustrating how the presence of gflow allows measurement errors to be corrected as the computation progresses. where a is an input, e and f are outputs, and the measurement planes are given by λ(a) = λ(b) = XY, λ(c) = XZ and λ(d) = YZ. As usual, we denote the measurement angles by α : V → [0, 2π), where V is the vertex set of Γ. Using Definition 2.14 (that is, we translate according to Table 1), we obtain the corresponding ZX-diagram: Note that the labelled open graph we started with has a gflow (g, ≺) given by the following partial order with the function g taking the values

It follows that we have
Odd (g(a)) = {a, c, d, e} from which it is easy to verify that the conditions (g1)-(g5) hold. By Theorem 2.39, the presence of gflow guarantees that we can correct the measurement errors provided that we measure the qubits according to the partial order. We demonstrate this for Γ in Figure 4.
Thus suppose a measurement error of π occurs when performing the measurement corresponding to the vertex c, as indicated in the top left part of Figure 4. The labels in this figure refer to the rules in Figure 2. In order to get the left diagram on the second row, we move each red π-phase past the corresponding Hadamard gate, which changes the colour of the phase to green. For the right diagram, the left green π travels past the green node on the left and flips the sign of α(d). Next, to obtain the left diagram on the third row, the middle green π travels along the middle triangle and past another Hadamard gate to become a red π. Finally, in the bottom left diagram, the red π on the left has been fused with −α(d); and the red π on the right has passed through the Hadamard gate switching its colour to green, and the adjacent green nodes have fused into a node with phase π − π 2 = π 2 . We then rearrange the diagram so that it looks like a measurement pattern again.
Note that all the vertices that are affected by the error are above c in the partial order and hence 'not yet measured' at this stage of the computation. Thus the necessary corrections may be applied to these vertices when they are measured. Figure 4: Propagation of a measurement error of the pattern in Example 2.43.   This follows immediately from the proofs of Ref. [39, but it is not explicitly stated in that paper.

Rewriting while preserving the existence of gflow
In this section, we study a variety of topics dealing with labelled open graphs and gflow. In the first subsection, we show how certain graph operations, such as local complementation and pivoting, affect the gflow. In Section 3.2 we give a polynomial time algorithm for finding extended gflow using the concept of maximally delayed gflow. We combine this notion with that of a focused extended gflow in Section 3.3 to transform a given gflow to give it certain useful properties.

Graph operations that preserve the existence of gflow
In this section, we prove some of our main technical lemmas, establishing that local complementation and related graph rewrites interact well with the gflow of the graph.
First, we show that a labelled open graph resulting from the local complementation of a labelled open graph with gflow will also have a gflow.
The proof of this lemma can be found in Appendix B. Note that the condition that the complemented vertex is not an output can in fact be dropped: Proof. The proof is basically the same as that of Lemma 3.1 if we take g(u) and g (u) to be empty. The output vertex has no label, so its label does not need to be updated. Now by applying this lemma three times we see that a pivot also preserves the existence of a gflow.
For more details regarding the correctness of this corollary, we refer to Appendix B. Somewhat surprisingly, the deletion of some types of vertices preserves the existence of gflow: Proof. First, observe that u ∈ g(u) since λ(u) = XY. Thus u ∈ g (v) for either case of the definition. Hence, g is indeed a function on the graph G \ u. To check that g is indeed a gflow we check the necessary conditions for all Since the labels of all the vertices stay the same, (g3)-(g5) remain satisfied.
Remark 3.5. The condition that λ(u) = XY is necessary in the previous lemma. Removing a vertex with label XY will, in general, lead to a labelled open graph which no longer has a gflow. For instance consider the following labelled open graph: where the first two vertices both have label XY. This graph has a gflow specified by I ≺ u ≺ O and g(I) = {u}, g(u) = {O}, but removing u will disconnect the graph and hence the resulting graph does not have a gflow.
Note that if we were to consider the same labelled open graph, but with u measured in a different plane, it would not have a gflow to start with. This is because, if it did, we would need u ∈ g(I), so that I ≺ u but also u ∈ g(u) so that I ∈ Odd (g(u)) giving u ≺ I. Hence this does not contradict the lemma.
The next corollary shows how the previous results can be combined to remove a vertex with arity 2 from a labelled open graph while preserving gflow. In the ZX-calculus, the idea behind this is that we use (i1) to remove a vertex and then (f ) to fuse the adjacent vertices (cf. Definition 2.27). Recall from that definition that G whereĝ is as defined in Corollary 3.3.

Proof.
A computation using Corollary 3.3 and Lemma 3.4.
Proof. Suppose Γ has gflow (g, ≺). Let g be the extension of g to domain V \ O which satisfies g (u) = {u }, and let ≺ be the transitive closure of ≺ ∪{(u, u )}.
The tuple (g , ≺ ) inherits (g1) and (g3)-(g5) for all v ∈ V \ O because the correction sets have not changed for any of the original vertices. Furthermore, u ∈ Odd G (g (v)) for Now, for u, (g1) holds because u ≺ u by definition, (g2) holds because Odd G (g (u)) = {u}, and (g3) can easily be seen to hold. Thus, (g , ≺ ) is a gflow for Γ .
Proof. Suppose Γ has gflow (g, ≺). Let g be the extension of g to domain V \ O which satisfies g (u ) = {u}, and let ≺ be the transitive closure of ≺ ∪{(u , w) : w ∈ N G (u)∪{u}}. The tuple (g , ≺ ) inherits the gflow properties for all v ∈ V \ O because the correction sets have not changed for any of the original vertices and because the additional inequalities in ≺ do not affect the gflow properties for any v ∈ V \ O. The latter is because ) for any v ∈ V \ O since its only neighbour u was an input in Γ and thus satisfies u / Now, for u , (g1) holds by the definition of ≺ . Note that Odd G (g(u )) = N G (u), so (g2) also holds by the definition of ≺ . Finally, (g3) holds because u / ∈ g(u ) and u ∈ Odd G (g(u )) = N G (u). Thus, (g , ≺ ) is a gflow for Γ .

Finding extended gflow
Ref. [37] gives a polynomial time algorithm for finding gflow for labelled open graphs with all measurements in the XY-plane. In this section, we present an extension of this algorithm that works for measurements in all three measurement planes.
Before doing so, we note a few details from the algorithm. The intuition behind the procedure is to 'maximally delay' any measurements, thereby keeping potential correction options available for as long as possible. As a result, the algorithm finds a gflow of minimal 'depth' (a notion we will make precise later).
The algorithm works backwards: Starting from the output vertices, it iteratively constructs disjoint subsets of vertices that can be corrected by vertices chosen in previous steps. Viewed instead as information travelling forwards, from the inputs to the outputs, this corresponds to only correcting a vertex at the last possible moment, hence the name maximal delayed.
and there exists a k such that the inequality is strict. A gflow (g, ≺) is maximally delayed if there exists no gflow of the same open graph that is more delayed. There exists a polynomial time algorithm that decides whether a given labelled open graph has an extended gflow, and that outputs such a gflow if it exists. Moreover, the output gflow is maximally delayed.
The proof of this theorem can be found in Appendix C.

Focusing extended gflow
In Section 2.5, we introduced the notion of focused gflow for labelled open graphs in which all measurements are in the XY plane. There is no canonical generalisation of this notion to labelled open graphs with measurements in multiple planes. Hamrit and Perdrix suggest three different extensions of focused gflow to the case of multiple measurement planes, which restrict correction operations on non-output qubits to only a single type of Pauli operator overall [26,Definition 2]. Here, we go a different route by requiring that non-output qubits only appear in correction sets, or odd neighbourhoods of correction sets, if they are measured in specific planes. This means the correction operators which may be applied to some non-output qubit depend on the plane in which that qubit is measured.
The new notion of focused gflow will combine particularly nicely with the phase-gadget form of MBQC+LC diagrams of Section 4.4. We begin by proving some lemmas that allow any gflow to be focused in our sense.
Proof. As the correction set only changes for v, the gflow properties remain satisfied for all other vertices. Now, suppose w ∈ g (v), then w ∈ g(v) ∨ w ∈ g(w). In the former case, v ≺ w , and in the latter case, v ≺ w ≺ w , since (g, ≺) is a gflow, so (g1) holds. Similarly, suppose w ∈ Odd (g (v)), then by linearity of Odd (·) we have ). Thus (g3)-(g5) hold and (g , ≺) is a gflow. We can construct this g in a number of steps that is polynomial in the number of vertices of G.
Proof. Let g 0 := g, we will modify the function in successive steps to g 1 , g 2 , and so on. For each non-negative integer k in turn, define Note w k ∈ S k implies w k = v, as well as either w k ∈ g k (v) or w k ∈ Odd (g k (v)). Thus if (g k , ≺) is a gflow, then v ≺ w k , and hence by Lemma 3.12, (g k+1 , ≺) is also a gflow. Since (g 0 , ≺) is a gflow, this means (g k , ≺) is a gflow for all k. Now, if w k ∈ S k,XY , then w k ∈ Odd (g k (w k )) by (g3). This implies w k / ∈ Odd (g k+1 (v)), and thus w k / ∈ S k+1 . Similarly, if w k ∈ S k,XZ ∪ S k,YZ , then w k ∈ g k (w k ) by (g4) or (g5). This implies w k / ∈ g k+1 (v), and thus w k / ∈ S k+1 . Hence, in each step we remove a minimal element from the set.
Suppose there exists w ∈ S k+1 \ S k , then either w ∈ g k (w k ) or w ∈ Odd (g k (w k )); in either case w k ≺ w . In other words, we always remove a minimal element from the set and add only elements that come strictly later in the partial order. Therefore, the process terminates after n ≤ |V | steps, at which point S n = ∅, and the process requires O(|V | 2 ) operations at each step. The total complexity is therefore O(|V | 3 ). The function g = g n has the desired properties: (1) holds because we never modify the value of the function on inputs other than v, (2) and (3) hold because S n = ∅, and (4) was shown to follow from Lemma 3.12.
Based on these lemmas, we can now show the focusing property: first for arbitrary labelled open graphs and then for labelled open graphs corresponding to an MBQC diagram in phase-gadget form. These results state that correction sets can be simplified to only contain qubits measured in the XY plane. Moreover, side-effects of corrections (i.e. effects on qubits other than the one being corrected) never affect qubits measured in the XY plane. Proposition 3.14. Let (G, I, O, λ) be a labelled open graph which has gflow. Then (G, I, O, λ) has a maximally delayed gflow (g, ≺) with the following properties for all v ∈ V : This maximally delayed gflow can be constructed in a number of steps that is polynomial in the number of vertices in G.
Proof. Let (g 0 , ≺) be a maximally delayed gflow of (G, I, O, λ). Set n := |V | and consider the vertices in some order v 1 , . . . , v n . For each k = 1, . . . , n, let g k be the function that results from applying Lemma 3.13 to the gflow (g k−1 , ≺) and the vertex v k . Then g k satisfies the two properties for the vertex v k . The function g k also equals g k−1 on all inputs other than v k , so in fact g k satisfies the two properties for all vertices v 1 , . . . , v k . Thus, g n satisfies the two properties for all vertices. Moreover, the partial order does not change, so (g n , ≺) is as delayed as (g 0 , ≺); i.e. it is maximally delayed. Hence if g := g n , then (g, ≺) has all the desired properties. The construction of each successive g k+1 via Lemma 3.13 takes O(n 3 ) operations, which we perform at most n times, giving a complexity of O(n 4 ).
The extended notion of focused gflow also allows us to prove another result which will be useful for the optimisation algorithm later.
First, note that if a labelled open graph has gflow, then the labelled open graph that results from deleting all vertices measured in the XZ or YZ planes still has gflow. Proof. Consider some v ∈ O such that λ(v) = XY. Note that a vertex with no neighbours is in the even neighbourhood of any set of vertices. Therefore we must have |N G (v)| ≥ 1, since (G, I, O, λ) has gflow and v must be in the odd neighbourhood of its correction set by (g3). Now suppose for a contradiction that v / ∈ I and |N G (v)| = 1. Denote by u the single neighbour of v.
If the labelled open graph contains any vertices measured in the XZ or YZ planes, by Lemma 3.15, we can remove those vertices while preserving the property of having gflow. Since λ(v) = XY, the removal process preserves v. The new labelled open graph has gflow and v cannot have gained any new neighbours, so u must also be preserved by the argument of the first paragraph above. Thus, without loss of generality, we will assume that all non-outputs of (G, I, O, λ) are measured in the XY plane.
The labelled open graph (G, I, O, λ) has gflow and satisfies λ(w) = XY for all w ∈ O, so by Theorem 2.45 it has a focused gflow (g, ≺). To satisfy the gflow condition (g3) for v, that is, to satisfy v ∈ Odd G (g(v)), we must have u ∈ g(v). This then implies v ≺ u by (g1). Since ), contradicting (g3). Therefore, the initial assumption must be wrong, i.e. if v / ∈ I then |N G (v)| ≥ 2.

Simplifying measurement patterns
In the previous section, we saw several ways in which labelled open graphs can be modified while preserving the existence of gflow. In this section, we will see how these modifications can be done on measurement patterns in a way that preserves the computation being performed. The goal of the simplifications in this section is to reduce the number of qubits needed to implement the computation. Since we are specifically interested in patterns with gflow, we will represent a pattern by a ZX-diagram in MBQC+LC form, which carries essentially the same information.
Before we find qubit-removing rewrite rules however, we establish how local Cliffords in an MBQC+LC form diagram can be changed into measurements in Section 4.1 and how local complementations affect a pattern in Section 4.2. We use local complementations to remove Clifford vertices from a pattern in Section 4.3, and to change a pattern so that only two measurement planes are necessary in Section 4.4. Finally, in Section 4.5 we find some further simplifications that allow the removal of additional qubits.

Transforming local Cliffords into measurements
We used MBQC+LC diagrams as an extension of MBQC form diagrams. In this section we will see that we can always convert the local Clifford gates into measurements to turn the diagram into MBQC form. Proof. Any single-qubit Clifford unitary can be expressed as a composite of three phase shifts [4, Lemma 3]. Note that this result holds with either choice of colours, i.e. any single-qubit Clifford unitary can be expressed as α β γ or α β γ . Now, with the green-red-green version, for any Clifford operator on an input, we can 'push' the final green phase shift through the graph state part onto the outgoing wire. There, it will either merge with the measurement effect or with the output Clifford unitary: . . . If γ ∈ {0, π}, merging the phase shift with a measurement effect may change the angle but not the phase label, e.g. if γ = π: If γ ∈ { π 2 , − π 2 }, merging the phase shift with a measurement effect will flip the phase labels XZ and YZ, e.g. if γ = − π 2 : Thus we need to add at most two new qubits to the MBQC-form part when removing a Clifford unitary on the input. Combining these properties, we find that rewriting to MBQC form adds at most 2p+4q new qubits to the pattern. Proof. By applying Lemma 4.1 repeatedly, we can incorporate any local Clifford operators into the MBQC form part of the diagram. Lemmas 3.7 and 3.8 ensure that each step preserves the property of having gflow.

Local complementation and pivoting on patterns
Lemmas 2.31 and 2.32 showed how to apply a local complementation and pivot on a ZXdiagram by introducing some local Clifford spiders. In this section we will show how these rewrite rules can be used on MBQC+LC diagrams. ∈ I, so -with the exception of the output wire or the edge to the measurement effect -all edges incident on u connect to neighbouring vertices in the graph. The input wires on the other qubits can be safely ignored. To get back an MBQC+LC diagram after Lemma 2.31 is applied to u, we only need to rewrite the measurement effects, and hence we need to construct new λ and α for these measurement effects. We do that as follows.
First of all, there are no changes to the measurement effects on vertices v ∈ N (u)∪{u}, and hence for those vertices we have λ (v) = λ(v) and α (v) = α(v).
The vertex u gets a red π 2 phase from the application of Lemma 2.31. If u ∈ O, then it has no associated measurement plane or angle. In this case, this red π 2 simply stays on the output wire, as allowed in an MBQC+LC diagram. When u / ∈ O, there are three possibilities, depending on λ(u): • If λ(u) = XY, then the new measurement effect is i.e. λ (u) = XZ and α (u) = π 2 − α(u).
• If λ(u) = YZ, then the new measurement effect is i.e. λ (u) = YZ and α (u) = α(u) + π 2 . The vertices v that are neighbours of u get a green − π 2 phase. Again, if such a v is an output, this phase can be put as a local Clifford on the output. If it is not an output, then there are also three possibilities depending on λ(v): • If λ(v) = XY, then the new measurement effect is With these changes, we see that the resulting diagram D is indeed in MBQC+LC form. The underlying graph G(D ) results from the local complementation about u of the original graph G(D). Furthermore, the measurement planes changed in the same way as in Lemma 3.1, and hence if D had gflow, then D will also have gflow. Proof. If u is not an input vertex, the result is immediate from Lemma 4.3.
If instead u is an input vertex, we modify D by replacing the input wire incident on u by an additional graph vertex u measured in the XY-plane at angle 0, and a Hadamard unitary on the input wire: . . . A pivot is just a sequence of three local complementations. Thus, the previous lemma already implies that when Lemma 2.32 is applied to an MBQC+LC diagram the resulting diagram can also be brought back into MBQC+LC form. Nevertheless, it will be useful to explicitly write out how the measurement planes of the vertices change. i.e. λ (a) = XZ and α (a) = π 2 − α(a). The only other changes are new green π phases on vertices w ∈ N (u) ∩ N (v). For measured (i.e. non-output) vertices, these preserve the measurement plane and are absorbed into the measurement angle in all three cases: If instead w is an output vertex, we consider the green π phase shift as a Clifford operator on the output wire. The measurement planes and the graph change exactly like in Corollary 3.3 and hence D has gflow when D had gflow.

Removing Clifford vertices
In this section, we show that if a qubit is measured in one of the Pauli bases, i.e. at an angle which is an integer multiple of π 2 , it can be removed from a pattern while preserving the semantics as well as the property of having gflow. We say a measured vertex u ∈ G is Clifford when α(u) = k π 2 for some k. Our goal will be to remove as many internal Clifford vertices as possible. We make a key observation for our simplification scheme: a YZ-plane measurement with a 0 or π phase can be removed from the pattern by modifying its neighbours in a straightforward manner. These aπ phase shifts on the right-hand side can be absorbed into the measurement effects of the neighbouring vertices (or, for output vertices, considered as a local Clifford operator). Absorbing an aπ phase shift into a measurement effect does not change the measurement plane, only the angle. The resulting diagram D is then also in MBQC+LC form. Since G(D ) is simply G(D) with a YZ or XZ plane vertex removed, D has gflow if D had gflow by Lemma 3.4.
We can combine this observation with local complementation and pivoting to remove vertices measured in other planes or at other angles. Lemma 4.8. Let D be a ZX-diagram in MBQC+LC form with vertices V . Suppose u ∈ V is a non-input vertex measured in the YZ or XY plane with an angle of ± π 2 . Then there is an equivalent diagram D with vertices V \ {u}. If D has gflow, then D does as well.
Proof. We apply a local complementation about u using Lemma 2.31 and reduce the diagram to MBQC+LC form with Lemma 4.3. By these lemmas, if the original diagram had gflow, this new diagram will also have gflow. As can be seen from Lemma 4.3, if u was in the XY plane, then it will be transformed to the XZ plane and will have a measurement angle of π 2 ∓ π 2 . As a result its measurement angle is of the form aπ for a ∈ {0, 1}. If instead it was in the YZ plane, then it stays in the YZ plane, but its angle is transformed to π 2 ± π 2 in which case it will also be of the form aπ for a ∈ {0, 1}. In both cases we can remove the vertex u using Lemma 4.7 while preserving semantics and the property of having gflow. Proof. We apply a pivot to uv using Lemma 2.32 and reduce the diagram to MBQC+LC form with Lemma 4.5. If the original diagram had gflow, this new diagram will also have gflow. As can be seen from Lemma 4.5, if λ(u) = XY then λ (u) = YZ with α (u) = α(u) = aπ. If instead we had λ(u) = XZ (and thus α(u) = (−1) a π 2 ), then λ (u) = XZ, but α (u) = π 2 − α(u) = π 2 − (−1) a π 2 = aπ. In both cases, using Lemma 4.7, u can be removed while preserving semantics and the existence of gflow. Combining the three previous lemmas we can remove most non-input Clifford vertices. The exceptions are some non-input Clifford vertices which are only connected to input and output vertices. While it might not always be possible to remove such vertices, when the diagram has a gflow, we can find an equivalent smaller diagram: Lemma 4.11. Let D be a ZX-diagram in MBQC+LC form with vertices V that has a gflow. Let u be a non-input measured vertex that is only connected to input and output vertices. Suppose that either λ(u) = XY with α(u) = aπ for a ∈ {0, 1} or λ(u) = XZ with α(u) = (−1) a π 2 . Then there exists an equivalent diagram D which has gflow and has vertices V \{u}.
Proof. We prove the result for λ(u) = XY and α(u) = aπ. The other case is similar.
We claim that u is connected to at least one output that is not also an input. In order to obtain a contradiction suppose otherwise. The diagram then looks as follows: Here 'LC' indicates that there are local Clifford operators on the inputs. Since D has gflow, the entire diagram must be (proportional to) an isometry, and hence it must still be an isometry if we remove the local Cliffords on the inputs. But we then have the map aπ . . .
as the first operation in the diagram. This map is a projector, and it is not invertible. This is a contradiction, as the entire diagram cannot then be an isometry. Therefore, u must be connected to some output vertex v, which is not an input. We can thus perform a pivot about uv. This adds a Hadamard operator after v, and changes the label of u to YZ. We can then remove u using Lemma 4.7. As all these operations preserve gflow, the resulting diagram still has gflow.
The following result about removing Pauli measurements (i.e. Clifford vertices) from patterns while preserving semantics is already contained in Ref. [27,Section III.A] (if outside the context of MBQC), and is also mentioned in Ref. [29]. Nevertheless, we are the first to explicitly state the effects of this process on the measurement pattern and the gflow. Theorem 4.12. Let D be a ZX-diagram in MBQC+LC form that has gflow. Then we can find an equivalent ZX-diagram D in MBQC+LC form, which also has gflow and which contains no non-input Clifford vertices. The algorithm uses a number of graph operations that is polynomial in the number of vertices of D.
Proof. Starting with D we simplify the diagram step by step using the following algorithm: 1. Using Lemma 4.8 repeatedly, remove any non-input YZ or XY measured vertex with a ± π 2 phase.
2. Using Lemma 4.7 repeatedly, remove any non-input vertex with measured in the YZ or XZ plane with angle aπ.
3. Using Lemma 4.9 repeatedly, remove any XY vertex with an aπ phase and any XZ vertex with a ± π 2 phase that are connected to any other internal vertex. If any have been removed, go back to step 1.  I, O, λ, α) is a uniformly deterministic MBQC pattern representing a unitary operation. Assume the pattern involves q inputs and n qubits measured at non-Clifford angles, i.e. q := |I| and n := {u ∈ O | α(u) = k π 2 for any k ∈ Z} . Then we can find a uniformly deterministic MBQC pattern that implements the same unitary and uses at most (n + 8q) measurements. This process finishes in a number of steps that is polynomial in the number of vertices of G.

If
Proof. Let D be the ZX-diagram in MBQC form from Lemma 2.16 that implements the same unitary as the MBQC pattern P := (G, I, O, λ, α). Since P is uniformly deterministic, it has gflow, and hence D also has gflow by Definition 2.41. Let D be the ZX-diagram in MBQC+LC form produced by Theorem 4.12. Since D has no internal Clifford vertices, its MBQC-form part can have at most n internal vertices. It may still have boundary Clifford vertices, and by unitarity |O| = |I| = q, so the MBQC-form part contains at most (n + 2q) vertices.
Denote by D the MBQC-form diagram produced by applying Lemma 4.1 to D . Then D has at most ((n + 2q) + 6q) vertices in its MBQC-form part.
We can construct a new pattern P from D using Lemma 2.21. As D has gflow, P also has gflow, and hence is uniformly deterministic. The new pattern P involves at most (n + 8q) qubits.
For the complexity of these operations:

Phase-gadget form
Using the local complementation and pivoting rules of Section 4.2 we can transform the geometry of MBQC+LC form diagrams so that they no longer contain any vertices measured in the XZ plane, nor will YZ vertices be connected. The name 'phase gadget' refers to a particular configuration of spiders in the ZXcalculus, which, in our setting, corresponds to spiders measured in the YZ plane. Phase gadgets have been used particularly in the study of circuit optimisation [12,19,35].
In Section 4.5 we introduce another form, called reduced (Definition 4.20), which requires the pattern to be in phase-gadget form. As can be seen from Lemma 4.3, this process changes the measurement plane of u from XZ to YZ and it does not affect the labels of any vertices that are measured in the XY-plane.
• If there is no such connected pair, nor any vertex that is measured in the XZ-plane then D k is already in the desired form, so halt.
The number of vertices not measured in the XY-plane decreases with each step, and no vertices are added, so this process terminates in at most n steps, where n is the number of vertices in D. Each step requires checking every pair of vertices, or performing local complementation, each of which have complexity O(n 2 ), so the total complexity is O(n 3 ). Since a pivot is just a sequence of local complementations, D k+1 has gflow if D k had gflow (Proposition 4.4). Finally every step preserves equivalence, so D k+1 is equivalent to D k . Proposition 4.16 finds a phase-gadget form for an MBQC+LC diagram, but note that the phase-gadget form is not guaranteed to be unique.

Further pattern optimisation
In Section 4.3 we saw that we can remove all non-input Clifford qubits from a pattern while preserving both determinism and the computation the pattern implements. We will show in this section that it is also possible to remove certain qubits measured in non-Clifford angles.
These measurement pattern rewrite rules, seen then as transformations of ZX-diagrams, were used in Ref. [35] to reduce the T-count of circuits. We will see how in our context they can be used to remove a qubit from a pattern, again while preserving determinism. First of all, any internal YZ vertex with just one neighbour can be fused with this neighbour, resulting in the removal of the YZ vertex: Note that, by Proposition 3.16, if the diagram has gflow and equal numbers of inputs and outputs, then it has no internal XY vertices with just one neighbour. Thus, if the diagram is in phase-gadget form (cf. Definition 4.14), the above lemma allows us to remove all internal vertices which have a single neighbour.
Our second rewrite rule allows us to also 'fuse' YZ vertices that have the same set of neighbours: Proof. We apply the following rewrite: Proof. Assume for a contradiction that N (u) = N (v) and that the diagram has gflow. Note that, for any subset of vertices S, we have u ∈ Odd (S) ⇐⇒ v ∈ Odd (S). In particular, as u ∈ Odd (g(u)) by (g3), we have v ∈ Odd (g(u)) and thus u ≺ v by (g2). Yet, swapping u and v in the above argument, we also find v ≺ u, a contradiction. Thus, if the diagram has gflow, distinct vertices u and v must have distinct neighbourhoods We can now combine these rewrite rules with our previous results to get a more powerful rewrite strategy: Definition 4.20. Let D be an MBQC+LC diagram. We say D is reduced when: • It is in phase-gadget form (see Definition 4.14).
• It has no internal Clifford vertices.
• Every internal vertex has more than one neighbour.
• If two distinct vertices are measured in the same plane, they have different sets of neighbours.

Theorem 4.21.
Let D be an MBQC+LC diagram with gflow and equal numbers of inputs and outputs. Then we can find an equivalent diagram D that is reduced and has gflow. This process finishes in a number of steps that is polynomial in the number of vertices of D.
Proof. Starting with D, we simplify the diagram step by step with the following algorithm: Each of the steps preserves gflow, and hence at every stage of the algorithm the diagram has gflow. By construction, if algorithm has terminates, every vertex is now of type YZ or XY, and YZ vertices are only connected to XY vertices. Furthermore, every YZ vertex must have more than one neighbour and have a different set of neighbours than any other YZ vertex. This is also true for the XY vertices by the existence of gflow and the requirement that the number of inputs match the number of outputs (using Lemma 4.19 and Proposition 3.16). Hence, the resulting diagram has all the properties needed for it to be reduced.
To show this process terminates consider the lexicographic order: • Number of vertices in the diagram Remark 4.22. The algorithm described above uses the same idea as that described in Ref. [35]. But while they describe the procedure in terms of modifying a graph-like ZXdiagram, we describe it for MBQC+LC diagrams, a more general class of diagrams. Furthermore, we prove that the procedure preserves the existence of gflow. The existence of gflow is used in the next section to show how to recover a circuit from an MBQC+LC diagram.

Circuit extraction
In this section we will see that we can extract a circuit from a measurement pattern whose corresponding labelled open graph has a gflow. The extraction algorithm modifies that of Ref. [23] so that it can handle measurements in multiples planes (not just the XY plane). Instead of describing the algorithm for measurement patterns, we describe it for the more convenient form of MBQC+LC diagrams.
The general idea is that we modify the diagram vertex by vertex to bring it closer and closer to resembling a circuit. We start at the outputs of the diagram and work our way to the inputs. The gflow informs the choice of which vertex is next in line to be 'extracted' (specifically, this will always be a vertex maximal in the gflow partial order). By applying various transformations to the diagram, we change it so that the targeted vertex can easily be pulled out of the MBQC-form part of the diagram and into the circuit-like part. The remaining MBQC-form diagram is then one vertex smaller. Since all the transformations preserve gflow, we can then repeat the procedure on this smaller diagram until we are finished.
Before we explain the extraction algorithm in detail in Section 5.1, we state some relevant lemmas. where M is the biadjacency matrix of the output vertices to the neighbours of D, and M is the matrix produced from M by adding row 1 to row 2, modulo 2. If the full diagram on the LHS has gflow, then so does the RHS.
Proof. The equality is proved in Ref. [23,Proposition 6.2]. There it is also shown that this preserves gflow when all measurements are in the XY plane, but the same proof works when measurement in all three planes are present.
Proof. We claim that if (g, ≺) is a gflow for G, then it is also a gflow for G . Note that Odd G (g(v)) ∩ O = Odd G (g(v)) ∩ O as the only changes to neighbourhoods are among the output vertices. It is thus easily checked that all properties of Definition 2.36 remain satisfied.  I, O, λ ) also has a gflow. Note that G does contain v, which is still an output vertex in G , but does not contain u, and hence v has no neighbours in G . By Theorem 2.45, G has a focused gflow, and because G has the same number of inputs as outputs, its reversed graph also has a gflow (g, ≺) by Corollary 2.47. In this reversed graph v is an input and, since it is not an output, it is measured in the XY plane. It therefore has a correction set g(v) so that v ∈ Odd (g(v)). But because v has no neighbours, this is a contradiction. We conclude that indeed λ(u) = XY.
For any set A ⊆ V , let N G (A) = v∈A N G (v). Recall the partition of vertices according to the partial order of the gflow into sets V ≺ k , which is introduced in Definition 3.9.
Lemma 5.4. Let (G, I, O, λ) be a labelled open graph in phase-gadget form, which furthermore satisfies O = ∅. Suppose (G, I, O, λ) has a gflow. Then the maximally delayed gflow, (g, ≺), constructed in Proposition 3.14 exists and moreover N G (V ≺ 1 ) ∩ O = ∅, i.e. the gflow has the property that, among the non-output vertices, there is a vertex which is maximal with respect to the gflow order and also connected to an output vertex.
Proof. By Proposition 3.14, there exists a maximally delayed gflow of (G, I, O, λ) such that no element of a correction set (other than possibly the vertex being corrected) is measured in the YZ plane.
Since the open graph does not consist solely of outputs, the set V ≺ 1 (as defined in Definition 3.9) is non-empty, so the following arguments are non-trivial. For ). There are no self-loops, hence this v must be connected to at least one output, and we are done. As the graph is in phase-gadget form, there are no vertices labelled XZ and hence from now on assume that λ(v) = YZ for all v ∈ V ≺ 1 . We distinguish two cases.
1. If V ≺ 2 = ∅, then the only non-output vertices are in V ≺ 1 . Now, any connected component of the graph G must contain an input or an output. The vertices in V ≺ 1 are all labelled YZ and thus appear in their own correction sets; this means they cannot be inputs because inputs do not appear in correction sets. The vertices in V ≺ 1 are not outputs either, so each of them must have at least one neighbour. Yet the labelled open graph is in phase-gadget form. This implies that two vertices both labelled YZ cannot be adjacent, and all vertices in V ≺ 1 are labelled YZ. Thus any vertex v ∈ V ≺ 1 must have a neighbour in O, and we are done.
• Suppose g(w) ∩ V ≺ 1 = ∅, then there exists a YZ vertex in the correction set of w since all elements of V ≺ 1 are measured in the YZ plane. But our gflow satisfies the properties of Proposition 3.14, and hence this cannot happen. Odd (g(w)). There are two further subcases.
-If λ(w) = XY, we have w ∈ g(w) and hence g(w) ⊆ O so that there must be some o ∈ O that is connected to v and we are done. -Otherwise, if λ(w) = YZ, then w ∈ g(w). Yet both v and w are measured in the YZ plane, so they are not neighbours, and hence there still must be an o ∈ O that is connected to v to have v ∈ Odd (g(w)).
Thus, the gflow (g, ≺) has the desired property in all possible cases.

General description of the algorithm
We first walk through a high-level description of how to extract a circuit from a diagram in MBQC+LC form with gflow, explaining why every step works. After that, we present a more practical algorithm in Section 5.2. As we wish the output to be a unitary circuit, we will assume that the diagram has an equal number of inputs and outputs. The process will be to make sequential changes to the ZX-diagram that make the diagram look progressively more like a circuit. During the process, there will be a 'frontier': a set of green spiders such that everything to their right looks like a circuit, while everything to their left (and including the frontier vertices themselves) is an MBQC+LC form diagram equipped with a gflow. We will refer to the MBQC-form diagram on the left as the unextracted part of the diagram, and to the circuit on the right as the extracted part of the diagram. For example: extracted unextracted (6) In this diagram, we have merged the XY measurement effects with their respective vertices, in order to present a tidier picture. The matrix M is the biadjacency matrix between the vertices on the frontier and all their neighbours to the left of the frontier. For the purposes of the algorithm below, we consider the extracted circuit as no longer being part of the diagram, and hence the frontier vertices are the outputs of the labelled open graph of the unextracted diagram.
Step 0: First, we transform the pattern into phase-gadget form using Proposition 4.16, ensuring that all vertices are measured in the XY or YZ planes, and that vertices measured in the YZ plane are only connected to vertices measured in the XY plane. This can be done in polynomial time, and preserves the interpretation of the diagram. Furthermore, the resulting diagram still has gflow.
Step 1: We unfuse any connection between the frontier vertices as a CZ gate into the extracted circuit, and we consider any local Clifford operator on the frontier vertices as part of the extracted circuit. For example: . . .    Thus, the unextracted diagram continues to be in MBQC form and it continues to have gflow. If the only unextracted vertices are on the frontier, go to step 5, otherwise continue to step 2.
Step 2: The unextracted diagram is in phase-gadget form and has gflow. Thus, by Lemma 5.4, it has a maximally delayed gflow (g, ≺) such that 1 is the 'most delayed' layer before the frontier vertices, which are the outputs of the labelled open graph (see Definition 3.9). Such a gflow can be efficiently determined by first finding any maximally delayed gflow using the algorithm of Theorem 3.11 and then following the procedure outlined in Proposition 3.14.
Now, if any of the vertices in V ≺ 1 are labelled XY, pick one of these vertices and go to step 3. Otherwise, all the maximal non-output vertices (with respect to ≺) must have label YZ; go to step 4.
Step 3: We have a maximal non-output vertex v labelled XY, which we want to extract. Since it is maximal in ≺, we know that g(v) ⊆ O by Definition 3.9. As the gflow is maximally delayed, we have Odd (g(v)) ∩ O = {v}. We now follow the strategy used in Ref. [23] for the 'XY-plane only' case, illustrating it with an example. Consider the following diagram, in which the vertex v and its correction set g(v) are indicated: For clarity, we are ignoring the measurement effects on the left-hand-side spiders, and we are not showing any frontier vertices that are inputs (although note that by definition of a gflow, the vertices of g(v) cannot be inputs). In the above example, the biadjacency matrix of the bipartite graph between the vertices of g(v) on the one hand, and their neighbours in the unextracted part on the other hand, is where the rows correspond to vertices of g(v), and vertices are ordered top-to-bottom. We do not include the bottom-most frontier vertex in the biadjacency matrix, as it is not part of g(v), and we do not include the bottom left spider, as it is not connected to any vertex in g(v).
The property that Odd (g(v)) ∩ O = {v} now corresponds precisely to the following property of the matrix: if we sum up all the rows of this biadjacency matrix modulo 2, the resulting row vector contains a single 1 corresponding to the vertex v and zeroes everywhere else. It is straightforward to see that this is indeed the case for the matrix of Eq. (8).
Now pick any frontier vertex w ∈ g(v). Lemma 5.1 shows that the application of a CNOT to two outputs corresponds to a row operation on the biadjacency matrix, which adds the row corresponding to the target to the row corresponding to the control. Hence if, for each w ∈ g(v) \ {w}, we apply a CNOT with control and target on the output wires of w and w , the effect on the biadjacency matrix is to add all the other rows of the On the underlying labelled open graph this corresponds to removing w and adding v to the list of outputs. We need to check that this preserves the existence of a gflow. The only change we need to make is that for all v = v with w ∈ g(v ) we set g (v ) = g(v )\{w}. As w's only neighbour is v, removing w from g(v ) only toggles whether v ∈ Odd (g (v )).
Since v is a part of the outputs in the new labelled open graph, this preserves all the properties of being a gflow. Now that the vertex w has been removed, the number of vertices in the unextracted part of the diagram is reduced by 1. We now go back to step 1.
Step 4: All the maximal vertices are labelled YZ. Since we chose our gflow according to Lemma 5.4, we know that at least one of these vertices is connected to an output, and hence a frontier vertex. Pick such a vertex v, and pick a w ∈ O ∩ N G (v) (this set is non-empty). Pivot about vw using Lemma 2.32 and reduce the resulting diagram to MBQC form with Lemma 4.5. Afterwards, v has label XY and w has a new Hadamard gate on its output wire (which will be dealt with in the next iteration of step 1).
We have changed one vertex label in the unextracted part of the diagram from YZ to XY. Since no step introduces new YZ vertices, step 4 can only happen as many times as there are YZ vertices at the beginning. Go back to step 1.
Step 5: At this point, there are no unextracted vertices other than the frontier vertices, all of which have arity 2 and can be removed using rule (i1) of Figure 2. Yet the remaining frontier vertices might be connected to the inputs in some permuted manner and the inputs might carry some local Cliffords: This is easily taken care of by decomposing the permutation into a series of SWAP gates, at which point the entire diagram is in circuit form.
Correctness and efficiency: Since step 3 removes a vertex from the unextracted diagram, and step 4 changes a measurement plane from YZ to XY (and no step changes measurement planes in the other direction), this algorithm reduces the lexicographic order (# unextracted vertices, # unextracted vertices with λ = Y Z) each time we repeat an iteration of the process, so that the algorithm terminates. Each step described above takes a number of graph-operations polynomial in the number of unextracted vertices, and therefore this entire algorithm takes a number of steps polynomial in the number of vertices. All steps correspond to ZX-diagram rewrites, so the resulting diagram is a circuit that implements the same unitary as the pattern we started with.

A more practical algorithm
Now we know that the algorithm above is correct and will always terminate, we can take a couple of short-cuts that will make it more efficient.
In step 2, instead of using the gflow to find a maximal vertex, we do the following: Write down the biadjacency matrix of the bipartite graph consisting of frontier vertices on one side and all their neighbours on the other side. For example, the Diagram (7) would give the matrix: Any row in this matrix containing a single 1 corresponds to an output vertex with a single neighbour. By Lemma 5.3, this neighbour is of type XY. As an example, in the matrix in Eq. (11), the first row has a single 1 in the first column, and hence the top-left spider of Diagram (7) is the unique XY neighbour to the first output. Similarly, the second row has a single 1, appearing in column 2, and hence the second spider from the top on the left in Diagram (7) is the unique neighbour to the second output.
If we found at least one row with a single 1 with this method, we implement the row operations corresponding to the Gaussian elimination procedure as a set of CNOT gates using Lemma 5.1. Doing this with Diagram (7) gives: D D = (12) We see that every row which had a single 1 now corresponds to a frontier spider with a single neighbour, and hence we can extract vertices using the technique of Eq. (9): As we now extract multiple vertices at a time, there could be connections between the new frontier vertices (for instance between the top two frontier spiders in Eq. (13)). These are taken care of in the next iteration of step 1, turning those into CZ gates.
If the Gaussian elimination does not reveal a row with a single 1, then we are in the situation of step 4. We perform pivots involving a vertex with label YZ and an adjacent frontier vertex until there is no vertex with a label YZ which is connected to a frontier vertex. We then go back to step 1.
With these short-cuts, it becomes clear that we do not need an explicitly calculated gflow in order to extract a circuit. The fact that there is a gflow is only used to argue that the algorithm is indeed correct and will always terminate. Pseudocode for this algorithm can be found in Appendix D.
With the results of this section we have then established the following theorem.
Theorem 5.5. Let P be a measurement pattern with n inputs and outputs containing a total of k qubits, and whose corresponding labelled open graph has a gflow. Then there is an algorithm running in time O(n 2 k 2 + k 3 ) that converts P into an equivalent n-qubit circuit that contains no ancillae.
Proof. The runtime for the extraction algorithm is dominated by Gaussian elimination of the biadjacency matrices which has complexity O(n 2 m), where n is the number of rows, corresponding to the number of outputs, and m is the number of neighbours these output vertices are connected to. In principle m could be as large as the number of vertices in the graph and hence could be as large as k (although in practice it will be much smaller than that). In the worst case, performing a pivot operation also requires toggling the connectivity of almost the entire graph, which requires k 2 elementary graph operations. Since we might have to apply a pivot and a Gaussian elimination process for every vertex in the graph, the complexity for the entire algorithm is bounded above by O(k(n 2 k + k 2 )) = O(n 2 k 2 + k 3 ).
Note that if k ≥ O(n 2 ), which will be the case for most useful computation, the bound becomes O(k 3 ). In practice however we would not expect to see this worst-case complexity as it would only be attained if everything is almost entirely fully connected all the time. This does not seem possible because the pivots and Gaussian elimination always toggle connectivity, and hence a highly connected graph in one step will become less connected in the following step.

Conclusions and Future Work
We have given an algorithm which extracts a circuit from any measurement pattern whose underlying labelled open graph has extended gflow. This is the first algorithm which works for patterns with measurements in multiple planes, and does not use ancillae. Simultaneously, it is the most general known algorithm for extracting quantum circuits from ZX-calculus diagrams.
We have also developed a set of rewrite rules for measurement patterns containing measurements in all three planes. For each of these rewrite rules, we have established the corresponding transformations of the extended gflow. The rewrite rules can be used to reduce the number of qubits in a measurement pattern, in particular eliminating all qubits measured in a Pauli basis. Additionally, we have generalised the notions of focused gflow and maximally delayed gflow to labelled open graphs with measurements in multiple planes, and we have described algorithms for finding such gflows.
The pattern optimization algorithm of Theorem 4.21 and the circuit extraction algorithm of Section 5 have been implemented in the ZX-calculus rewriting system PyZX 4 [34]. The reduction in non-Clifford gates using this method matches the state-of-the-art for ancillae-free circuits [35] at the time of development.
Our circuit extraction procedure resynthesizes the CNOT gates in the circuit. Depending on the input circuit this can lead to drastic decreases in the 2-qubit gate count of the circuit [23,34], but in many cases it can also lead to an increase of the CNOT count. Such increases are avoided in the procedure of Ref. [35], where two-qubit gates are not resynthesized.
Yet re-synthesis of two-qubit gates may be necessary anyway in many applications: current and near-term quantum devices do not allow two-qubit gates to be applied to arbitrary pairs of qubits. Thus, general circuits need to be adapted to the permitted connections; this is called routing. Our extraction algorithm currently uses a standard process of Gaussian elimination to produce the two-qubit gates required to implement the circuit, which implicitly assumes that two-qubit gates can be applied between any pair of qubits in the circuit. It may be useful to replace this procedure with one incorporating routing, such as the ZX-calculus-based routing approach by Kissinger and Meijer-van de Griend [32]. This would allow circuits to be adapted to the desired architecture as they are being extracted.
It would also be interesting to consider whether these routing algorithms can be used more abstractly to transform general measurement patterns to more regular patterns with restricted connectivity.
Throughout this appendix, shall denote equality up to a global phase; that is, for linear maps L and K, we have L K iff there is an angle α ∈ [0, 2π) such that L = e iα K.
Recall from Definition 2.33 that the linear map implemented by a particular set of measurement outcomes in a pattern is called a branch of the pattern. We begin by making this more precise.
Definition A.1. Let P be a measurement pattern with n measurement commands. A branch of the pattern is a sequence p of length n whose entries are binary digits 0 and 1 together with a linear map P p , where each measurement is replaced with an outcome corresponding to either 0 or 1 as indicated by the corresponding entry in p.
If s is a binary sequence with k entries where k ≤ n, we may also define an 'intermediate pattern' P s by replacing the first k measurements in P by an outcome corresponding to the entries in s.
We may now state various forms of determinism (Definition 2.33) using the notation of the above definition. A pattern P with n measurement commands is deterministic if for any branches p and q there is a complex number c such that P p = c P q . It is strongly deterministic if for any branches p and q we have P p P q . It is stepwise deterministic if the pattern P s is deterministic for any binary sequence s with at most n entries. Recall that a pattern is uniformly deterministic if it is deterministic for any choice of measurement angles in the measurement commands appearing in P.
It will be useful to encode the measurement planes using following functions for any labelled open graph (G, I, O, λ).
The following observation will be the starting point for our proof of the main theorem.
Lemma A.2. Let s i be the binary digit indicating the measurement outcome of qubit i.
for both branches p.
where ≺ denotes concatenation of commands following the order ≺. Moreover, P Γ,g,α realizes the linear map associated with Γ at angles α (Definition 2.12) in the sense that for any branch p we have Remark A.6. Note that the pattern in equation (14) is uniformly deterministic, as it is (strongly) deterministic independently of the choice of α. It can be shown [17,Proposition 7.3.4] that any strongly deterministic pattern implements a unitary embedding. Thus the map in equation (15) is guaranteed to be one. In particular, if |I| = |O|, then the map is unitary.
Proof. First observe that P Γ,g,α is indeed runnable; the conditions (g1) and (g2) ensure that no command acts on a qubit that has been measured 6 , and all the other conditions in Definition 2.8 are easily verified from the shape of the pattern. For strong determinism, it is sufficient to show the claimed equality (15), as this shows that any two branches are equal (up to a global phase). The proof of the equality is based on Lemma A.2 and the following observation. Using the fact that the Pauli matrices are involutive, we show that for all i ∈ O, where I is the identity matrix. The first equality follows by taking u ∈ g(i) and j ∈ N G (u) such that j is in the even neighbourhood of g(i). Then the number of neighbours of j in g(i) is even, so that Z j appears in the product an even number of times. For the second equality, take u ∈ g(i) and j ∈ N G (u) such that j is in the odd neighbourhood of g(i). Then the number of neighbours of j in g(i) is odd, so that Z j appears in the product an odd number of times.
Thus we compute from equation (14): Here we used: (s1) The fact that the correction terms do not act on the vertex i.
(s3) Lemma A.2 and that X i and Z i anti-commute.
(s5) Equations (16) and the definition of X s i g(i) .
(s6) The facts that X i and Z i anti-commute, and that commands acting on different qubits commute.
(s7) Lemma A.4 and (g1) in order to commute the graph stabilisers past the liner maps.
Thus the pattern is strongly deterministic. It remains to show that P Γ,g,α is stepwise deterministic. Thus consider a subgraph of Γ (with I, O and λ restricted appropriately) obtained by removing some minimal vertices in the order ≺. The interpretation is that those vertices have already been measured. By conditions (g1) and (g2), for any vertex i in the subgraph, the correction set g(i) and its odd neighbourhood do not contain any of the removed vertices, and are thus themselves contained in the subgraph. Therefore we may restrict g to the subgraph, and the odd neighbourhood of each g(i) in the subgraph will be equal to its odd neighbourhood in the full graph. Thus the conditions (g3), (g4) and (g5) are satisfied by the subgraph. If we also restrict the partial order to the subgraph, we find that the conditions (g1) and (g2) are satisfied. Hence the gflow of the full graph restricts to a gflow on the subgraph. Therefore, since the measurements in P Γ,g,α follow the order ≺, we may think of each measurement as removing a minimal vertex in Γ. As we have just seen, the graph with some minimal vertices removed has a gflow given by restriction of the original gflow, so that repeating the preceding parts of the proof will yield another (strongly) deterministic pattern. This pattern is equal to the proper sub-pattern of P Γ,g,α which does not involve the alreadyperformed measurement and its corresponding corrections. Therefore, the original pattern is stepwise deterministic.

B Proofs for Section 3
The following lemma records how the odd neighbourhood of an arbitrary set of vertices changes under local complementation.
Now we can prove the main lemma.
Proof. We divide the proof into three parts, first proving that g (u) satisfies the required properties, then proving that g (v) for v = u satisfies (g1) and (g2), and finally showing that g (v) satisfies the conditions (g3)-(g5). Each part is further subdivided based on the values of λ(u) or λ(v). Part 1: Note that condition (g1) is trivially satisfied by g (u). We now show that g (u) satisfies the remaining conditions, subdividing into two cases based on λ(u).
So g (u) indeed satisfies all the required conditions. Part 2: We show that the correction set g(v) for any v = u satisfies Conditions (g1) and (g2) of the gflow definition. The proof is split into subcases depending on whether u ∈ Odd G (g(v)). Case 2a: If u / ∈ Odd G (g(v)), first note that Hence if w ∈ Odd G u (g(v)), then either w ∈ Odd G (g(v)) or w ∈ g(v), so v = w or v ≺ w; this is Condition (g2). Condition (g1) is trivially still satisfied. Case 2b: If u ∈ Odd G (g(v)), first note that v ≺ u. If w ∈ g (v), then w ∈ g(v) or w ∈ g (u) or w = u, and in each case either w = v or v ≺ w, this is Condition (g1).
Case 3b: Suppose v / ∈ N G (u) and distinguish cases according to λ(v).
• Suppose λ(v) = XY, this case is analogous to the corresponding one in Subcase 3a, so Condition (g3) is satisfied.
• Suppose λ(v) = YZ, then v ∈ g(v) and v / ∈ Odd G (g(v)). Furthermore, λ (v) = YZ. We have which together give Condition (g5). Proof. Recall that G ∧ uv = G u v u. Hence by applying Lemma 3.1 three times we get the correct underlying graph, which has a gflow. It remains to show that the labels change in the manner stated. Let us denote the labelling function associated with G u by λ (as in Lemma 3.1). Similarly, we denote the labelling and correction set functions for G u v by λ and g . We then compute: and by symmetry the same holds for u. For w ∈ O \ {u, v} we have , whence, denoting complementation in the vertex set by an overline, we obtain Similarly, we haveλ if w ∈ N G (v) and λ(w) = XZ λ(w) otherwise, which reduces toλ(w) = λ(w).

C Finding maximally delayed gflow in multiple measurement planes
The original algorithm for finding a maximally delayed gflow from Ref. [37] is restricted to patterns where all measurements are in the XY-plane. Here we extend this procedure to measurements in the YZ and XZ-planes, in the manner of Ref. [9]. We will always assume that any connected component of a labelled open graph contains an input or an output. This is because any connected component containing neither inputs nor outputs contributes only an irrelevant scalar factor to a measurement-based computation. In the presence of gflow, this assumption implies the following stronger property. Proof. Let (g, ≺) be a gflow on (G, I, O, λ), and let v ∈ V be arbitrary. We will show that either In the first case, there is nothing to prove. In the second case, consider the different measurement planes.
• Finally, suppose λ(v) = YZ, then v ∈ g(v) and v / ∈ Odd (g(v)) by (g5). Note that v / ∈ I since inputs cannot appear in correction sets. Also, v is not an output, but its connected component must intersect I ∪ O, so v must have a neighbour w. Proof. To show that V ≺ 0 ⊆ O: Assume towards a contradiction that there exists u ∈ V ≺ 0 \ O. Then u ∈ max ≺ V by Definition 3.9 and g(u) is either {u} or ∅ by (g1) of Definition 2.36.
• Thus g(u) = {u}. Now, since u is not an output, Lemma C.1 implies u has a neighbour; call this neighbour v. But u ∈ g(u) and v ∼ u imply v ∈ Odd (g(u)) and therefore u ≺ v by (g2).
This contradicts the property that u ∈ max ≺ V . Therefore such a u cannot exist and instead , which considers all outputs to be maximal. Then (g, ≺ ) is a gflow of (G, I, O, λ): • Conditions (g1) and (g2) of Definition 2.36 are satisfied by ≺ because elements of O are not in the domain of g • Conditions (g3)-(g5) are unaffected because the image of g has not changed.
Proof. We first show that if (g, ≺) is a maximally delayed gflow, then for any u ∈ V ≺ 1 , we have g(u) ⊆ O ∪ {u}. This is because if v ∈ g(u) and v = u, then u ≺ v by property (g1). Furthermore, by the definition of V ≺ 1 , if u ≺ v then v ∈ V ≺ 0 , and therefore v ∈ O by Lemma C.2. Combined with properties (g2) and (g3)-(g5), this shows that Odd otherwise.
Let ≺ be the partial order defined by 7 u ≺ v if        u = w and u ≺ v u = w and v ∈ K u = w and v ∈ Odd (g (w)) \ {w} (23) Then property (g1) of the gflow definition holds by the properties of (g, ≺) and the second case of the definition of ≺ . Property (g2) holds by the properties of (g, ≺) and the third case of the definition of ≺ . Properties (g3)-(g5) are satisfied for all v ∈ V \{w} since g has not changed for those vertices. For w itself, only one of properties (g3)-(g5) is relevant, and it is satisfied by condition j. Now, assume towards a contradiction that w / ∈ V ≺ 1 , i.e. assume that there exists k > 1 such that w ∈ V ≺ k . Our construction of ≺ in conjunction with condition j implies that w ∈ V ≺ 1 . For any u ∈ V define the depth of u, written d(u), as the index k such that u ∈ V ≺ k , and define d (u) as the index k such that u ∈ V ≺ k . Our assumption implies that d (w) < d(w), and our construction of ≺ implies that d (u) ≤ d(u) for all vertices u. This shows that: Therefore (g , ≺ ) is more delayed than (g, ≺), contradicting the assumption that (g, ≺) is maximally delayed. Hence any vertex w satisfying one of conditions 1-3 must be in V ≺ 1 , i.e. V ≺,XY We combine Lemma C.4 with Lemma C.2 to construct a partial converse to Lemma C.3; if we know that (g,≺) is maximally delayed on (G, I, O ∪ V ≺ 1 , λ), (with V ≺ 1 defined as in Lemma C.4) we want to show that (g, ≺) is maximally delayed on (G, I, O, λ). Proof. Recall from Lemma C.3 thatg is the restriction of g to the domain V \ (V ≺ 0 ∪ V ≺ 1 ) and≺ =≺ \(V ≺ 1 × V ≺ 0 ). Working towards a contradiction, suppose that (g , ≺ ) is a gflow of (G, I, O, λ) which is more delayed than (g, ≺). By Lemmas C.2 and C.4 we know that V ≺ 0 = V ≺ 0 and that V ≺ 1 = V ≺ 1 . Let j be such that: Such a j must exist, and be greater than 1, because (g , ≺ ) is more delayed that (g, ≺). Define (g ,≺ ) analogously to (g,≺): By Lemma C.3 (g ,≺ ) is maximally delayed for (G, I, O ∪ V ≺ 1 , λ), and by our assumption so is (g,≺). Our contradiction will be shown by noting that (g ,≺ ) is more delayed than (g,≺): and This contradicts (g,≺) being maximally delayed, and therefore (g, ≺) must be maximally delayed.
We now adapt the main gflow theorem and proof from Ref. [37,Theorem 2] to our multi-planar setting. The idea is to iteratively construct the layers V ≺ k , finding candidate correction sets via matrix manipulation in F 2 . • Correctness of gflow: At the k th recursive call, the set C constructed by the algorithm in the for loop at line 8 corresponds to the layer V ≺ k of the partition induced by the returned strict partial order (Definition 3.9). The construction of K corresponds to finding a correcting set in j≤k V ≺ j that has the desired properties for its odd neighbourhood (as specified in conditions (g2)-(g5) of the gflow definition, see Lemma C.4.) Furthermore the restriction K ⊆ O ensures condition (g1), thus if the algorithm returns a solution, then the solution satisfies the conditions of a gflow.
• Maximality of gflow: By induction on the number of non-output vertices, we prove that if the given labelled open graph has a gflow, then the algorithm outputs a maximally delayed gflow. Assume that the given labelled open graph has a gflow.
-The base case is when there is no non-output vertex, and here there is no correction needed: The empty flow (g, ∅) (where g is a function with an empty domain) is a maximally delayed gflow.
-For the inductive step suppose that there exist some non-output vertices; according to Lemma C.4 the elements of V ≺ 1 are those that satisfy one of the tests inside the loop at line 8. Thus, after the loop (line 19), C = V ≺ 1 . Since the existence of a gflow is assumed, V ≺ 1 cannot be empty (all non-output vertices have to be corrected), thus the algorithm is called recursively on the labelled open graph (G, I, O ∪ V ≺ 1 , λ) which has fewer non-output vertices. Lemma C.3 ensures the existence of a gflow in (G, I, O ∪ V ≺ 1 , λ), and by induction we know that the recursive call returns a maximally delayed gflow for (G, I, O ∪ V ≺ 1 , λ). By Lemma C.5 we know that this construction for V ≺ 0 and V ≺ 1 yields a maximally delayed gflow for (G, I, O, λ).
• Complexity: First form the matrix A from the adjacency matrix M by removing those rows that correspond to output vertices, and keeping all columns that correspond to outputs that are not also inputs (i.e. keep candidate vertices for K). Notice that finding a solution for K inside the for loop at line 8 consists of solving a system Ax = b j in F 2 , where b j is either a column vector with a 1 corresponding to the vertex u (in the XY and XZ cases), or b j is the column vector of all 0s (in the YZ case.) The resulting column vector x has a 1 in position x v if v ∈ K and a 0 otherwise.
Set n = |V |, = |O|, = |O \ I|, and j to range over the n − vertices in O. In order to solve these n − equations the block matrix if K exists then 16: C ← C ∪ {u} 17: g(u) ← K Assign a correction set for u 18: d(u) ← k Assign a depth value for u 19: if C = ∅ then 20: if O = V then 21: return (true, g, d) Halt, returning maximally delayed g and depth values d 22: else 23: return (false, ∅, ∅) Halt if no gflow exists 24: else 25: return GFLOWAUX(M , λ, I, O ∪ C, k+1, d)