Visualization

pyQuil programs may be converted to LaTeX circuit diagrams, or even rendered immediately in a Jupyter Notebook. The main entry point of LaTeX generation is to_latex(). For inline rendering in a notebook, the main entry point is display().

Both of these functions take an optional DiagramSettings, which may be used to control some aspects of circuit layout and appearance.

class pyquil.latex.DiagramSettings(texify_numerical_constants=True, impute_missing_qubits=False, label_qubit_lines=True, abbreviate_controlled_rotations=False, qubit_line_open_wire_length=1, right_align_terminal_measurements=True)[source]

Settings to control the layout and rendering of circuits.

Attributes

texify_numerical_constants Convert numerical constants, such as pi, to LaTeX form.
impute_missing_qubits Include qubits with indices between those explicitly referenced in the Quil program.
label_qubit_lines Label qubit lines.
abbreviate_controlled_rotations Write controlled rotations in a compact form.
qubit_line_open_wire_length The length by which qubit lines should be extended with open wires at the right of the diagram.
right_align_terminal_measurements Align measurement operations which appear at the end of the program.
pyquil.latex.to_latex(circuit, settings=None)[source]

Translates a given pyQuil Program to a TikZ picture in a LaTeX document.

Here are some high points of the generation procedure (see pyquil/latex/_diagram.py):

  • The most basic building block are the TikZ operators, which are constructed by the functions in _diagram.py (e.g. TIKZ_CONTROL, TIKZ_NOP, TIKZ_MEASURE).

  • TikZ operators are maintained by a DiagramState object, with roughly each qubit line in a diagram represented as a list of TikZ operators on the DiagramState.

  • The DiagramBuilder is the actual driver. This traverses a Program and, for each instruction, performs a suitable manipulation of the DiagramState. At the end of this, the DiagramState is traversed and raw LaTeX is emitted.

  • Most options are specified by DiagramSettings. One exception is this: it is possible to request that a certain subset of the program is rendered as a group (and colored as such). This is specified by a new pragma in the Program source:

    PRAGMA LATEX_GATE_GROUP <name>? … PRAGMA END_LATEX_GATE_GROUP

    The <name> is optional, and will be used to label the group. Nested gate groups are currently not supported.

Parameters:
  • circuit (Program) – The circuit to be drawn, represented as a pyquil program.
  • settings (Optional[DiagramSettings]) – An optional object of settings controlling diagram rendering and layout.
Return type:

str

Returns:

LaTeX document string which can be compiled.

pyquil.latex.display(circuit, settings=None, **image_options)[source]

Displays a PyQuil circuit as an IPython image object.

Note

For this to work, you need two external programs, pdflatex and convert, to be installed and accessible via your shell path.

Further, your LaTeX installation should include class and style files for standalone, geometry, tikz, and quantikz. If it does not, you need to install these yourself.

Parameters:
  • circuit (Program) – The circuit to be drawn, represented as a pyquil program.
  • settings (DiagramSettings) – An optional object of settings controlling diagram rendering and layout.
Returns:

PNG image render of the circuit.

Return type:

IPython.display.Image