Graph state simulator

graphix.graphsim module

This provides an efficient graph state simulator using the decorated graph method.

Graph simulator.

class graphix.graphsim.GraphState(nodes=None, edges=None, vops=None, use_rustworkx: bool = False)[source]

Factory class for graph state simulator.

class graphix.graphsim.BaseGraphState[source]

Base class for graph state simulator.

Performs Pauli measurements on graph states. You can choose between networkx and rustworkx as the backend. The default is rustworkx if installed, otherwise networkx.

ref: M. Elliot, B. Eastin & C. Caves, JPhysA 43, 025301 (2010) and PRA 77, 042307 (2008)

Each node has attributes:
hollow:

True if node is hollow (has local H operator)

sign:

True if node has negative sign (local Z operator)

loop:

True if node has loop (local S operator)

__init__()[source]
abstract add_edges_from(edges: list[tuple[int, int]]) None[source]

Add edges and initialize node properties of newly added nodes.

Parameters:

edges (list[tuple[int, int]]) – must be given as list of 2-tuples (u, v)

Return type:

None

abstract add_nodes_from(nodes: list[int]) None[source]

Add nodes and initialize node properties.

Parameters:

nodes (list[int]) – A list of nodes.

Return type:

None

abstract adjacency() Iterator[source]

Return an iterator over (node, adjacency dict) tuples for all nodes.

Returns:

An iterator over (node, adjacency dictionary) for all nodes in the graph.

Return type:

Iterator

advance(node: int) None[source]

Flip the loop (local S) of a node.

If the loop already exist, sign is also flipped, reflecting the relation SS=Z. Note that application of S gate is different from advance if there exist an edge from the node.

Parameters:

node (int) – graph node to advance the loop.

Return type:

None

apply_vops(vops: dict[int, int]) None[source]

Apply local Clifford operators to the graph state from a dictionary.

Parameters:

vops (dict) – dict containing node indices as keys and local Clifford indices as values (see graphix.clifford.CLIFFORD)

Return type:

None

abstract degree() Iterator[tuple[int, int]][source]

Return an iterator for (node, degree) tuples, where degree is the number of edges adjacent to the node.

draw(fill_color: str = 'C0', **kwargs)[source]

Draw decorated graph state.

Negative nodes are indicated by negative sign of node labels.

Parameters:
  • fill_color (str) – optional, fill color of nodes

  • kwargs – optional, additional arguments to supply networkx.draw().

abstract property edges: EdgeView | EdgeList

Return the set of edges.

equivalent_fill_node(node: int) int[source]

Fill the chosen node by graph transformation rules E1 and E2.

If the selected node is hollow and isolated, it cannot be filled and warning is thrown.

Parameters:

node (int) – node to fill.

Returns:

result – if the selected node is hollow and isolated, result is 1. if filled and isolated, 2. otherwise it is 0.

Return type:

int

equivalent_graph_e1(node: int) None[source]

Tranform a graph state to a different graph state representing the same stabilizer state.

This rule applies only to a node with loop.

Parameters:

node1 (int) – A graph node with a loop to apply rule E1

Return type:

None

equivalent_graph_e2(node1: int, node2: int) None[source]

Tranform a graph state to a different graph state representing the same stabilizer state.

This rule applies only to two connected nodes without loop.

Parameters:
  • node1 (int) – connected graph nodes to apply rule E2

  • node2 (int) – connected graph nodes to apply rule E2

Return type:

None

flip_fill(node: int) None[source]

Flips the fill (local H) of a node.

Parameters:

node (int) – graph node to flip the fill

Return type:

None

flip_sign(node) None[source]

Flip the sign (local Z) of a node.

Note that application of Z gate is different from flip_sign if there exist an edge from the node.

Parameters:

node (int) – graph node to flip the sign

Return type:

None

abstract get_isolates() list[int][source]

Return a list of isolated nodes (nodes with no edges).

Returns:

A list of isolated nodes.

Return type:

list[int]

get_vops() dict[int, int][source]

Apply local Clifford operators to the graph state from a dictionary.

Parameters:

vops (dict) – dict containing node indices as keys and local Clifford indices as values (see graphix.clifford.CLIFFORD)

abstract property graph: Graph | None

Return the graph itself.

h(node: int) None[source]

Apply H gate to a qubit (node).

Parameters:

node (int) – graph node to apply H gate

Return type:

None

abstract local_complement(node: int) None[source]

Perform local complementation of a graph.

Parameters:

node (int) – chosen node for the local complementation

Return type:

None

measure_x(node: int, choice: int = 0) int[source]

Perform measurement in X basis.

According to original paper, we realise X measurement by applying H gate to the measured node before Z measurement.

Parameters:
  • node (int) – qubit index to be measured

  • choice (int, 0 or 1) – choice of measurement outcome. observe (-1)^choice

Returns:

result – measurement outcome. 0 or 1.

Return type:

int

measure_y(node: int, choice: int = 0) int[source]

