# ZX-calculus

The ZX-calculus is a rigorous graphical language for reasoning about linear maps between qubits, which are represented as string diagrams called ZX-diagrams. A ZX-diagram consists of a set of generators called spiders that represent specific tensors. These are connected together to form a tensor network similar to Penrose graphical notation. Due to the symmetries of the spiders and the properties of the underlying category, topologically deforming a ZX-diagram (i.e. moving the generators without changing their connections) does not affect the linear map it represents. In addition to the equalities between ZX-diagrams that are generated by topological deformations, the calculus also has a set of graphical rewrite rules for transforming diagrams into one another. The ZX-calculus is universal in the sense that any linear map between qubits can be represented as a diagram, and different sets of graphical rewrite rules are complete for different families of linear maps. ZX-diagrams can be seen as a generalisation of quantum circuit notation, and they form a strict subset of tensor networks which represent general fusion categories and wavefunctions of quantum spin systems.

## History

The ZX-calculus was first introduced by Bob Coecke and Ross Duncan in 2008 as an extension of the categorical quantum mechanics school of reasoning. They introduced the fundamental concepts of spiders, strong complementarity and most of the standard rewrite rules.
In 2009 Duncan and Perdrix found the additional Euler decomposition rule for the Hadamard gate, which was used by Backens in 2013 to establish the first completeness result for the ZX-calculus. Namely that there exists a set of rewrite rules that suffice to prove all equalities between stabilizer ZX-diagrams, where phases are multiples of $\pi /2$, up to global scalars. This result was later refined to completeness including scalar factors.
Following an incompleteness result, in 2017, a completion of the ZX-calculus for the approximately universal $\pi /4$ fragment was found, in addition to two different completeness results for the universal ZX-calculus (where phases are allowed to take any real value).
Also in 2017 the book Picturing Quantum Processes was released, that builds quantum theory from the ground up, using the ZX-calculus. See also the 2019 book Categories for Quantum Theory.

## Informal introduction

ZX-diagrams consist of green and red nodes called spiders, which are connected by wires. Wires may curve and cross, arbitrarily many wires may connect to the same spider, and multiple wires can go between the same pair of nodes. There are also Hadamard nodes, usually denoted by a yellow box, which always connect to exactly two wires.
ZX-diagrams represent linear maps between qubits, similar to the way in which quantum circuits represent unitary maps between qubits. ZX-diagrams differ from quantum circuits in two main ways. The first is that ZX-diagrams do not have to conform to the rigid topological structure of circuits, and hence can be deformed arbitrarily. The second is that ZX-diagrams come equipped with a set of rewrite rules, collectively referred to as the ZX-calculus. Using these rules, calculations can be performed in the graphical language itself.

### Generators

