- Compilation/
ZX-Calculus Intermediate Representation
ZX-Calculus Intermediate Representation
We briefly review the fundamental building blocks of the ZX-calculus necessary to understand ZX-calculus based compilation passes. For a hands on demo we refer to our intro to ZX-calculus and for a more in-depth introduction, we refer to arXiv:2012.13966.
Z and X spiders
The fundamental building blocks of the ZX-calculus are Z and X spiders, represented by green and red dots in the following manner:


Note that a Z spider on one qubit is equivalent to an RZ gate up to a global phase as R_Z(\alpha) = e^{-i\frac{\alpha}{2}} |0 \rangle \langle 0| + e^{i\frac{\alpha}{2}} |1\rangle \langle 1| = e^{-i\frac{\alpha}{2}}\left(|0 \rangle \langle 0| + e^{i \alpha} |1\rangle \langle 1| \right), and, in particular, with the same convention that the angle has a factor of 1/2. Thus, spiders with odd multiples of \frac{\pi}{4} are considered T gates, in that they have the same cost. The same is true for an X spider and the RX gate.
The Hadamard gate has a special symbol in form a yellow box and can be decomposed as three spiders of type Z, X and Z:
It is often, for convenience, also simply written as a blue dotted (sometimes also solid) line between Z or X spiders:
We sometimes want to remove all X spiders from a diagram. We can use the identity X = H Z H for this:

Calculus rules
The ZX-calculus has the following fundamental rewrite rules:
Due to rules (h) and (i2), all rules are valid with interchanged colors between red and blue, as well. Normalization constants and global phases are typically ignored, such that equivalences of two diagrams as depicted in the rewrite rules are always equalities up to scalar factors.
Transforming circuits into ZX-diagrams
We already saw how Z and X spiders are equivalent to RZ
and RX
gates, respectively. Further, a CNOT gate is represented by a Z spider on the control and a X spider on the target qubit:

With (CNOT, H, Z_\alpha) we already have a universal gate set. Other standard gates are easily translated into ZX-diagrams using their definitions and the rewrite rules above. For example, a \text{CZ}_{ij} = H_j \text{CNOT} H_j gate can be written as
Some mid-level gates like a Toffoli or CCZ operator can be written in terms of ZX-diagrams by first transforming them into (Clifford + T) diagrams. However, there is a more compact representation when we generalize the yellow Hadamard box to be its own "spider" defined by \sum_{i_1, .., i_m} \sum_{j_1, .., j_n} a^{i_1 \cdots i_m j_1 \cdots j_n} |j_1, .., j_n\rangle \langle i_1, .., i_m| for some arbitrary complex number a in the box. For example, the Toffoli gate then becomes the following:
By introducing this Hadamard spider we are technically leaving the ZX-calculus and entering the ZH-calculus, but we shall refer to them interchangeably here, as is the case in pyzx
.
Transforming ZX-diagrams to circuits
Every quantum circuit can be easily translated to a ZX-diagram. However, the reverse is not true because the ZX-calculus, describing general linear maps, is more general than quantum circuits, which describe unitary operators.
Yet, when we start from a quantum circuit and perform only a subset of rewrite rules that maintain a property called generalized flow, or gflow, there is an efficient extraction algorithm to retrieve a quantum circuit again [2, 3]. In particular, [2] introcued an algorithm when all operators are either Clifford or (Clifford + T) and [3] proposes a general procedure for arbitrary circuits, represented by ZX-diagrams with gflow. A modern implementation is given in pyzx.extract_circuit().