Perform measurement in Y basis.

According to original paper, we realise Y measurement by applying S,Z and H gate to the measured node before Z measurement.

Parameters:
  • node (int) – qubit index to be measured

  • choice (int, 0 or 1) – choice of measurement outcome. observe (-1)^choice

Returns:

result – measurement outcome. 0 or 1.

Return type:

int

measure_z(node: int, choice: int = 0) int[source]

Perform measurement in Z basis.

To realize the simple Z measurement on undecorated graph state, we first fill the measured node (remove local H gate)

Parameters:
  • node (int) – qubit index to be measured

  • choice (int, 0 or 1) – choice of measurement outcome. observe (-1)^choice

Returns:

result – measurement outcome. 0 or 1.

Return type:

int

abstract neighbors(node: int) Iterator[source]

Return an iterator over all neighbors of node n.

Parameters:

node (int) – A node in the graph

Returns:

An iterator over all neighbors of node n.

Return type:

iter

abstract property nodes: NodeView | NodeList

Return the set of nodes.

abstract number_of_edges(u: int | None = None, v: int | None = None) int[source]

Return the number of edges between two nodes.

Parameters:
  • u (int, optional) – A node in the graph

  • v (int, optional) – A node in the graph

Returns:

The number of edges in the graph. If u and v are specified, return the number of edges between those nodes.

Return type:

int

abstract remove_edge(u: int, v: int) None[source]

Remove an edge from the graph.

Parameters:
  • u (int) – A node in the graph

  • v (int) – A node in the graph

Return type:

None

abstract remove_edges_from(edges: list[tuple[int, int]]) None[source]

Remove all edges specified in the list.

Parameters:

edges (list of tuples) – A list of edges to remove from the graph.

Return type:

None

abstract remove_node(node: int) None[source]

Remove a node from the graph.

Parameters:

node (int) – A node in the graph

Return type:

None

abstract remove_nodes_from(nodes: list[int]) None[source]

Remove all nodes specified in the list.

Parameters:

nodes (list) – A list of nodes to remove from the graph.

Return type:

None

s(node: int) None[source]

Apply S gate to a qubit (node).

Parameters:

node (int) – graph node to apply S gate

Return type:

None

abstract subgraph(nodes: list) Graph | None[source]

Return a subgraph of the graph.

Parameters:

nodes (list) – A list of node indices to generate the subgraph from.

Returns:

A subgraph of the graph.

Return type:

GraphObject

to_statevector() Statevec[source]

Convert the graph state into a state vector.

z(node: int) None[source]

Apply Z gate to a qubit (node).

Parameters:

node (int) – graph node to apply Z gate

Return type:

None

class graphix.graphsim.NXGraphState(nodes: list[int] | None = None, edges: list[tuple[int, int]] | None = None, vops: dict[int, int] | None = None)[source]

Graph state simulator implemented with networkx.

See BaseGraphState for more details.

__init__(nodes: list[int] | None = None, edges: list[tuple[int, int]] | None = None, vops: dict[int, int] | None = None)[source]

Instantiate a graph simulator.

Parameters:
  • nodes (list[int]) – A container of nodes (list, dict, etc)

  • edges (list[tuple[int, int]]) – list of tuples (i,j) for pairs to be entangled.

  • vops (dict[int, int]) – dict of local Clifford gates with keys for node indices and values for Clifford index (see graphix.clifford.CLIFFORD)

add_edges_from(edges) None[source]

Add edges and initialize node properties of newly added nodes.

See BaseGraphState.add_edges_from().

add_nodes_from(nodes) None[source]

Add nodes and initialize node properties.

See BaseGraphState.add_nodes_from().

adjacency() Iterator[source]

Return an iterator over (node, adjacency dict) tuples for all nodes.

See BaseGraphState.adjacency().

degree() Iterator[tuple[int, int]][source]

Return an iterator for (node, degree) tuples, where degree is the number of edges adjacent to the node.

property edges: EdgeView

Return the set of edges.

get_isolates() list[int][source]

Return a list of isolated nodes (nodes with no edges).

See BaseGraphState.get_isolates().

property graph: Graph

Return the graph itself.

local_complement(node)[source]

Perform local complementation of a graph.

See BaseGraphState.local_complement().

neighbors(node) Iterator[source]

Return an iterator over all neighbors of node n.

See BaseGraphState.neighbors().

property nodes: NodeView

Return the set of nodes.

number_of_edges(u: int | None = None, v: int | None = None) int[source]

Return the number of edges between two nodes.

See BaseGraphState.number_of_edges().

remove_edge(u: int, v: int) None[source]

Remove an edge from the graph.

See BaseGraphState.remove_edge().

remove_edges_from(edges: list[tuple[int, int]]) None[source]

Remove all edges specified in the list.

See BaseGraphState.remove_edges_from().

remove_node(node: int) None[source]

Remove a node from the graph.

See BaseGraphState.remove_node().

