ZxFermion is a Python package built on top of PyZX designed for the manipulation and visualisation of circuits of Pauli gadgets. With built-in Clifford tableau logic using Stim, ZxFermion allows users to quickly implement proofs and test ideas.
VQE algorithms used in quantum chemistry often utilise the UCC framework in which excitation operators have a natural representation as Pauli gadgets. ZxFermion provides a comprehensive toolset designed to be used in a Jupyter notebook environment. Export functionality can be used to generated research paper quality diagrams.
All of the following diagrams were made using ZxFermion's pdf(name='filename') method.
from zxfermion import Gadget, GadgetCircuit
from zxfermion.gates import XPhase, ZPhase, CX, CZ, X, ZWe begin by introducing the Gadget class, which we use to represent Pauli gadgets. The Gadget class takes a Pauli string and a phase as inputs. By default, Pauli gadgets are represented in their form in the ZX calculus.
gadget = Gadget('YZX', phase=1/2)
gadget.draw()Setting as_gadget=False allows users to visualise Pauli gadgets in their expanded form.
gadget = Gadget('YZX', phase=1/2, as_gadget=False)
gadget.draw()We can construct circuits of Pauli gadgets using the GadgetCircuit class by passing an ordered list of Gadget instances to the gates parameter. The variable parameter allows users to specify the LaTeX symbol used to render each Pauli gadget's phase when exporting to PDF.
gadget1 = Gadget('YZX', phase=1/2, variable='theta')
gadget2 = Gadget('XZY', phase=1/2, variable='phi')
circuit = GadgetCircuit(gates=[gadget1, gadget2])
circuit.draw()The GadgetCircuit class also allows users to represent circuits of standard quantum gates.
circuit = GadgetCircuit([CX(0, 1), CZ(1, 2), X(1), ZPhase(0, 3/4), XPhase(0, 1/2), CX(0, 2), CX(0, 1), CZ(1, 2)])
circuit.draw(stack=True)Using Stim as a backend, users can easily observe the effect of Pauli and Clifford gates on Pauli gadgets. Consider the following circuit of Pauli gadgets, which represents a paired double excitation operator.
circuit = GadgetCircuit([
Gadget('YXXX', phase=1/4),
Gadget('XYXX', phase=1/4),
Gadget('XXYX', phase=-1/4),
Gadget('YYYX', phase=-1/4),
Gadget('YYXY', phase=1/4),
Gadget('XXXY', phase=1/4),
Gadget('XYYY', phase=-1/4),
Gadget('YXYY', phase=-1/4)
])
circuit.draw()It is easy to show the effect of conjugating the circuit by CNOT gates.
circuit.apply(CX(0, 3), draw=True)
circuit.apply(CX(0, 2), draw=True)
circuit.apply(CX(0, 1), draw=True)
circuit.draw()Setting as_gadget=True allows users to visualise any of the standard quantum gates as Pauli gadgets.
- Class for representing the CX gate.
- Class for representing the CZ gate.
- Class for representing the X gate.
- Class for representing the Z gate.
- Class for representing a general rotation gate in the X basis.
- Class for representing a general rotation gate in the Z basis.
- Class for representing a
$\pi/2$ rotation in the X basis.
- Class for representing a
$\pi/2$ rotation in the Z basis.
- Class for representing a
$3\pi/2$ rotation in the X basis.
- Class for representing a
$3\pi/2$ rotation in the Z basis.
- Class for representing Pauli gadgets.
- Setting
as_gadget=Trueallows users to represent the gadget in its simplified form. - Setting
as_gadget=Falseallows users to represent the gadget as a CNOT ladder construction.
- Class for representing circuits of Pauli gadgets and other quantum gates.
- The gates parameter takes a list of
Gadget,X,Z,CX...
- Return type:
None
- Return type:
GadgetGraph
- Return type:
None
- Return type:
str | None
- Return type:
None
- Return type:
None
- Return type:
GadgetCircuit
- Return type:
str | None
- Inherits from the
zxfermion.BaseGraphclass (see above). - Implements methods for handling the graphing of the
Gadgetclass and other quantum gates.
- Return type:
None
- Return type:
None
- Return type:
None
- Return type:
None
- Return type:
None
- Return type:
None
- Return type:
None
- Extends the
pyzx.GraphSclass. Implements a number of additional methods for handling ZX diagrams. - Please see the PyZX documentation.
- Returns the minimum qubit of the current graph.
- Return type:
int
- Returns the maximum qubit of the current graph.
- Return type:
int
- Returns the input row index of the current graph.
- Return type:
int | float
- Returns the output row index of the current graph.
- Return type:
int | float
- Returns the leftmost row index of the current graph
- Return type:
int | float
- Returns the rightmost row index of the current graph
- Return type:
int | float
- Returns the left padding (separation between
input_rowandleft_row) of the current graph. - Return type:
int | float
- Returns the right padding (separation between
output_rowandright_row) of the current graph. - Return type:
int | float
- Returns the combined vertex indices of the inputs and outputs of the current graph.
- Return type:
list[int]
- Returns the row indices of the current graph (from
left_rowtoright_rowinclusive). - Return type:
list[int | float]
- Returns the depth of the current graph from
input_rowtooutput_row. - Excludes vertices outside of graph bounds.
- Return type:
int | float
- Returns the leftmost row index of the graph along the specified qubit.
- Return type:
int | float
- Returns the rightmost row index of the graph along the specified qubit.
- Return type:
int | float
- Returns the leftmost row index of the graph between the specified qubits.
- Return type:
int | float
- Returns the rightmost row index of the graph between the specified qubits.
- Return type:
int | float
- Returns the vertex indices of the graph excluding vertices positioned outside the graph bounds.
- Excludes input and outputs vertices.
- Return type:
list[int]
- Returns the vertex indices of the graph positioned outside the graph bounds.
- Excludes input and outputs vertices.
- Return type:
list[int]
- Return the vertex indices along the specified qubit and excluding the input and output vertices.
- Return type:
list[int]
- Removes the edge connecting the input and output vertices for the specified qubit.
- Return type:
None
- Creates simple edges between the specified vertex indices.
- Return type:
None
- Positions the input row at the specified row index.
- Return type:
None
- Positions the output row at the specified row index.
- Return type:
None
- Sets the left padding (separation between input row and left row) of the graph.
- Return type:
None
- Sets the right padding (separation between output row and right row) of the graph.
- Return type:
None
- Sets the number of qubits for the graph.
- Maintains first
2 * num_qubitsvertex indices as the input and output indices. - Return type:
None
- Sets the number of qubits for the graph if
num_qubitsis greater than the current number of qubits. - Return type:
None
- Overrides the
pyzx.GraphS.compose()to allow addition of graphs of different dimension. - Setting
stackparameter asTruewill stack graphs acting on disjoint set of qubits. - Return type:
None
- Displays a latex matrix for the current gate,
GadgetorGadgetCircuit. - Return type:
str | None
- Generates a tikz file for the current gate,
GadgetorGadgetCircuit. - Return type:
str | None
- Generates a tex file for the current gate,
GadgetorGadgetCircuit. - Return type:
None
- Generates a pdf file for the current gate,
GadgetorGadgetCircuit. - Return type:
None
- Draws the current gate,
GadgetorGadgetCircuit. - Return type:
None
- Class for handling the interaction of the
Gadgetclass with the Pauli and Clifford gates. - Built on top of Stim.






