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.render_circuit_jupyter(orient=None)¶
Render a circuit as jupyter cell output.
- display.view_browser(browser_new=2, sleep=5)¶
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:
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
API Reference:¶
Display a circuit as html.
- class pytket.circuit.display.CircuitDisplayConfig(*, min_height='400px', min_width='500px', orient=None, render_options=<factory>)[source]¶
- 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:
- class pytket.circuit.display.IncludeRawExtension(environment)[source]¶
- parse(parser)[source]¶
If any of the
tags
matched this method is called with the parser as first argument. The token the parser stream is pointing at is the name token that matched. This method has to return one or a list of multiple nodes.- Return type:
Output
- tags: t.Set[str] = {'include_raw'}¶
if this extension parses this is the list of tags it’s listening to.
- class pytket.circuit.display.RenderOptions(*, zx_style=None, condense_c_bits=None, recursive=None, condensed=None, dark_theme=None, system_theme=None, transparent_bg=None, crop_params=None, interpret_math=None)[source]¶
- display.get_circuit_renderer()¶
Get a configurable instance of the circuit renderer. :type config:
CircuitDisplayConfig
|None
:param config: CircuitDisplayConfig to control the default render options.- Return type:
- display.render_circuit_as_html(jupyter=False, orient=None)¶
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: