Compilation of algorithm-specific graph states for quantum circuits

We present a quantum circuit compiler that prepares an algorithm-specific graph state from quantum circuits described in high level languages, such as Cirq and Q#. The computation can then be implemented using a series of non-Pauli measurements on this graph state. By compiling the graph state directly instead of starting with a standard lattice cluster state and preparing it over the course of the computation, we are able to better understand the resource costs involved and eliminate wasteful Pauli measurements on the actual quantum device. Access to this algorithm-specific graph state also allows for optimisation over locally equivalent graph states to implement the same quantum circuit. The compiler presented here finds ready application in measurement based quantum computing, NISQ devices and logical level compilation for fault tolereant implementations.


I. INTRODUCTION
The circuit model is ubiquitous in the field of quantum computing and is used to represent and analyse implementations of various quantum algorithms.However this is not a unique choice and other models also exist for instance that of measurement based quantum computing (MBQC) [1,2].In MBQC, one starts with a standard square lattice cluster state, which is a certain type of quantum graph state, and performs single qubit measurements to teleport the logical quantum state through the lattice.Gates are applied to the state by the choice of measurement basis and corrections due to the probabilistic nature of measurement outcomes are handled by dynamically adapting the basis of future measurements based on past outcomes [3] as well as classically tracking the Pauli frame [4].Pauli basis measurements are sufficient to perform Clifford operations, however to perform non-Clifford operations, additional rotated basis measurements are required.There have been recent efforts to build concrete methods for algorithm development in MBQC like software frameworks such as [5,6].
We present a compiler which employs an alternate workflow where instead of starting with the lattice cluster state, we prepare an algorithm-specific graph state.The latter is essentially a local Clifford equivalent graph state of the system after all the Clifford gates in the circuit have been applied.Once the algorithm-specific graph state is prepared, the computation is carried out by performing non-Pauli measurements on the qubits as before.In this manner we classically perform the Clifford part of the computation efficiently [7,8] and leave only the non-Clifford part to the quantum device.Specifying both an algorithm-specific graph state and a mea-surement procedure completely determines the quantum computation leaving us free to discard the circuit model completely and treat the computation as an instruction set to prepare a graph state and perform a sequence of measurements.This is conceptually similar to stabilizer simulators that use graph states as an underlying representation [9], however we demonstrate a compilation framework that accommodates arbitrary circuits and not just Clifford circuits.
In this framework, questions of optimal implementations can be addressed in terms of graph optimisation in local Clifford orbits (see section VI A) opening new avenues for physical architecture designs.The algorithmspecific graph state also naturally represents the connectivity requirements for implementing a particular algorithm allowing us to determine the minimum device resources needed.While from the perspective of compilation we require knowledge of both the algorithm-specific graph state as well the measurement procedure to implement the computation, for the purposes of algorithm benchmarking and quantum resource estimation, it sufficient to know the graph state and the number of non-Pauli measurements required.Another application of algorithm-specific graphs is for logical level compilation for a fault tolerant error correction code.By encoding the computation in a graph state, we dispense with the requirement of performing complex state evolution in an error corrected code and only require the generation of the graph state followed by logical qubit measurements.
To perform all the Clifford operations first, we compile a given quantum circuit described in high level languages, such as Cirq and Q#, in the so called ICM (Initialisation-CNOT-Measurement) form [10,11], the details of which are given in Sec.V A. The variation of the ICM form we use allows for any circuit to be implemented by a series of Clifford gates followed by non-Pauli measurements.The quantum state prepared by the circuit prior to measurement is a stabilizer state and hence can alternatively be generated from an algorithm-specific graph state by using appropriate local Clifford corrections [12].To determine this aglorithm-specific graph state, we simulate the Clifford gates directly using well known stabilizer simulation techniques [8,13] and we employ a graph conversion algorithm which computes a locally equivalent graph state given any stabilizer state (see section IV).It is worth pointing out here that there exists an interesting parallel to our techniques in the literature of circuit optimisation using the ZX calculus [14].The authors of [15] for instance are able to remove nodes in the ZX diagram which represent Clifford gates by appropriate rewrite rules.Determining whether there is a exact mathematical correspondence between these two techniques is left to future work.
This paper is structured as follows: In section II we detail some preliminaries about the stabilizer formalism, graph states and their representation as a tableau.Section III gives a brief introduction to measurement based quantum computing.In section IV we describe the stabilizer to graph state conversion and present an example.Then in section V A we review the ICM formalism and show how to decompose an arbitrary quantum circuit into a Clifford block initialisation followed by non-Pauli measurements.We explicitly show this workflow for the controlled-V † and the Toffolli gates in section V.In section VI A we discuss the implications of the local equivalence of graph states for resource optimisation and in section VI B we comment on the utility of the compiler for optical quantum computing, NISQ devices and fault tolerant implementations.A technical overview of our implementation of the compiler is given in section VII.Finally, we conclude and discuss future work in section VIII.
Our implementation of the compiler can be found at https://github.com/QSI-BAQS/Jabalizer.The source code for all the examples used in this paper can be found on the branch "manuscript".

II. PRELIMINARIES
Graph states are at the foundation of our compiler, and are an important subclass of stabilizer states widely used in measurement based quantum computing [1].Stabilizer states and graph states are related concepts and, in the following, we offer a brief review.

A. Stabilizer Formalism
Stabilizer states which were initially introduced as a formal description of quantum error correction codes [16][17][18] have been extensively studied and find wide application [17,19,20].We will now give a brief review of the mathematical formalism of stabilizer states, their representation and connection to graph states.The interested reader is referred to Sec. 10.5.1 of [21] for a through treatment.

Stabilizer states
Operations which map the set of stabilizer states to itself constitute the Clifford group of operations and the well known Gottesman-Knill theorem proves that all such operations can be efficiently simulated on a classical computer [7].
The Pauli group G n of n qubits is the set of all n-fold tensor product combinations of {±1, ±i, } × {I, X, Y, Z}, where I is the single qubit identity operator and X, Y , Z are the single qubit Pauli operators, respectively.We say that a commuting subgroup S n of G n stabilises a vector space V if for all elements s ∈ S n and v ∈ V, sv = v.These stabilizer groups will consist of Pauli group elements with coefficients +1 and -1 only since they must satisfy s 2 = I ⊗n .A concise way of representing these subgroups is by specifying a set of generators of the subgroup S = {s 1 , s 2 , . . ., s k } such that any element of the group can be represented as products of these generators.For a 2 n dimensional system, specifying n independent generators uniquely specifies a stabilizer state.The set of operations that map the set of all stabilizer states to itself is called the Clifford group of operations.If we only use Clifford operations and measurements in our quantum circuit, we can completely describe the evolution of the state vector classically by keeping track of the stabilizer generators of the state and their evolution under Clifford operations [8,9].

Evolving stabilizer states under Clifford operations
State evolution of stabilizer states can be performed in the Heisenberg picture by conjugating the stabilizers with the unitary operations acting upon the state since for the evolution where s i stabilizes the evolved state |ψ .The efficiency of classically simulating stabilizer circuits in this way was improved upon by [8] using the so-called CHP approach which tracks both stabilizers and anti-stabilizers (antistabilizers of a state along with the stabilizers span the entire Pauli group G n ), improving the performance of measurements within this model.We will confine our discussion to using just the stabilizer generators for simplicity since both methods are conceptually equivalent.

B. Tableau representation
Using the relations X 2 = Z 2 = I and ZX = iY , any stabilizer s ∈ S n can be thought of as tensor products of terms X x Z z and a phase (−1) Such a representation is known as a stabilizer tableau.For a stabilizer s ∈ S we refer to its corresponding row in the tableau, not including the final phase column, as r(s).The tableau representation has the useful property that for two stabilizers s i , s j ∈ S, r(s i ) + r(s j ) = r(s i s j ), where the addition is modulo 2. This means that we can replace any row in the tableau with its sum with another row if we correct for the phases, since if s i and s j are a valid stabilizer, so too is s i s j .The phase of the product s i s j can be efficiently computed by taking the product of the stabilizers term by term and multiplying together all the resulting phases.

C. Graph states
Given a mathematical graph defined through a vertex set V and an undirected, unweighted edge set E, the graph state is prepared by initialising qubits in the |+ state for each vertex and applying a controlled-Z operation between qubits with a shared edge.Equivalently, they are defined through a canonical stabilizer set of the form for all vertex labels i, where n i is the edge neighbourhood of the i th vertex in the graph.For a graph state, with a diagonal X-block (i.e.identity matrix), the Z-block in the tableau representation shown in Eq. (1) will correspond to the adjacency matrix of the abstract classical graph.Thus for a graph state the tableau will be of the form where A is the graph adjacency matrix and p is a column vector of phases which are by convention all +1.

III. MEASUREMENT BASED QUANTUM COMPUTING
This section is a brief introduction to measurement based quantum computing.It is not our intention to be comprehensive and the reader is referred to [3,22,23] for further details.We will confine our discussion to elements that have a direct relevance to algorithm-specific graph compilation.
In MBQC the primary computational resource is a cluster state which is a special kind of graph state.The edges in a cluster state are confined to be between nearest neighbour nodes and the nodes themselves are arranged in a regular lattice structure.We will focus on the canonical example of the the 2D square lattice in which MBQC is known to be universal.Note that other lattice structures are possible and 3D lattices in particular allow for fault tolerant topological codes [24].Once the cluster state is prepared the entire computation is carried out by a series of single qubit measurement rounds, with the measured observable for each qubit depending on the measurement outcomes from previous rounds.Once all the measurements are done, the result of the computation can be determined by classical post processing.We now look at the details of how a universal set of gates (and thus any quantum circuit) can be implemented using MBQC.

