Pattern Manipulation

graphix.pattern module

class graphix.pattern.Pattern(input_nodes: Iterable[int] | None = None, cmds: Iterable[N | M | E | C | X | Z | S | T] | None = None, output_nodes: Iterable[int] | None = None)[source]

MBQC pattern class.

Pattern holds a sequence of commands to operate the MBQC (Pattern.seq), and provide modification strategies to improve the structure and simulation efficiency of the pattern accoring to measurement calculus.

ref: V. Danos, E. Kashefi and P. Panangaden. J. ACM 54.2 8 (2007)

list(self)

list of commands.

each command is a list [type, nodes, attr] which will be applied in the order of list indices.
type: one of {‘N’, ‘M’, ‘E’, ‘X’, ‘Z’, ‘S’, ‘C’}
nodes: int for {‘N’, ‘M’, ‘X’, ‘Z’, ‘S’, ‘C’} commands, tuple (i, j) for {‘E’} command
attr for N: none
attr for M: meas_plane, angle, s_domain, t_domain
attr for X: signal_domain
attr for Z: signal_domain
attr for S: signal_domain
attr for C: clifford_index, as defined in graphix.clifford
n_node

total number of nodes in the resource state

Type:

int

__init__(input_nodes: Iterable[int] | None = None, cmds: Iterable[N | M | E | C | X | Z | S | T] | None = None, output_nodes: Iterable[int] | None = None) None[source]

Construct a pattern.

Parameters:
  • input_nodes (Iterable[int] | None) – Optional. List of input qubits.

  • cmds (Iterable[Command] | None) – Optional. List of initial commands.

  • output_nodes (Iterable[int] | None) – Optional. List of output qubits.

add(cmd: N | M | E | C | X | Z | S | T) None[source]

Add command to the end of the pattern.

An MBQC command is an instance of graphix.command.Command.

Parameters:

cmd (graphix.command.Command) – MBQC command.

extend(*cmds: N | M | E | C | X | Z | S | T | Iterable[N | M | E | C | X | Z | S | T]) None[source]

Add sequences of commands.

Parameters:

cmds – sequences of commands

clear() None[source]

Clear the sequence of pattern commands.

replace(cmds: list[N | M | E | C | X | Z | S | T], input_nodes: list[int] | None = None) None[source]

Replace pattern with a given sequence of pattern commands.

Parameters:
  • cmds – list of commands

  • input_nodes – optional, list of input qubits (by default, keep the same input nodes as before)

reorder_output_nodes(output_nodes: Iterable[int]) None[source]

Arrange the order of output_nodes.

Parameters:

output_nodes (iterable of int) – output nodes order determined by user. each index corresponds to that of logical qubits.

reorder_input_nodes(input_nodes: Iterable[int]) None[source]

Arrange the order of input_nodes.

Parameters:

input_nodes (iterable of int) – input nodes order determined by user. each index corresponds to that of logical qubits.

simulate_pattern(backend: StatevectorBackend | Literal['statevector'] = 'statevector', input_state: State | Statevec | Iterable[State] | Iterable[ExpressionOrSupportsComplex] | Iterable[Iterable[ExpressionOrSupportsComplex]] = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, **kwargs: Any) Statevec[source]
simulate_pattern(backend: DensityMatrixBackend | Literal['densitymatrix'], input_state: State | DensityMatrix | Iterable[State] | Iterable[ExpressionOrSupportsComplex] | Iterable[Iterable[ExpressionOrSupportsComplex]] = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, **kwargs: Any) DensityMatrix
simulate_pattern(backend: TensorNetworkBackend | Literal['tensornetwork', 'mps'], input_state: State | Iterable[State] | Iterable[ExpressionOrSupportsComplex] | Iterable[Iterable[ExpressionOrSupportsComplex]] = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, **kwargs: Any) MBQCTensorNet
simulate_pattern(backend: Backend[_StateT_co], input_state: Data = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, **kwargs: Any) _StateT_co

Simulate the execution of the pattern by using graphix.simulator.PatternSimulator.

Available backend: [‘statevector’, ‘densitymatrix’, ‘tensornetwork’]

Parameters:
  • backend (str) – optional parameter to select simulator backend.

  • rng (Generator, optional) – Random-number generator for measurements. This generator is used only in case of random branch selection (see RandomBranchSelector).

  • kwargs (keyword args for specified backend.)

Returns:

compute_max_degree() int[source]

Get max degree of a pattern.

Returns:

max_degree – max degree of a pattern

Return type:

int

extract_clifford() dict[int, Clifford][source]

Extract Clifford commands.

Returns:

vops

Return type:

dict

connected_nodes(node: int, prepared: set[int] | None = None) list[int][source]

Find nodes that are connected to a specified node.

These nodes must be in the statevector when the specified node is measured, to ensure correct computation. If connected nodes already exist in the statevector (prepared), then they will be ignored as they do not need to be prepared again.

Parameters:
  • node (int) – node index

  • prepared (list) – list of node indices, which are to be ignored

Returns:

node_list – list of nodes that are entangled with specified node

Return type:

list

perform_pauli_measurements(ignore_pauli_with_deps: bool = False) None[source]

Perform Pauli measurements in the pattern using efficient stabilizer simulator.

Parameters:
  • ignore_pauli_with_deps (bool) – Optional (False by default). If True, Pauli measurements with domains depending on other measures are preserved as-is in the pattern. If False, all Pauli measurements are preprocessed. Formally, measurements are swapped so that all Pauli measurements are applied first, and domains are updated accordingly.

  • seealso: (..) – measure_pauli():

to_ascii(left_to_right: bool = False, limit: int = 40, target: Container[command.CommandKind] | None = None) str[source]

Return the ASCII string representation of the pattern.

to_unicode(left_to_right: bool = False, limit: int = 40, target: Container[command.CommandKind] | None = None) str[source]

Return the Unicode string representation of the pattern.

to_latex(left_to_right: bool = False, limit: int = 40, target: Container[command.CommandKind] | None = None) str[source]

Return a string containing the LaTeX representation of the pattern.

standardize() None[source]

Execute standardization of the pattern.

‘standard’ pattern is one where commands are sorted in the order of ‘N’, ‘E’, ‘M’ and then byproduct commands (‘X’ and ‘Z’) and finally Clifford commands (‘C’).

shift_signals(method: str = 'direct') dict[int, set[int]][source]

Perform signal shifting procedure.

Extract the t-dependence of the measurement into ‘S’ commands and commute them to the end of the command sequence where it can be removed. This procedure simplifies the dependence structure of the pattern.

Ref for the original ‘mc’ method:
  1. Danos, E. Kashefi and P. Panangaden. J. ACM 54.2 8 (2007)

Parameters:

method (str, optional) – ‘direct’ shift_signals is executed on a conventional Pattern sequence. ‘mc’ shift_signals is done using the original algorithm on the measurement calculus paper.

Returns:

signal_dict – For each node, the signal that have been shifted.

Return type:

dict[int, set[int]]

is_standard(strict: bool = False) bool[source]

Determine whether the command sequence is standard.

Parameters:

strict (bool, optional) – If True, ensures that C commands are the last ones.

Returns:

is_standard – True if the pattern is standard

Return type:

bool

extract_graph() nx.Graph[int][source]

Return the graph state from the command sequence, extracted from ‘N’ and ‘E’ commands.

Returns:

graph_state

Return type:

nx.Graph[int]

extract_nodes() set[int][source]

Return the set of nodes of the pattern.

parallelize_pattern() None[source]

Optimize the pattern to reduce the depth of the computation by gathering measurement commands that can be performed simultaneously.

This optimized pattern runs efficiently on GPUs and quantum hardwares with depth (e.g. coherence time) limitations.

minimize_space() None[source]

Optimize the pattern to minimize the max_space property of the pattern.

The optimized pattern has significantly reduced space requirement (memory space for classical simulation, and maximum simultaneously prepared qubits for quantum hardwares).

draw_graph(flow_from_pattern: bool = True, 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 underlying graph of the pattern with flow or gflow structure.

Parameters:
  • flow_from_pattern (bool) – If True, the command sequence of the pattern is used to derive flow or gflow structure. If False, only the underlying graph is used.

  • 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, 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.

max_space() int[source]

Compute the maximum number of nodes that must be present in the graph (graph space) during the execution of the pattern.

For statevector simulation, this is equivalent to the maximum memory needed for classical simulation.

Returns:

n_nodes – max number of nodes present in the graph during pattern execution.

Return type:

int

to_qasm3(filename: Path | str, input_state: dict[int, State] | State = graphix.states.PlanarState(Plane.XY, 0)) None[source]

Export measurement pattern to OpenQASM 3.0 file.

See graphix.qasm3_exporter.pattern_to_qasm3().

Parameters:
  • filename (Path | str) – File name to export to. Example: "filename.qasm".

  • input_state (dict[int, State] | State, default BasicStates.PLUS) – The initial state for each input node. Only |0⟩ or |+⟩ states are supported.

graphix.pattern.measure_pauli(pattern: Pattern, *, ignore_pauli_with_deps: bool = False) Pattern[source]

Perform Pauli measurement of a pattern by fast graph state simulator.

Uses the decorated-graph method implemented in graphix.graphsim to perform the measurements in Pauli bases, and then sort remaining nodes back into pattern together with Clifford commands. Users are required to ensure there are no input nodes with graphix.pattern.Pattern.remove_input_nodes() before using this function.

TODO: non-XY plane measurements in original pattern

Parameters:
  • pattern (graphix.pattern.Pattern object)

  • ignore_pauli_with_deps (bool) – Optional (False by default). If True, Pauli measurements with domains depending on other measures are preserved as-is in the pattern. If False, all Pauli measurements are preprocessed. Formally, measurements are swapped so that all Pauli measurements are applied first, and domains are updated accordingly.

Returns:

new_pattern – pattern with Pauli measurement removed. only returned if copy argument is True.

Return type:

graphix.Pattern object

See also

graphix.pattern.Pattern.remove_input_nodes