Importing and exporting quantum circuits and ZX-diagrams¶
There are several ways to import and export circuits and ZX-diagrams in PyZX.
Importing and exporting quantum circuits¶
There are a number of standards for representing quantum circuits that are supported in PyZX. To see if PyZX supports a certain file format, just call load
:
circuit = zx.Circuit.load("path/to/circuit.extension")
The currently supported formats are
QASM,
the ASCII format of Quipper,
the simple .qc format used for representing quantum circuits in LaTex,
and the qsim format used by Google.
To convert a PyZX circuit to these formats, use to_qasm
, to_quipper
, to_qc
.
PyZX also offers a convenience function to construct a circuit out of a string containing QASM code using either from_qasm
or qasm
.
To convert a Circuit into a PyZX Graph (i.e. a ZX-diagram), call the method to_graph
.
Importing and exporting ZX-diagrams¶
A ZX-diagram in PyZX is represented as an instance of Graph
. A ZX-diagram can be loaded using the .qgraph
format that Quantomatic uses, via from_json
. It can be converted into that format using to_json
.
Apart from this reversible representation, there are also several one-way translations for exporting ZX-diagrams from PyZX. A graph can be exported to GraphML format using to_graphml
.
To export a ZX-diagram to tikz for easy importing to Latex, use to_tikz
.
Additionally, PyZX diagrams can be directly exported into the applications Tikzit using the tikzit
function or edited in Quantomatic using the function edit_graph
.
Finally, to display a ZX-diagram in Jupyter call draw
and to create a matplotlib picture of the ZX-diagram use draw_matplotlib
.
Some ZX-diagrams can be converted into an equivalent circuit. For complicated ZX-diagrams, the function extract_circuit
is supplied. For ZX-diagrams that come directly from Circuits, e.g. those produced by calling c.to_graph
for a Circuit c
, one can also use the static method from_graph
, which is more lightweight.