Visualization tool

graphix.visualization module

This module provides functions to visualize the resource state of MBQC pattern. If flow or gflow exist, the tool take them into account and show the information flow as directed edges.

class graphix.visualization.GraphVisualizer(og: OpenGraph[Measurement], local_clifford: Mapping[int, Clifford] | None = None)[source]

A class for visualizing MBQC graphs with flow or gflow structure.

og

The open graph to be visualized

Type:

OpenGraph

local_clifford

dict specifying the local clifford for each node.

Type:

dict

__init__(og: OpenGraph[Measurement], local_clifford: Mapping[int, Clifford] | None = None) None
determine_figsize(l_k: Mapping[int, int] | None, pos: Mapping[int, _Point] | None = None, node_distance: tuple[float, float] = (1, 1)) _Point[source]

Return the figure size of the graph.

Parameters:
  • l_k (dict) – Layer mapping.

  • pos (dict) – dictionary of node positions.

  • node_distance (tuple) – Distance multiplication factor between nodes for x and y directions.

Returns:

figsize – figure size of the graph.

Return type:

tuple

place_all_corrections(layers: Mapping[int, int]) dict[int, _Point][source]

Return the position of nodes based on the pattern.

Parameters:

layers (dict) – Layer mapping obtained from the measurement order of the pattern.

Returns:

pos – dictionary of node positions.

Return type:

dict

place_causal_flow(flow: CausalFlow[AbstractPlanarMeasurement]) dict[int, _Point][source]

Return the position of nodes based on the flow.

Parameters:
  • f (dict) – flow mapping.

  • l_k (dict) – Layer mapping.

Returns:

pos – dictionary of node positions.

Return type:

dict

place_edge_paths(flow: Mapping[int, AbstractSet[int]], pos: Mapping[int, _Point]) tuple[dict[_Edge, list[_Point]], dict[_Edge, list[_Point]]][source]

Return the path of edges and gflow arrows.

Parameters:
  • flow (dict) – flow mapping (including gflow or any correction flow)

  • pos (dict) – dictionary of node positions.

Returns:

  • edge_path (dict) – dictionary of edge paths.

  • arrow_path (dict) – dictionary of arrow paths.

place_edge_paths_without_structure(pos: Mapping[int, _Point]) dict[_Edge, list[_Point]][source]

Return the path of edges.

Parameters:

pos (dict) – dictionary of node positions.

Returns:

edge_path – dictionary of edge paths.

Return type:

dict

place_pauli_flow(flow: PauliFlow[AbstractMeasurement]) dict[int, _Point][source]

Return the position of nodes based on the Pauli flow.

Parameters:
  • g (dict) – gflow mapping.

  • l_k (dict) – Layer mapping.

Returns:

pos – dictionary of node positions.

Return type:

dict

Notes

This method accepts gflows, as gflows are particular cases of Pauli flows.

place_without_structure() dict[int, _Point][source]

Return the position of nodes based on the graph.

Returns:

pos – dictionary of node positions.

Return type:

dict

Returns:

pos – dictionary of node positions.

Return type:

dict

visualize(show_pauli_measurement: bool = True, show_local_clifford: bool = False, show_measurement_planes: bool = False, show_loop: bool = True, node_distance: tuple[float, float] = (1, 1), figsize: tuple[int, int] | None = None, filename: Path | None = None) None[source]

Visualize the graph with flow or gflow structure.

If there exists a flow structure, then the graph is visualized with the flow structure. If flow structure is not found and there exists a gflow structure, then the graph is visualized with the gflow structure. If neither flow nor gflow structure is found, then the graph is visualized without any structure.

Parameters:
  • show_pauli_measurement (bool) – If True, the nodes with Pauli measurement angles are colored light blue.

  • show_local_clifford (bool) – If True, indexes of the local Clifford operator are displayed adjacent to the nodes.

  • show_measurement_planes (bool) – If True, the measurement planes are displayed adjacent to the nodes.

  • show_loop (bool) – whether or not to show loops for graphs with gflow. defaulted to True.

  • node_distance (tuple) – Distance multiplication factor between nodes for x and y directions.

  • figsize (tuple) – Figure size of the plot.

  • filename (Path | None) – If not None, filename of the png file to save the plot. If None, the plot is not saved. Default in None.

visualize_from_pattern(pattern: Pattern, show_pauli_measurement: bool = True, show_local_clifford: bool = False, show_measurement_planes: bool = False, show_loop: bool = True, node_distance: tuple[float, float] = (1, 1), figsize: tuple[int, int] | None = None, filename: Path | None = None) None[source]

Visualize the graph with flow or gflow structure found from the given pattern.

If pattern sequence is consistent with flow structure, then the graph is visualized with the flow structure. If it is not consistent with flow structure and consistent with gflow structure, then the graph is visualized with the gflow structure. If neither flow nor gflow structure is found, then the graph is visualized with all correction flows.

Parameters:
  • pattern (Pattern) – pattern to be visualized

  • show_pauli_measurement (bool) – If True, the nodes with Pauli measurement angles are colored light blue.

  • show_local_clifford (bool) – If True, indexes of the local Clifford operator are displayed adjacent to the nodes.

  • show_measurement_planes (bool) – If True, the measurement planes are displayed adjacent to the nodes.

  • show_loop (bool) – whether or not to show loops for graphs with gflow. defaulted to True.

  • node_distance (tuple) – Distance multiplication factor between nodes for x and y directions.

  • figsize (tuple) – Figure size of the plot.

  • filename (Path | None) – If not None, filename of the png file to save the plot. If None, the plot is not saved. Default in None.

