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, config)[source]

Class to manage circuit rendering within a given jinja2 environment.

get_render_options(full=False, _for_js=False)[source]

Get a dict of the current render options.

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

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

Return type:

dict[str, bool]

render_circuit_as_html(circuit, jupyter=False, orient=None)[source]

Render a circuit as HTML for inline display.

Parameters:
Return type:

str | None

render_circuit_jupyter(circuit, orient=None)[source]

Render a circuit as jupyter cell output.

Parameters:
Return type:

None

save_render_options()[source]

Save the current render options to pytket config.

Return type:

None

set_render_options(**kwargs)[source]

Set rendering defaults.

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

  • min_width – str, initial width of circuit display.

  • orient – ‘row’ | ‘column’, stacking direction for multi-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.

  • interpret_math – bool, try to render params and box names as math.

Return type:

None

view_browser(circuit, browser_new=2, sleep=5)[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 (dict[str, str | float | dict] | Circuit | list[dict[str, str | float | dict] | Circuit]) – the Circuit(s) or serialized Circuit(s) to render. Either a single circuit or a list of circuits to compare.

  • browser_new (int) – new parameter to webbrowser.open, default 2.

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

Return type:

None

pytket.circuit.display.get_circuit_renderer(config=None)[source]

Get a configurable instance of the circuit renderer. :type config: Optional[CircuitDisplayConfig] :param config: CircuitDisplayConfig to control the default render options.

Return type:

CircuitRenderer

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.config.render_options.condense_c_bits = False # You can also set the properties on the instance directly
circuit_renderer.config.min_height = "300px" # Change the display height
print("Render options:")
print(circuit_renderer.get_render_options()) # View currently set render options

circuit_renderer.save_render_options()  # Export current render options to the pytket config

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}

You can save the render options to the pytket config via circuit_renderer.save_render_options(). You can then import the render functions directly with your config already applied:

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/saved 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