Quantum Outpost
gates and circuits advanced · 17 min read · By LIPAI WANG ·

ZX-Calculus: A Visual Quantum Calculus for Circuit Optimization

ZX-calculus is a graphical rewriting language for quantum circuits, introduced by Coecke and Duncan in 2008. Quantum circuits become diagrams of green and red 'spiders'; circuit equivalences become diagrammatic rewrites. The 2017 completeness result and the PyZX 2019 toolchain made ZX a practical T-count optimization framework, and 2022-2024 results extend it to mixed states, error correction, and ground-state algorithms. This tutorial covers the core diagrammatic rules, the rewriting strategy, and what ZX delivers in production compilers.

Prerequisites: Tutorial 53: The Solovay-Kitaev Theorem, Tutorial 54: Toffoli Decomposition

Most tools for quantum circuit optimization work on the matrix or operator-list representation: a sequence of gates, with rewrite rules expressed as algebraic identities. ZX-calculus (Coecke and Duncan 2008) takes a different approach: represent quantum circuits as graphs of two kinds of nodes — green and red “spiders” — and express equivalences as graph-rewriting rules. The result is a graphical calculus that is locally rewriting-friendly, complete (every true circuit identity can be derived by ZX rules), and unusually good at finding T-count optimizations that are invisible in the gate-list representation.

Until 2017, ZX was primarily a theoretical framework. The 2017 completeness theorem (Hadzihasanovic, then Jeandel-Perdrix-Vilmart) made ZX a rigorous optimization engine. The 2019 PyZX library made it practical: drop in a circuit, optimize via diagrammatic rewriting, get out a circuit with measurably fewer T gates. Recent extensions (2022-2024) cover mixed states, error-correcting codes, and even classical-controlled circuits.

This tutorial covers the core ZX rules, the rewriting strategy, and where ZX wins in practice over conventional T-count optimization passes.

The two spiders

ZX-calculus has just two kinds of nodes:

  • Green spiders (Z-spiders): represent computations in the Z-basis (0,1|0\rangle, |1\rangle). A green spider with phase α\alpha and nn inputs and mm outputs is the linear map

    0+eiα1.\cdots \langle 0 \cdots | + e^{i\alpha} \cdots \langle 1 \cdots |.
  • Red spiders (X-spiders): represent computations in the X-basis (+,|+\rangle, |-\rangle). A red spider is the X-basis analog of the green spider.

Spiders are connected by wires. The wires are typeless — there’s no distinction between input and output, just connections. A ZX diagram is a multigraph of spiders connected by wires. The diagram represents a linear map; the linear map is the same regardless of how the diagram is drawn.

Common gates translate to small diagrams:

  • Identity wire = a green spider with phase 0, 1 input, 1 output (just a wire).
  • Z gate = green spider with phase π\pi.
  • X gate = red spider with phase π\pi.
  • CNOT = green spider on control connected to red spider on target.
  • Hadamard = a special “yellow box” or equivalently a green spider sandwiched in two H-Hadamard wires.
  • T gate = green spider with phase π/4\pi/4.

The translation is mechanical. A standard quantum circuit becomes a ZX diagram by mapping each gate to its spider representation and connecting wires.

The four core rewrite rules

ZX-calculus has a small set of rewrite rules. Two diagrams that can be transformed into each other by these rules represent the same linear map.

1. Spider fusion

Two same-color spiders connected by one or more wires can be fused into a single spider whose phase is the sum of the original phases:

[green α]──[green β]   ⟹   [green (α+β)]

This is the workhorse of ZX optimization. Many adjacent rotations in a quantum circuit fuse into single spiders, reducing the gate count.

2. π-copy

A red spider with one input and a green-π attached to one leg can “push” the π through:

[red α]──[green π]──   ⟹   [red(-α)]──[green π]──

This rule lets you “color-change” by passing a Pauli through. It’s the structural reason CNOT cancellations work.

3. Color change

Sandwiching a green spider in Hadamard wires turns it into a red spider, and vice versa:

H──[green α]──H   ⟹   [red α]

This connects the two color systems — they’re related by Hadamard conjugation, just like X and Z are related by H.

4. Bialgebra rule (Hopf algebra)

A green spider connected to two red spiders, with no other connections, can be redrawn as two red spiders connected to two green spiders in a “X” pattern:

[red]──[green]──[red]
              =
[red]   [red]
   \\ //
    X
   //  \\
[green]   [green]

This is the Hopf-algebra structure of ZX. It’s the ZX analog of “controlling something has structure.”

There are a few additional rules (Euler decomposition, scalar simplifications) but these four cover most circuit optimization.

Why ZX finds optimizations gate-list passes miss

The fundamental advantage of ZX is locality of optimization. A gate-list representation forces optimizations to consider gates in their literal sequential order. ZX diagrams have no inherent order — spiders connected to other spiders can be rewritten regardless of where they appear in the circuit.

Concrete example: two T gates separated by many other gates may be combinable into a single T gate (or simplified entirely) if no intermediate gate disturbs their joint structure. In gate-list form, this requires noticing that the intermediate gates commute past one of the T gates. In ZX form, the two T-spiders are simply both green-π/4 spiders that fuse if connected directly — the intermediate “non-disturbing” gates are visible as graph isolation, not as a sequence to walk through.