A. Gate implementation
The primary driver of circuit simulation on a cluster state is a teleportation protocol which allows us to perform single qubit rotations.An arbitrary single qubit state can be encoded into a linear cluster using the Controlled-Z operation as shown in figure 1. Measuring the observable , on the leftmost qubit has the effect of teleporting the state |ψ to the next site with the addition of a Hadamard gate and Pauli Z correction depending on the measurement outcome.Note that it is often convenient to represent A(θ) as the z-rotated X observable cos θX − sin θY .Repeating the gate teleportation circuit in figure 1 twice with measurements angles θ 1 and θ 2 and outcomes s 1 and s 2 , respectively, leads to the output state where in the second line we have commuted the Hadamard operator on the left through to meet the one on the right which converts the Z correction and rotation to an X correction and rotation with R x (θ) = HR z (θ)H.In the last line we commute the Z correction to the left using the relation e iθX Z = Ze −iθX .It is straightforward to see that repeating this procedure we are able to implement a series of alternating X and Z rotations, three of which is sufficient to implement any single qubit unitary gate.
There are two unwanted effects due to the measurement dependent Pauli corrections: 1) there is a Pauli operator of the form X fx( s) Z fz ( s) , where f x ( s) and f z ( s) are functions of the binary measurement outcome vector s (here s = (s 1 , s 2 ) T ); and 2) The R x and R z rotation angles are flipped from θ i to −θ i if an odd number of Z or X operators are commuted through it, respectively.
We do not need to do any active correction for the first effect since at the end of the circuit we are going to measure in the computational Z basis.The presence of a Z operator has no effect on the Z measurement and the presence of an X operator induces a relabeling between 0 and 1, which can be accounted for in post-processing as long as we keep track of all the measurement outcomes.
As for the second effect, as seen in equation ( 3), whether R x (θ) or R x (−θ) is implemented depends on the previous measurement outcome s 1 and in general all previous measurement outcomes for an arbitrary long sequence of gates.Since we can determine beforehand if there is a flip we will measure the observable A(−θ) instead of A(θ) to compensate for it.The general single qubit unitary in Euler normal form U (ζ, η, ξ), where U (ζ, η, ξ) = e −ζX/2 e −ηZ/2 e −ξX/2 , is implemented using a 5 qubit linear cluster as shown in figure 2a.CNOT gates can be implemented using a 15 qubit cluster state as shown in figure 2b.
Finally, the preparation of the state |ψ itself can be incorporated into the protocol by starting with a suitable single qubit rotation.Since arbitrary single qubit gates and CNOT gates together form a universal set of gates, any quantum circuit can be simulated on a 2D cluster state.

