Introduction to the ZXcalculus — PennyLane documentation
Creator: Romain Moyard. Posted: 6 June, 2023.
The ZXcalculus is a graphical language for reasoning about quantum computations and circuits. Launched by Coecke
and Duncan , it may symbolize any linear map, and may be thoughtabout a diagrammatically full
generalization of the same old circuit illustration. The ZXcalculus relies on class principle, an method to
arithmetic which research objects when it comes to their relations fairly than in isolation. Thus, the ZXcalculus
gives a rigorous solution to perceive the construction underlying quantum issues, utilizing the hyperlink between quantum
operations fairly than the quantum operations themselves.
On this tutorial, we first give an outline of the constructing blocks of the ZXcalculus, known as ZXdiagrams,
and the foundations for reworking them, known as rewriting guidelines. We additionally present how the ZXcalculus may be prolonged to ZXH
calculus. The ZXcalculus can also be promising for quantum machine studying, thus we current how the parametershift rule
may be derived utilizing ZXdiagrams. We are going to then bounce to the coding a part of the tutorial and present how PennyLane is
builtin with PyZX , a Python library for ZXcalculus, and how one can rework your circuit to a
ZXdiagram. We then apply what we’ve realized to be able to optimize the variety of Tgates of a recognized benchmark circuit.
We additionally present that simplifying a ZXdiagram doesn’t all the time find yourself with a diagramlike graph, and that circuit
extraction is a most important ache level of the ZX framework. This tutorial will give a broad overview of what ZXcalculus can
supply while you need to analyze quantum issues.
ZXdiagrams
This introduction follows the works of the East et al. and van de Wetering . Our aim is to
introduce a whole language for quantum data, for which we’d like two components: ZXdiagrams and their
rewriting guidelines. We begin by introducing ZXdiagrams, a graphical depiction of a tensor community representing an
arbitrary linear map. Later, we are going to introduce ZX rewriting guidelines, which along with diagrams defines the
ZXcalculus. We observe the scalar conference of East et al. (it’s extra appropriate to the multiH field
conditions, see the ZXH part).
A ZXdiagram is an undirected multigraph; you’ll be able to transfer vertices with out affecting the underlying
linear map. The vertices are known as Z and Xspiders, which symbolize two sorts of linear maps. The sides are known as
wires, and symbolize the size on which the linear maps are appearing. Due to this fact, the perimeters symbolize qubits in
quantum computing. The diagram’s wires on the left and proper are known as inputs and outputs, respectively.
The primary constructing block of the ZXdiagram is the Zspider. In many of the literature, it’s depicted as a inexperienced vertex.
The Zspider takes an actual part (alpha in mathbb{R}) and represents the next linear map (it accepts any
variety of inputs and outputs, and the variety of inputs doesn’t have to match the variety of outputs):
It’s straightforward to see that the same old Zgate may be represented with a singlewire Zgate:
As you’ve most likely already guessed, the second constructing block of the ZXdiagram is the Xspider. It’s often depicted
as a crimson vertex. The Xspider additionally takes an actual part (alpha in mathbb{R}) and it represents the next
linear map (it accepts any variety of inputs and outputs):
It’s straightforward to see that the same old Xgate may be represented with a singlewire Xspider:
From strange quantum principle, we all know that the Hadamard gate may be decomposed into X and Z rotations, and might due to this fact
be represented in ZXcalculus. In an effort to make the diagram simpler to learn, we introduce the Hadamard gate as a yellow
field:
This yellow field can also be usually represented as a blue edge to be able to additional simplify the show of the diagram.
Beneath, we are going to talk about a generalization of the yellow field to a 3rd spider, forming the ZXHcalculus. It is vital
to notice that the yellow field is by itself a rewrite rule for the decomposition of the Hadamard gate. The yellow field
permits us to put in writing the connection between the X and Zspider as follows.
A particular case of the Z and Xspiders are diagrams with no inputs (or outputs). They’re used to symbolize states that
are unnormalized. If a spider has no inputs and outputs, it merely represents a fancy scalar. You’ll find the same old
illustration of quantum states beneath:
Equally, you get the (vert 1rangle) state and (vert rangle) state by changing the zero part with
(pi).
The phases are (2pi) periodic, and when a part is the same as (0) we omit the zero image from the spider.
A easy inexperienced vertex is a Zspider with zero part and a easy crimson vertex is an Xspider with zero part.
Now that we have now these two fundamental constructing blocks, we will begin composing them and stacking them on high of one another.
Composition consists of becoming a member of the outputs of a diagram to the inputs of one other diagram. Stacking two
ZXdiagrams on high of one another represents the tensor product of the corresponding tensors.
We illustrate the foundations of stacking and composition by constructing an equal CNOT gate (as much as a world part). We
begin by stacking a single wire with a phaseless Zspider with one enter wire and two output wires. We present the
ZXdiagram and corresponding matrix beneath:
Subsequent, we stack a single wire with a phaseless Xspider with two enter wires and single output wire. Once more, we offer
the matrix:
Lastly, we compose the 2 diagrams, which means that we be a part of the 2 outputs of the primary diagram with the 2 inputs of
the second diagram. By doing this we receive a CNOT gate — you’ll be able to persuade your self by doing the matrix multiplication
between the 2 diagrams.
We’ve already talked about {that a} ZXdiagram is an undirected multigraph; the place of the vertices doesn’t matter,
nor does the trajectory of the wires. We will transfer vertices round, bend and unbend, and cross and uncross wires
so long as the connectivity and the order of the inputs and outputs is maintained. Specifically, bending a line so
that it adjustments path from left to proper, or viceversa, isn’t allowed. None of those deformations have an effect on the
underlying linear map, which means that ZXdiagrams have all kinds of topological symmetries. As an example,
the 2 diagrams beneath each symbolize the CNOT gate:
Which means we will draw a vertical line with out ambiguity, which is the same old approach of representing the CNOT gate:
We’ve simply proven that we will categorical any Z rotation and X rotation with Z and Xspiders. Due to this fact, it’s enough
to create any onequbit rotation on the Bloch sphere. By composing and stacking,
we will additionally create the CNOT gate. Due to this fact, we have now a common gate set! We will additionally create the (0) state and
(+) state on any variety of qubits. Due to this fact, we will symbolize any quantum state. Normalization is likely to be wanted
(e.g., for the CNOT gate) and we carry out this by including advanced scalar vertices.
It seems that the flexibility to symbolize an arbitrary state implies the flexibility to symbolize an arbitrary linear
map. Utilizing a mathematical consequence known as the ChoiJamiolkowski isomorphism , for any linear map (L)
from (n) to (m) wires, we will bend the incoming wires to the correct and discover an equal state on
(n + m) wires. Thus, any linear map is equal to some state, and since we will create any state,
we will create any map! This exhibits that ZXdiagrams are a common instrument for reasoning about linear maps. However this
doesn’t imply the illustration is straightforward!
For a extra indepth introduction, see and .
ZXcalculus: rewriting guidelines
ZXdiagrams coupled with rewriting guidelines kind the ZXcalculus. Beforehand, we introduced the foundations for composing and
stacking diagrams and talked in regards to the topological symmetries equivalent to deformations. On this part,
we offer rewriting guidelines that can be utilized to simplify diagrams with out altering the underlying linear map. This could
be very helpful for quantum circuit optimization and for exhibiting that some computations have a quite simple kind within the
ZX framework (e.g., teleportation).
Within the following guidelines, the colors are interchangeable.

