Now Reading
Introduction to the ZX-calculus — PennyLane documentation

Introduction to the ZX-calculus — PennyLane documentation

2023-06-06 06:17:07

Creator: Romain Moyard. Posted: 6 June, 2023.

The ZX-calculus 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 thought-about a diagrammatically full
generalization of the same old circuit illustration. The ZX-calculus relies on class principle, an method to
arithmetic which research objects when it comes to their relations fairly than in isolation. Thus, the ZX-calculus
gives a rigorous solution to perceive the construction underlying quantum issues, utilizing the hyperlink between quantum
operations fairly than the quantum operations themselves.—-PennyLane-documentation.gif

After this tutorial you’ll perceive tips on how to symbolize quantum teleportation and simplify it within the ZX-calculus!

On this tutorial, we first give an outline of the constructing blocks of the ZX-calculus, known as ZX-diagrams,
and the foundations for reworking them, known as rewriting guidelines. We additionally present how the ZX-calculus may be prolonged to ZXH
calculus. The ZX-calculus can also be promising for quantum machine studying, thus we current how the parameter-shift rule
may be derived utilizing ZX-diagrams. We are going to then bounce to the coding a part of the tutorial and present how PennyLane is
built-in with PyZX , a Python library for ZX-calculus, and how one can rework your circuit to a
ZX-diagram. We then apply what we’ve realized to be able to optimize the variety of T-gates of a recognized benchmark circuit.
We additionally present that simplifying a ZX-diagram doesn’t all the time find yourself with a diagram-like graph, and that circuit
extraction is a most important ache level of the ZX framework. This tutorial will give a broad overview of what ZX-calculus can
supply while you need to analyze quantum issues.


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: ZX-diagrams and their
rewriting guidelines. We begin by introducing ZX-diagrams, 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
ZX-calculus. We observe the scalar conference of East et al. (it’s extra appropriate to the multi-H field
conditions, see the ZXH part).

A ZX-diagram is an undirected multi-graph; you’ll be able to transfer vertices with out affecting the underlying
linear map. The vertices are known as Z- and X-spiders, 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 ZX-diagram is the Z-spider. In many of the literature, it’s depicted as a inexperienced vertex.
The Z-spider 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):—-PennyLane-documentation.jpeg

The Z-spider.

It’s straightforward to see that the same old Z-gate may be represented with a single-wire Z-gate:—-PennyLane-documentation.jpeg

The Z-gate.

As you’ve most likely already guessed, the second constructing block of the ZX-diagram is the X-spider. It’s often depicted
as a crimson vertex. The X-spider additionally takes an actual part (alpha in mathbb{R}) and it represents the next
linear map (it accepts any variety of inputs and outputs):—-PennyLane-documentation.jpeg

The X-spider.

It’s straightforward to see that the same old X-gate may be represented with a single-wire X-spider:—-PennyLane-documentation.jpeg

The X-gate.

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 ZX-calculus. In an effort to make the diagram simpler to learn, we introduce the Hadamard gate as a yellow

The Hadamard gate as a yellow field and its ZX decomposition.

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 ZXH-calculus. 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 Z-spider as follows.—-PennyLane-documentation.jpg

Methods to rework an X-spider to a Z-spider with the Hadamard gate.—-PennyLane-documentation.jpg

Methods to rework an Z-spider to a X-spider with the Hadamard gate.

A particular case of the Z- and X-spiders 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:—-PennyLane-documentation.jpeg

The zero state and plus state as a ZX-diagram.

Equally, you get the (vert 1rangle) state and (vert -rangle) state by changing the zero part with

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 Z-spider with zero part and a easy crimson vertex is an X-spider 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
ZX-diagrams 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 Z-spider with one enter wire and two output wires. We present the
ZX-diagram and corresponding matrix beneath:—-PennyLane-documentation.jpg

Phaseless Z-spider with one enter wire and two output wires (see the definition of the Z-spider) stacked with a
single wire.

Subsequent, we stack a single wire with a phaseless X-spider with two enter wires and single output wire. Once more, we offer
the matrix:—-PennyLane-documentation.jpg

Single wire stacked with a phaseless X-spider with two inputs wires and one output wire.

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.—-PennyLane-documentation.jpg

The composition of the 2 diagrams is a CNOT gate.

We’ve already talked about {that a} ZX-diagram is an undirected multi-graph; 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 vice-versa, isn’t allowed. None of those deformations have an effect on the
underlying linear map, which means that ZX-diagrams have all kinds of topological symmetries. As an example,
the 2 diagrams beneath each symbolize the CNOT gate:—-PennyLane-documentation.jpeg