B. Circuit transpilation
A high level circuit, described in terms of gates acting on qubits, can be implemented using a 2D square lattice cluster state of sufficient size.In the following, we describe the implementation method.
Assuming that a circuit consists of single qubit rotations and nearest neighbour CNOTs (which any circuit can be implemented as) the first step is to etch out the circuit topology out of the lattice cluster state.This is done using Pauli Z measurements which disconnect the measured node from the cluster as shown in figure 3. Once we have this topology which will support the gates what remains is to measure the remaining qubits in an adaptive fashion to simulate the gate operations.
One could in principle implement the gates in the same order as they are done in the circuit description.However, it turns out this is not necessary, or indeed even prefer- is implemented by the linear cluster shown above by measuring the first qubit in Pauli X basis and the other three qubits in the A(θ) = cos θX − sin θY basis, with θ being ξ, η and ζ, respectively, for the second, third and fourth qubits from the left.Based on previous measurement outcomes, A(−θ) could me measured instead of A(θ).(b) A CNOT gate can be implemented between a source qubit and a target qubit using the 15 qubit cluster state shown and simultaneous Pauli measurements as indicated on each qubit.Figure 3: The logical circuit is transpiled for MBQC by 1) starting with a lattice cluster state of sufficient size.2) Performing Pauli Z measurements to disconnect qubits and produce the required topology.3) All Pauli measurements are performed in the first round of measurement (implements all Clifford gates).4) Adaptive measurements are performed on the remaining qubits over several rounds until the computation is finished.5) Post processing of the measurement results recovers the output of the logical circuit.
able [23].The basis in which to measure a particular qubit k depends on the measurement outcomes of a set of qubits b k which is called the backward cone of the qubit k.This implies that the qubits in the cluster state can be partitioned into disjoint sets {Q t } determining different measurement rounds.The measurement of the set Q 0 which contains qubits who's measurement basis does not depend on the outcome of any other measurement is conducted first.The results of this measurement round is used to determine the basis to measure qubits in Q 1 , and so on until the computation is finished.
The set Q 0 contains all the qubits on which Pauli measurements are to be performed.The reason for this is that the adaptive nature of the measurement basis is governed by the presence of a Pauli operator σ i ∈ {X, Y, Z} on the qubit to be measured.The measured observable σ j is transformed by this Pauli operator as σ i σ j σ i = (−1) δij σ j .In other words, we either measure σ j or −σ j which is just a relabelling of the measurement outcomes.All Clifford operations can be implemented in the MBQC scheme with just Pauli measurements, which means that the first round of measurement Q 0 implements all Clifford gates in the circuit.Note that this does not depend on the temporal ordering of these gates in the actual quantum circuit; in fact even the final computational basis state measurements at the end of the circuit are implemented in this first round.This doesn't mean that the output of the quantum circuit is known after the first round, the measurement outcomes of all the qubits are required to reconstruct the output of the logical circuit.As a consequence of this measurement based implementation of the Clifford gates, there will be Pauli corrections scattered throughout the circuit which have to be propagated either to the end of the circuit to modify the the final measurement outcomes or to the start of the circuit to initialise the Pauli frame which tracks the accumulation of Pauli operators on the qubits.
Since the set of stabilizer states is invariant under Pauli measurements, after the first round of measurement we are left with a stabilizer state |ψ S which is local Clifford equivalent to some graph state(s) [12,25].These Clifford equivalent set of graph states are referred to as algorithm-specific graph states and can be considered the fundamental resource required to implement the quantum logic circuit.

C. Algorithm based graph compilation
Instead of preparing the algorithm-specific graph state through measurement as described in section III B, we could instead efficiently compute it classically and prepare it directly.This is a consequence of the fact that all the operations that generate the algorithm-specific graph state are Clifford operations on stabilizer states and the Gottesman-Knill theorem [7].In this manner we are not performing any classically simulatable operations on a quantum device but saving those resources for the genuinely quantum part.
The most obvious way to generate an algorithm-specific graph is to start with a stabilizer description of a cluster state and following the procedure shown in figure 3 etch out the required circuit topology with Z basis measurements and perform all the Pauli basis measurements implementing the measurement round Q 0 .The obtained stabilizer state can be converted to a graph state using the procedure explained in section IV.
Alternatively, we can go one step further and not encode the Clifford gates into the cluster at all but simulate them as unitary gates at the logic circuit level before we encode the circuit into a graph state.To do this we recast the circuit in a form called the ICM (Initialise-CNOT-Measurement) form as described in section V A which allows all Clifford gates to be implemented first at the logical circuit level.Our method is described in Section V.

IV. STABILIZER STATE TO GRAPH STATE CONVERSION
Every stabilizer state can be converted to a graph state using local Clifford operations.This means that given any stabilizer tableau, we can perform a process akin to Gaussian elimination augmented with local Clifford operations to obtain a corresponding graph state [12].Note that this graph state is not unique and there could be multiple graph states which are local Clifford equivalent to a given stabilizer state.

A. Graph conversion algorithm
We now present an algorithm to convert any stabilizer state into a graph state (a pseudo-code is given in appendix B).The algorithm uses the fact that modulo 2 addition of the rows of the tableau corresponds to multiplication of the corresponding stabilizers, up to phase correction.i.e. for a stabilizer set S = {s 1 , s 2 , . . ., s n }, we can replace the row corresponding to s j in the tableau with rowsum(i, j), where rowsum is a function that does modulo 2 addition for the X and Z blocks of the two rows and computes the appropriate phase for the final column.An efficient way to compute this phase from the tableau is given in [8].To convert an arbitrary stabilizer to a graph state, we need to transform the X block into an identity matrix, and the Z block will automatically be symmetric to ensure that all the stabilizers commute [12].The full algorithm is as follows -Remove zero columns for the X block: a) If there is a zero column in the X-block, we perform a Hadamard operation by swapping that X column with its corresponding Z column since this Z column has to be non-zero.This is because a valid stabilizer state set cannot have both the Z and X column be all zeros for a qubit.That would imply that there is only an identity stabilising that qubit for all the stabilizers in the set which would leave the state of the qubit unspecified.

