← Quantum Store · Guides · Lessons · Glossary · Packages · About · Türkçe
University of Oxford and the ZX-calculus community
Circuit simplification with the ZX-calculus in PyZX
Official source: zxcalc/pyzx
Download ZIP (free)Simplify a random Clifford+T circuit with PyZX, the Python tool for the ZX-calculus born in Oxford, in a tested example that lowers the T-gate count and checks that the result is equivalent.
What is PyZX?
PyZX is a Python library that turns quantum circuits into graphs called ZX-diagrams and simplifies them with rule-based rewriting. The ZX-calculus was developed at the University of Oxford, and PyZX came out of the same circle.
When to use it
- When you want to reduce the number of T gates, the most expensive gate on fault-tolerant quantum computers.
- When you need to check whether two circuits do the same thing
(
compare_tensors). - When doing research on compilers and circuit optimisation.
The example in this package
devre_sadelestir.py generates a random 5-qubit Clifford+T circuit with 120
gates, simplifies it with full_reduce and extracts a circuit again. In our
test run the T-gate count fell from 24 to 14 and the total gate count from 120
to 71. At the end the script checks that the two circuits apply the same
unitary.
Next steps
- Increase
depthand watch how the gain changes. - Visualise the diagram in Jupyter with
zx.draw(graf). - Export your own Qiskit circuit as QASM and load it into PyZX with
zx.Circuit.from_qasm(...).
Installation and running
pip install pyzx
python devre_sadelestir.py
Tested with: pyzx 0.10.7, Python 3.12.
Official source
- Repository: zxcalc/pyzx
- Latest release: v0.10.7 — 1 October 2026
- Licence: Apache-2.0
- Stars: 549
- Default branch commit when the catalogue was built:
e2a1408b2859(12 October 2026)
Links
Package contents
devre_sadelestir.pyREADME.en.md— this fileREADME.md— the same guide in TurkishLICENSE— the licence of the files in this packageKAYNAK.json— a machine-readable summary of the official sourceUPSTREAM-LICENSE.txt— the licence text of the official project
Licence
The example code and the guide in this package were written by the Quantum Store editors and are MIT licensed. The project it uses is subject to its own licence (Apache-2.0).
Quantum Store — https://hakanerdogan.com/en
Example code
The example shipped in the package, run and checked. Comments and printed messages are in Turkish; the code is the same in any language. Everything is in the ZIP.
devre_sadelestir.py
"""PyZX ile rastgele bir Clifford+T devresini ZX-hesabıyla sadeleştir.
Çalıştırma:
pip install pyzx
python devre_sadelestir.py
"""
import random
import pyzx as zx
random.seed(7)
devre = zx.generate.CNOT_HAD_PHASE_circuit(qubits=5, depth=120, clifford=False)
print("Orijinal :", devre.stats())
graf = devre.to_graph()
zx.full_reduce(graf) # ZX-diyagramını yerinde sadeleştirir
graf.normalize()
yeni = zx.extract_circuit(graf.copy()).to_basic_gates()
yeni = zx.basic_optimization(yeni)
print("Sadeleşmiş:", yeni.stats())
once, sonra = devre.tcount(), yeni.tcount()
print(f"T kapısı sayısı: {once} -> {sonra}")
# İki devre aynı unitary'yi (global faz dahil olmadan) uygulamalı.
assert zx.compare_tensors(devre, yeni, preserve_scalar=False)
assert sonra <= once
print("Tamam: devreler eşdeğer ve T sayısı azalmadı/azaldı.")
Related concepts
- Quantum gate · A quantum gate is a basic operation that reversibly changes the state of one or more qubits; quantum programs are sequences of gates.