The building blocks or generators of the ZX-calculus are graphical representations of specific states, unitary operators, linear isometries, and projections in the computational basis $|0\rangle ,|1\rangle$ and the Hadamard-transformed basis $|+\rangle ={\frac {|0\rangle +|1\rangle }{\sqrt {2}}}$ and $|-\rangle ={\frac {|0\rangle -|1\rangle }{\sqrt {2}}}$. The colour green (or sometimes white) is used to represent the computational basis and the colour red (or sometimes grey) is used to represent the Hadamard-transformed basis. Each of these generators can furthermore be labelled by a phase, which is a real number from the interval $[0,2\pi )$. If the phase is zero it is usually not written.
The generators are:
ZX-calculus generators, informally :
| Type | Corresponding linear map | Remarks |
| state | $|0\rangle +e^{i\alpha }|1\rangle$ | For $\alpha =0$ and $\alpha =\pi$, this map corresponds to unnormalised versions of the Hadamard-transformed basis states $\mid +\rangle$ and $\mid -\rangle$, respectively. For $\alpha ={\frac {\pi }{4}}$, this is an unnormalised version of the T magic state ${\frac {|0\rangle +e^{i\pi /4}|1\rangle }{\sqrt {2}}}$. |
| state | $|+\rangle +e^{i\alpha }|-\rangle$ | For $\alpha =0$ and $\alpha =\pi$, this map corresponds to unnormalised versions of the computational basis states $\mid 0\rangle$ and $\mid 1\rangle$, respectively. |
| unitary map | $|0\rangle \langle 0|+e^{i\alpha }|1\rangle \langle 1|$ | This map is a rotation about the Z-axis of the Bloch sphere by an angle $\alpha$. For $\alpha =\pi$, it is the Z Pauli matrix. |
| unitary map | $|+\rangle \langle +|+e^{i\alpha }|-\rangle \langle -|$ | This map is a rotation about the X-axis of the Bloch sphere by an angle $\alpha$. For $\alpha =\pi$, it is the X Pauli matrix. |
| unitary map | $|+\rangle \langle 0|+|-\rangle \langle 1|$ | This map is the Hadamard gate often used in quantum circuits. |
| isometry | $|00\rangle \langle 0|+e^{i\alpha }|11\rangle \langle 1|$ | For $\alpha =0$, this map represents a copy operation in the computational basis. For the same value of $\alpha$, it also corresponds to the smooth split operation of lattice surgery. |
| isometry | $|++\rangle \langle +|+e^{i\alpha }|--\rangle \langle -|$ | For $\alpha =0$, this map represents a copy operation in the Hadamard-transformed basis. For the same value of $\alpha$, it also corresponds to the rough split operation of lattice surgery. |
| partial isometry | $|0\rangle \langle 00|+e^{i\alpha }|1\rangle \langle 11|$ | For $\alpha =0$, this map represents a controlled-NOT operation followed by a destructive Z measurement on the target qubit post-selected to the state $|0\rangle$. For the same value of $\alpha$, it also corresponds to the smooth merge (without byproduct operators) of lattice surgery. |
| partial isometry | $|+\rangle \langle ++|+e^{i\alpha }|-\rangle \langle --|$ | For $\alpha =0$, this map represents a controlled-NOT operation followed by a destructive X measurement on the control qubit post-selected to the state $|+\rangle$. For the same value of $\alpha$, it also corresponds to the rough merge (without byproduct operators) of lattice surgery. |
| projection | $\langle 0|+e^{i\alpha }\langle 1|$ | For $\alpha =0$ or $\alpha =\pi$, this map corresponds to a destructive X measurement post-selected to the state $\mid +\rangle$ or $\mid -\rangle$, respectively. |
| projection | $\langle +|+e^{i\alpha }\langle -|$ | For $\alpha =0$ or $\alpha =\pi$, this map corresponds to a destructive Z measurement post-selected to the state $\mid 0\rangle$ or $\mid 1\rangle$, respectively. |

### Composition

The generators can be composed in two ways:
* sequentially, by connecting the output wires of one generator to the input wires of another;
* in parallel, by stacking two generators vertically.
These laws correspond to the composition and tensor product of linear maps.
Any diagram written by composing generators in this way is called a ZX-diagram. ZX-diagrams are closed under both composition laws: connecting an output of one ZX-diagram to an input of another creates a valid ZX-diagram, and vertically stacking two ZX-diagrams creates a valid ZX-diagram.

### Only topology matters

Two diagrams represent the same linear operator if they consist of the same generators connected in the same ways. In other words, whenever two ZX-diagrams can be transformed into one another by topological deformation, then they represent the same linear map. Thus, the controlled-NOT gate can be represented as follows:

### Diagram rewriting

The following example of a quantum circuit constructs a GHZ-state. By translating it into a ZX-diagram, using the rules that "adjacent spiders of the same color merge", "Hadamard changes the color of spiders", and "parity-2 spiders are identities", it can be graphically reduced to a GHZ-state:
Any linear map between qubits can be represented as a ZX-diagram, i.e. ZX-diagrams are universal. A given ZX-diagram can be transformed into another ZX-diagram using the rewrite rules of the ZX-calculus if and only if the two diagrams represent the same linear map, i.e. the ZX-calculus is sound and complete.