PyZX 2019 demonstrated this on real algorithms:

  • Quantum chemistry circuits: 30-50% T-count reduction over gate-list-only optimization.
  • Reversible-arithmetic circuits: 20-40% T-count reduction.
  • Random Clifford+T circuits: 10-30% T-count reduction on average.

The savings compound with other optimization passes. ZX is typically run after gate-list passes have done initial optimization, picking up the residual gains that gate-list passes can’t see.

Hadamard nodes and T-count optimization

A specific pattern that ZX handles well: Hadamard sandwiches around T gates. The pattern

HTHH \cdot T \cdot H

in gate-list form looks like three gates. In ZX form, the H-T-H is a green spider with phase π/4\pi/4 between two H-decorated wires. The H-decoration (called a “Hadamard node” in ZX) can be commuted past other gates with appropriate phase corrections, often allowing the central T-spider to fuse with another T elsewhere in the diagram.

The 2018 Heyfron-Campbell paper applied this systematically and showed T-count reductions of 20-50% on standard benchmark circuits. The PyZX tool implements these optimizations automatically.

A small ZX example

Concrete code using PyZX to optimize a circuit:

import pyzx as zx
import numpy as np

# Build a small Clifford+T circuit.
n_qubits = 4
circuit = zx.Circuit(n_qubits)
circuit.add_gate("HAD", 0)
circuit.add_gate("CNOT", 0, 1)
circuit.add_gate("T", 1)
circuit.add_gate("HAD", 1)
circuit.add_gate("CNOT", 1, 2)
circuit.add_gate("T", 2)
circuit.add_gate("HAD", 2)
circuit.add_gate("CNOT", 2, 3)
circuit.add_gate("T", 3)
circuit.add_gate("CNOT", 0, 3)
circuit.add_gate("T", 3)
circuit.add_gate("CNOT", 0, 3)

# Convert to ZX diagram.
diagram = circuit.to_graph()

# Initial T-count.
print(f"Initial T-count: {circuit.tcount()}")

# Apply ZX optimization passes.
zx.simplify.full_reduce(diagram)
optimized_circuit = zx.extract_circuit(diagram).to_basic_gates()

print(f"Optimized T-count: {optimized_circuit.tcount()}")
print(f"Optimized gate list:")
for gate in optimized_circuit.gates[:20]:  # first 20 gates
    print(f"  {gate}")

Sample output:

Initial T-count: 4
Optimized T-count: 2

The ZX optimization recognized that two of the T gates were separated by gates that didn’t disturb their joint structure, and combined them. Real production circuits have hundreds or thousands of T gates and benefit proportionally.

ZX in production

The 2026 production picture for ZX:

  • PyZX (Quanlse, Quantum Information Group at Oxford) — open-source, the main tool. Used in academic research and as a compiler back-end.
  • Quantinuum’s compiler (TKET) — incorporates ZX-style optimization passes for T-count reduction.
  • Microsoft Q# resource estimator — uses ZX-derived insights for some optimization phases, though not branded as such.
  • Google Cirq — has ZX support via the cirq-pyzx integration.

For most production fault-tolerant compilation in 2026, ZX is one optimization pass among several. It is not a complete compiler — it specializes in T-count reduction and circuit-level rewriting, not in routing or hardware-specific compilation.

Recent extensions

ZX has been extended in several directions in 2022-2025:

  • Mixed-state ZX (CPTP-ZX, 2020-2021): extends ZX to handle density matrices and quantum channels, useful for noise-aware optimization.
  • ZX for error correction (2022-2024): stabilizer codes have natural ZX representations that simplify decoder design.
  • Differentiable ZX (2024): allows gradient-based optimization of ZX diagrams, useful for variational circuits.
  • Phase-gadget completeness (Bian-Perdrix 2023): stronger completeness results for restricted ZX fragments that are easier to implement automatically.

The 2026 frontier is ZX as a general-purpose quantum-information rewriting framework — covering not just gate-model circuits but error correction, channels, and (some) measurement-based protocols.

Common misconceptions

“ZX-calculus replaces standard quantum circuits.” No. ZX is an alternative representation with different optimization properties. The output of ZX rewriting is converted back to a standard circuit for hardware execution. Most software still uses circuit notation as the primary interface.

“ZX is only useful for theoretical work.” Production compilers do use it. PyZX and TKET demonstrate measurable T-count savings on real algorithms.

“ZX-calculus is the same as the topological-quantum-field-theory framework.” They are related (both use diagrams in tensor categories) but distinct. ZX has specific spider structure; TQFTs have more general categorical structures.

“ZX optimization is fully automatic.” Mostly automatic for T-count reduction, but other circuit metrics (depth, gate count, ancilla usage) often require human guidance or specialized passes. The PyZX full_reduce is good for T-count but doesn’t always optimize for all metrics simultaneously.

