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.
- render_circuit_as_html(circuit, jupyter=False, orient=None)[source]¶
Render a circuit as HTML for inline display.
- Parameters:
circuit (
dict
[str
,str
|float
|dict
] |Circuit
|list
[dict
[str
,str
|float
|dict
] |Circuit
]) – the circuit(s) to render.jupyter (
bool
) – set to true to render generated HTML in cell output.orient (
Union
[Literal
['row'
],Literal
['column'
],None
]) – the direction in which to stack circuits if multiple are present. One of ‘row’ or ‘column’.
- Return type:
- 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:
- 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 towebbrowser.open
, default 2.sleep (
int
) – Number of seconds to sleep before deleting file, default 5.
- Return type:
- 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:
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