← 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

SoftwareCompilation and optimisationLicence: Apache-2.0Version: 0.10.78.5 KB

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

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

  1. Increase depth and watch how the gain changes.
  2. Visualise the diagram in Jupyter with zx.draw(graf).
  3. 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

Links

Package contents

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

← All packages

Share: X · LinkedIn