Make the X block lower triangular:
b) Iterating i from 1 to n , if X ii 1 = 1, we can set all X ji with j > i to equal 0 by replacing all rows j such that X ji = 1 with rowsum(i, j).We do this for all rows j > i. c) If for some i, X ii = 0, but there exists some j > i such that X ji = 1, we swap rows i and j and perform the logic in step b).
d) If there is no j ≥ i such that X ji = 1, there must exist some Z ji = 1, since we know that for every stabilizer state the X block can be made full rank using local operations and rowsums, the only freedom left is to bring it from the Z block.We perform a Hadamard operation swapping the i th column of the X and Z block to achieve this and perform the logic in step b) or c) depending on whether X ii is 0 or 1.

Make the X block diagonal:
e) For i from n to 1, if any X ji = 1, replace row j with rowsum(i, j).

Make Z block diagonal zero:
f) For all i such that Z ii = 1, we perform the phase gate transformation on qubit i which performs the transformation XZ → X, leaving us with Z ii = 0.

Make all phases positive:
g) For all rows with a negative phase we apply a Z gate transformation which takes X → −X.

B. Clifford circuit graph evolution example: GHZ
If we start with a Clifford circuit, we can apply the conversion algorithm directly.For example, taking the three qubit GHZ generation circuit, the steps for converting the GHZ state to a graph state are shown in Tab.I.The GHZ circuit, 1 Here the first subscript refers to the row and second the column.as shown in the first two rows of Tab.I.The remaining rows of Tab.I describes the conversion of this stabilizer state to a graph state through the application of Clifford gates (rowsums do not correspond to physical operations).Since this conversion process is invertible we can instead view this in the opposite direction -beginning with the algorithm-specific graph state in the final row of Tab.I and applying the inverse of the local transformation rules (in this case Hadamard gates on qubits 3 and 2) generates the output of the Clifford circuit.

V. A COMPILER FOR ALGORITHM-SPECIFIC GRAPH STATES
We introduce a circuit compiler formalism which allows us to systematically decompose any given circuit using T-state injection [21] and a variation of it using T gate teleportation which will form part of our compiler pipeline.
In this section, we show that the ability to perform all Clifford operations simultaneously in the first round of measurement in the MBQC model has a corresponding analogue in the circuit model.Here, instead of initialising a standard cluster state and performing Pauli measure- ments we implement non-Clifford gates using gate teleportation as shown in figure 6, which allows all Clifford gates to be implemented first.
We will also provide a step-by-step derivation of an algorithm-specific graph state for some typical quantum gates.The basic principles demonstrated can be extended to arbitrary quantum circuits.
In the MBQC model, the Clifford gates could be implemented first because of the invariance of Pauli measurement bases due to Pauli corrections as explained in sec III B. The corresponding property that allows this in the circuit model is that Pauli corrections due to the teleported non-Clifford gates when commuted through a Clifford gate do not change the Clifford gate but only the correction.This allows us to track the corrections and propagate them to the end of the circuit directly before subsequent measurements.This will impose a temporal ordering on the measurements that implement the teleported gates since the Pauli corrections due to previous measurements will determine the measurement bases for current measurements.

A. ICM formalism
The ICM form of a quantum circuit (figure 4) was introduced in [10] for logical level QEC compilation.It is a decomposition of an arbitrary circuit into three layers -1) an initialisation layer preparing all qubits into one of 4 states, 2) an array of CNOTs and 3) a staggered measurement block in the Z and X basis with the basis choice of subsequent blocks depending on the measurement outcomes of previous ones.
The ICM formalism can be understood to be systematic state injection with selective measurement corrections applied through the basis choice in the measurement blocks.While the state injection circuit in figure 5 1 with an input state H |ψ , U Z (θ) = I (i.e., A(θ = 0) = X) and a T gate acting on the output.Hence, the output of this circuit must be T HZ s H |ψ = T X s |ψ .When the measurement outcome is 0, we have a T gate applied as desired but when the outcome is 1 there is an X operator acting before T which we need to commute to the outside using the equivalence (up to global phase) T X = XT † .This gate can be corrected to a T gate by applying the operator P X hence confirming the circuit output in figure 5.
The CNOT and measurement blocks of the ICM form are Clifford operations and Pauli measurements, but the initialisation block requires the preparation of non-Clifford states for state injection.For our purpose we modify this scheme as shown in [11] known as inverse ICM, such that the initialisation block only prepares stabilizer states, but as a trade-off the measurements are now non-Pauli measurements.This is achieved using the gate teleportation circuit shown in figure 6.This circuit can be derived from the gate teleportation circuit in figure 1 by absorbing the Hadamard gate from the output to the right of the CZ gate and the Hadamard from the input |+ state to the left of it and setting U Z (θ) = T .