Each diagrams symbolize the identical CNOT gate.

Which means we will draw a vertical line with out ambiguity, which is the same old approach of representing the CNOT gate:—-PennyLane-documentation.jpeg

Ordinary illustration of the CNOT gate as a ZX-diagram.

We’ve simply proven that we will categorical any Z rotation and X rotation with Z- and X-spiders. Due to this fact, it’s enough
to create any one-qubit 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 Choi-Jamiolkowski 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 ZX-diagrams are a common instrument for reasoning about linear maps. However this
doesn’t imply the illustration is straightforward!

For a extra in-depth introduction, see and .

ZX-calculus: rewriting guidelines

ZX-diagrams coupled with rewriting guidelines kind the ZX-calculus. 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.

  1. For the reason that X-gate and Z-gate don’t commute, non-phaseless vertices of various coloration don’t commute.

  2. 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.—-PennyLane-documentation.jpeg

    The (f)use rule.

  3. The (pi) -copy rule describes tips on how to pull an X-gate via a Z-spider (or a Z-gate via an X-spider).
    Since X and Z anticommute, pulling the X-gate via a Z-spider introduces a minus signal into the Z part.—-PennyLane-documentation.jpeg

    The ((pi))-copy rule.

  4. The state-copy rule captures how easy one-qubit 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.—-PennyLane-documentation.jpg

    The state (c)opy rule, the place (a) is an integer.

  5. 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 self-loops.—-PennyLane-documentation.jpeg

    The (id)entity removing rule.

  6. 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 .—-PennyLane-documentation.jpg

    The (b)ialgebra rule.

  7. 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 state-copy rule , but it surely’s helpful to document
    it as a separate rule.—-PennyLane-documentation.jpeg

    The (ho)pf rule.


Now that we have now all the required instruments, let’s see tips on how to describe teleportation as a ZX-diagram 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:

  1. Alice applies the CNOT gate adopted by the Hadamard gate.

  2. Alice measures the 2 qubits that she has.

  3. Alice sends the 2 measurement outcomes to Bob.

  4. Given the outcomes, Bob conditionally applies the Z- and X-gate to his qubit.

  5. 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:—-PennyLane-documentation.jpeg

The teleportation circuit.

Allow us to convert this quantum circuit right into a ZX-diagram. The measurements are represented by the state X-spider
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 Choi-Jamiolkowski-equivalent 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
X-spider of the CNOT. We additionally merge the Hadamard gate with the (beta) state, as a result of collectively it represents a
Z-spider. Then we will fuse the three Z-spiders by merely including their phases. After that, we see that the Z-spider
part vanishes (modulo (2pi)) and might due to this fact be simplified utilizing the id rule. Then we will fuse the
two X-spiders by including their phases. We discover that the part once more vanishes modulo (2pi) and we will get rid
of the final X-spider. Teleportation is a straightforward wire connecting Alice and Bob!—-PennyLane-documentation.png

The teleportation ZX-diagram simplified from however there’s a scalar issue due to a special
scalar conference. You are actually in a position to perceive, step-by-step, the teleportation gif from the start of this

The ZXH-calculus

The universality of the ZX-calculus 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 multi-leg H-box,
outlined as follows:—-PennyLane-documentation.jpg

The H-box, a 3rd generator.

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 H-box 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

An H-box 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 multi-leg H-box, the Toffoli gate may be represented with three Z-spiders and three
H-boxes — two easy Hadamard gates and one three-ary H-box — as proven beneath:—-PennyLane-documentation.jpg


The addition of the multi-leg H-box along with an extra set of rewriting guidelines types the ZXH-calculus. You’ll be able to
discover extra particulars and the rewriting guidelines within the literature .

Let’s present that this ZXH-diagram is certainly a Toffoli-gate. This operation is outlined by conditionally making use of an
X-gate 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 (X-gate).
For that goal, we have to add a brand new rewriting rule that’s a part of the ZXH-calculus: the take up rule.—-PennyLane-documentation.jpg

The (ab)sorb rule.

We begin by making use of our Toffoli diagram on a (ket{11}) state, which corresponds to 2 X-spiders with a part
of (pi) stacked with our diagram. We apply the copy rule on the 2 teams of X-spiders and Z-spiders on the
wires 0 and 1. After that we will apply the newly launched take up rule on one of many X-spiders related to the
H-Field. Then we acknowledge the Fourier relation and might exchange the X-spider and H-Field by a Z-spider. Then it’s straightforward
to use the fuse rule on the 2 Z-spiders. Once more, we acknowledge the Fourier relation and acquire a single X-spider 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 X-spider on the goal. It implies that we have now a bit flip on the goal.—-PennyLane-documentation.jpg

