Overview
The ZX calculus, introduced by Coecke and Duncan, is a way of writing linear maps between qubit spaces as diagrams and proving equalities between them by local rewrites. A diagram is a tensor network with only three kinds of node: Z spiders (built from the computational basis), X spiders (built from the basis) and Hadamard boxes. Each spider carries a phase. Every qubit circuit, state, measurement and post-selected operation can be drawn this way, and every linear map between qubit spaces has a diagram (universality).
The value of the calculus lies in its rules. A handful of identities, such as spider fusion, express the interaction of the two bases, and for suitable rule sets they are complete: if two diagrams denote the same map, the rules can rewrite one into the other. Circuit equalities that take pages of matrix algebra become short graph rewrites. The calculus is used for circuit optimisation, notably reducing the number of gates, for checking circuit equivalence, for describing measurement-based and lattice-surgery computations, and for teaching. The textbook is Coecke and Kissinger.
Intuition
A Z spider with phase says "all my legs carry the same computational-basis value, and the value 1 picks up a phase ". So a Z spider with one input and two outputs copies basis states (it is the control of a CNOT), and a Z spider with one input and one output is the phase gate . An X spider says the same in the basis; read in the computational basis, an X spider with two inputs and one output computes the parity of its inputs, so it is the target of a CNOT. The rules then become plausible: two Z spiders joined by a wire both insist on a shared value, so they are one spider and their phases add; a Hadamard swaps the two bases, so it swaps the colours.
What makes diagrams better than circuits for some tasks is that they forget the time order. Only connectivity matters, so a phase can be slid along a wire through any number of Z spiders. A circuit optimiser built on this can merge phases that a gate-by-gate optimiser never sees as adjacent.
Prerequisites
The circuit model, Pauli and Clifford gates, and the idea of a tensor contraction. For the graph-state material, graph states and local complementation. The categorical foundations are covered in category theory but are not needed for computation.
Mathematical Definitions
A ZX diagram with inputs and outputs is a finite undirected multigraph whose internal vertices are generators and which has input and output boundary wires. The generators, with their interpretation as linear maps for a generator with incoming and outgoing wires, are:
where and (an empty tensor power is the number 1). is the Z spider (drawn green), the X spider (drawn red), the Hadamard box, and the spider's phase; a phase of 0 is omitted. Plain wires denote the identity, and crossing wires the swap.
The interpretation of a whole diagram is the tensor contraction of its generators along the internal wires, with the boundary wires left open. Because each spider is symmetric in its inputs and in its outputs, and because bending a wire (turning an input into an output) is implemented by the cup , the interpretation depends only on the graph, not on how it is drawn ("only connectivity matters").
Two diagrams are equal when their interpretations are equal; many papers instead work up to a nonzero scalar factor. The calculus was introduced by Coecke and Duncan; the textbook is Coecke and Kissinger. The definition uses , which follows from and .
- Qubits only; phases taken modulo .
- Diagrams are read as tensor networks, so only connectivity matters, not the drawing.
Theorems
Write and for the interpretations of the spiders. Then:
- Fusion. If outputs of are connected to inputs of , the result is . A wire joining a spider to itself can be deleted. The same holds for X spiders.
- Identity. .
- Colour change. , and .
- -copy. , where is the Pauli .
- Copy. , , and .
- Bialgebra. .
Rules 1, 3 and 6 (each up to scalars) are among the original axioms of Coecke and Duncan, and rule 4 follows from their two rules (K1, K2) together with fusion; the textbook treatment with scalars is Coecke and Kissinger. By colour change, every rule has a version with the colours swapped.
- Interpretation of ZX diagrams as linear maps with the unnormalised spiders defined there; scalars are stated exactly, not dropped.
Proof · Proof of spider fusion and the basic ZX rules
Write .
1. Fusion. Contracting wires between the two spiders gives
since for . A self-loop contracts an output with an input of the same term, contributing , so it can be removed. For X spiders conjugate by Hadamards (rule 3), which commute with contraction because is real and symmetric, so along each internal wire.
2. Identity. , and .
3. Colour change. for , so , using . And by multiplying the matrices.
4. -copy. is the Pauli , which maps . Hence .
5. Copy. The first two identities are immediate from the definition. For the last, .
6. Bialgebra. Both sides are maps from two qubits to two qubits; compare them on basis states. First, sends , since . So the left side sends . On the right side, sends , the middle swap reorders this to , and gives . Multiplying by matches the left side.
The same calculation shows followed by is the CNOT gate with the Z spider on the control, . These computations are the standard ones of Coecke and Kissinger.
Check. and . Then , using and .
Use. Read as a diagram rewrite, this lets a Hadamard box be replaced by three spiders, so diagrams can be written with spiders alone. Duncan and Perdrix showed that this equation is not derivable from the original rules of the calculus and is needed to reason about graph states; it is part of the rule set that Backens proved complete for stabilizer diagrams.
- and are the one-input, one-output spiders of the ZX calculus.
For all and every linear map there is a ZX diagram with (Coecke and Duncan; Coecke and Kissinger).
Restricting phases gives the familiar fragments: diagrams with all phases in denote, up to nonzero scalars, exactly the stabilizer maps (the setting of Backens), and phases in give the Clifford+T fragment (Jeandel, Perdrix and Vilmart). The proof below covers the universality statement only.
- Arbitrary real phases are allowed for the universality statement; the fragment statements restrict phases and are taken up to nonzero scalars.
Proof · Proof of universality of ZX diagrams
Composition and tensor product of diagrams correspond to composition and tensor product of maps, so it suffices to build a generating set.
Pieces. (a) By the basic rules, is CNOT. (b) Single-qubit unitaries: every is by the ZYZ decomposition. Since with , and , , , every is a scalar times a product of one-input, one-output spiders. (c) States and effects: and . (d) Scalars: the spider with no wires is the number . For , the product takes every modulus in as varies over ; multiplying by copies of reaches every positive modulus with phase . Phase is the square of phase , and . So any scalar multiple of a diagram is a diagram.
Square, contractive case. Let act on qubits with operator norm . The block matrix
on qubits (first qubit selecting the block) is unitary (a standard unitary dilation of a contraction), as one checks by multiplying out and using , which holds because for every polynomial and hence for the square root. Then . By exact universality of CNOT and single-qubit gates (Nielsen and Chuang, section 4.5), is a finite circuit of pieces (a) and (b), and the state and effect are pieces (c), so is a scalar times a diagram, hence a diagram by (d).
General case. Let be nonzero (the zero map is times any diagram) and . Put and , a contraction on qubits. Then , and is the scalar times a diagram built from and pieces (c).
A set of rewrite rules for ZX diagrams is sound if every rule preserves the interpretation, and complete for a class of diagrams if, whenever two diagrams in the class satisfy , can be transformed into by finitely many applications of the rules. The following are proved.
- Stabilizer fragment. With all phases multiples of , the rules of the calculus, including the Euler decomposition of the Hadamard gate, are complete (Backens). The proof rewrites every diagram to a normal form made of a graph state and local Clifford operations.
- The original rules are not complete in general. With arbitrary phases the rule set of Coecke and Duncan, even with the Euler decomposition added, is incomplete (Schröder de Witt and Zamdzhiev).
- Clifford+T fragment. With phases multiples of , adding two new axioms gives a complete rule set (Jeandel, Perdrix and Vilmart); the proof goes through a translation to the ZW calculus, a graphical calculus for integer matrices.
- All pure-qubit diagrams. Complete axiomatisations for diagrams with arbitrary phases were given by Hadzihasanovic, Ng and Wang, and an axiomatisation in which every rule has a physical reading and all but two are proved necessary by Vilmart.
Completeness is an existence statement: it says a rewrite sequence exists, not that one can be found efficiently. Deciding equality of general diagrams is at least as hard as deciding equality of quantum circuits.
- Each statement refers to a specific rule set given in the cited paper; completeness is relative to that rule set and to the stated fragment of phases.
- Equalities in the stabilizer result are stated with the scalar conventions of that paper; later results handle scalars exactly.
Given a ZX diagram promised to denote a unitary map, producing a quantum circuit of polynomially related size that implements it is -hard, and hence at least as hard as strong simulation of quantum circuits (de Beaudrap, Kissinger and van de Wetering). The same paper gives a second result that does not depend on the output being a circuit: an oracle that takes such a diagram and samples from the output distribution of the computation it describes would solve -complete problems with efficient randomised algorithms.
The positive side: if the diagram's underlying graph has a generalised flow, a deterministic extraction algorithm exists (Duncan, Kissinger, Perdrix and van de Wetering), and Backens et al. extend extraction to further classes of diagrams. Rewrite strategies that preserve such a flow therefore always return a circuit.
- Input is an arbitrary ZX diagram promised to denote a unitary; the output must be a circuit of size polynomial in the diagram.
- Worst case; restricted classes of diagrams (for example those with generalised flow) have efficient extraction.
Worked Examples
Two CNOTs cancel
By the basic rules, , where is a Z spider on the control joined by one wire to an X spider on the target. So , where is followed by . Fuse the two Z spiders on the control (they share the control wire) into one Z spider with an input, an output and two side legs; fuse the two X spiders on the target likewise. The fused spiders are joined by two parallel wires. Compute the resulting map directly: for control value , the X spider receives on its side legs, and
because . The control side is then , so and . The step "two parallel wires between spiders of different colours can be cut, at the cost of a scalar" is the Hopf rule; here the scalar is .
Merging gates through a CNOT
exactly. Consider the circuit with both gates on the control. The control of the CNOT is a Z spider, so all three Z spiders on the control wire fuse into one, with phase . Unfusing gives with : the -count drops from 2 to 0. This is the simplest case of what phase teleportation does globally.
Algorithms and Implementations
Graph-theoretic simplification. Duncan, Kissinger, Perdrix and van de Wetering convert a circuit to a diagram of Z spiders joined by Hadamard edges and remove Clifford spiders using rewrites derived from local complementation and pivoting. They prove that the simplified diagrams always have a generalised flow, so a circuit can be extracted deterministically; for Clifford circuits this gives a new normal form, asymptotically optimal in size.
-count reduction. Kissinger and van de Wetering add phase teleportation: run the simplification only to discover which non-Clifford phases can be merged, then apply those mergers to the original circuit, leaving all other gates in place. On the ancilla-free benchmark circuits they report matching or improving on earlier -count methods for most circuits, by up to 50% on some. Both methods are implemented in the open-source PyZX library.
Extraction in general. Extraction is #P-hard for arbitrary diagrams of unitaries, so practical rewrite strategies keep diagrams inside classes with known extraction procedures (Backens et al.).
Applications
- Measurement-based computation. Graph states are diagrams of Z spiders joined by Hadamard edges, single-qubit measurements are spiders with one leg, and the flow conditions that make a measurement pattern deterministic (see measurement flow) are the same conditions that make circuit extraction possible. See measurement-based quantum computing.
- Lattice surgery. de Beaudrap and Horsman show that the merge and split operations of lattice surgery on surface codes are described exactly by Z and X spiders, so lattice-surgery procedures can be designed and verified as diagrams.
- Fault tolerance. Bombin et al. use ZX diagrams to describe circuit-based, measurement-based and fusion-based fault-tolerant protocols in one framework.
- Equivalence checking. Rewriting the composite of one circuit with the inverse of another towards the identity proves equivalence when it succeeds; completeness guarantees a rewrite exists, though not that a given strategy finds it.
Computational Complexity
Interpreting a diagram is a tensor contraction, so evaluating a diagram exactly is as hard as strong simulation of circuits in general. Extracting a circuit from an arbitrary diagram of a unitary is #P-hard.
Limitations and Common Pitfalls
- Scalars. Many papers draw diagrams "up to a nonzero scalar". That is harmless for unitaries but wrong for probabilities, postselection and sums of diagrams; keep scalars (as in the worked examples) whenever they matter.
- Which rule set. "The ZX calculus is complete" refers to a particular axiomatisation and phase fragment. The original rules of Coecke and Duncan needed the Euler decomposition added even to reason about graph states (Duncan and Perdrix), and they are incomplete once arbitrary phases are allowed.
- Completeness is not an algorithm. A complete rule set guarantees that a rewrite sequence exists but gives no bound on its length or a way to find it.
- Simplified diagrams are not circuits. After aggressive rewriting a diagram may have no efficient extraction; optimisers must preserve a flow-like invariant.
- Two-qubit gate counts. -count reductions can come with more CNOTs after extraction; report both counts.
Related Research
The categorical setting of the calculus (dagger-compact categories and interacting Frobenius algebras) is developed in Coecke and Duncan and Coecke and Kissinger; see also category theory. For complete axiomatisations with arbitrary phases see Hadzihasanovic, Ng and Wang and Vilmart; for the Clifford+T fragment, Jeandel, Perdrix and Vilmart.