B. Controlled-V † graph states
Consider the Clifford + T gate decomposition of the controlled-V † gate show in figure 7. The V † gate is one Figure 6: T gate teleportation in the inverse ICM formalism where the measured observable is A π 4 = T † XT .T † can be similarly implemented by measuring the observable A − π 4 = T XT † .Using this subcircuit we are able to decompose any Clifford + T circuit into a stabilizer state initialisation followed by non-stablizer measurements.
of the square roots of the X gate and is defined as, The T and T † gates are implemented using the teleportation algorithm in figure 6 with the Pauli corrections propagated to the end of the circuit, as shown in figure 8.
In this example, all the non-stabilizer measurements can be performed simultaneously since the only Pauli corrections for the measured qubits are Z corrections that changes the measurement of A(θ) → ZA(θ)Z = −A(θ), which is a classical relabelling of the measurement out-  comes.However, this is not the case in general, for example, if we wanted to now implement a T gate on the qubit b out , the presence or absence of the X correction will decide the measurement basis since XA(θ)X = A(−θ).
Note that in the state injection picture of the ICM form, measurement outcomes determined whether T gates or T † gates were implemented due to the Pauli X correction, we can see the same holds true here.We will not apply unitary corrections for these Pauli gates but rather modify the the measurement basis to account for them.
For example if we know based on previous measurement that an X gate correction acts on a qubit before an A(θ) basis measurement, we will instead perform an A(−θ) basis measurement.These Pauli corrections thus give a time ordered measurement pattern with feed-forward which can be determined at compilation time using Pauli tracking [4].
For inputs |ab = |00 , the state of the system before the measurements are performed, ignoring the Pauli corrections is |0000+ + |0110− , where |± = |0 ± |1 .This is locally equivalent to the graph state shown in figure 9 (see App.A for a detailed derivation).We can equivalently start with the graph state and apply the local corrections to generate the same output using the circuit given in figure 10.
The intuition from the graph in figure 9 is that qubits 1 and 4 should factor out in the pre-measurement state -indeed, this is true for the state |0000+ + |0110− .
To perform the controlled-V † gate we implement the measurements in the A − π 4 basis on qubits 1, 2 and A π 4 on qubit 3.For ease of calculation, we apply the local corrections explicitly rather than absorbing them into the measurement basis and also assume the measurement outcomes r = t = s = 0.This can always be achieved in practice by measuring the qubits 2 and 3 in the basis of −A − π 4 and −A π 4 , respectively, if r = 1 and applying a X correction to qubit 5 if t ⊕ s = 1.
The operators A ± π 4 can be written in their spectral decomposition as, as 0, projects the computational basis states as, The measurement projects the state as, giving the output state in the last two qubits as, This is of course the output of a controlled-V † acting on the input |ab = |00 .A less trivial example is given by choosing the input to be |ab = |+0 .This is equivalent to applying a Hadamard gate to the first qubit at the beginning of the circuit in figure 8. Since this only initialises a different stabilizer state and all Pauli corrections are introduced after this point, the measurement sequence and how future measurements are influenced by previous measurement outcomes remain unchanged.In this case, the stabilizer state produced before measurement is given by, which can be converted to the graph state shown in figure 11 (see appendix A for a full derivation).The post measurement state in this case will be, Normalising the state above and tracing out the measured qubits, we get the output state as, The V † gate acts on the computational basis states as, The action of the controlled-V † on the initial state |+0 can be seen to be, as expected.

C. Toffoli gate graph states
We describe the decomposition of a Toffoli gate.We start with the seven T gate decomposition shown in figure 12.We apply the same procedure as before and teleport the T gates using the circuit in figure 6.The graph states produced along with the local corrections and qubits to be measured are given in Tab.II.From the generated graphs it is clear that one can only generate entanglement between all the output qubits if both the controls of the Toffolli gate are active (not in the |0 state).

VI. DISCUSSION
It is important to note that the graph states produced by our algorithm are not unique.There can be many graph states that are equivalent under local Clifford operations [12] and hence locally equivalent to the output stabilizer state of the circuit.This fact can be exploited to optimise over different graph state implementations.We now discuss this in more detail as well as consider the hardware compatibility of our approach.

A. Resource optimisation
It has been shown that transformation between graph states using local Clifford operations is completely characterised by repeated application of the local complementation operations [12].In graph theoretic terms for a vertex k with neighbourhood n k , the local complementation will invert the edges (remove if there is one, add if there is not) between all vertices in n k .Physically this operation on vertex k is performed by the operation Whether two graph states are indeed equivalent under local Clifford operations can be determined in polynomial time [26].This gives us an additional degree of freedom to optimise our algorithm-specific graph state.Depending on the physical resources it might be desirable to reduce the total edge count, degree (number of edges per node) or prepare an initial state with a simple topology such as a linear graph state.All these problems are well studied in classical graph theory and find ready application here [27][28][29].

