Frequently Asked Questions

Most of these questions actually aren’t asked that frequently, but I figured this would be a good place to put all this information.

What is the ZX-calculus? What are ZX-diagrams?

ZX-diagrams are a graphical representation of quantum processes. For those who know what that means: they are a type of tensor network. The benefit of using ZX-diagrams over quantum circuits is that, first, ZX-diagrams can represent arbitrary linear maps and not just unitaries, and second, it comes equipped with a set of useful rewrite rules. These rewrite rules are collectively known as the ZX-calculus. If you want to learn more, check out the Wikipedia page, this review article, or this book.

But why use the ZX-calculus?

Quantum circuits are very ‘rigid’. They have the same number of input and output wires, and gates occur at specific locations on these wires. ZX-diagrams are more flexible, which allows us to find (in principle) more optimisations. Since we are not restricted to unitary circuits, we can also use ZX-diagrams to reason about non-unitary models of computation like Measurement-based Quantum Computation or lattice surgery in surface codes. There are some issues when doing this rewriting though, which is that it can be hard to transform a ZX-diagram back into a quantum circuit. You can still do this however, if you are smart about it.

So what has been done with the ZX-calculus?

A bunch of things! Below I list just the things related to quantum computing where there are concrete numbers to back-up the benefits of using it over standard quantum circuits.

Where do I go to ask questions about PyZX and the ZX-calculus?

Check out the ZX-calculus Discord channel. Otherwise you could go to the Quantum Computing Stack Exchange and tag your question with zx-calculus. If you have a feature request or something you think might be a bug, feel free to create an issue on Github.

I don’t like Python. Can I do ZX-calculus things in different languages?

Some of the functionality of PyZX has been ported to the ZXCalculus.jl Julia package. A port to the Rust language, quizx, is also available. These packages have the benefit of being a lot faster, but the drawback of been less feature-rich, for instance having less capabilities of visualising the results.

What are some things people have done with PyZX?

Below there is a list of papers where the authors either contributed new features to PyZX, or otherwise used PyZX in their work (for instance to optimise circuits they use, or to benchmark against). I try to keep this list up-to-date. If you see something missing, let me know! You might also want to take a look at the full list of ZX papers tagged PyZX (although this does not include papers that do not use ZX-diagrams themselves).

Implemented additional features:

  • Meijer-van de Griendt implemented a CNOT routing algorithm in PyZX based on Steiner trees.

  • East et al. built on the ZH-calculus rewrite strategy in PyZX to automatically simplify AKLT states. See also Richard East’s PhD thesis.

  • Ryan Krueger in his Master thesis looked at using simulated annealing and genetic algorithms to improve simplification and reduce CNOT count of the resulting circuits. Related strategies were proposed in Korbinian Staudacher’s Master thesis.

  • Borgna et al. implemented mixed quantum-classical optimization in PyZX. This allows you to represent and simplify circuits that include measurement and classical control.

Used PyZX:

  • Lehmann et al. implemented some ZX-calculus rewrite rules in Coq in order to formally verify correctness of rewrite rules.

  • Hanks et al. used the ZX-calculus to optimise braided circuits on surface codes. PyZX helped them optimise the circuits to a size where they could do further optimisation by hand.

  • Neri et al. used PyZX in their pipeline to compile quantum circuits to something that could run on physical quantum computers.

Benchmarked against PyZX:

  • Yeh et al. benchmarked PyZX against Qiskit transpilation and finds that in some cases PyZX improves gate counts beyond that of Qiskit.

  • Hietala et al. implemented a verified quantum compiler, also in Coq, and benchmarked it against PyZX (amongst other libraries). They find that while PyZX is better at T-count optimisation, it is worse at reducing two-qubit gate count.

  • Kharkov et al. built a general compiler benchmark platform and compared various compilers on a variety of different metrics.

What is PyZX not good at?

PyZX was originally built to optimize T-count. It is not so good at optimizing two-qubit gate count (like the number of CNOTs). This behaviour however changes quite drastically per type of circuit. You might find for instance that it blows up the number of CNOTs if you start with a circuit full of Toffoli’s, while if you give it a Trotterized chemistry circuit, it will be able to perform a lot better.

PyZX also doesn’t implement any of the tricks to optimally compile one and two-qubit circuits, such as using the Euler Decomposition to combine adjacent single-qubit rotations, or the KAK decomposition to reduce every two-qubit circuit to have at most three CNOTs.

PyZX is quite fast, but it is still written in Python, and as such has its limits. If you have a circuit with tens of thousands of gates it should run quickly enough, but if you go to millions of gates, it will start to lag. If speed is your concern, check out quizx.