visualize_graph(pos: Mapping[int, _Point], place_paths: Callable[[Mapping[int, _Point]], tuple[Mapping[_Edge, Sequence[_Point]], Mapping[_Edge, Sequence[_Point]] | None]], l_k: Mapping[int, int] | None, corrections: tuple[Mapping[int, AbstractSet[int]], Mapping[int, AbstractSet[int]]] | None, show_pauli_measurement: bool = True, show_local_clifford: bool = False, show_measurement_planes: bool = False, show_loop: bool = True, node_distance: tuple[float, float] = (1, 1), figsize: _Point | None = None, filename: Path | None = None) None[source]

Visualizes the graph.

Nodes are colored based on their role (input, output, or other) and edges are depicted as arrows or dashed lines depending on whether they are in the flow mapping. Vertical dashed lines separate different layers of the graph. This function does not return anything but plots the graph using matplotlib’s pyplot.

Parameters:
  • pos (Mapping[int, _Point]) – Node positions.

  • place_paths (Callable[) – [Mapping[int, _Point]], tuple[Mapping[_Edge, Sequence[_Point]], Mapping[_Edge, Sequence[_Point]] | None]

  • ] – Given scaled node positions, return the mapping of edge paths and the mapping of arrow paths.

  • l_k (Mapping[int, int] | None) – Layer mapping if any.

  • corrections (tuple[Mapping[int, AbstractSet[int]], Mapping[int, AbstractSet[int]]] | None) – X and Z corrections if any.

  • show_pauli_measurement (bool) – If True, the nodes with Pauli measurement angles are colored light blue.

  • show_local_clifford (bool) – If True, indexes of the local Clifford operator are displayed adjacent to the nodes.

  • show_measurement_planes (bool) – If True, the measurement planes are displayed adjacent to the nodes.

  • show_loop (bool) – whether or not to show loops for graphs with gflow. defaulted to True.

  • node_distance (tuple) – Distance multiplication factor between nodes for x and y directions.

  • figsize (tuple) – Figure size of the plot.

  • filename (Path | None) – If not None, filename of the png file to save the plot. If None, the plot is not saved. Default in None.

graphix.pretty_print module

This modules provides functions to format patterns and flows.

class graphix.pretty_print.OutputFormat(value)[source]

Enumeration of the output format for pretty-printing.

graphix.pretty_print.angle_to_str(angle: Angle, output: OutputFormat, max_denominator: int = 1000, multiplication_sign: bool = False) str[source]

Return a string representation of an angle given in units of π.

  • If the angle is a “simple” fraction of π (within the given max_denominator and a small tolerance), it returns a fractional string, e.g. “π/2”, “2π”, or “-3π/4”.

  • Otherwise, it returns the angle in radians (angle * π) formatted to two decimal places.

Parameters:
  • angle (float) – The angle in multiples of π (e.g., 0.5 means π/2).

  • output (OutputFormat) – Desired formatting style: Unicode (π symbol), LaTeX (pi), or ASCII (“pi”).

  • max_denominator (int, optional) – Maximum denominator for detecting a simple fraction (default: 1000).

  • multiplication_sign (bool) – Optional (default: False). If True, the multiplication sign is made explicit between the numerator and π: 2×π in Unicode, 2 \times \pi in LaTeX, and 2*pi in ASCII. If False, the multiplication sign is implicit: in Unicode, 2\pi in LaTeX, 2pi in ASCII.

Returns:

The formatted angle.

Return type:

str

graphix.pretty_print.command_to_str(cmd: N | M | E | C | X | Z | S | T, output: OutputFormat) str[source]

Return the string representation of a command according to the given format.

Parameters:
  • cmd (Command) – The command to pretty print.

  • output (OutputFormat) – The expected format.

graphix.pretty_print.pattern_to_str(pattern: Pattern, output: OutputFormat, left_to_right: bool = False, limit: int | None = 40, target: Container[command.CommandKind] | None = None) str[source]

Return the string representation of a pattern according to the given format.

Parameters:
  • pattern (Pattern) – The pattern to pretty print.

  • output (OutputFormat) – The expected format.

  • left_to_right (bool, optional) – If True, the first command will appear at the beginning of the resulting string. If False (the default), the first command will appear at the end of the string.

  • limit (int | None, optional) – If set to an int (default: 40), only first limit commands are printed, and an ellipsis is added at the end to indicate that some commands have been elided. If limit=None, there is no limit on the number of printed commands.

  • target (Container[command.CommandKind], optional) – If set, only commands of kinds specified in target are printed.

graphix.pretty_print.flow_to_str(flow: PauliFlow[AbstractMeasurement], output: OutputFormat, multiline: bool = False) str[source]

Convert a flow object to a formatted string representation.

Parameters:
  • flow (PauliFlow[AbstractMeasurement]) – The flow object to be formatted.

  • output (OutputFormat) – The desired output format (ASCII, LaTeX or Unicode).

  • multiline (bool, optional) – If True, format each correction set on a separate line (or LaTeX line break). If False, format each correction set on a single line separated by commas. Default is False.

Returns:

A string representation of the flow object formatted according to the specified output format and layout.

Return type:

str

graphix.pretty_print.xzcorr_to_str(xzcorr: XZCorrections[AbstractMeasurement], output: OutputFormat, multiline: bool = False) str[source]

Convert an XZCorrections object to a formatted string representation.

Parameters:
  • flow (XZCorrections[AbstractMeasurement]) – The XZCorrections object to be formatted.

  • output (OutputFormat) – The desired output format (ASCII, LaTeX or Unicode).

  • multiline (bool, optional) – If True, format each correction set on a separate line (or LaTeX line break). If False, format each correction set on a single line separated by commas. Default is False.

Returns:

A string representation of the XZCorrections object formatted according to the specified output format and layout.

Return type:

str