The ZX-calculus has two important meta-rules:

- Only connectivity matters
- Everything is still true if you swap red and green

The rule is simple: If two green spiders are joined (there is a wire from one to the other) then you can merge those two spiders together. As you do so the newly merged spider maintains all its other connections. If the spiders had angles on them (called phases) then you add those angles together, remembering to work modulo $2 \pi$.

Wires that don't have a node at both ends are called "open wires." These indicate that we don't mind what those wires are attached to at the other end. Think of them as boundaries that allow us to embed our small diagram into some larger context. The "..." indicates that we also don't mind how many of these wires there are (including possibly 0 wires.)

The usual presentation of this rule is a little more complicated, because it says that if you have have one or more wires between the nodes you can merge them, but this version will do for now.

The green spider rule, saying you can merge two green spiders if they are joined by a wire

The analogous red spider rule, found by just swapping the colours red and green everywhere

Since everything remains true if you swap red and green (you need to swap it everywhere!) then this rule, called the red spider rule, also holds true. It says precisely the same thing, but for red spiders.

The wires joining the spiders can be as long as you like, can bend around, leave the node in any direction, and are allowed to cross other wires freely. As the first meta-rule says: All that matters is the connectivity ("how many wires are there") between the nodes.

It's important to remember that these rules are equalities: Whenever you see part of a diagram that looks like the left hand side of a rule you can replace it with the right hand side of the rule, but also whenever you see an instance of the right hand side of a rule you can replace it with the left hand side of the rule too.

Here is a rule involving a green $\alpha$ spider (meaning it can have any angle on it) and a red $\pi$ spider, running along a single wire. The rule says that we can swap the order of these two spiders, but it has the side affect of flipping the sign in front of the $\alpha$.

The $\pi$-commutation rule (as this is known) we show here is actually only true up to a complex scalar factor. This is something that can be easily fixed by just including the scalar, but many people prefer to work with scalars left implicit (to avoid clutter in their diagrams.)

The pi-commutation rule (up to a complex scalar factor)

The Hadamard rule allows you to convert red spiders to green spiders and back again using Hadamard gates

This is called the Hadamard rule, and it describes the action of the Hadamard gate (the yellow squares.) It says that we can use Hadamard gates to turn a green spider into a red spider (and, because of the second meta-rule, red spiders into green spiders.) It can be shown that two Hadamard gates applied in succession cancel each other out, meaning that not only is this a very versatile rule, but there is also a limit to how many Hadamard gates you will need in your diagram. There need to be Hadamard gates (yellow squares) on all of the wires in the left hand diagram.

Why is it a Hadamard gate, not a Hadamard spider? The Hadamard (in ZX) is limited to having two wires and two wires only. One can actually build it from red and green spiders, and so it's really just a short-hand symbol, but it's such a useful one that it is sometimes given the same level of status as the red and green spiders.

Since two Hadamard gates in a row cancel each other out, the creation and deletion of pairs of Hadamards is often done implicitly. This means that one can "pull" a Hadamard gate through a spider, changing its colour, and creating new Hadamarad gates on every other leg.

These aren't all of the rules of the ZX-calculus, but they should give you enough indication of of how to read the rules. In 2018 a complete set of rules for the ZX-calculus was found: This means that if you have two diagrams that describe the same matrix (we talk about how to do that later,) then the rules of the ZX-calculus can take you from one diagram to the other. Since then other complete sets of rules have been found, and it is now a matter of choosing the right set of rules for the job in hand.

Now that we know what the rules are, let's talk about how to use them. The intuitive notion behind rewriting (as rule application is also known) is to identify part of a diagram that matches the left hand side of your rule, and replace it with the right hand side (or vice versa,) keeping the rest of the diagram in place.

Here we have applied the Hadamard rule (described earlier) to the diagram, by first identifying a place where it can be applied, then replacing the matched region with the right hand side of the rule.

Since we can bend wires and move nodes around as much as we like, it can often be hard to spot where such a rule application can take place. Remember that all that matters is which nodes are connected to which other nodes.

The dashed rectangle shows where we are applying the Hadamard rule

Choices of which rule to apply and where have consequences

To the left we show two possible chains of rules (out of many.) The grey arrows show rule applications. Starting at the top we pull the Hadamard gate through a spider, but we have a choice of which spider to use! After doing so we merge the adjacent spiders of the same colour. The ZX-calculus does not have a terminating (nor confluently oriented) set of rewrite rules, so choices of which rules to apply and where to apply them really do matter.

The ZX-calculus overlaps with quantum computing, traditional logic, and category theory. It uses ideas that have similar, but not identical, names in these areas. Here is a short glossary that will hopefully help a reader when they go on to read other documents.

One can build all quantum circuits from single qubit Z and X rotations, and two qubit CNOT gates. Quantum circuit diagrams are very rigid in structure: There are a fixed number of wires, and a fixed direction of flow in the diagram.

To turn a quantum circuit diagram into a ZX-calculus diagram is extremely easy:

- Keep wires as they are
- All Z rotation gates become green spiders
- All X rotation gates become red spiders
- All CNOT gates become a (connected) pair of red and green spiders, with the green spider on the control qubit.

The CNOT gate, here shown so the wire connecting the two nodes does not run vertically

Interpretations of the bent wire ("cup"), Hadamard, and green spider elements of the ZX-calculus

There are ZX diagrams that do not correspond to any unitary circuit
(this isn't a problem, it just means ZX can describe non-unitary matrices.)
As such there is no general method to turn a ZX diagram into a circuit diagram,
although a lot of work has gone into working out how to do so when you can.
What we

To do this we only need to know what to do with green nodes, bent wires, and Hadamard gates (and how to glue these elements together.) This is because we can turn any red nodes into green nodes using the Hadamard rule described earlier. We also need to pick a direction of flow in our diagram. We'll be using left-to-right here, but any other orientation is possible (and needs to be clearly stated.)

The big square brackets mean "interpretation of", and sends a diagram to a matrix. Spiders can have any number of input and output legs, and a spider with $n$ inputs and $m$ outputs is interpreted as a matrix with $2^n$ columns and $2^m$ rows.

ZX-calculus diagrams are read the same way that circuit diagrams are read: If your circuit is running left-to-right then you break the diagram down into columns. You view everything happening in a column as happening at the same time, and take the tensor product of all the elements in that column. Then you view the sequence of columns as a sequence of operations, taking the matrix composition of those operations.

To the right is a diagram that has been broken down so that each cell in the grid contains just one element. Remember you can bend wires around as needed to make this easier to do.

An indication of how to break a diagram down into smaller diagrams, so that each cell contains only one element.

Bent wires are a thing that may catch you off guard if you come from circuit diagrams. The interpretation of a bare wire running with the flow of your diagram is just the identity matrix. How about when a wire bends round to form a "cup"? This interpretation is given above, but we leave out what to do when the wire forms a "cap". This is because reflecting a diagram (in the vertical axis) has the effect of applying a transpose to the matrix interpretation.

For diagrams that flow left-to-right there is no interpretation for a vertical wire. We get around this by giving any vertical wires the slightest of bends, so that it forms either a cup or a cap (and it does not matter which option one chooses.)

Hopefully this introduction will give you an indication of how to read ZX diagrams and their rules. The publications page gives a list of the latest papers, and you can filter by authors, titles, keywords, and abstracts to find the content you are looking for. There is also a Discord where you can ask questions about ZX or discuss general other topics in the quantum computing sphere. If you have any comments or suggestions on how we can improve this tutorial, please contact a member of the website team.

Website by Hector Miller-Bakewell and John van de Wetering, with thanks to Dom Horsman and Richard East