B. Compatible architecture
MBQC based circuit execution is well suited to many optical quantum computing architectures [30] as well as NISQ devices [31].However, the compilation workflow presented here is not confined to any particular device architecture as one could use it as the logical level representation for any quantum device.An attractive application would be in the execution of fault tolerant quantum circuits using a suitable error corrected code such as the surface code [32][33][34].The nodes of the algorithm-specific graph will now be logical qubits in a protected subspace.This could offer several simplifications during run time as the only gates that need to be implemented are the ones that generate the graph state (H and CZ) and gates to implement fault tolerant rotated basis measurements dispensing with the need for lattice surgery (outside graph state generation) and dynamical ancillary routing operations [35].This makes exact determination of the space-time volume of executing quantum algorithms in a fault tolerant manner much more tractable than it is presently.

VII. TECHNICAL OVERVIEW
In this section we describe the technical details of how different parts of the compiler pipeline are built.The ICM part of the pipeline has been built using Cirq, so Q# circuits are first translated into Cirq circuits.Nevertheless, our compiler is output agnostic and can generate OpenQasm circuits, too.

A. Q# to Cirq conversion
In order to support high-level Q# algorithms [36], an intermediate component is needed to extract the circuit representation.Q# is a functional language which is suited for hybrid quantum-classical algorithms and often exhibits non-trivial branching.
The Q# host program uses a simulator backend to perform compilation into a qubit register and a sequence of high-level operations.The choice of simulator backend depends on the nature of the experiment being run, but all are supported through the same translation extension.These operations are decomposed into primitive operations including X, Y, Z, H, S, T, CX, R, Measure gates.In the case of state space simulator backends, operations are sometimes only decomposed into SWAP, CCNOT, CCX, CCZ gates.
To yield a circuit representation, an extension attaches a hook to the simulator before the experiment is run.Dur-

H4H5H6H7H8H9
Table II: Toffoli gate implementation for different inputs.The input and output qubits are shown in green and blue respectively.The circuit is implemented by generating the graph state, applying the local corrections and measuring all the qubits except the output qubits in the A ± π 4 basis.Pauli corrections to the measurements are assumed to be classically tracked through the circuit to determine the measurement sequence.ing execution, a call is sent to the extension during each operation for parsing.If real-time operation is desired, the gates are broken down and streamed to the next stage of the pipeline.Otherwise, the circuit representation of the experiment from start to finish is stored in an output file.As our desired output format is Cirq, the stream of operations is converted into an initialisation of a Cirq qubit register and a sequence of moments.A standalone module implementing this part of the pipeline can be found at https://github.com/QSI-BAQS/Trace2Cirq.git.

B. ICM compilation
To perform an ICM decomposition, the compiler has to take a given Clifford + T gate circuit and teleport all T and T † gates using ancillas, as described in Sec.V A. The Clifford + T decomposition can be performed using Cirq's native decompose protocols and the main technical challenge left is when the compiler encounters a T gate it needs to determine which wire this gate must act on depending on previous gate teleportations.For example, in the controlled-V † decomposition in figure 8, when the compiler decomposes the T gate, it needs to know that the decomposition is applied to the second wire if the T † gate has already been decomposed.To do this the compiler adds an op_id flag to every operation to keep track of the order of the operations and the class SplitQubit is used to track teleported qubits.When a gate is teleported, a new SplitQubit instance is created for the ancilla which inherits the op_id of the operation that generated it and a reference to this new wire is stored in the original one.Comparing a gate's op_id with the those of the wire and it's children, the compiler is able to determine the correct wire to apply the decomposition on.

C. Stabilizer simulation
The stabilizer simulator has been designed with a top level data structure which uses an integer array to track the tableau defined in Sec.II B and a backend which makes calls to a CHP simulator.This gives front-end access to the conceptually simpler tableau description while still using the more efficient CHP formalism for actual computation.The stabilizer backend we use is STIM [13].
This part of the pipeline receives the inverse ICM decomposed Cirq circuit and computes the output stabilizer state produced by the circuit before the measurement sequence is applied.The graph conversion algorithm described in Sec.IV A is now applied to the stabilizer state and the compiler returns the appropriate graph state and a list of local operations that convert it back to the output stabilizer state.The full compiler can be found here https://github.com/QSI-BAQS/Jabalizer.

