qiskit.dagcircuit.DAGDependency
class DAGDependency
Object to represent a quantum circuit as a directed acyclic graph via operation dependencies (i.e. lack of commutation).
The nodes in the graph are operations represented by quantum gates. The edges correspond to non-commutation between two operations (i.e. a dependency). A directed edge from node A to node B means that operation A does not commute with operation B. The object’s methods allow circuits to be constructed.
The nodes in the graph have the following attributes: ‘operation’, ‘successors’, ‘predecessors’.
Example:
Bell circuit with no measurement.
┌───┐
qr_0: ┤ H ├──■──
└───┘┌─┴─┐
qr_1: ─────┤ X ├
└───┘
The dependency DAG for the above circuit is represented by two nodes. The first one corresponds to Hadamard gate, the second one to the CNOT gate as the gates do not commute there is an edge between the two nodes.
Reference:
[1] Iten, R., Moyard, R., Metger, T., Sutter, D. and Woerner, S., 2020. Exact and practical pattern matching for quantum circuit optimization. arXiv:1909.05270
Create an empty DAGDependency.
__init__
__init__()
Create an empty DAGDependency.
Methods
__init__ () | Create an empty DAGDependency. |
add_creg (creg) | Add clbits in a classical register. |
add_op_node (operation, qargs, cargs) | Add a DAGDepNode to the graph and update the edges. |
add_qreg (qreg) | Add qubits in a quantum register. |
copy () | Function to copy a DAGDependency object. |
depth () | Return the circuit depth. |
direct_predecessors (node_id) | Direct predecessors id of a given node as sorted list. |
direct_successors (node_id) | Direct successors id of a given node as sorted list. |
draw ([scale, filename, style]) | Draws the DAGDependency graph. |
get_all_edges () | Enumaration of all edges. |
get_edges (src_id, dest_id) | Edge enumeration between two nodes through method get_all_edge_data. |
get_in_edges (node_id) | Enumeration of all incoming edges for a given node. |
get_node (node_id) | param node_idlabel of considered node. |
get_nodes () | returnsiterator over all the nodes. |
get_out_edges (node_id) | Enumeration of all outgoing edges for a given node. |
predecessors (node_id) | Predecessors id of a given node as sorted list. |
size () | Returns the number of gates in the circuit |
successors (node_id) | Successors id of a given node as sorted list. |
to_networkx () | Returns a copy of the DAGDependency in networkx format. |
to_retworkx () | Returns the DAGDependency in retworkx format. |
topological_nodes () | Yield nodes in topological order. |
Attributes
calibrations | Return calibration dictionary. |
global_phase | Return the global phase of the circuit. |
add_creg
add_creg(creg)
Add clbits in a classical register.
add_op_node
add_op_node(operation, qargs, cargs)
Add a DAGDepNode to the graph and update the edges.
Parameters
- operation (qiskit.circuit.Instruction) – operation as a quantum gate.
- qargs (list[Qubit]) – list of qubits on which the operation acts
- cargs (list[Clbit]) – list of classical wires to attach to.
add_qreg
add_qreg(qreg)
Add qubits in a quantum register.
calibrations
Return calibration dictionary.
The custom pulse definition of a given gate is of the form
{‘gate_name’: {(qubits, params): schedule}}
copy
copy()
Function to copy a DAGDependency object. :returns: a copy of a DAGDependency object. :rtype: DAGDependency
depth
depth()
Return the circuit depth. :returns: the circuit depth :rtype: int
direct_predecessors
direct_predecessors(node_id)
Direct predecessors id of a given node as sorted list.
Parameters
node_id (int) – label of considered node.
Returns
direct predecessors id as a sorted list
Return type
List
direct_successors
direct_successors(node_id)
Direct successors id of a given node as sorted list.
Parameters
node_id (int) – label of considered node.
Returns
direct successors id as a sorted list
Return type
List
draw
draw(scale=0.7, filename=None, style='color')
Draws the DAGDependency graph.
This function needs pydot <https://github.com/erocarrera/pydot>, which in turn needs Graphviz <https://www.graphviz.org/>` to be installed.
Parameters
- scale (float) – scaling factor
- filename (str) – file path to save image to (format inferred from name)
- style (str) – ‘plain’: B&W graph ‘color’ (default): color input/output/op nodes
Returns
if in Jupyter notebook and not saving to file,
otherwise None.
Return type
Ipython.display.Image
get_all_edges
get_all_edges()
Enumaration of all edges.
Returns
corresponding to the label.
Return type
List
get_edges
get_edges(src_id, dest_id)
Edge enumeration between two nodes through method get_all_edge_data.
Parameters
- src_id (int) – label of the first node.
- dest_id (int) – label of the second node.
Returns
corresponding to all edges between the two nodes.
Return type
List
get_in_edges
get_in_edges(node_id)
Enumeration of all incoming edges for a given node.
Parameters
node_id (int) – label of considered node.
Returns
corresponding incoming edges data.
Return type
List
get_node
get_node(node_id)
Parameters
node_id (int) – label of considered node.
Returns
corresponding to the label.
Return type
node
get_nodes
get_nodes()
Returns
iterator over all the nodes.
Return type
generator(dict)
get_out_edges
get_out_edges(node_id)
Enumeration of all outgoing edges for a given node.
Parameters
node_id (int) – label of considered node.
Returns
corresponding outgoing edges data.
Return type
List
global_phase
Return the global phase of the circuit.
predecessors
predecessors(node_id)
Predecessors id of a given node as sorted list.
Parameters
node_id (int) – label of considered node.
Returns
all predecessors id as a sorted list
Return type
List
size
size()
Returns the number of gates in the circuit
successors
successors(node_id)
Successors id of a given node as sorted list.
Parameters
node_id (int) – label of considered node.
Returns
all successors id as a sorted list
Return type
List
to_networkx
to_networkx()
Returns a copy of the DAGDependency in networkx format.
to_retworkx
to_retworkx()
Returns the DAGDependency in retworkx format.
topological_nodes
topological_nodes()
Yield nodes in topological order.
Returns
node in topological order.
Return type
generator(DAGNode)