“ZX is the future of quantum compilation.” It’s part of the future. Other frameworks (penalty-based optimization, machine-learning-driven compilation, hardware-aware passes) are also active research areas. ZX is a strong tool but not the only one.

Decision rule

Add ZX optimization to your compilation pipeline when:

  1. You’re targeting fault-tolerant computation. T-count is the binding cost; ZX’s specialty is T-count reduction.
  2. Your circuits are large (>1000 gates). The optimization payoff scales with circuit size; small circuits don’t benefit much.
  3. You can afford the compilation time. ZX optimization is polynomial-time but slower than gate-list-only passes. For algorithms compiled once and run many times, the trade is worth it.
  4. You’re using PyZX-supported gate sets. Standard Clifford+T is the cleanest case; some extended gate sets need additional translation.

Don’t bother with ZX when:

  1. You’re on NISQ hardware where T isn’t specially expensive. The optimization payoff is in the FTQC era.
  2. Your circuits are small enough that gate-list optimization suffices.
  3. You need to preserve specific circuit structure (e.g., for randomized benchmarking) that ZX rewriting would break.

For production fault-tolerant resource estimation, ZX is a standard tool — not the only one, but a clear contributor to the cumulative gate-count reductions that have made fault-tolerant resource estimates more favorable over time.

Exercises

1. Spider fusion in practice

Two adjacent green spiders in a ZX diagram have phases π/3\pi/3 and π/6\pi/6. What is the result of fusing them? What gate would this represent?

Show answer

Fusion: combined phase = π/3+π/6=π/2\pi/3 + \pi/6 = \pi/2. The fused green spider has phase π/2\pi/2, representing an SS gate. The original two spiders represented two rotations Rz(π/3)R_z(\pi/3) and Rz(π/6)R_z(\pi/6) that, when applied sequentially, equal a single Rz(π/2)=SR_z(\pi/2) = S. Spider fusion is the diagrammatic version of “adjacent rotations sum” — but the diagrammatic version sees fusions across non-adjacent positions when wires connect through the graph, which is what makes ZX powerful.

2. The H-T-H pattern

In a circuit, a Hadamard precedes and follows a T gate: HTHH \cdot T \cdot H. What ZX rewrite simplifies this, and what is the resulting equivalent circuit?

Show answer

In ZX, T=T = green-π/4 spider. HTHH \cdot T \cdot H becomes “Hadamard node, green-π/4, Hadamard node.” Applying the color-change rule: H-green-H = red. So HTH=H \cdot T \cdot H = red-π/4 spider, which is X\sqrt{X}-and-phase, equivalent to X1/4=eiπX/8X^{1/4} = e^{-i \pi X / 8}. The ZX rewrite turns HTHH T H into a single X1/4X^{1/4} rotation. This isn’t a T-count reduction (still 1 non-Clifford rotation), but it is a gate-count reduction (3 gates → 1 gate). Combined with other rewrites, it can enable further T-count optimizations.

3. Why ZX is graph-based

A gate-list representation of a circuit forces a sequential order. A ZX diagram does not. Why is this an advantage for optimization?

Show answer

In a gate list, two T gates separated by other gates are “non-adjacent” — even if those intervening gates commute past one of the T gates. To see the optimization, you have to walk through the gate list, identify the commutation, and rearrange. In ZX, the two T-spiders are connected (or not) based on the actual wires of the diagram, not based on left-to-right ordering. If the two T-spiders are connected by a wire, fusion is local; the intervening gates only matter for the connection topology, not for adjacency. This locality is why ZX finds optimizations gate-list passes miss: the relevant “adjacency” in ZX is graph adjacency, which is invariant under gate reorderings, while gate-list adjacency depends on the specific ordering.

4. When to add ZX to a pipeline

A compiler pipeline already has a gate-list T-count optimizer that achieves ~85% of the optimal T-count. Should you add ZX as another pass?

Show answer

Yes, often. ZX typically achieves the remaining 5-15% on top of a good gate-list pass. The two pass types capture different optimizations: gate-list is local-in-time, ZX is global-in-graph-structure. They compose well — gate-list first, then ZX, recovers most of the available T-count savings. The compilation overhead is real (ZX’s full_reduce takes roughly polynomial time in the gate count), so for very large circuits this might be 30-second per ZX pass. For algorithms compiled once and run many times (most fault-tolerant production work), the trade is clearly worth it. For algorithms compiled per-run (some research workflows), the overhead may not be amortized.

Where this goes next

This concludes the four-tutorial gates-and-circuits deepening (53-56). The track now has 8 tutorials covering basic gates (4), Solovay-Kitaev synthesis (1), Toffoli decomposition (1), controlled-unitary synthesis (1), and ZX-calculus (1) — the full pipeline from a high-level algorithm specification through to fault-tolerant gate counts. Future tutorials in this track may cover hardware-specific compilation (transpilation for IBM heavy-hex, Quantinuum native gates), pulse-level compilation (QASM3 with hardware control), and machine-learning-aided circuit optimization.


Weekly dispatch

Quantum, for people who already code.

One serious tutorial per week, plus the industry moves that actually matter. No hype, no hand-waving.

Free. Unsubscribe anytime. We will never sell your email.