Toffoli-diagram utilized on the (ket{11}) state.

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 ZXH-diagram is certainly the Toffoli

The ZX-calculus for quantum machine studying

We now transfer away from the usual use of the ZX-calculus to be able to present its utility for calculus and,
extra particularly, for quantum derivatives (the parameter-shift rule). What follows isn’t applied in PennyLane or PyZX.
By including derivatives to the framework, it exhibits that the ZX-calculus has a job to play in analyzing quantum machine
studying issues. After studying this part, try to be satisfied that the ZX-calculus can be utilized to review any
sort of quantum-related drawback.

Certainly, not solely is the ZX-calculus 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 ZX-diagram (theorem 2 of Zhao et al. ). We may even present
that the concept can be utilized to show the parameter-shift rule!

Partial spinoff as a ZX-diagram

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 graph-like kind
(see the Graph optimization and circuit extraction part). There are a number of guidelines that make sure the transformation
to a graph-like diagram. We exchange the 0 state by crimson phaseless spiders, and we rework the parameterized circuit
to its graph-like 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).—-PennyLane-documentation.jpg

Now we are going to examine the partial spinoff of the diagram representing the expectation worth. The theory is
the next:—-PennyLane-documentation.jpg

Theorem 2: The spinoff of the expectation worth of a Hamiltonian given a parameterized as a ZX-diagram.

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:—-PennyLane-documentation.jpg

Two Z-spiders relying on the (j)-th angle.

Now we have now a easy formulation the place we will simply take the spinoff:—-PennyLane-documentation.jpg

The spinoff of two spiders relying on the (j)-th angle.

See Also

The theory 2 is proved — we simply expressed the partial spinoff as a ZX-diagram!

Parameter-shift rule as a ZX-diagram

This theorem can be utilized to show the parameter-shift rule. Let’s contemplate the next ansatz that we rework to
its graph-like diagram.—-PennyLane-documentation.jpg

The circuit (on the left) is translated to a ZX-diagram.—-PennyLane-documentation.jpg

The entire circuit is translated to a graph-like ZX-diagram.

We then apply the earlier theorem to get the partial spinoff relative to (theta_1).—-PennyLane-documentation.jpg

The spinoff is utilized on the ZX-diagram

The second step is to take the X-spider 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 Z-spider) and (ket{-}) ((2pi) Z-spider) states and utilizing the fusion rule for the Z-spider.
We receive the parameter-shift rule!—-PennyLane-documentation.jpg

Through the use of theorem 2, we will add an X-spider and shift the phases within the Z-spiders. Then, by explicitly decomposing
the spider with the (ket{+}) and (ket{-}) states, we show the parameter-shift rule!

You’ll find extra details about the differentiation and integration of ZX-diagrams with QML functions within the
following paper .

ZX-diagrams with PennyLane

Now that we have now launched the formalism of the ZX-calculus, let’s dive into some code and present what you are able to do with
PennyLane! PennyLane v0.28 added ZX-calculus performance to the fold. You should use the
to_zx() rework decorator to get a ZX-diagram from a PennyLane
QNode, whereas from_zx() transforms a ZX-diagram 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 ZX-diagram 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


tutorial zx calculus

You can even take a ZX-diagram in PyZX, convert it right into a PennyLane tape and use it in your
QNode. Invoking the PyZX circuit generator:

import random

random_circuit = pyzx.generate.CNOT_HAD_PHASE_circuit(qubits=3, depth=10)

graph = random_circuit.to_graph()

tape = qml.transforms.from_zx(graph)


Circuit  on 3 qubits with 10 gates.
        4 is the T-count
        6 Cliffords amongst which
        5 2-qubit 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 ZX-calculus is extra common and extra versatile than the same old circuit illustration. We will due to this fact symbolize
circuits with ZX-diagrams and apply rewriting guidelines to simplify them — like we did for teleportation. However, not each
ZX-diagram 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:—-PennyLane-documentation.jpg

The simplification and extraction of ZX-diagrams, content material from web page 2 of .

