PennyLane
  • Why PennyLane
  • Getting Started
  • Documentation
  • Ecosystem
Install
Install
  1. Compilation/
  2. ZX-Calculus Intermediate Representation

ZX-Calculus Intermediate Representation

OverviewDetailsCompilationResources

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:

The mathematical definition of a Z spider in the ZX-calculus.
The mathematical definition of an X spider in the ZX-calculus.

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:

The Hadamard gate is defined by a yellow box and can be decomposed as three ZXZ spiders

It is often, for convenience, also simply written as a blue dotted (sometimes also solid) line between Z or X spiders:

The Hadamard gate is sometimes, for convenience, also represented as a dotted blue line between spiders

We sometimes want to remove all X spiders from a diagram. We can use the identity X = H Z H for this:

An X rotation can be rewritten as a Z rotation sandwiched by two Hadamard gates.

Calculus rules

The ZX-calculus has the following fundamental rewrite rules:

The fundamental rewrite rules from the ZX-calculus. All rules are also true for interchanged colors.

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:

CNOT gate represented as a ZX-diagram with a X spider on the target and a Z spider on the control 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

A CZ can be written as two Z spiders connected by an Hadamard edge.

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:

Toffoli representation using a Hadamard spider

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().

PennyLane

PennyLane is an open-source software framework for quantum machine learning, quantum chemistry, and quantum computing, with the ability to run on all hardware. Built with ❤️ by Xanadu.

Stay updated with our newsletter

For researchers

  • Research
  • Features
  • Demos
  • Compilation
  • Datasets
  • Performance
  • Learn
  • Videos
  • Documentation
  • Teach

For learners

  • Learn
  • Codebook
  • Teach
  • Videos
  • Challenges
  • Demos
  • Compilation
  • Glossary

For developers

  • Features
  • Documentation
  • API
  • GitHub
  • Datasets
  • Demos
  • Compilation
  • Performance
  • Devices
  • Catalyst

© Copyright 2025 | Xanadu | All rights reserved

TensorFlow, the TensorFlow logo and any related marks are trademarks of Google Inc.

Privacy Policy|Terms of Service|Cookie Policy|Code of Conduct