VIII. CONCLUSION
We have presented a compiler to generate an algorithmspecific graph state based on a circuit description.It was shown that once the circuit is decomposed into a Clifford + T gate circuit using standard quantum circuit libraries we can use a teleported T gate implementation of the circuit.This allows the circuit to have a Clifford gate initialisation followed by non-Pauli measurements.The Clifford initialisation is simulated efficiently and an algorithm-specific graph state is extracted.Hardware level implementation of any circuit is now possible by generating this graph state, applying the local corrections and performing nonstabilizer measurements supplemented with tracking the Pauli frame.Our implementation is built for using Cirq circuits as input, but also accepts Q# circuits which are internally converted to Cirq circuits.
One of the main advantages of implementing the circuit in this way is that we are able to separate classical and quantum resources needed for a given quantum algorithm because the Clifford part of the circuit is classically simulated.The graph structure gives us an intuitive understanding of the entanglement structure that the algorithm utilised and how different qubits are connected and if some are superfluous.This leads naturally to questions of optimisation since the generated graph state is not unique.Depending on the physical hardware, different optimisations could be desirable such as reducing edge count, node degree etc. similarly to the co-design approach described in [37].
It is an open question whether the algorithm-specific graph state generated using our method is the same as the the one generated by the traditional circuit etching method on the 2D cluster state, and is the subject of future work.Table VI: Graph conversion of the final state in Tab.V

Figure 1 :
Figure 1: Gate teleportation circuit: Here R z (θ) = e −iθZ/2 and measuring the observable A(θ) = R † z (θ)XR z (θ) on the first qubit has the effect of teleporting the state R z (θ) |ψ to the next site with the addition of a Hadamard gate and Pauli Z correction depending on the measurement outcome.

Figure 2 :
Figure 2: (a) An arbitrary single qubit unitary in the Euler representationU rot = e −i ζ 2 Z e −i η 2 X e −i ξ 2 Zis implemented by the linear cluster shown above by measuring the first qubit in Pauli X basis and the other three qubits in the A(θ) = cos θX − sin θY basis, with θ being ξ, η and ζ, respectively, for the second, third and fourth qubits from the left.Based on previous measurement outcomes, A(−θ) could me measured instead of A(θ).(b) A CNOT gate can be implemented between a source qubit and a target qubit using the 15 qubit cluster state shown and simultaneous Pauli measurements as indicated on each qubit.

Figure 4 :
Figure 4: ICM form of a quantum circuit.The I block initialises each qubit in either the state |0 or the states |0 + e ikπ |1 , with k ∈ {0, 1/2, 1/4}.The CNOT block is an array of CNOTs.The measurement blocks performs measurement in the X or Z basis and subsequent measurements bases are decided based on the outcome.

Figure 8 :
Figure 8: Teleported T gate implementation of the controlled-V † gate with the Pauli corrections propagated to the end.

Figure 9 :
Figure9: Local Clifford equivalent graph state before measurement for the controlled-V † decomposition in figure8for inputs |ab = |00 .The input a and output qubits are shown as green and blue, respectively.The circuit is implemented by applying local Clifford gates and measuring all the qubits except the output qubits.

Figure 10 :
Figure10: Compiled circuit: The Hadamard and the CZ gates prepare the graph state given in figure9and then the local corrections are applied to generate the same output as the circuit in figure8.In practise the local corrections can be absorbed into the measurement basis choice as shown for the first 3 qubits.The remaining corrections are also similarly propagated forward until measurement.

Figure 11 :
Figure 11: Locally equivalent graph state before measurement produced by the circuit in figure 8 for inputs |ab = |+0 .Input and output nodes are shown in green and blue, respectively.
Table III: Gate transformations of the controlled-V † decomposition give in figure 8 excluding final measurements for inputs |ab = |00 .Table IV: Graph conversion of the final state in Tab.III.Input qubits are colored green and output qubits are colored blue.Gate transformations of the controlled-V † decomposition give in figure 8 excluding final measurements for inputs |ab = |+0 .

Table I :
Conversion of a 3 qubit GHZ state to a graph state.The action of a Hadamard on a qubit is to swap Zs and Xs for the column corresponding to the qubit.
rowsum(i → j) replaces s j with s i s j .prepares the state |ψ = |000 + |111 (we assume implicit normalisation throughout this paper for simplicity).Starting from the canonical stabilizer for the all zero state, the circuit does the following stabilizer transformation, can be verified directly, it can also be understood from the basic MBQC gate teleportation circuit in figure1.Using the fact that .Here we have commuted the T gate through the control of the CNOT gate.Using the fact that conjugating the target of a CNOT gate with a Hadamard induces a CZ gate, we can see this circuit is equivalent to the gate teleportation circuit in figure 4 X k is the square root of X operation acting on qubit k and √ iZ = e i π 4 Zj is the square root of Z operation acting on qubit j.If two graph states |G and |G are equivalent under local Clifford transformation there exist some finite set of local complemention operations {l i } such that,