To simplify ZX-diagrams, not solely can we use the rewriting guidelines outlined beforehand, however we will additionally use graph-theoretic
transformations known as native complementation and pivoting. These are particular transformations that may solely be
utilized to “graph-like” ZX-diagrams. As outlined in , a ZX-diagram is graph-like if

  1. All spiders are Z-spiders.

  2. Z-spiders are solely related through Hadamard edges.

  3. There are not any parallel Hadamard edges or self-loops.

  4. Each enter or output is related to a Z-spider and each Z-spider is related to at most one enter or output.

A ZX-diagram is known as a graph state whether it is graph-like: each spider is related to an output and there are not any
phaseless spiders. Moreover, it was proved that each ZX-diagram is the same as a graph-like ZX-diagram. Thus,
after conversion into graph-like kind, we will use graph-theoretic instruments on all ZX-diagrams.

The essential concept is to make use of the graph-theoretic 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:

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

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

  3. 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 graph-like diagram and performs the next:

  1. Take away all inside correct Clifford spiders,

  2. Take away adjoining pairs of inside Pauli spiders,

  3. 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)

def mod_5_4():
    qml.CNOT(wires=[3, 4]),
    qml.CNOT(wires=[0, 4]),
    qml.CNOT(wires=[3, 4]),
    qml.CNOT(wires=[0, 4]),
    qml.CNOT(wires=[0, 3]),
    qml.CNOT(wires=[0, 3]),
    qml.CNOT(wires=[3, 4]),
    qml.CNOT(wires=[2, 4]),
    qml.CNOT(wires=[3, 4]),
    qml.CNOT(wires=[2, 4]),
    qml.CNOT(wires=[2, 3]),
    qml.CNOT(wires=[2, 3]),
    qml.CNOT(wires=[3, 4]),
    qml.CNOT(wires=[2, 4]),
    qml.CNOT(wires=[1, 4]),
    qml.CNOT(wires=[2, 4]),
    qml.CNOT(wires=[1, 4]),
    qml.CNOT(wires=[1, 2]),
    qml.CNOT(wires=[1, 2]),
    qml.CNOT(wires=[2, 4]),
    qml.CNOT(wires=[1, 4]),
    qml.CNOT(wires=[0, 4]),
    qml.CNOT(wires=[1, 4]),
    qml.CNOT(wires=[0, 4]),
    qml.CNOT(wires=[0, 1]),
    qml.CNOT(wires=[0, 1]),
    qml.CNOT(wires=[1, 4]),
    qml.CNOT(wires=[0, 4]),
    return qml.expval(qml.PauliZ(wires=0))

g = mod_5_4()

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


tutorial zx calculus

We see that after making use of the process, we find yourself with solely 16 inside Z-spiders and 5 boundary spiders. We additionally
see that each one non-Clifford phases seem on the inside spiders. The simplification process was profitable,
however we have now a graph-like ZX-diagram with no quantum circuit equal. We have to extract a circuit!

The extraction of circuits is a extremely non-trivial process and could be a #P-hard 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 graph-state from which a Clifford circuit may be
extracted utilizing a complete of eight layers with just one layer of CNOTs.

For non-Clifford circuits, the issue is extra advanced as a result of we’re left with non-Clifford inside spiders. From
the diagram produced by the simplification process, the extraction progresses via the diagram from
right-to-left, consuming gates on the left and including gates on the correct. It produces higher outcomes than different
cut-and-resynthesize 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())


Circuit  on 5 qubits with 56 gates.
        8 is the T-count
        48 Cliffords amongst which
        22 2-qubit gates (0 CNOT, 22 different) and
        26 Hadamard gates.

Instance: T-count optimization

A concrete utility of those ZX optimization methods is the discount of the costly non-Clifford T-count
of a quantum circuit. Certainly, T-count optimization is an space the place the ZX-calculus 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

g = mod_5_4()
t_count = pyzx.tcount(g)
print("T rely earlier than optimization:", t_count)


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
circuit-like. 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)


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.


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 T-count! 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 T-count by
20 and have 10 extra S gates. The variety of CNOT gates remained the


Now that you’ve got learn this tutorial, you need to have the ability to use the ZX-calculus to unravel your quantum issues.
You’ll be able to describe quantum circuits with the ZX-spiders and the H-box and create ZXH-diagrams.
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
ZX-calculus is greater than promising for quantum machine studying. It was not lined on this introduction, however the
ZX-calculus can be used for quantum-error correction — it’s no marvel why some quantum physicists
name ZX-calculus the “Swiss military knife” of quantum computing instruments!


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 El-Khateeb.

In regards to the writer

Romain Moyard

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)

Gallery generated by Sphinx-Gallery

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top