For the reason that Xgate and Zgate don’t commute, nonphaseless vertices of various coloration don’t commute.

The fuse rule applies when two spiders of the identical sort are related by a number of wires. We will fuse
spiders by merely including the 2 spiders’ phases and eradicating the connecting wires. 
The (pi) copy rule describes tips on how to pull an Xgate via a Zspider (or a Zgate via an Xspider).
Since X and Z anticommute, pulling the Xgate via a Zspider introduces a minus signal into the Z part. 
The statecopy rule captures how easy onequbit states work together with a spider of the alternative color. It
is just legitimate for states which are multiples of (pi) (due to this fact (a) is an integer), so we have now
computational foundation states (within the X or Z foundation). Principally, should you pull a foundation state via a spider of the
reverse coloration, it copies it onto every outgoing wire. 
The id rule states that phaseless spiders with one enter and one output are equal to the id
and might due to this fact be eliminated. That is much like the rule that Z and X rotation gates, that are phaseless,
are equal to the id. This rule gives a solution to do away with selfloops. 
A bialgebra is a mathematical construction with a product (combining two wires into one) and a coproduct (
splitting a wire into two wires) the place, roughly talking, we will pull a product via a coproduct at the price of
doubling. That is much like the relation loved by the XOR algebra and the COPY coalgebra. This rule isn’t
simple to confirm and particulars may be present in . 
The Hopf rule is a bit just like the bialgebra rule, telling us what occurs once we attempt to pull a coproduct
via a product. As an alternative of doubling, nevertheless, they decouple, leaving us with an unconnected projector and a state.
Once more, this relation is glad by XOR and COPY, and the corresponding algebraic construction is known as a Hopf
algebra. This seems to observe from the bialgebra and the statecopy rule , but it surely’s helpful to document
it as a separate rule.
Teleportation
Now that we have now all the required instruments, let’s see tips on how to describe teleportation as a ZXdiagram and simplify it
with our rewriting guidelines. The outcomes are surprisingly elegant! We observe the reason from . You’ll be able to
discover an introduction to teleportation in
the MBQC demo.
Teleportation is a protocol for transferring quantum data (a state) from Alice (the sender) to Bob (the
receiver). To carry out this, Alice and Bob first have to share a maximally entangled state. The protocol for Alice to ship
her quantum state to Bob is as follows:

