qml.transforms.CommutationDAG

class CommutationDAG(tape)[source]

Bases: object

Class to represent a quantum circuit as a directed acyclic graph (DAG). This class is useful to build the commutation DAG and set up all nodes attributes. The construction of the DAG should be used through the transform qml.transforms.commutation_dag.

Parameters

tape (QuantumTape) – PennyLane quantum tape representing a quantum circuit.

Reference:

[1] Iten, R., Moyard, R., Metger, T., Sutter, D. and Woerner, S., 2020. Exact and practical pattern matching for quantum circuit optimization. doi.org/10.1145/3498325

graph

Return the DAG object.

size

Return the size of the DAG object.

graph

Return the DAG object.

Returns

Networkx representation of the DAG.

Return type

networkx.MultiDiGraph()

size

Return the size of the DAG object.

Returns

Number of nodes in the DAG.

Return type

int

add_edge(node_in, node_out)

Add an edge (non commutation) between node_in and node_out.

add_node(operation)

Add the operation as a node in the DAG and updates the edges.

direct_predecessors(node_id)

Return the direct predecessors of the given node.

direct_successors(node_id)

Return the direct successors of the given node.

draw([filename])

Draw the DAG object.

get_edge(node_in, node_out)

Get the edge between two nodes if it exists.

get_edges()

Get all edges as an iterable.

get_node(node_id)

Add the operation as a node in the DAG and updates the edges.

get_nodes()

Return iterable to loop through all the nodes in the DAG.

predecessors(node_id)

Return the predecessors of the given node.

successors(node_id)

Return the successors of the given node.

add_edge(node_in, node_out)[source]

Add an edge (non commutation) between node_in and node_out.

Parameters
  • node_in (int) – Id of the ingoing node.

  • node_out (int) – Id of the outgoing node.

Returns

Id of the created edge.

Return type

int

add_node(operation)[source]

Add the operation as a node in the DAG and updates the edges.

Parameters

operation (qml.operation) – PennyLane quantum operation to add to the DAG.

direct_predecessors(node_id)[source]

Return the direct predecessors of the given node.

Parameters

node_id (int) – Id of the node in the DAG.

Returns

List of the direct predecessors of the given node.

Return type

list[int]

direct_successors(node_id)[source]

Return the direct successors of the given node.

Parameters

node_id (int) – Id of the node in the DAG.

Returns

List of the direct successors of the given node.

Return type

list[int]

draw(filename='dag.png')[source]

Draw the DAG object.

Parameters

filename (str) – The file name which is in PNG format. Default = ‘dag.png’

get_edge(node_in, node_out)[source]

Get the edge between two nodes if it exists.

Parameters
  • node_in (int) – Id of the ingoing node.

  • node_out (int) – Id of the outgoing node.

Returns

Default weight is 0, it returns None when there is no edge.

Return type

dict or None

get_edges()[source]

Get all edges as an iterable.

Returns

Iterable over all edges.

Return type

networkx.classes.reportviews.OutMultiEdgeDataView

get_node(node_id)[source]

Add the operation as a node in the DAG and updates the edges.

Parameters

node_id (int) – PennyLane quantum operation to add to the DAG.

Returns

The node with the given id.

Return type

CommutationDAGNOde

get_nodes()[source]

Return iterable to loop through all the nodes in the DAG.

Returns

Iterable nodes.

Return type

networkx.classes.reportviews.NodeDataView

predecessors(node_id)[source]

Return the predecessors of the given node.

Parameters

node_id (int) – Id of the node in the DAG.

Returns

List of the predecessors of the given node.

Return type

list[int]

successors(node_id)[source]

Return the successors of the given node.

Parameters

node_id (int) – Id of the node in the DAG.

Returns

List of the successors of the given node.

Return type

list[int]