remove_nodes_from(nodes: list[int]) None[source]

Remove all nodes specified in the list.

See BaseGraphState.remove_nodes_from().

subgraph(nodes: list) Graph[source]

Return a subgraph of the graph.

See BaseGraphState.subgraph().

class graphix.graphsim.RXGraphState(nodes: list[int] | None = None, edges: list[tuple[int, int]] | None = None, vops: dict[int, int] | None = None)[source]

Graph state simulator implemented with rustworkx.

See BaseGraphState for more details.

__init__(nodes: list[int] | None = None, edges: list[tuple[int, int]] | None = None, vops: dict[int, int] | None = None)[source]

Initialize graph state simulator.

Parameters:
  • nodes (list[int]) – A container of nodes (list, dict, etc)

  • edges (list[tuple[int, int]]) – list of tuples (i,j) for pairs to be entangled.

  • vops (dict[int, int]) – dict of local Clifford gates with keys for node indices and values for Clifford index (see graphix.clifford.CLIFFORD)

add_edges_from(edges)[source]

Add edges and initialize node properties of newly added nodes.

See BaseGraphState.add_edges_from().

add_nodes_from(nodes: list[int])[source]

Add nodes and initialize node properties.

See BaseGraphState.add_nodes_from().

adjacency() Iterator[source]

Return an iterator over (node, adjacency dict) tuples for all nodes.

See BaseGraphState.adjacency().

degree() Iterator[tuple[int, int]][source]

Return an iterator for (node, degree) tuples, where degree is the number of edges adjacent to the node.

property edges: EdgeList

Return the set of edges.

get_isolates() list[int][source]

Return a list of isolated nodes (nodes with no edges).

See BaseGraphState.get_isolates().

property graph: rx.PyGraph

Return the graph itself.

local_complement(node)[source]

Perform local complementation of a graph.

See BaseGraphState.local_complement().

neighbors(node) Iterator[int][source]

Return an iterator over all neighbors of node n.

See BaseGraphState.neighbors().

property nodes: NodeList

Return the set of nodes.

number_of_edges(u: int | None = None, v: int | None = None) int[source]

Return the number of edges between two nodes.

See BaseGraphState.number_of_edges().

remove_edge(u: int, v: int) None[source]

Remove an edge from the graph.

See BaseGraphState.remove_edge().

remove_edges_from(edges: list[tuple[int, int]]) None[source]

Remove all edges specified in the list.

See BaseGraphState.remove_edges_from().

remove_node(node: int) None[source]

Remove a node from the graph.

See BaseGraphState.remove_node().

remove_nodes_from(nodes: list[int]) None[source]

Remove all nodes specified in the list.

See BaseGraphState.remove_nodes_from().

subgraph(nodes: list) rx.PyGraph[source]

Return a subgraph of the graph.

See BaseGraphState.subgraph().

class graphix.graphsim.NodeList(node_nums: list[int] | None = None, node_datas: list[dict] | None = None, node_indices: list[int] | None = None)[source]

Node list class for RXGraphState.

In rustworkx, node data is stored in a tuple (node_num, node_data), and adding/removing nodes by node_num is not supported. This class defines a node list with node_num as key.

__init__(node_nums: list[int] | None = None, node_datas: list[dict] | None = None, node_indices: list[int] | None = None)[source]

Initialize a node list.

add_node(nnum: int, ndata: dict, nidx: int) None[source]

Add a node to the list.

add_nodes_from(node_nums: list[int], node_datas: list[dict], node_indices: list[int]) None[source]

Add nodes to the list.

get_node_index(nnum: int) int[source]

Return the index of the node nnum.

remove_node(nnum: int) None[source]

Remove a node from the list.

remove_nodes_from(node_nums: list[int]) None[source]

Remove nodes from the list.

class graphix.graphsim.EdgeList(edge_nums: list[tuple[int, int]] | None = None, edge_datas: list[dict] | None = None, edge_indices: list[int] | None = None)[source]

Edge list class for RXGraphState.

In rustworkx, edge data is stored in a tuple (parent, child, edge_data), and adding/removing edges by (parent, child) is not supported. This class defines a edge list with (parent, child) as key.

__init__(edge_nums: list[tuple[int, int]] | None = None, edge_datas: list[dict] | None = None, edge_indices: list[int] | None = None)[source]

Initialize an edge list.

add_edge(enum: tuple[int, int], edata: dict, eidx: int) None[source]

Add an edge to the list.

add_edges_from(edge_nums: list[tuple[int, int]], edge_datas: list[dict], edge_indices: list[int]) None[source]

Add edges to the list.

get_edge_index(enum: tuple[int, int]) int[source]

Return the index of the edge enum.

remove_edge(enum: tuple[int, int]) None[source]

Remove an edge from the list.

remove_edges_by_node(nnum: int)[source]

Remove all edges connected to the node nnum.

remove_edges_from(edge_nums: list[tuple[int, int]]) None[source]

Remove edges from the list.