Alice applies the CNOT gate adopted by the Hadamard gate.

Alice measures the 2 qubits that she has.

Alice sends the 2 measurement outcomes to Bob.

Given the outcomes, Bob conditionally applies the Z and Xgate to his qubit.

Bob finally ends up with the identical state as Alice beforehand had. Teleportation is full!
Within the strange quantum circuit notation, we will summarize the process as follows:
Allow us to convert this quantum circuit right into a ZXdiagram. The measurements are represented by the state Xspider
parameterized with boolean parameters (alpha) and (beta). The cup represents the maximally entangled
state shared between Alice and Bob. As you may anticipate from earlier feedback about bending wires, their shared state
is ChoiJamiolkowskiequivalent to the id linear map.
Let’s simplify the diagram by making use of some rewriting guidelines. Step one is to fuse the (a) state with the
Xspider of the CNOT. We additionally merge the Hadamard gate with the (beta) state, as a result of collectively it represents a
Zspider. Then we will fuse the three Zspiders by merely including their phases. After that, we see that the Zspider
part vanishes (modulo (2pi)) and might due to this fact be simplified utilizing the id rule. Then we will fuse the
two Xspiders by including their phases. We discover that the part once more vanishes modulo (2pi) and we will get rid
of the final Xspider. Teleportation is a straightforward wire connecting Alice and Bob!
The ZXHcalculus
The universality of the ZXcalculus doesn’t assure the existence of a easy illustration, even for easy
linear maps. For instance, the Toffoli gate (the quantum AND gate) requires round 25 spiders (Z and X)! We beforehand
launched the Hadamard gate as a yellow field, which motivates the introduction of a brand new generator: the multileg Hbox,
outlined as follows:
The parameter (a) may be any advanced quantity, and the sum is over all (i_1, … , i_m, j_1, … ,
j_n in {0, 1}). Due to this fact, an Hbox represents a matrix the place its entries are equal to (1) apart from the
backside proper aspect, which is (a). This can enable us to concisely categorical the Toffoli gate, as we are going to see
shortly.
An Hbox with one enter wire and one output wire, with (a=1), is a Hadamard gate as much as international part. Thus,
we omit the parameter when it is the same as (1). The Hadamard gate is typically represented by a blue edge
fairly than a field.
Due to the introduction of the multileg Hbox, the Toffoli gate may be represented with three Zspiders and three
Hboxes — two easy Hadamard gates and one threeary Hbox — as proven beneath:
The addition of the multileg Hbox along with an extra set of rewriting guidelines types the ZXHcalculus. You’ll be able to
discover extra particulars and the rewriting guidelines within the literature .
Let’s present that this ZXHdiagram is certainly a Toffoligate. This operation is outlined by conditionally making use of an
Xgate on the goal wire, it implies that solely the state (ket{110}) and (ket{111}) is not going to map to
themselves ((ket{110}) to (ket{111}) and (ket{111}) to (ket{110})). We are going to present that if
one gives the state (ket{11}) on the 2 first wires, it outcomes to a bit flip on the third wire (Xgate).
For that goal, we have to add a brand new rewriting rule that’s a part of the ZXHcalculus: the take up rule.
We begin by making use of our Toffoli diagram on a (ket{11}) state, which corresponds to 2 Xspiders with a part
of (pi) stacked with our diagram. We apply the copy rule on the 2 teams of Xspiders and Zspiders on the
wires 0 and 1. After that we will apply the newly launched take up rule on one of many Xspiders related to the
HField. Then we acknowledge the Fourier relation and might exchange the Xspider and HField by a Zspider. Then it’s straightforward
to use the fuse rule on the 2 Zspiders. Once more, we acknowledge the Fourier relation and acquire a single Xspider on
the goal wire. We simply proved that by offering the (ket{11}) state on the 2 management wires,
it all the time applies an Xspider on the goal. It implies that we have now a bit flip on the goal.
In case you do the identical process with the others states on the 2 controls ((ket{00}), (ket{11}),
(ket{10}), (ket{01})) with barely totally different guidelines (the explosion rule), you’ll all the time find yourself with
an empty goal and an identical states for the controls. We then have proved that our ZXHdiagram is certainly the Toffoli
gate!
The ZXcalculus for quantum machine studying
We now transfer away from the usual use of the ZXcalculus to be able to present its utility for calculus and,
extra particularly, for quantum derivatives (the parametershift rule). What follows isn’t applied in PennyLane or PyZX.
By including derivatives to the framework, it exhibits that the ZXcalculus has a job to play in analyzing quantum machine
studying issues. After studying this part, try to be satisfied that the ZXcalculus can be utilized to review any
sort of quantumrelated drawback.
Certainly, not solely is the ZXcalculus helpful for representing and simplifying quantum circuits, but it surely was proven that we
can use it to symbolize gradients and integrals of parameterized quantum circuits . On this part,
we are going to observe the proof of the concept that exhibits how the spinoff of the expectation worth of a Hamiltonian
given a parameterized state may be derived as a ZXdiagram (theorem 2 of Zhao et al. ). We may even present
that the concept can be utilized to show the parametershift rule!
Partial spinoff as a ZXdiagram
Let’s first describe the issue. With out lack of generalization, let’s suppose that we start with the pure state
(ket{0}) on all (n) qubits. Then we apply a parameterized unitary (U) that is determined by (vec{
theta}=(theta_1, …, theta_m)), the place (theta_i in [0, 2pi]).
Consequently, the expectation worth of a Hamiltonian (H) is given by:
[braket{H} = bra{0} U(vec{theta}) H U(vec{theta})^{dagger} ket{0}.]
We now have seen that any circuit may be represented by a ZX diagram, however as soon as once more, we need to use the graphlike kind
(see the Graph optimization and circuit extraction part). There are a number of guidelines that make sure the transformation
to a graphlike diagram. We exchange the 0 state by crimson phaseless spiders, and we rework the parameterized circuit
to its graphlike ZX diagram. We name the obtained diagram (G_U(vec{theta})), this diagram is the same as
the unitary as much as a relentless (c).
Now we are going to examine the partial spinoff of the diagram representing the expectation worth. The theory is
the next:
Let’s show theorem 2, and first we contemplate a partial spinoff on the spider with respect to (theta_j). The
spider essentially seems on each side, however they’ve phases of reverse indicators and inverse inputs/outputs. By merely
writing their definitions and increasing the formulation, we receive:
Now we have now a easy formulation the place we will simply take the spinoff:
The theory 2 is proved — we simply expressed the partial spinoff as a ZXdiagram!
Parametershift rule as a ZXdiagram
This theorem can be utilized to show the parametershift rule. Let’s contemplate the next ansatz that we rework to
its graphlike diagram.
We then apply the earlier theorem to get the partial spinoff relative to (theta_1).
The second step is to take the Xspider with part (pi) and explicitly write the formulation (ket{+}bra{+}
– ket{}bra{}). We will then separate the diagram into two components by recalling the definition of the (ket{+})
(phaseless Zspider) and (ket{}) ((2pi) Zspider) states and utilizing the fusion rule for the Zspider.
We receive the parametershift rule!
You’ll find extra details about the differentiation and integration of ZXdiagrams with QML functions within the
following paper .
ZXdiagrams with PennyLane
Now that we have now launched the formalism of the ZXcalculus, let’s dive into some code and present what you are able to do with
PennyLane! PennyLane v0.28 added ZXcalculus performance to the fold. You should use the
to_zx()
rework decorator to get a ZXdiagram from a PennyLane
QNode
, whereas from_zx()
transforms a ZXdiagram right into a PennyLane
tape. We’re utilizing the PyZX library beneath the hood to symbolize the ZX diagram. As soon as your circuit is a PyZX
graph, you’ll be able to draw it, apply some optimization, extract the underlying circuit, and return to PennyLane.
Let’s begin with a quite simple circuit consisting of three gates and present that you could symbolize the
QNode
as a PyZX diagram:
Now that you’ve got a ZXdiagram as a PyZx object, you need to use all of the instruments from the PyZX library to rework the
graph. You’ll be able to simplify the circuit, draw it, and get a brand new understanding of your quantum computation.
For instance, you need to use the matplotlib drawer to get a visualization of the diagram. The drawer returns a
matplotlib
determine, and due to this fact it can save you it domestically with savefig operate, or just present it domestically.
fig = pyzx.draw_matplotlib(g)
# The next traces are added as a result of the determine is mechanically closed by PyZX.
supervisor = plt.determine().canvas.supervisor
supervisor.canvas.determine = fig
fig.set_canvas(supervisor.canvas)
plt.present()
You can even take a ZXdiagram in PyZX, convert it right into a PennyLane tape and use it in your
QNode
. Invoking the PyZX circuit generator:
import random
random.seed(42)
random_circuit = pyzx.generate.CNOT_HAD_PHASE_circuit(qubits=3, depth=10)
print(random_circuit.stats())
graph = random_circuit.to_graph()
tape = qml.transforms.from_zx(graph)
print(tape.operations)
Out:
Circuit on 3 qubits with 10 gates.
4 is the Tcount
6 Cliffords amongst which
5 2qubit gates (5 CNOT, 0 different) and
1 Hadamard gates.
[T(wires=[0]), T(wires=[0]), CNOT(wires=[2, 0]), T(wires=[2]), CNOT(wires=[1, 0]), CNOT(wires=[0, 2]), T(wires=[2]), Hadamard(wires=[0]), CNOT(wires=[2, 1]), CNOT(wires=[2, 1])]
We get a tape equivalent to the randomly generated circuit that we will use in any QNode
. This
performance shall be very helpful for our subsequent subject: circuit optimization.
Diagram optimization and circuit extraction
The ZXcalculus is extra common and extra versatile than the same old circuit illustration. We will due to this fact symbolize
circuits with ZXdiagrams and apply rewriting guidelines to simplify them — like we did for teleportation. However, not each
ZXdiagram has a corresponding circuit. To get again to circuits, a way for circuit extraction is required. For
a rigorous introduction to this energetic and promising area of utility, see . The essential concept is
captured beneath:
To simplify ZXdiagrams, not solely can we use the rewriting guidelines outlined beforehand, however we will additionally use graphtheoretic
transformations known as native complementation and pivoting. These are particular transformations that may solely be
utilized to “graphlike” ZXdiagrams. As outlined in , a ZXdiagram is graphlike if

