Now that we have covered the basic concepts of quantum computing, it is time to do some interesting stuff. In the previous chapter we discussed string diagrams and how they can be used to represent linear maps and, in particular, quantum circuits. Actually calculating the linear map they represent is tedious though, and in some cases also (impossibly) hard, as the dimension of the spaces involved scales exponentially in the number of qubits. It would then certainly be nice if we could work directly with string diagrams instead. In this chapter we will introduce the ZX-calculus, a graphical language that works with a particular type of string diagrams we call ZX-diagrams. These ZX-diagrams can represent any quantum computation. In addition, the ZX-calculus gives us rules for manipulating ZX-diagrams. In this way we can prove interesting properties of states, unitaries, projections, or any other linear map between qubits we want, all the while working with diagrams instead of those pesky matrices. In this chapter we will see how to represent quantum circuits as ZX-diagrams, and how to use the basic rules of the ZX-calculus to start proving some interesting things about them. We will just barely be scratching the surface however, as there is much more to be said about the things we can do with the ZX-calculus. In fact, we will be spending the rest of the book doing exactly that!
ZX-diagrams arise as compositions and tensor products of the following basic linear maps:
Z-spiders | X-spiders |
These generators are called spiders for no other reason than the fact that they look a bit like spiders when you draw them. Let’s focus on the Z-spider. Most of what we say will also be true of the X-spider, by symmetry. First, note this is actually not just a single map, but a family of linear maps of the form:
where the notation means ‘ tensor copies’, e.g.
The number is the number of input ‘legs’ and the number is the number of output ‘legs’. These two numbers together are called the arity of a spider. These correspond to the number of qubits which the map takes as input and output, respectively:
| (3.1) |
where and are the - and -fold tensor products of bras and kets, respectively, and we take the convention that . Keep in mind that, just like in circuit notation, the ordering of composition is flipped between the picture and the bra-ket expression. For example, wires coming in the left side of the diagram above correspond to bras which actually appear on the right side of the bra-ket expression. That’s why the ’s and ’s seem to have flipped around in equation (3.1). We can also describe a Z-spider in matrix notation. Start with a matrix of all ’s, then add a to the top-left corner and an to the bottom-right corner:
When , we’ll get a matrix (i.e. a scalar), so the top-left corner is the bottom-right corner. In that case, the resulting scalar is :
Unlike quantum gates, Z-spiders can have a different number of inputs from outputs, and hence do not need not be unitary. In fact, they are often not even invertible.
Exercise 3.1. Show that for , Z-spiders always correspond to matrices with rank 2. What are the other possibilities for the rank of a Z-spider if or is 0?
However, when , Z-spiders are unitary and correspond to the familiar Z-phase gates we met in the previous chapter:
| (3.2) |
We don’t always write angles on spiders, in which case the ‘default’ angle is assumed to be :
We will call such spiders phase-free. Some familiar quantum states arise out of phase-free Z-spiders when . Namely, we obtain (unnormalised) Bell, GHZ, and -fold GHZ states:
To normalise these states, we should multiply both sides in these examples by . We will indicate a scalar multiple of a ZX-diagram just like we would for any other kind of linear map. For example:
Most the time we won’t bother with normalisation unless it really matters. It usually doesn’t. Finally, looking at Z-spiders with a single output leg and , we can construct X-basis (a.k.a. “plus” basis) elements and , again up to normalisation:
| (3.3) |
X-spiders are pretty much the same as Z-spiders, but everything is defined relative to the X-basis rather than the Z-basis:
| (3.4) |
Hence, all of the special cases highlighted above apply equally well to X-spiders, after substituting and . In particular, when , the X-spider is unitary and represents the -phase gate:
| (3.5) |
It will be useful for us to calculate the definition of an X-spider in terms of the Z-basis. This will enable us, for example, to do concrete matrix calculations involving both kinds of spiders, as well as understand some of the interaction properties we’ll meet later in this chapter. We’ll start with the basis states. Note that the Z and the X bases have a symmetric presentation with respect to each other:
Consequently, we can modify (3.3) to get an expression for the two Z-basis states:
| (3.6) |
Notice that X-spiders are used to construct Z-basis states and vice-versa. This is a common “gotcha” in ZX-calculation: to get basis elements, make sure you use spiders of the opposite colour. Now, let’s calculate the matrix for the special case where , and :
| (3.7) |
If we expand the first term in the expression above, we’ll get a sum over all 8 possible combinations of Z-basis elements, times the normalisation factor:
Expanding the second term in (3.7), we get almost the same thing, except we pick up a sign whenever there is an odd number of 1’s:
In Eq. (3.7) we add these two terms together, so we see that when we expand them that each term with an odd number of 1’s cancels, while those with an even number add together. We hence have:
| (3.8) |
We see then that this map is ‘classical’: it sends Z-basis states to Z-basis states. In particular, it maps and to , while and are mapped to . But this is exactly what the XOR does! So we have where is the XOR of the two bits .
Exercise 3.2. What classical map would we get if we instead took the X-spider with 2 inputs, 1 output and a phase?
We can generalise this construction to see what any X-spider does on states in the Z-basis. Let’s consider a general X-spider as defined in (3.4). Expanding the first term gives a uniform superposition over all Z-basis elements:
The normalisation factor comes from the fact that we have copies of and copies of , each of which contribute a , which multiply together. Expanding the second term in (3.4), we again get a sum over all of the Z-basis elements, but with different coefficients inside the sum:
Every term in the sum gets an , but when any of the ’s or ’s is , it also gets a factor of . Putting the two terms together, we get this expression for an X-spider:
where the coefficients are given by:
Now, that’s a pretty big expression, so let’s try to make some sense out of it. First, note that, whenever the bit string contains an even number of 1’s, this term has a coefficient of , whereas if it contains an odd number of 1’s, it’s coefficient is . This can be written succinctly in terms of an XOR as follows:
Here, we used the fact that the XOR of all the elements in the bitstring tells us if there were an even number of 1s or an odd number. For that reason, the XOR of a bunch of bits is sometimes called the parity (where the word “parity” means the property of something being even vs. odd). If we specialise to the case where , we get the numbers and , respectively, in the two cases above. By swallowing the into the normalisation factor, we can write the coefficients for a phase-free X-spider as follows:
We can think of such spiders as a family of “generalised XOR” maps.
We will now give two equivalent definitions of ZX-diagrams: one that is closely related to quantum circuit notation and the other to tensor networks.
Just like gates form the building blocks for circuits, Z-spiders and X-spiders form the building blocks for ZX-diagrams, under tensor product and composition. For example:
A ZX-diagram can be built iteratively by composing other ZX-diagrams either horizontally, by connecting the output wires of the first to the input wires of the second, or vertically, simply by ‘stacking’ the diagrams to create the tensor product. The base case of this inductive construction starts with the Z- and X-spiders. To demonstrate how this works, let’s work through an example: we will show how to construct the CNOT gate using a Z- and X-spider. The first ingredient we need is the phase-free Z-spider with 1 input and 2 outputs:
| (3.9) |
Its matrix has 2 columns and 4 rows. In general, the matrix of a general ZX-diagram with inputs and outputs will have columns, and rows. Suppose now that we wish to vertically compose the spider (3.9) with an identity, which has matrix diag. The way we calculate the result is with the Kronecker product:
Note that we said above that a ZX-diagram is built by composing spiders. We see here that that is not entirely true: we also need some ‘structural’ components like the identity wire. We’ll say a bit more about this later. We calculate the matrix of the other ingredient we need similarly:
Now to make a CNOT we need to horizontally compose these two subdiagrams:
| (3.10) |
To see that this is indeed a CNOT we calculate its matrix. On the level of the matrix, the horizontal composition of diagrams corresponds to matrix multiplication:
| (3.11) |
Up to a scalar factor of this is indeed the matrix of the CNOT gate. If this matrix calculation business seems tedious: that is precisely why we want to use the ZX-calculus! We will see that many identities which would normally be proven by working with matrices can be replaced with graphical reasoning instead. In Eq. (3.10) we have first the Z-spider and then the X-spider. As we will see later, spiders have many symmetries, and for this reason we can also write the CNOT as first an X-spider and then a Z-spider:
| (3.12) |
Exercise 3.3. Prove Eq. (3.12) by directly calculating the matrix the right-hand side represents and showing it agrees with Eq. (3.11).
Because it doesn’t matter in which direction the middle wire points, top-left to bottom-right or bottom-left to top-right, we can actually without ambiguity just write the wire vertically:
| (3.13) |
If we want to view this diagram as being composed of simple pieces via horizontal and vertical composition then we have to pick an orientation for this wire, but generally we can just leave it like it is. As a nice bonus this then matches nicely with the quantum circuit notation for a CNOT gate:
| (3.14) |
We saw that to construct the diagram for a CNOT we needed to use an identity wire. Note that however we could also write the identity as a spider, since:
| (3.15) |
To construct arbitrary ZX-diagrams we also need the ability to swap wires, so that we can connect the spiders up however we want. We can also represent a swap just using spiders, by using the standard trick to build it out of three CNOT gates:
| (3.16) |
Finally, we also need the cup and cap we saw in Section 2.1.7, but again we can construct these using spiders:
| (3.17) |
For the cap we just make the inputs into outputs instead. Note that Eq. (3.17) is just Eq. (3.15) but with the input bended to be an output. We see then that the “structural generators” of identity, swap, cup and cap can actually be constructed using spiders, so that we can in fact say that a ZX-diagram only consists of spiders. We will however consider these structural pieces as first-class citizens of ZX-diagrams, and not as derived from spiders, since they have useful properties by themselves, which we will see more of in Section 3.1.3.
For those readers familiar with tensor networks it might be helpful to note that ZX-diagrams are in fact just tensor networks. Indeed, considering them as tensor networks, our notation matches the standard notion of a graphical tensor network as introduced by Penrose. Considered as a tensor network, each wire in a ZX-diagram corresponds to a 2-dimensional index and a wire between two spiders denotes a tensor contraction. The Z- and X-spiders are then defined as:
where range over . Note that in both cases, the definition of the spider doesn’t depend on the order of indices nor whether the indices appear in the upper or lower position. Hence, permuting, raising, and lowering indices has no effect on the definition of a spider. For example:
So, unless it is important to distinguish inputs and outputs for some other reason (e.g. to do matrix multiplication in the usual way), we might as well take all indices to be upper indices. This allows us to simplify the expression for the Z-spider to:
When , so phase-free Z-spiders further simplify to:
This is a generalisation of the Kronecker delta matrix we introduced in Definition 2.1.3 from two indices to any number of indices. The intuition is very much same: a Z-spider ‘forces’ a collection of indices to take the same value by going to 0 whenever they do not. Consider for example the following tensor network involving a single Z-spider and some generic matrix :
We can compute the elements of the tensor in terms of as follows:
In the rightmost step, we use a Kronecker-delta-like simplification to replace the sum over multiple indices with a sum over a single index. In fact, when the Z-spider does have a phase, we can do a similar simplification by collapsing all of its summed-over indices into a single index, but we should keep a 1-index Z-spider around to keep track of the phase. Generalising the example above, we have:
which can be written as a tensor contraction and simplified as follows:
X-spiders are defined identically to Z-spiders, except with respect to the X-basis . For tensor networks it is more convenient to define everything in terms of a single, fixed basis. Hence, we express an X-spider concretely in terms of the Z-basis. We already computed the coefficients of this expression at the end of Section 3.1.1, so we use them here:
Just like the Z-spider, the X-spider treats inputs and outputs identically, so we can permute indices or raise/lower them at will:
Recall that in the previous section, we interpreted a ZX-diagram as a tensor and composition of some linear maps. This required us to make an arbitrary choice of how to “chop up” the diagram. Equivalently, since we now have expressions for spiders as tensors, we can interpret a ZX-diagram directly as a tensor network. For example the ZX-diagram:
can be interpreted as a tensor network as follows. First assign an arbitrary index to each wire, e.g.
then write as a contraction involving X- and Z-spiders:
Just like for general tensor networks, all of the indices that are not inputs or outputs are summed over. ZX-diagrams are just tensor networks whose basic components are only X-spider and Z-spider tensors. Certainly most of the ways tensor networks are used in the literature allow the component tensors to be any indexed set of complex numbers. A natural question then is: why focus only on spider tensors and not more general components? There are two reasons. First, we don’t lose anything in terms of expressiveness by restricting to spiders. Namely, any linear map on qubits can be constructed from spiders, and hence any tensor network with 2D wires can also be constructed purely out of spider tensors (we will have more to say about this in Section 3.1.7). Second, ZX-diagrams come with an extremely useful set of rewrite rules, the ZX-calculus, that lets us transform and simplify the tensor network itself. If our basic tensors were “black box” collections of numbers, we wouldn’t be able to do this. We’ll first meet these rules in Section 3.2, and we’ll be using them throughout the book.
Exercise 3.4. Show that the following equality is true, by writing down the left-hand side as a tensor contraction and simplifying it:
We noted in the previous section that spiders are very symmetric tensors, and that we were allowed to raise, lower or permute their indices however we wanted. In this section we will see what this looks like graphically. Namely, this translates into some nice interactions between cups and caps, swap, and spiders. The SWAP interacts with spiders the way you would expect. Namely, that you can ‘slide’ a spider along the wires, such as in the equation commuting a CNOT gate through a swap below:
| (3.18) |
It is possible to write down a set of equations that formally captures this behaviour of ‘sliding through a swap’, but we will not bother with that and instead let your intuition do the work. An important point to note though is that we are not working in a ‘braided’ setting, and hence the swap is self-inverse:
| (3.19) |
The fact that we can permute the indices in the tensor of a spider corresponds to being able to swap the wires of their inputs or outputs:
| (3.20) |
These symmetries hold for all phases , for spiders with any number of input and output wires, and for the swapping of any of their inputs or outputs. We can’t just swap the wires among the inputs and outputs: we can also swap an input with an output. We do this using the cup and cap. Just as we can slide spiders along the wires of a swap gate (cf. (3.18)), we can slide a spider along the wires of cups and caps. For instance:
| (3.21) |
This again works for Z-spiders and X-spiders, with any phase, and with any number of input or output wires:
| (3.22) |
This might just look like some meaningless bending of wires and that is exactly the point. Spiders treat inputs and outputs in essentially the same way, where the distinction between the two is really just bookkeeping, which we represent in the graphical notation as “bending wires”. As a consequence, ZX-diagrams enjoy a particularly strong version of the principle of Only Connectivity Matters that was introduced in Section 2.1.6. There, we saw that the only relevant data in a string diagram was which inputs were connected to which outputs. For ZX-diagrams, the order and direction of wires connecting to a single spider are irrelevant. Hence, we can treat a ZX-diagram essentially as a labelled, undirected graph with some inputs and outputs. To make more clear the type of freedom this entails, only connectivity matters means that the following ZX-diagrams all represent the same matrix.
| (3.23) |
That is, any way we can interpret the diagrams above as compositions and tensor products of spiders, swaps, identities, cups, and caps will yield the same matrix simply because the spiders and their connectivity are the same. Note that while we can freely move around the spiders in the diagram, we have to make sure the order of the inputs and outputs of the diagram as a whole stays the same. So for instance, we have:
| (3.24) |
This works because for instance the top input stays connected to the same Z-spider in both diagrams, and the same for the other output and inputs. However, the following is not equal:
| (3.25) |
Even though the spiders in both diagrams are still connected in the same way, we see that in the second diagram the first input is connected to the X-spider while it was connected to the Z-spider in the first diagram.
Remark 3.1.1. We now know enough about ZX-diagrams to address the elephant in the room: why and , but not ? One reason for this is that can be presented as a composition of and , and hence we don’t need it. In principle however, we could have worked with the ‘XY’-calculus instead of the ZX-calculus, but there is an important reason to prefer and over . Namely, the and eigenbases are self-conjugate meaning that each of the vectors in the basis is equal to its own componentwise complex conjugate, for instance: . This is not the case for the eigenbasis where we have . As a result, the ‘Y-spider’ does not have the nice symmetry between inputs and outputs of (3.22) that the Z- and X-spiders do, which makes it a bit more tricky to work with.
In the diagram deformation example given in equation (3.23), the zero-arity X-spider with a phase can be moved completely freely throughout the diagram. This is because a zero-arity spider, or more generally any ZX-diagram with zero inputs and outputs, simply contributes an overall scalar factor. For instance, we have:
| (3.26) |
For this reason, we call ZX-diagrams with no inputs and no outputs scalar ZX-diagrams. Putting two such diagrams side-by-side corresponds to the tensor product, which for scalars just means multiplication. Note that just by using combinations of the scalar diagrams of (3.26) we can represent any complex number as a scalar ZX-diagram.
Exercise 3.5. By combining the diagrams from (3.26), find a ZX-diagram to represent the following scalar values :
.
for any .
.
for any value .
Find a general description or algorithm to construct the ZX-diagram for any complex number .
When working with ZX-diagrams we will often ignore non-zero scalar factors unless they are relevant to the calculation. This is for the same reason that physicists will sometimes work with unnormalised quantum states: it is simply inconvenient and sometimes unnecessary to know the exact scalar values. Recall that we use the notation to mean and are equal up to a non-zero scalar factor (see Remark 2.3.1). Because any scalar can be represented by a ZX-diagram, we could work just with ZX-diagrams even if we want to have the correct scalar values. For instance, if we want the scalar-accurate CNOT gate we could write:
| (3.27) |
However, we will mostly just write the actual number we need instead of the diagram representing the number. So instead of (3.27) we would just write:
| (3.28) |
There are some useful global operations on ZX-diagrams that correspond to well-known matrix operations. First of all, if we take a ZX-diagram and negate all the phases in the spiders, so that for instance is mapped to , and is mapped to , the matrix of the resulting diagram is the conjugate of the matrix of the original diagram (recall that the conjugate of a complex matrix is the pointwise complex conjugate of each the elements of the matrix). The transpose of a matrix is also easily represented by ZX-diagrams: we use cups and caps to change every input into an output and vice versa.
Note that these ‘crossovers’ of the wires are necessary to ensure that the first input is mapped to the first output, instead of to the last output. If we apply this procedure to the CNOT gate we can verify that the CNOT gate is indeed self-transpose:
Here in the first step we slid the spiders along the wires to the bottom, and in the second step we applied the first yanking equation (2.10). The adjoint of a matrix is constructed by taking the transpose and conjugating the matrix. Hence, the adjoint of a ZX-diagram is constructed by interchanging the inputs and outputs using cups and caps as described above, and then negating all the phases of the spiders in the diagram.
We introduce some special notation for the Hadamard gate:
This ‘Hadamard box’ is the final generator of ZX-diagrams that we will need (although note that we can in fact also define it in terms of spiders if we want to; see Section 3.2.5). By inspecting the matrix of the Hadamard it is easy to check that it is self-transpose:
| (3.29) |
This means that, just like with the spiders, the orientation of the Hadamard is not important, which is why we are allowed to draw it as a simple box. Also just like with the spiders, we can slide Hadamards along wires as intuition would suggest.
We have now seen that quite a number of quantum gates and constructions can be represented in the ZX-calculus. This raises the following question: exactly which linear maps can be represented in the ZX-calculus. The answer to this is simple: everything. Here, ‘everything’ means any complex matrix of size for some and . To see why this is the case, let’s first look at which unitaries we can represent. Eqs. (3.2) and (3.5) show that we can represent and phase gates using spiders. Using the Euler decomposition, any single-qubit unitary can be written as a composition of and phase gates, and hence we can represent any single-qubit unitary as a ZX-diagram. In Eq. (3.14) we additionally saw how to represent the CNOT gate as a ZX-diagram. Note that this equation involves a complex number to get the correct scalar factor, but in Exercise 3.5 we saw how to represent any complex number as a ZX-diagram, so that is no problem. In Section 2.3.5 we recalled that any many-qubit unitary can be written as a circuit of CNOT gates and single-qubit unitaries. Hence, we then know we can write any unitary as a ZX-diagram:
Note that for most unitaries, the diagram we would get when we apply this procedure would be exponentially large, so it would not necessarily be useful to write it as a ZX-diagram. However, we can do it, and that is the important part in this section. Now let’s see which states we can represent as a ZX-diagram. We know we can represent using an X-spider (Eq. (3.6)). Hence, by taking tensor products we can represent . Now if we have any other normalised -qubit state , there will be some -qubit unitary that maps to (think back to your linear algebra classes, why is this the case?). We can then write:
| (3.30) |
So we can represent any normalised state as a ZX-diagram. Since we also have all complex numbers, we can multiply the normalised state by a number to get an unnormalised state. Or, the other way around: if we have an unnormalised state we want to represent, we first use Eq. (3.30) to represent the normalised as a ZX-diagram, and then compose this with the number , also represented as a ZX-diagram.
Now finally, let’s see how to represent arbitrary linear maps as diagrams. Suppose we are given some linear map from qubits to qubits. We can transform this into an -qubit state by the Choi-Jamiołkowski isomorphism:
As this is a state, we know we can represent it as a ZX-diagram!
| (3.31) |
Here is a complex number needed to scale the state to the right normalisation. We can now bend back the wires in order to get a diagram for itself:
| (3.32) |
We see now that we can indeed represent any linear map between qubits as a ZX-diagram.
We call this property universality. Later on we will also consider various restrictions of ZX-diagrams. We can then also say that this restriction is universal for a given subset of linear maps, if we can represent any linear map from this subset using a ZX-diagram satisfying this restriction. It is important to emphasise again that just because we can represent any linear map using a ZX-diagram, this does not mean that such representations will necessary be ‘nice’. It is an ongoing project to find good representations of useful linear maps in the ZX-calculus. For instance, in Chapter 9 we will introduce a new (derived) generator that allows us to more easily represent Toffoli gates in the ZX-calculus, since the ‘native’ representation as a ZX-diagram is a bit cumbersome.
The thing that makes ZX-diagrams so useful, above and beyond plain old tensor networks, is a collection of rewrite rules called the ZX-calculus. These rules enable you to replace a little piece of a ZX-diagram with another, equivalent piece, without changing the linear map represented by the diagram. Let’s look at a simple example, involving a Z-spider from 1 to 2 wires and an X-spider from 2 to 1:
Then, using the fact that:
we can do a little calculation:
and we see that we have found a pair of ZX-diagrams that are equal, up to a scalar:
| (3.33) |
We won’t see many concrete calculations with bras and kets in this chapter, because once we know a few simple rules, we will be able to apply them graphically to do all the calculations we need to. For example, now that we have equation (3.33), we can use it to derive equations between bigger ZX-diagrams. Here’s one:
Note the scalar factor pops out in front, by linearity. The most commonly-used version of the ZX-calculus, and indeed what is often referred to as the ZX-calculus consists of the following set of rules in Figure 3.0.
We will use these 7 rules quite a bit in the coming pages, so we’ve tried to give them some reasonably easy-to-remember names:
spider fusion | ||
colour change | ||
-commutation rule | ||
strong complementarity | ||
identity rule | ||
hadamard-hadamard cancel rule | ||
euler decomposition of hadamard |
This is sometimes also called the stabiliser or Clifford ZX-calculus, for reasons that will become clear in Section 5.5. Note that some of the rules of Figure 3.0 only hold up to a non-zero scalar. This is fine for most of the book, since we often don’t care about the exact scalar factors. In Section 3.6.2 we discuss rewriting with scalars in more detail, and we present a scalar-accurate version of the rewrites in Figure 3.0. These rules of the ZX-calculus will be our main tool for reasoning about quantum computation in this book, so we better make sure we are all on the same page about them. In the next few subsections we will discuss where each of the rules comes from, and some examples of how you can use them to prove interesting things.
The most fundamental of all the graphical rewrite rules allowed in the ZX-calculus is spider fusion. This says that we can fuse two spiders together when they are of the same colour and connected by one or more wires. When spiders are fused, their phases are added together. In terms of diagrams:
| (3.34) |
Note that we interpret the numbers and as phases and , and hence this addition is assumed to be modulo . Spider fusion can be used to prove many well-known identities of quantum circuits. For instance, that a -phase gate commutes through the control of a CNOT gate:
Flipping the colours, we can use spider fusion to show that an gate commutes through the target of a CNOT gate:
That phases add when spiders fuse essentially generalises the observation that two rotations of the Bloch sphere in the same direction add together:
| (3.35) |
As we have and (cf. (3.6)), we also see that spider fusion proves that applying the Pauli gate to gives :
| (3.36) |
An equation like is given similarly:
| (3.37) |
Note here that we have . The following rewrite rule we already saw in Eq. (3.15):
| (3.38) |
We call this rule identity removal. This rule can be interpreted in several ways. Considering a 1-input 1-output spider as a phase gate over either the Z- or X-axis, this rule says that a rotation by 0 degrees does nothing. Combining it with spider fusion it says that the inverse of a rotation by is a rotation by :
Composing the diagrams of (3.38) with a cup it says we can make a Bell state by either taking a maximally entangled state over the Z basis or over the X basis:
Identity removal also allows us to get rid of self-loops on spiders:
| (3.39) |
Here in the first step we applied the rule in reverse to add an identity. Then we fused the two spiders that are now connected by two wires using the spider fusion rule.
Example 3.2.1. The following quantum circuit implements the GHZ state :
We can verify this by translating it into a ZX-diagram and simplifying it using the rules we have just seen:
| (3.40) |
By the definition of the Z-spider, this last diagram is equal to which is indeed the (unnormalised) 3-qubit GHZ state.
The second set of rules of the ZX-calculus we will look at concerns the interaction of the Pauli X and Z gates and their eigenstates with the spiders. Recall from Eqs. (3.3) and (3.6) that:
The spider fusion rule already covers the interaction of a couple of these maps and states. Indeed we can use the spider fusion rule to show that a Z gate commutes through a Z-spider:
Spider fusion also shows what happens when a or state is applied to a Z-spider:
By flipping all the colours we then also know that an X gate commutes through an X spider and that the and fuse into an X spider. A more interesting question is what happens to an X gate when it encounters a Z-spider (or vice versa, a Z gate that encounters an X-spider). Consider a Z-spider with just a single input and no phase. Its linear operator is then given by . Hence, if we apply an X gate to the input the resulting linear operator is
as an X gate interchanges and . This is easily seen to be equivalent to a Z-spider followed by an X gate on each of the outputs:
Hence, in terms of diagrams, we have:
| (3.41) |
We will refer to this as the -copy rule. By applying the appropriate cups and caps we see that such a -copy rule continues to hold regardless of how many inputs and outputs the spider has. But what about when the spider has a non-zero phase? Using spider fusion in reverse to unfuse the phase we can reduce this to a simpler question:
| (3.42) |
Hence, we need to resolve what happens when we apply an X gate to a state:
Ignoring the global phase we can write this in terms of a diagram:
| (3.43) |
The most generic case, together with its colour-flipped counterpart is then:
| (3.44) |
With this rule we can also see what happens if there are already multiple phases present:
| (3.45) |
We see then that the position of the ’s on the wires of the spider gets toggled. Similar copy rules hold for the eigenstates of the X and Z operators. Indeed, applying a to the 1-input Z-spider it is clear that we get the output :
| (3.46) |
The same works for (although we then do get a global phase of that we will ignore):
| (3.47) |
By introducing a Boolean variable that is either or we can represent the last two equations as a single parametrised equation:
| (3.48) |
Here the phase is either zero (when ), or (when ), and hence the input represents the -basis state (being either or ). As before, the same equation but with the colours flipped also continues to hold, which gives rules for copying and states through X-spiders. We will refer to all these rules as state-copy rules. These state-copy rules only hold when the spider being copied has a phase of or . For any other phase the analogue of (3.48) does not hold, nor would we expect it to! Quantum theory famously does not allow you to clone arbitrary states. Note here the important quantifier of ‘arbitrary’. The reason that a Z-spider can act as a cloning process for the and states is because they are orthogonal. It does not clone any other state. Note that the orientation of the wires in the rewrite rules we have derived is irrelevant, as we can pre-compose and post-compose each rule with ‘cups and caps’ to change inputs to outputs and vice versa. For instance, for the ‘reverse’ of the state-copy rule:
| (3.49) |
These copy rules all deal with the Pauli and and their eigenstates. We can prove similar copy rules for the Pauli and its eigenstates, since we could also define a Y-spider as mentioned in Remark 3.1.1. In particular, we will find a way to write the eigenstates in Exercise 3.15.
Example 3.2.2. Knowing how the Pauli operators commute through a circuit (or a more general computation) is important for several areas in quantum computation, such as when verifying the functioning of an error correcting code, implementing a measurement-based quantum computation, or calculating the stabiliser tableau of a Clifford circuit. The ZX-calculus makes it easy to remember the rules for how the Paulis propagate. Indeed, the only rules required are spider fusion , -copy rule , the colour-change rule to be introduced in the next section (and occasionally an identity will need to be removed with ). For instance, suppose we wish to know how the Pauli operator commutes through the following circuit:
We write it as a ZX-diagram with the Pauli and on the left, and start pushing them to the right:
Note that we have used both to commute spiders of the same colour through each other (like the Pauli Z through the control of the CNOTs, or the Pauli Z through the S gate), as well as to combine the phases of the Paulis in order to cancel out the phases. We see that we end up with the Pauli . Of course, this process was quite lengthy, because every rewrite step is shown. Once you get comfortable with this process, many of the steps can be combined and it becomes easy to see where each of the Paulis ends up.
As has been noted several times in the preceding sections, when we have derived some rule in the ZX-calculus, an analogous rule with the colours interchanged also holds. This follows from the rules concerning the Hadamard gate:
| (3.50) |
The first of these rules states that the Hadamard gate is self-inverse, while the second says that commuting a Hadamard through a spider changes its colour (reflecting the fact that the Hadamard interchanges the eigenbasis of the Pauli and ). Note that the Hadamard commutation rule also holds for spiders that have no inputs, so for instance:
| (3.51) |
Let us give an example to demonstrate how the two Hadamard rules (3.50) imply the colour-inverse of any other rule we have. For instance, suppose we want to prove the colour-inverse of Eq. (3.41):
| (3.52) |
We start with the left-hand side of the colour-swapped version, and then insert two Hadamards on the input by applying the self-inverse rule in reverse:
We then commute one of the Hadamards all the way to the outputs:
We now see the left-hand side of the original (not colour-swapped) equation (3.52), and hence we can apply it:
Now it remains to commute the Hadamards back to the left, and to cancel the resulting double Hadamard:
We have hence succeeded in proving the colour-inverse of Eq. (3.52):
Using the Hadamard commutation rule we can also prove that surrounding a spider with Hadamards changes its colour:
| (3.53) |
In Exercise (2.13) we saw a circuit for building the CZ gate using a CNOT and Hadamards. We can translate this circuit to a ZX-diagram and then simplify it using the Hadamard rules:
| (3.54) |
We see that the diagram we get looks symmetrically on the control and target qubit. This is because a CZ gate is acting symmetrically on its two qubits. I.e. we have . So here the ZX-diagram directly reflects a useful property the unitary has.
Exercise 3.6. Help the trapped phase find its way to the exit.
Note that it might multiply itself before the end.
The previous sets of rules all have a very distinct topologically intuitive character: spider fusion allows you to fuse adjacent spiders of the same colour; identity removal and the Hadamard self-inverse rule allow you to remove certain vertices; the -copy, state-copy and colour-change rule allow you to commute certain generators through spiders by copying them to the other side. It is therefore relatively easy (after you gain some practice) to spot where they can be applied and what the effect of their application will be on the structure of the rest of the diagram. The last major rule of the ZX-calculus takes more time to work with intuitively, although it does correspond to a natural and crucial property of the interaction of the Z- and X-spider.
Before we introduce the rule, let us give some motivation. Treating the and states as Boolean bits, we can view the phase-free 1-input, -output Z-spider as the COPY gate that copies the bit to and the bit to . Indeed, this is exactly what Eq. (3.48) states. Analogously, we can view the phase-free 2-input, 1-output X-spider as the XOR gate:
We already saw this in matrix form in Eq. (3.8), but we can also prove it using spider fusion and the fact that . The XOR gate and the COPY gate have a natural commutation relation: first XORing bits, and then copying the outcome is the same as first copying each of the bits and then XORing the resulting pairs:
| (3.55) |
This equation says that the XOR algebra and the COPY coalgebra together form a bialgebra. This is why the analogous equation in the ZX-calculus is often called the bialgebra rule:
| (3.56) |
However, for reasons we will explain below, we will refer to this rule as (a special case of) strong complementarity. This rule is essential to many proofs in the ZX-calculus. As a demonstration of its utility, let us prove a variation on the well-known ‘three CNOTs make a swap’ circuit identity. We will start with a circuit containing two CNOT gates, and deform it to make clear where we can apply the rule:
| (3.57) |
We then see that we can apply Eq. (3.56) in reverse, and then deform the diagram again to bring it into a simpler form:
| (3.58) |
We could have done all this without the diagram deformation, as the rewrite rules apply regardless of the orientation of the inputs and outputs. The circuit equality is then given in a single step by an application of Eq. (3.56):
| (3.59) |
We can see this as another interpretation of strong complementarity: it relates CNOT gates to the SWAP gate.
We called Eq. (3.59) a version of ‘three CNOTs make a swap’, but it is of course not the real deal. Let’s actually try to prove the real ‘three CNOTs make a swap’ rule:
| (3.60) |
Here in the last step we simply pushed the spiders of the first CNOT through the wires of the SWAP. To finish the proof it then remains to show that two CNOTs applied in succession cancel each other:
| (3.61) |
The first step towards doing this might seem clear, namely, we fuse the adjacent spiders of the same colour:
| (3.62) |
But now we are seemingly stuck. The solution comes from another equation between the Boolean gates XOR and COPY. When we first apply an XOR and then a COPY, we can commute the XOR by essentially ‘copying’ it through the COPY gate. But what happens when we first apply a COPY and then an XOR gate to the two outputs of the COPY:
We see that regardless of the input, we output a . Generalising the notion of an XOR gate to allow for a 0-input XOR gate that just outputs its unit (namely ), and a 0-output COPY gate that copies its output zero times (and hence discards it) we can write this relation diagrammatically as:
| (3.63) |
An algebra (XOR) and a coalgebra (COPY) satisfying this equation are known together as a Hopf algebra. Which is why the following analogous equation between the Z- and X-spider is often called the Hopf rule:
| (3.64) |
However, we will refer to this rewrite rule as the complementarity rule, because this rule can also be seen as a consequence of the -basis and -basis defining complementary measurements: having maximal information about the outcome of a measurement on a state (namely, we will obtain precisely 1 outcome with probability 1) entails minimal information about the outcome of an measurement (namely, we will obtain any outcome with equal probability). Writing and for the Z- and X-eigenbases, we can see that they are mutually unbiased:
| (3.65) |
It turns out that complementarity can be derived from strong complementarity using some clever deformation of the diagram and the rules from the previous sections:
| (3.66) |
In this derivation, we first deformed the diagram, then we introduced identities, we unfused some spiders, applied the strong complementarity rule, copied a state twice, and in the last equation we removed the dangling scalar diagram as it just introduces some scalar factor. The complementarity rule is exceedingly useful. Indeed, combining it with some spider fusion we can use it to show that we can always cancel pairs of connections between spiders of opposite colours:
| (3.67) |
So if two spiders of opposite colour are connected by wires, then this can be reduced to wires. In particular, we can finish the proof that 2 CNOTs cancel each other out, and thus that 3 CNOTs make a SWAP:
| (3.68) |
Exercise 3.7. We proved Eq. (3.60) by applying strong complementarity in the reverse direction. Show that you can also prove it by using it in the forward direction (for instance by applying Eq. (3.56) to the top-left Z- and X-spiders). You will also need to use spider fusion and complementarity to finish the proof.
We can extend equation (3.56) to spiders of arbitrary arity to obtain the full strong complementarity rule:
| (3.69) |
That is, for any two connected phase-free spiders of different colours we can apply strong complementarity, resulting in a fully connected bipartite graph as on the right-hand side. For this is exactly Eq. (3.56). For or this follows in a trivial manner by adding and removing identities (cf. Eq. (3.38)). When or this is just the state-copy rule, i.e. (the colour-swapped) Eq. (3.46). Now suppose and , we will show how to derive its strong complementarity rule using just spider fusion and the version of the rule:
| (3.70) |
Exercise 3.8. Prove the remaining cases of bialgebra for . Hint: first use induction on , and then on .
There are two common mistakes people make when using the strong complementarity rule. The first is that the rule (3.56) only works when the phases on the spiders are zero. When the phases are a modification is possible by combining it with the -copy rules (3.44):
| (3.71) |
Exercise 3.9. Prove Eq. (3.71) using the rules of the ZX-calculus we have seen so far.
But when one of the spiders contains some arbitrary phase , the result will be more complicated:
| (3.72) |
The additional bit of diagram we get is called a phase gadget. We will have a lot more to say about phase gadgets in Chapter 7. The second common mistake is that people apply the rule (3.56) from right-to-left without paying attention to how many outputs the spiders have, wrongfully equating the following diagrams:
| (3.73) |
The correct way to apply strong complementarity here is to first unfuse the spider:
| (3.74) |
Because strong complementarity is one of the most useful rewrite rules, but also the hardest one to see when to use, we want to share one more perspective on what it means topologically. Recall that we can push a phase of the opposite colour through a spider:
| (3.75) |
Note that this is actually two equations, since it holds for both and . Let’s see if we can’t compress this into just a single equation. To do this, we will unfuse the phases and reduce them to just a single instance, by applying the copy rule (3.48) in reverse:
Now let’s compare this again to the left-hand side of Eq. (3.75), where we also unfuse the phase onto its own spider:
| (3.76) |
Now we have an equation which holds for and , where the phase occurs in the same position: as an input to the first qubit. We can then see this as an equation of linear maps where we input a specific state into the top input. But these states are respectively and (up to a scalar factor). This means that this equation holds for a basis of the qubit! But then the linear maps themselves need to be equal regardless of the state that we input. We hence get:
| (3.77) |
But we recognise this as an instance of strong complementarity! This then gives us another perspective on strong complementarity: it is a ‘controlled’ version of the pushing we saw before, where we don’t have to specify what the phase is we want to push. We instead just leave the wire open.
Remark 3.2.3. We need to be a bit careful with this reasoning we used here, because this is actually one of the areas where the scalar factors do matter. We used the fact that if for a basis and linear maps and , that then . But this only works if we have equality on the nose for all . If we just had so that the scalar factor could be different for every , then we wouldn’t get the equality , or even . To demonstrate this, let’s consider and . Then , and , but of course is not proportional to . The reason we were still warranted in making the conclusion we did based on Eq. (3.76), is because here the proportionality constant happens to be the same for both and .
Using this realisation of ‘strong complementarity as controlled pushing’, we can give a nice visual intuition to what happens when you apply strong complementarity:
| (3.78) |
Here we added an additional Z-spider the X-spider is connected to at the start to make it look a little more intuitive. We see then that in the same way that for pushing, the appears on all the other legs of the spider, with strong complementarity the X-spider also appears on all the other legs of the Z-spider.
Exercise 3.10. Prove the following circuit identity by translating them to ZX-diagrams and rewriting the left-hand side into the right-hand side.
Exercise 3.11. Consider the following parametrised unitary:
Using the ZX-calculus show that and that . Note that this equation in particular implies that .
Using the ZX-calculus show that:
| (3.79) |
This previous equation implies that:
| (3.80) |
Show that this indeed holds by calculating the matrix of both sides.
Show that Eq. (3.80) holds by using the ZX-calculus to reduce the left-hand side to a diagram that is clearly symmetric under interchanging the two qubits.
The final rule of the ZX-calculus we need to introduce is a way to write the Hadamard in terms of spiders. Recall that any single-qubit unitary gate is equal (up to global phase) to a Z-rotation followed by an X-rotation, followed once again by a Z-rotation:
This is then of course also true for the Hadamard. In particular for :
| (3.81) |
Here we have included the correct global phase for good measure. This Euler decomposition is just one possible way to write the Hadamard in terms of spiders. There is in fact an entire family of representations that will also be useful to note:
| (3.82) |
Exercise 3.12. Prove that all the equations of (3.82) hold in the ZX-calculus, by using Eq. (3.81) and the other rewrite rules of the ZX-calculus we have seen so far.
By including the rule (3.81) it looks like we are creating an asymmetry in the phases in the rules of Figure 3.0 with special significance being given to over . However, (3.82) shows that the situation is still fully symmetric. In particular, all the rules still hold when we flip the value of all phases from to . In particular we have the following ‘meta-rule’.
Proposition 3.2.4. An equation in the ZX-calculus between diagrams and can also be proven when we flip all the phases of spiders in the diagrams of and .
Such ‘flipping of the phases’ corresponds to taking the complex conjugate of the linear map the diagram represents.
Exercise 3.13. The rules presented in Figure 3.0 are actually not all necessary. Show that we can prove using Eq. (3.81) and the other rewrite rules of Figure 3.0.
Exercise 3.14. Prove using the ZX-calculus that we can get rid of Hadamard self-loops at the cost of acquiring a phase:
| (3.83) |
Exercise 3.15. There are two ways in which we can write the eigenstate of the Pauli operator (up to global phase). Namely, as or as . Prove this equivalence using the ZX-calculus:
| (3.84) |
An analogous equation holds for , which boils down to flipping the signs of the phases in the spiders.
Now that we have seen all the rules of the ZX-calculus, let’s see what we can do with them! In the previous section we have already seen a couple of small use-cases: simplifying a circuit to verify that it implements the correct state in Example 3.2.1, or pushing phases through a circuit to see where it maps Pauli gates in Example 3.2.2. Here we will give several more small examples that demonstrate the use of the ZX-calculus in reasoning about quantum computing. This will also demonstrate several best practices when trying to simplify a diagram using the ZX-calculus.
The following circuit implements the well-known magic state injection protocol, where a magic state is pushed onto a qubit using a measurement and potential correction:
| (3.85) |
Here the double wire represents a classical measurement outcome being fed forward into the gate, so that the gate is only applied if the measurement outcome corresponded to . We can represent this in the ZX-calculus by introducing a Boolean variable to represent the measurement outcome. We can then easily prove its correctness:
| (3.86) |
Most of this proof is spider (un)fusion, but there are a couple of interesting bits. The step labelled actually consists of two branches, because if , then the X-spider can be removed using so that the phase on the Z-spider remains , while if , then is indeed applied, and the phase flips to . Combining these two branches we see that the phase becomes . In the step after that we make the observation that . The usage of a variable to denote a measurement outcome is an easy way to deal with circuits that contain measurements and classically-controlled gates. While it is easy to use, it does have the drawback that it is less clear in which direction the information is ‘flowing’. Here is the result of a measurement outcome that we don’t have control over, while denotes a phase gate that we only apply when we saw the measurement outcome . For more complicated scenario’s where we do care about showing this causality more clearly we could adopt a variant of the ZX-calculus where we allow both classical and quantum wires to exist. We will however not need this additional machinery in this book.
The standard state-teleportation protocol consists of two parties, Alice and Bob, that share a maximally entangled state . Alice does some quantum operations, measures her states, sends the classical measurement outcomes to Bob, and Bob does some quantum corrections based on those outcomes. At the end Bob has the state that Alice started out with. We can represent this as a quantum circuit as follows:
Here the label is to denote that the bottom two qubits start in the maximally entangled Bell state. By representing the measurement outcomes by Boolean variables and we can again represent this in the ZX-calculus and prove the correctness of the protocol:
Here we used the fact that is either or which are both modulo , and hence the spider can be removed using . We see that we end up with a wire going directly from Alice and Bob. Hence, whatever state Alice puts in her qubit wire will end up at Bob.
Let us now do a more involved calculation with the ZX-calculus, demonstrating how one goes about systematically simplifying a diagram. Suppose you are given the following (somewhat randomly chosen) quantum circuit:
| (3.87) |
Suppose further that you wish to know which qubits are entangled after applying this circuit. Calculating the state directly gives
While a trained eye might be able to spot that the 2nd qubit is unentangled, and that the remainder form a GHZ-state, at first glance this is altogether not obvious. Instead, let’s simplify this same circuit in the ZX-calculus and see where this gets us. In this simplification we have a step marked (*) which will be explained after.
If you don’t stare at diagrams for a living like we do, it might not necessarily be clear what is happening here, but we are in fact following a simple algorithm. Most of the steps follow the general pattern that we always try to fuse spiders of the same colours with (though we don’t do that here for all spiders at the same time to prevent cluttering), always remove identities with , always copy states through spiders with , and always remove parallel wires between spiders of opposite colours using the Hopf rule (3.64). Hadamards are moved out of the way with when no other obvious move is available. Additionally we have used the rewrites of Eq. (3.83) and (3.84). We have marked one rewrite by (*), this is just an application of the Euler decomposition rule:
This equation is an example of how a series of Clifford phase gates can often be combined together into a simpler series of phase gates. We will have a lot more to say about Clifford gates in Chapter 5. The size of the circuit (3.87) is nearing the limit of what is still comfortable to rewrite manually. In fact, we used some software to verify and help with the rewriting here: the algorithm we just described is easily implemented, so that the entire simplification can be automated. We presented the derivation here in detail to show how one would go about systematically simplifying a ZX-diagram. We will look at more detailed approaches to simplifying diagrams in other chapters.
Exercise 3.16. Write the following circuit with input state as a ZX-diagram and simplify it to figure out which qubits are entangled to each other.
Back in Section 2.4, we introduced the Bernstein-Vazirani algorithm, as a simple example where a quantum computer gets a modest advantage over a classical computer. Namely, one can extract a secret piece of information from an unknown function on bits using just a single quantum query to the function (i.e. one use of the unitary oracle ), rather than classical queries. Here is the problem, stated formally: Given: a function such that there exists a fixed bitstring such that . Find: . We saw that the quantum algorithm for solving this problem is very simple. If we simply perform the following circuit:
we obtain the bitstring as the measurement outcomes. We gave a proof using bra-ket calculations in Section 2.4. We’ll now give a different proof using the ZX calculus. This follows almost immediately from finding a nice diagram to capture the oracle . We claim that can be written as follows:
| (3.88) |
where the labels on the wires mean there is an edge connecting the Z spider on the -th qubit to the one X spider if and only if . This works because, as we saw in Section 3.1.1, the X dot acts like an XOR on the computational basis. Hence, if we take the XOR of all the bits where , we get the dot product . If we additionally XOR in some other bit , we get . Since the Z dot copies computational basis elements, (3.88) does exactly what the Bernstein-Vazirani oracle should do:
Now, let’s see what happens if we plug into the first qubits and into the last qubit. In Section 3.1.1, we saw that:
Plugging in, we get:
In the first step, the -labelled Z spider is copied through the X spider. This will give a -labelled Z spider on the last output and anywhere there is a wire to one of the other Z spiders, i.e. whereever . If we fuse these in, we see that we get a gate where and identity where . We can capture this succintly in the middle picture above by writing this as the Z-phase gate . After the final spider fusion, we can see the state on the first -qubits is an element of the X basis. Hence, if we measure in the X basis (i.e. perform Hadamards then measure in the Z basis), we will obtain the outcome with certainty. This completes the correctness proof for the Bernstein-Vazirani algorithm.
We have already seen that ZX-diagrams are a strict superset of circuit notation. That is, if we have a quantum circuit, it is straightforward to translate it into a ZX-diagram. But what about the converse? How do we translate a ZX-diagram back into a circuit? The first thing to note is that ZX-diagrams can represent more types of maps, that aren’t necessarily unitaries or isometries, and hence in general there is no way to turn an arbitrary diagram back into a circuit. But what if you know somehow that the diagram implements a unitary, how do we get a circuit back out? It turns out this is hard enough that we should state it is a proper Problem:
Problem 3.4.1 (Circuit Extraction). Given a ZX-diagram with designated inputs and outputs, which describes a unitary map. Produce a circuit whose gate-count is polynomial in the number of spiders in .
It turns out that Circuit Extraction is a “Hard with a capital H” problem in the complexity-theory sense, which you will be asked to prove in Exercise* 7.8. Nevertheless, if we impose some restrictions on a ZX diagram, the problem becomes much easier. In this section we will show how diagrams we call circuit-like can be made back into circuits.
First, it’s worth noting that obtaining a circuit with post-selection is relatively straightforward. A generic ZX-diagram can be constructed using the following 5 things:
We can see this by transforming each X-spider into a Z-spider using , and then unfusing spiders until they all have at most 3 legs. We can interpret each of these generators as circuit fragments:
where the first two require ancilla qubits in the states and and the second two require post-selection onto or . With this interpretation, there is an evident translation of any ZX-diagram into a post-selected circuit. First ‘unfuse’ the spiders into generators, then interpret them as post-selected circuits, e.g.
It could be that we are happy with this. Indeed any post-selected circuit corresponds to a particular sequence of measurement outcomes. And, if we start with a ZX-diagram that is not equal to zero, the post-selected circuit will also be non-zero, so it corresponds to a computation that is realisable with non-zero probability. However! The probability of getting the diagram we wanted will, in general, becomes exponentially small with the number of post-selections. So that kindof sucks. What would be much better is to turn a ZX-diagram into a computation that can be run deterministically (or at least with high probability) on a quantum computer.
One of the simplest kinds of ZX diagrams that are easy to extract are the circuit-like diagrams.
Definition 3.4.2. A ZX diagram is called circuit-like if it can be decomposed into a circuit consisting only of CZ, , and gates just by applying the colour change rule and unfusing spiders, i.e. applying the rule from right-to-left.
While this seems to be pretty much building the property we want directly into the definition, there is (essentially) an equivalent formulation for circuit-like diagrams which is called ZX diagrams with causal flow. We will meet these in Chapter 8 (specifically Section 8.2.4), along with several generalisations that are relevant to circuit optimisation and measurement-based quantum computing. Even though the circuit-like ZX diagrams are a very limited class of diagrams, we can already define a (pretty effective!) quantum circuit optimisation algorithm that never leaves the realm of circuit-like diagrams.
Algorithm 3.4.3. Circuit optimisation using circuit-like ZX diagrams
Replace every CNOT and gate by its decomposition into CZ, and , or equivalently use to turn all X spiders into Z spiders.
Apply the rules , , , and the following derived rule:
| (3.89) |
from left-to-right as much as possible.
Extract the circuit from the resulting circuit-like ZX diagram by unfusing spiders.
There are a few things that we should show before we are happy with Algorithm 3.4.3. First, we can see that (3.89) is indeed a derived rule. Since this is essentially just an “all-Z” variation on the complementarity rule we met in Section 3.2.4, this is straightforward enough to be an exercise for now.
Exercise 3.17. Show that (3.89) follows from the rules of the ZX calculus.
The second thing we would like to know is that we indeed have a circuit-like ZX diagram by the time we get to step 3. The final thing we need to know is which spiders to unfuse to turn the circuit-like diagram into a circuit. Let’s look at these properties in turn.
Proposition 3.4.4. Left-to-right applications of , , , and (3.89) preserve the property of being circuit-like.
Proof. preserves circuit-like diagrams by definition. Suppose we can apply the rule to a circuit-like ZX diagram. That means that there must be a 2-legged Z spider with phase 0. The only way this can arise from spider fusions of a circuit is if the circuit contains a gate. Deleting this trivial gate from the circuit results in a new circuit, which can be obtained by unfusing spiders after applying the rule. We can reason similarly about the rules and (3.89). The only way the left-hand sides of these rules can arise from spider fusions of a circuit is if the circuit contains a pair of cancelling gates or cancelling CZ gates, respectively:
Again, deleting these gates from the circuit will result in a new circuit, which we can obtain by unfusing spiders after applying the and (3.89) rules, respectively. □
Now, let’s see how to actually perform the spider-unfusions necessary to extract a circuit from a circuit-like diagram. It turns out we can actually just do this greedily. Imagine a frontier between the part of the diagram that has been decomposed into CZ, H, and gates. We initially start with the frontier on the outputs, and move the frontier toward the inputs, performing spider-unfusions as necessary. For this, we consider three cases:
If an gate is on the frontier, move it to the circuit part of the diagram.
If a 2-legged Z spider is to the left of the frontier, it can only be an gate, so move it to the circuit part.
If two spiders on the frontier are connected via a Hadamard gate, perform spider-unfusion as necessary to produce a CZ gate, and move it to the circuit part.
For circuit-like diagrams, this will never get stuck, and terminates when there are no spiders or Hadamards to the left of the frontier. Proving this is much easier when we have some extra tools in our tool belt (namely the aforemented notion of causal flow, which we introduce in Chapter 8), so we will postpone this proof until then. However, you should know enough at this point to code up a full-fledged circuit optimisation algorithm using the ZX calculus. You can see for yourself how well it works!
Spiders are a class of linear maps that can have any number of inputs and outputs. They come in two flavours: Z-spiders and X-spiders.
Spiders can be connected together to form ZX-diagrams. Stacking ZX-diagrams correspond to the tensor product of linear maps, and connecting ZX-diagrams corresponds to composition.
ZX-diagrams can be arbitrarily deformed as long as the order of inputs and outputs is preserved.
ZX-diagrams are universal, meaning that they can represent any linear map between any number of qubits.
In particular, any quantum circuit consisting of Z- and X-phase gates and CNOTs can be easily written as a ZX-diagram. Conversely, any ZX-diagram can be written as a post-selected quantum circuit.
The ZX-calculus is a set of graphical rewrite rules for transforming ZX-diagrams while preserving the linear map they represent.
There are 7 rules of the ZX-calculus (see Figure 3.0): spider fusion, colour change, -copy, strong complementarity, identity, Hadamard-Hadamard cancellation, and Euler decomposition of the Hadamard.
We can code up a pretty simple yet effective circuit simplification routine by converting all X-spiders to Z-spiders, and applying spider fusions, Hadamard-Hadamard cancellations and Hopf rules (Eq. (3.89)) wherever possible, and then extracting a circuit from this diagram.
While many of you might be familiar with mathematical rewriting of algebraic expressions, you might be unfamiliar with doing the same with diagrams, and might even be sceptical that what we are doing is even valid mathematics. To ease those worries, we will say a bit more about what is actually going on formally when we rewrite a diagram. The rewrite rules we have given in this chapter are of the form , where and are some ZX-diagrams with the same number of inputs and outputs. We then used these rewrite rules on larger diagrams that contain either or as subdiagrams. This is because when we are asserting , we are actually asserting an entire family of equalities. We are for instance also asserting that for any other ZX-diagram , and for any composable ZX-diagram . In a sense we are assuming we have the following implication:
| (3.90) |
In words: if appears as a subdiagram of some larger ‘ambient’ diagram , then can be replaced by inside of . We are saying we can apply the rewrite rule regardless of the context. An important question to settle here is whether this is sound. A rewrite rule in a language is called sound when it preserves the underlying semantics, i.e. the underlying meaning. In the setting of ZX-diagrams, the language is the set of diagrammatic rewrite rules of the ZX-calculus, and the semantics of a ZX-diagram is the linear map it represents. So a rule in the ZX-calculus is sound when the diagrams on each side of the equation represent the same matrix. For a single equation like soundness is easy to check: just calculate the matrix on both sides. But how can we be sure that employing this rewrite rule in a larger context as in (3.90) is still sound when we would have to check an infinite family of diagrams? To make the following discussion more clear, we will denote the matrix of a ZX-diagram by . We can then say a diagrammatic rule is sound when . By definition of how we calculate the matrix of a ZX-diagram, we have and , where this last composition is just matrix multiplication. Hence, if we have , then we also have , and hence is also sound. Similarly we have . Since we build any ZX-diagram by iteratively composing and tensoring, this shows that the rewrite rule stays sound regardless of the context it appears in, and hence that the implication in (3.90) is indeed true. Even given this discussion, some of you might still not be happy about this. How can all of this be rigorous mathematics when it is just pictures on a page? Since this is a quantum computing book and not a mathematics book, we won’t give the actual proof that all of this is solid here, and instead we will give a Proof by Fancy Words and point the interested reader to the References of this chapter. So here it comes: the right formal language to think about the ZX-calculus is category theory. In a symmetric monoidal category we can compose morphisms via both horizontal and vertical composition. The coherence theorem then tells us that the coherence isomorphisms of a symmetric monoidal category are enough to prove we can move morphisms around on a page while preserving equality. Because ZX-diagrams have cups and caps they are actually an example of a compact closed category, for which an even stronger coherence theorem is true, so that we can move the generators around the cups and caps as well. The interpretation into linear maps is a strong monoidal functor from ZX-diagrams to the category of complex vector spaces. The rewrite rules can be understood as an equivalence relation which respect composition and tensor product, so that it induces a quotient category of ZX-diagrams modulo equality by rewrite rule. Soundness of the rewrite rules means that the functor into linear maps restricts to this quotient category.
We have been ignoring non-zero scalar factors in ZX-diagrams in this chapter, and will continue to do so for most of the rest of the book. This is not however because the ZX-calculus is incapable of reasoning about scalars. The opposite in fact: because it is a straightforward exercise to deal with the scalar factors once you know a couple of tricks, it just isn’t worth the effort of including them most of the time. In this section we will see what some of these tricks are. So first, the rule set of Figure 3.0 is not taking into account the actual scalar factors. Instead, we should use a rule set where we do have all the correct scalars. See Figure 3.0.
Most of these rules should look familiar, but there are some perhaps weird-looking things going on. Let’s go through the differences rule by rule. The spider-fusion rule and the colour-change rule are unchanged. We have split up the strong complementarity rule into two parts, a bialgebra rule and a state-copy rule. This is necessary because the scalar factor for strong complementarity depends on the number of inputs and outputs of the diagram. These scalar subdiagrams included in the rules should make more sense once you recall what they are equal to:
The identity-removal rule and the Hadamard cancellation rule are still the same, but we now have chosen for a different Euler decomposition of the Hadamard that does not need a global phase to be accurate. We have replaced the pi-commutation rule for a more minimal one that has now acquired a scalar factor (which corresponds to a global phase of ). Finally, there are two new rewrite rules. The first rewrite rule tells us that in the presence of a zero scalar, essentially anything equals each other. For instance, as we will see later, we can use it to show that the identity wire disconnects. The second rule allows us to cancel some scalars. This essentially implements . Note that the right-hand side is the empty diagram. This rule is in fact necessary: there is no other rewrite rule that can produce an empty diagram, so the equation of can not be proven by any combination of the other rewrite rules. It can also be shown that is necessary using a more complicated argument. Now in this section we will show two things we can do with scalars. First, we will show that when there is a zero scalar that the entire diagram can be simplified, so that every two diagrams with this scalar in it are the same. Second, we will show that certain scalar diagrams can be ‘multiplied’ together in exactly the way we would expect for their corresponding scalar values. So let’s prove some things about scalar diagrams. In this section, whenever we refer to a rewrite rule like , we are meaning the rewrite rules from Figure 3.0, not Figure 3.0. Note that due to and that all the rules also hold with the colours interchanged as usual.
Proof.
Now we can show that any two diagrams that contain a zero scalar can be rewritten into each other by reducing them a unique normal form. This will require a couple of lemmas.
Proof.
Proof.
Proof. First we prove the (colour-reversed) version where :
| (3.91) |
And then we prove the general case:
Proposition 3.6.5. Any ZX-diagram with inputs and outputs containing a zero scalar can be rewritten to the following normal form:
| (3.92) |
Proof. Apply Lemma 3.6.3 to all the connections between spiders and the inputs and outputs. The resulting diagram is then that of Eq. (3.92) except that there might be some scalar pairs of spiders and some isolated spiders, at most one of which has a phase. This phase can be removed using Lemma 3.6.4. If it is an isolated spider then it can be decomposed into a pair of pairs using Lemma 3.6.1. Now all the scalar pairs of spiders can be removed using Lemma 3.6.2. □
Now let’s see how we can combine some scalar diagrams by ‘multiplying’ them together.
Proof.
As a special case, where we can show that :
Note that when the X-spider does not have a phase, that the phases simply dissappear as in Lemma 3.6.4. We can also simplify some expressions involving . This is because this diagram equals , and when we multiply it with itself, for instance, we get , which should be equal to a simpler diagram. In order to prove this, we will need the following scalar-accurate version of Eq. 3.84 in Exercise 3.15.
Proof. We prove this using the colour-reversed Euler decomposition rule of Figure 3.0:
Proof. First, when we have two subdiagrams we do the following rewrite:
| (3.93) |
When we have multiple diagrams resulting in this way, we can combine them using Lemma 3.6.6. We can use this to combine a and a as well:
| (3.94) |
We leave it to the reader to find an analogous simplification of Eq. (3.93) to combine . After applying these rewrites there are then at most two phases in the diagram, one coming from a possible or , and the other coming from a diagram which results from the rewrite (3.93). These can be combined as follows:
The case with is proven similarly. □
There is of course more to be said about calculating the values of small scalar ZX-diagrams in this way, but hopefully the reader can see now how this generally can be done. We will use these results on manipulating scalars in Section 5.5 when we prove that the rules of Figure 3.0 are actually enough to prove all equalities concerning ZX-diagrams where the phases are multiples of .
Categorical quantum mechanics The ZX-calculus came forth from the field of categorical quantum mechanics, an area of research initiated by [2] that aimed to study quantum phenomena abstractly, using nothing but the structure of symmetric monoidal categories. These are mathematical structures wherein both composition and tensor product make sense [205]: in a category we have composition [165], ‘monoidal’ means that there is a tensor product, and ‘symmetric’ means that we have a SWAP operation. In a ZX-diagram we can’t just swap two inputs, we can also swap an input with an output using cups and caps. A category with this kind of structure is called compact closed [141]. It is in this abstract category theory that it was proven that we can move the generators around on the page while preserving the semantics of the diagram, and hence why it even makes sense to write it as a diagram. The original categorical quantum mechanics paper [2] gave a proof of quantum teleportation that is essentially the basis of the ZX proof in Section 3.3.2.
The origin of the ZX-calculus The ZX-calculus was born on a bus in Iran in 2007. Bob Coecke, a professor at Oxford and Ross Duncan, a recently-finished PhD student, were discussing how to capture a certain feature of quantum measurements using category theory. In particular, they were thinking about mutually unbiased bases (mubs), i.e. sets of bases that pairwise satisfy equation (3.65) from Section 3.2.4.2. While mubs, which were first introduced by [204], are very simple to define, they have a suprisingly rich structure that is still only partially understood. A running joke at the time was, if you wanted to ruin a quantum computer scientist’s career (and personal life), you should just introduce them to the problem of classifying all mubs in dimension 6. Earlier that year, [55] gave a more “categorically flavoured” notion of fixing a measurement, which they called classical objects. These have gone under a variety of names—notably classical structures or dagger-special commutative Frobenius algebras—but these days most people refer to them as spiders. It seemed at the time that these abstract algebraic entities gave the correct notion of fixing an ONB without needing to refer to concrete Hilbert space structure, an intuition that was vindicated in 2013 when Coecke, Pavlovic, and Vicary showed that spiders are in 1-to-1 correspondence with ONBs [56]. By studying two families of spiders satisfying some interaction equations, Coecke and Duncan hoped to get some deeper insights in the behaviour of mubs, and hence the ZX-calculus was born [57]. In fact, it wasn’t called the ZX-calculus until almost three years later [58], with most early talks and papers referring to it as the “calculus of complementary observables”, simply “the graphical calculus”, or even the “Christmas calculus” due to its convention of using green and red dots for Z and X spiders. As the story goes, this convention was born from the fact that black and blue whiteboard markers in the Oxford Computing Laboratory were constantly running out of ink, so the only working ones were green and red.
Strong complementarity The name of strong complementarity for the rule of Section 3.2.4 was introduced in [59], but as noted in that section is also called the bialgebra equation. In fact, the bialgebra of a Z-spider and X-spider we have in the ZX-calculus is a very special case, as both spiders are Frobenius algebras. Two Frobenius algebras that form a bialgebra are called interacting Frobenius algebras. It was shown in [87] that these also form a Hopf algebra, essentially using the proof of Eq. (3.66), and hence that strong complementarity implies regular complementarity.
Completeness The version of the ZX-calculus from [57] was missing the Euler decomposition rule. This rule was introduced by [90], where the authors showed that, without this rule, the ZX-calculus is incomplete for Clifford ZX-diagrams. With this rule, one obtains a version of the ZX-calculus equivalent to the one used in this book, which was shown to be complete for Clifford ZX-diagrams by [19]. That same year, [203] showed that the Clifford ZX-calculus was incomplete for non-Clifford ZX-diagrams, i.e. if we allow angles that are not multiples of . The counter-example, suggested by Kissinger, came from considering the two possible Euler decompositions of a single-qubit unitary:
| (3.95) |
It is always possible to compute as (fairly elaborate) trigonometric functions of . When the angles on the LHS are not multiples, the angles on the RHS are typically irrational multiples of , and it is not possible to prove equation (3.95) using just the Clifford ZX rules. This remained the status of completeness for several years, and it seemed like it might even be the case that no finite set of rules would suffice to show completeness for a computationally universal fragment of the ZX-calculus such as Clifford+T ZX-diagrams (with multiple phases) or all ZX-diagrams (with arbitrary phases). Some smaller results appeared, such as the one-qubit Clifford+T completeness of [20] and the 2-qubit completeness of [63], but it remained unclear how to extend these to qubits. In fact, Aleks would have bet money in 2015 that no finite, complete set of rules for -qubit Clifford+T ZX-diagrams existed. Picturing Quantum Processes, the precursor to this book, which came out in the spring of 2017, had this problem as an “advanced exercise”, at least partially as a joke. However, it’s a good thing Aleks didn’t bet, because he would have lost his money. In the autumn of 2017, Jeandel, Perdrix, and Vilmart showed a complete ZX-calculus for the Clifford+T fragment [136], and later that same year Wang and Ng showed completeness for all ZX-diagrams [187]. The key idea in these two completeness theorems was to use an encoding into a related graphical calculus called the ZW-calculus, formulated by [53] and proved complete by [121]. While these presentations of the ZX-calculus were complete, they contained some redundancy, in the sense that some rules could be derived from others. A more compact presentation of the Clifford ZX-calculus, consisting just of the spider rules, strong complementarity, Euler, and colour change rules, was given by [24]. Similarly, a small complete set of rules for the full ZX-calculus was given by [229], essentially by adding (3.95) to the basic Clifford rules.
Measurement-based quantum computing Along with understanding the structure of mutually unbiased bases, a lot of the early development into ZX-calculus was motivated by providing a convenient notation for working with the one-way model of measurement-based quantum computation (MBQC), which we’ll discuss extensively in Chapter 8. The representation of cluster states as ZX-diagrams already appeared in the first paper [57] and started to be put to good use in subsequent work relating quantum circuits and measurement patterns [91] and doing ZX-based optimisation of quantum circuits [88].
Resources To some extent, this book grew out of the survey/tutorial paper ZX for the Working Quantum Computer Scientist [222], which presents some of the topics we cover in a more condensed form. A good place to start learning more about ZX is zxcalculus.com, which maintains a tagged list of 100 papers on ZX, related calculi, and applications. It also contains information about various communities, online seminars, and mailing lists. The diagrams in this book were typeset with the help of TikZiT, which acts as a GUI for making LaTeX suitable TikZ diagrams. TikZiT was made by Aleks in an effort to make typesetting the 5000 diagrams in his previous book somewhat less horrific, and his current coauthor John is very thankful for its existence.