## Formal definition

The category of ZX-diagrams is a dagger compact category, which means that it has symmetric monoidal structure (a tensor product), is compact closed (it has cups and caps) and comes equipped with a dagger, such that all these structures suitably interact. The objects of the category are the natural numbers, with the tensor product given by addition (the category is a PROP). The morphisms of this category are ZX-diagrams. Two ZX-diagrams compose by juxtaposing them horizontally and connecting the outputs of the left-hand diagram to the inputs of the right-hand diagram. The monoidal product of two diagrams is represented by placing one diagram above the other.
Indeed, all ZX-diagrams are built freely from a set of generators via composition and monoidal product, modulo the equalities induced by the compact structure and the rules of the ZX-calculus given below. For instance, the identity of the object $n$ is depicted as $n$ parallel wires from left to right, with the special case $n=0$ being the empty diagram.
The following table gives the generators together with their standard interpretations as linear maps, expressed in Dirac notation. The computational basis states are denoted by $\mid 0\rangle ,\vert 1\rangle$ and the Hadamard-transformed basis states are $\mid \pm \rangle ={\frac {1}{\sqrt {2}}}(\vert 0\rangle \pm \vert 1\rangle )$. The $n$-fold tensor-product of the vector $\mid \psi \rangle$ is denoted by $\mid \psi \rangle ^{\otimes n}$.
Generators of ZX-diagrams :
| Name | Diagram | Type | Linear map it represents |
| empty diagram | This is the common representation for an empty diagram in categorical quantum mechanics | $0\rightarrow 0$ | 1 |
| wire/identity | $-$ | $1\rightarrow 1$ | $\mid 0\rangle \langle 0\!\mid +\mid \!1\rangle \langle 1\mid$ |
| Bell state | This is the common representation for a cup diagram in categorical quantum mechanics | $0\rightarrow 2$ | $\mid 00\rangle +\vert 11\rangle$ |
| Bell effect | This is the common representation for a cap in categorical quantum mechanics | $2\rightarrow 0$ | $\langle 00\mid +\langle 11\mid$ |
| swap | This is the common representation of the swap morphism in the graphical language of symmetric monoidal categories | $2\rightarrow 2$ | $\mid 00\rangle \langle 00\vert +\vert 01\rangle \langle 10\vert +\vert 10\rangle \langle 01\vert +\vert 11\rangle \langle 11\vert$ |
| Z spider | This is the green Z-spider from the ZX-calculus, with a phase alpha and n inputs and m outputs | $n\rightarrow m$ | $\mid 0\rangle ^{\otimes m}\langle 0\vert ^{\otimes n}+e^{i\alpha }\vert 1\rangle ^{\otimes m}\langle 1\vert ^{\otimes n}$ |
| X spider | This is the red X-spider from the ZX-calculus, with a phase alpha and n inputs and m outputs | $n\rightarrow m$ | $\mid +\rangle ^{\otimes m}\langle +\vert ^{\otimes n}+e^{i\alpha }\vert -\rangle ^{\otimes m}\langle -\vert ^{\otimes n}$ |
| Hadamard | This is the standard yellow representation of a Hadamard gate in the ZX-calculus | $1\rightarrow 1$ | $\mid +\rangle \langle 0\vert +\vert -\rangle \langle 1\mid$ |
There are many different versions of the ZX-calculus, using different systems of rewrite rules as axioms. All share the meta rule "only the topology matters", which means that two diagrams are equal if they consist of the same generators connected in the same way, no matter how these generators are arranged in the diagram. The following are some of the core set of rewrite rules, here given "up to scalar factor": i.e. two diagrams are considered to be equal if their interpretations as linear maps differ by a non-zero complex factor.
Rules of the ZX-calculus :
| Rule name | Description |
| Z-spider fusion | Whenever two Z-spider touch, then can fuse together, and their phases add. This rule corresponds to the fact that the Z-spider represents an orthonormal basis - the computational basis. |
| X-spider fusion | See Z-spider fusion. |
| Identity rule | A phaseless arity 2 Z- or X-spider is equal to the identity. This rule states that the Bell-state is the same whether expressed in the computational basis or the Hadamard-transformed basis. In category-theoretic terms it says that the compact structure induced by the Z- and X-spider coincide. |
| Color change | The Hadamard-gate changes the color of spiders. This expresses the property that the Hadamard gate maps between the computational basis and the Hadamard-transformed basis. |
| Copy rule | A Z-spider copies arity-1 X-spiders. This expresses the fact that an arity-1 X-spider is proportional to a computational basis state (in this case $\vert 0\rangle$). |
| Bialgebra rule | A 2-cycle of Z- and X-spiders simplifies. This expresses the property that the computational basis and the Hadamard-transformed basis are strongly complementary. |
| $\pi$-copy rule | A NOT-gate (an arity-2 X-spider with a $\pi$ phase) copies through a Z-spider, and flips the phase of this spider. This rule states two properties at once. First, that NOT is a function map of the computational basis (it maps basis states to basis states), and second that when a NOT is commuted through a Z-rotation gate, that this rotation flips. |
| Euler decomposition | A Hadamard-gate can be expanded into three rotations around the Bloch sphere (corresponding to its Euler angles). Sometimes this rule is taken as the definition of the Hadamard generator, in which case the only generators of ZX-diagrams are the Z- and X-spider. |

