pytket.circuit.display#

Contains several functions for rendering interactive circuit diagrams.

Note

Rendering circuits with pytket.circuit.display requires an internet connection. Using the pytket circuit renderer offline can be done by installing the pytket-offline-display extension.

pip install pytket-offline-display

Display a circuit as html.

class pytket.circuit.display.CircuitRenderer(env: Environment)[source]#

Class to manage circuit rendering within a given jinja2 environment.

get_render_options(full: bool = False, _for_js: bool = False) Dict[str, bool][source]#

Get a dict of the current render options.

Parameters:
  • full – whether to list all available options, even if not set.

  • _for_js – Whether to convert options to js-compatible format, for internal use only.

render_circuit_as_html(circuit: Dict[str, str | float | dict] | Circuit, jupyter: bool = False) str | None[source]#

Render a circuit as HTML for inline display.

Parameters:
  • circuit – the circuit to render.

  • jupyter – set to true to render generated HTML in cell output.

render_circuit_jupyter(circuit: Dict[str, str | float | dict] | Circuit) None[source]#

Render a circuit as jupyter cell output.

Parameters:

circuit – the circuit to render.

set_render_options(**kwargs: bool | str) None[source]#

Set rendering defaults.

Parameters:
  • min_height – str, initial height of circuit display.

  • min_width – str, initial width of circuit display.

  • zx_style – bool, display zx style gates where possible.

  • condense_c_bits – bool, collapse classical bits into a single wire.

  • recursive – bool, display nested circuits inline.

  • condensed – bool, display circuit on one line only.

  • dark_theme – bool, use dark mode.

  • system_theme – bool, use the system theme mode.

  • transparent_bg – bool, remove the circuit background.

  • crop_params – bool, shorten parameter expressions for display.

view_browser(circuit: Dict[str, str | float | dict] | Circuit, browser_new: int = 2, sleep: int = 5) None[source]#

Write circuit render html to a tempfile and open in browser.

Waits for some time for browser to load then deletes tempfile.

Parameters:
  • circuit – the Circuit or serialized Circuit to render.

  • browser_newnew parameter to webbrowser.open, default 2.

  • sleep – Number of seconds to sleep before deleting file, default 5.

pytket.circuit.display.get_circuit_renderer() CircuitRenderer[source]#

Get a configurable instance of the circuit renderer.

Example usage:#

from pytket import Circuit
from pytket.circuit.display import get_circuit_renderer

circuit_renderer = get_circuit_renderer() # Instantiate a circuit renderer
circuit_renderer.set_render_options(zx_style=True) # Configure render options
circuit_renderer.condense_c_bits = False # You can also set the properties on the instance directly
print("Render options:")
print(circuit_renderer.get_render_options()) # View currently set render options

circuit_renderer.min_height = "300px" # Change the display height

circ = Circuit(2,2) # Define Circuit
circ.H(0).H(1).CX(0, 1).Rz(0.4, 1).CX(0, 1).H(0).H(1).measure_all()

circuit_renderer.render_circuit_jupyter(circ) # Render interactive display
Render options:
{'zx_style': True, 'condense_c_bits': False}

If you are happy with the default render options, you can import the render functions directly:

from pytket import Circuit
from pytket.circuit.display import render_circuit_jupyter

circ = Circuit(2,2) # Define Circuit
circ.H(0).H(1).CX(0, 1).Rz(0.4, 1).CX(0, 1).H(0).H(1).measure_all()

render_circuit_jupyter(circ) # Render with default options

This same diagram can be rendered with the offline renderer as follows

from pytket.extensions.offline_display import get_circuit_renderer, render_circuit_jupyter

custom_renderer = get_circuit_renderer()
custom_renderer.render_circuit_jupyter(circ) # Render configurable display as above
render_circuit_jupyter(circ) # Render using default options