All spiders are Zspiders.

Zspiders are solely related through Hadamard edges.

There are not any parallel Hadamard edges or selfloops.

Each enter or output is related to a Zspider and each Zspider is related to at most one enter or output.
A ZXdiagram is known as a graph state whether it is graphlike: each spider is related to an output and there are not any
phaseless spiders. Moreover, it was proved that each ZXdiagram is the same as a graphlike ZXdiagram. Thus,
after conversion into graphlike kind, we will use graphtheoretic instruments on all ZXdiagrams.
The essential concept is to make use of the graphtheoretic transformations to do away with as many inside spiders as potential.
Inside spiders are the one with out inputs or outputs related to them. We introduce some names for the spiders
relying on their phases:

A Pauli spider has a part that could be a a number of of (pi).

A Clifford spider has a part that could be a a number of of (frac{pi}{2}).

A correct Clifford spider is a Clifford spider with a part which is an odd a number of of (frac{pi}{2}).
Theorem 5.4 in gives an algorithm which takes a graphlike diagram and performs the next:

Take away all inside correct Clifford spiders,

Take away adjoining pairs of inside Pauli spiders,

Take away inside Pauli spiders adjoining to a boundary spider.
This process is applied in PyZX because the full_reduce()
operate. The complexity of the process is
(mathcal{O}(n^3)), the place (n) is the variety of spiders. Let’s create an instance with the circuit
mod_5_4. The circuit
(63) gates: (28) T
. gates, (28) CNOT
, (6)
Hadamard
and (1) PauliX
.
dev = qml.device("default.qubit", wires=5)
@qml.transforms.to_zx
@qml.qnode(gadget=dev)
def mod_5_4():
qml.PauliX(wires=4),
qml.Hadamard(wires=4),
qml.CNOT(wires=[3, 4]),
qml.adjoint(qml.T(wires=[4])),
qml.CNOT(wires=[0, 4]),
qml.T(wires=[4]),
qml.CNOT(wires=[3, 4]),
qml.adjoint(qml.T(wires=[4])),
qml.CNOT(wires=[0, 4]),
qml.T(wires=[3]),
qml.T(wires=[4]),
qml.CNOT(wires=[0, 3]),
qml.T(wires=[0]),
qml.adjoint(qml.T(wires=[3]))
qml.CNOT(wires=[0, 3]),
qml.CNOT(wires=[3, 4]),
qml.adjoint(qml.T(wires=[4])),
qml.CNOT(wires=[2, 4]),
qml.T(wires=[4]),
qml.CNOT(wires=[3, 4]),
qml.adjoint(qml.T(wires=[4])),
qml.CNOT(wires=[2, 4]),
qml.T(wires=[3]),
qml.T(wires=[4]),
qml.CNOT(wires=[2, 3]),
qml.T(wires=[2]),
qml.adjoint(qml.T(wires=[3]))
qml.CNOT(wires=[2, 3]),
qml.Hadamard(wires=[4]),
qml.CNOT(wires=[3, 4]),
qml.Hadamard(wires=4),
qml.CNOT(wires=[2, 4]),
qml.adjoint(qml.T(wires=[4])),
qml.CNOT(wires=[1, 4]),
qml.T(wires=[4]),
qml.CNOT(wires=[2, 4]),
qml.adjoint(qml.T(wires=[4])),
qml.CNOT(wires=[1, 4]),
qml.T(wires=[4]),
qml.T(wires=[2]),
qml.CNOT(wires=[1, 2]),
qml.T(wires=[1]),
qml.adjoint(qml.T(wires=[2]))
qml.CNOT(wires=[1, 2]),
qml.Hadamard(wires=[4]),
qml.CNOT(wires=[2, 4]),
qml.Hadamard(wires=4),
qml.CNOT(wires=[1, 4]),
qml.adjoint(qml.T(wires=[4])),
qml.CNOT(wires=[0, 4]),
qml.T(wires=[4]),
qml.CNOT(wires=[1, 4]),
qml.adjoint(qml.T(wires=[4])),
qml.CNOT(wires=[0, 4]),
qml.T(wires=[4]),
qml.T(wires=[1]),
qml.CNOT(wires=[0, 1]),
qml.T(wires=[0]),
qml.adjoint(qml.T(wires=[1])),
qml.CNOT(wires=[0, 1]),
qml.Hadamard(wires=[4]),
qml.CNOT(wires=[1, 4]),
qml.CNOT(wires=[0, 4]),
return qml.expval(qml.PauliZ(wires=0))
g = mod_5_4()
pyzx.simplify.full_reduce(g)
fig = pyzx.draw_matplotlib(g)
# The next traces are added as a result of the determine is mechanically closed by PyZX.
supervisor = plt.determine().canvas.supervisor
supervisor.canvas.determine = fig
fig.set_canvas(supervisor.canvas)
plt.present()
We see that after making use of the process, we find yourself with solely 16 inside Zspiders and 5 boundary spiders. We additionally
see that each one nonClifford phases seem on the inside spiders. The simplification process was profitable,
however we have now a graphlike ZXdiagram with no quantum circuit equal. We have to extract a circuit!
The extraction of circuits is a extremely nontrivial process and could be a #Phard drawback as proven on this paper by de Beaudrap et al . There are
two totally different algorithms launched in the identical paper. First, for Clifford circuits, the process will erase all
inside spiders, and the diagram is left in a graphstate from which a Clifford circuit may be
extracted utilizing a complete of eight layers with just one layer of CNOTs.
For nonClifford circuits, the issue is extra advanced as a result of we’re left with nonClifford inside spiders. From
the diagram produced by the simplification process, the extraction progresses via the diagram from
righttoleft, consuming gates on the left and including gates on the correct. It produces higher outcomes than different
cutandresynthesize methods. The extraction process is applied in PyZX because the operate
pyzx.circuit.extract_circuit. We will apply this process to the instance mod_5_4 above:
circuit_extracted = pyzx.extract_circuit(g.copy())
print(circuit_extracted.stats())
Out:
Circuit on 5 qubits with 56 gates.
8 is the Tcount
48 Cliffords amongst which
22 2qubit gates (0 CNOT, 22 different) and
26 Hadamard gates.
Instance: Tcount optimization
A concrete utility of those ZX optimization methods is the discount of the costly nonClifford Tcount
of a quantum circuit. Certainly, Tcount optimization is an space the place the ZXcalculus has proven superb outcomes .
Let’s begin by utilizing with the mod_5_4 circuit launched above. We utilized the to_zx()
decorator to be able to rework our circuit to a ZX graph. You will get this PyZX graph by calling the
QNode
:
g = mod_5_4()
t_count = pyzx.tcount(g)
print("T rely earlier than optimization:", t_count)
Out:
T rely earlier than optimization: 28
PyZX provides a number of choices for optimizing ZX graphs: full_reduce()
and teleport_reduce()
to call a pair. The full_reduce()
applies all optimization passes, however the remaining consequence is probably not
circuitlike. Changing again to a quantum circuit from a completely diminished graph is likely to be tough or unattainable.
Due to this fact, we as an alternative suggest utilizing teleport_reduce()
, because it preserves the diagram construction.
Due to this, the circuit doesn’t have to be extracted and may be instantly despatched again to PennyLane. Let’s see
the way it works:
g = pyzx.simplify.teleport_reduce(g.copy())
opt_t_count = pyzx.tcount(g)
print("T rely after optimization:", opt_t_count)
Out:
T rely after optimization: 8
The from_zx()
rework converts the optimized circuit again into PennyLane format,
and which is made potential as a result of we used pyzx.teleport_reduce and don’t have to extract
the circuit.
Out:
Variety of quantum gates: 53
Circuit gates: defaultdict(<class 'int'>, {'PauliX': 1, 'Hadamard': 6, 'CNOT': 28, 'T': 4, 'S': 6, 'Adjoint(T)': 4, 'Adjoint(S)': 4})
We now have diminished the Tcount! Taking a full census, the circuit incorporates (53) gates: (8)
T
gates, (28) CNOT
, (6) Hadamard
,
(1) PauliX
and (10) S
. We efficiently diminished the Tcount by
20 and have 10 extra S
gates. The variety of CNOT
gates remained the
identical.
Conclusion
Now that you’ve got learn this tutorial, you need to have the ability to use the ZXcalculus to unravel your quantum issues.
You’ll be able to describe quantum circuits with the ZXspiders and the Hbox and create ZXHdiagrams.
Moreover, you need to use the simplifying guidelines to get one other view of the underlying construction of your
circuit. We now have proved its utility for optimizing quantum circuits and proven that the
ZXcalculus is greater than promising for quantum machine studying. It was not lined on this introduction, however the
ZXcalculus can be used for quantumerror correction — it’s no marvel why some quantum physicists
name ZXcalculus the “Swiss military knife” of quantum computing instruments!
Acknowledgement
The writer would additionally wish to acknowledge the useful inputs of Richard East, David Wakeham and Isaac De Vlugt. The
writer can also be grateful for the gorgeous drawings by Guillermo Alonso and for the good thumbnail and teleportation
gif by Tarik ElKhateeb.
In regards to the writer
Romain Moyard
Romain is at the moment a quantum software program developer at Xanadu engaged on PennyLane. He works on creating helpful quantum machine studying instruments.
Complete working time of the script: ( 0 minutes 0.304 seconds)