## Applications

The ZX-calculus has been used in a variety of quantum information and computation tasks.
* It has been used to describe measurement-based quantum computation and graph states.
* The ZX-calculus is a language for lattice surgery on surface codes.
* It has been used to find and verify correctness of quantum error correcting codes.
* It has been used to optimize quantum circuits.

## Tools

The rewrite rules of the ZX-calculus can be implemented formally as an instance of double-pushout rewriting. This has been used in the software Quantomatic to allow automated rewriting of ZX-diagrams (or more general string diagrams). In order to formalise the usage of the "dots" to denote any number of wires, such as used in the spider fusion rule, this software uses bang-box notation to implement rewrite rules where the spiders can have any number of inputs or outputs.
A more recent project to handle ZX-diagrams is PyZX, which is primarily focused on circuit optimisation.
A LaTeX package zx-calculus can be used to typeset ZX-diagrams. Many authors also use the software TikZiT as a GUI to help typeset diagrams.

## Related graphical languages

The ZX-calculus is only one of several graphical languages for describing linear maps between qubits. The ZW-calculus was developed alongside the ZX-calculus, and can naturally describe the W-state and Fermionic quantum computing. It was the first graphical language which had a complete rule-set for an approximately universal set of linear maps between qubits, and the early completeness results of the ZX-calculus use a reduction to the ZW-calculus.
A more recent language is the ZH-calculus. This adds the H-box as a generator, that generalizes the Hadamard gate from the ZX-calculus. It can naturally describe quantum circuits involving Toffoli gates.

## Related algebraic concepts

Up to scalars, the phase-free ZX-calculus, generated by $0$-labelled spiders is equivalent to the dagger compact closed category of linear relations over the finite field $\mathbb {F} _{2}$. In other words, given a diagram with $n$ inputs and $m$ outputs in the phase-free ZX-calculus, its X stabilizers form a linear subspace of $\mathbb {F} _{2}^{n}\oplus \mathbb {F} _{2}^{m}$, and the composition of phase-free ZX diagrams corresponds to relational composition of these subspaces. In particular, the Z comonoid (given by the Z spider with one input and two outputs, and the Z spider with one input and no outputs) and X monoid (given by the X spider with one output and two inputs, and the X spider with one output and no inputs) generate the symmetric monoidal category of matrices over $\mathbb {F} _{2}$ with respect to the direct sum as the monoidal product.