345 lines
15 KiB
Plaintext
345 lines
15 KiB
Plaintext
---
|
||
title: DAGDependency (latest version)
|
||
description: API reference for qiskit.dagcircuit.DAGDependency in the latest version of qiskit
|
||
in_page_toc_min_heading_level: 1
|
||
python_api_type: class
|
||
python_api_name: qiskit.dagcircuit.DAGDependency
|
||
---
|
||
|
||
# DAGDependency
|
||
|
||
<Class id="qiskit.dagcircuit.DAGDependency" isDedicatedPage={true} github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L57-L617" signature="qiskit.dagcircuit.DAGDependency" modifiers="class">
|
||
Bases: [`object`](https://docs.python.org/3/library/functions.html#object "(in Python v3.13)")
|
||
|
||
Object to represent a quantum circuit as a Directed Acyclic Graph (DAG) 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.
|
||
|
||
```python
|
||
┌───┐
|
||
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](https://arxiv.org/abs/1909.05270)
|
||
|
||
Create an empty DAGDependency.
|
||
|
||
## Attributes
|
||
|
||
### calibrations
|
||
|
||
<Attribute id="qiskit.dagcircuit.DAGDependency.calibrations">
|
||
Return calibration dictionary.
|
||
|
||
The custom pulse definition of a given gate is of the form `{'gate_name': {(qubits, params): schedule}}`.
|
||
</Attribute>
|
||
|
||
### global\_phase
|
||
|
||
<Attribute id="qiskit.dagcircuit.DAGDependency.global_phase">
|
||
Return the global phase of the circuit.
|
||
</Attribute>
|
||
|
||
## Methods
|
||
|
||
### add\_clbits
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.add_clbits" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L194-L203" signature="add_clbits(clbits)">
|
||
Add individual clbit wires.
|
||
</Function>
|
||
|
||
### add\_creg
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.add_creg" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L217-L227" signature="add_creg(creg)">
|
||
Add clbits in a classical register.
|
||
</Function>
|
||
|
||
### add\_op\_node
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.add_op_node" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L426-L436" signature="add_op_node(operation, qargs, cargs)">
|
||
Add a DAGDepNode to the graph and update the edges.
|
||
|
||
**Parameters**
|
||
|
||
* **operation** ([*qiskit.circuit.Operation*](qiskit.circuit.Operation "qiskit.circuit.Operation")) – operation as a quantum gate
|
||
* **qargs** ([*list*](https://docs.python.org/3/library/stdtypes.html#list "(in Python v3.13)")*\[*[*Qubit*](circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")*]*) – list of qubits on which the operation acts
|
||
* **cargs** ([*list*](https://docs.python.org/3/library/stdtypes.html#list "(in Python v3.13)")*\[*[*Clbit*](circuit#qiskit.circuit.Clbit "qiskit.circuit.Clbit")*]*) – list of classical wires to attach to
|
||
</Function>
|
||
|
||
### add\_qreg
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.add_qreg" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L205-L215" signature="add_qreg(qreg)">
|
||
Add qubits in a quantum register.
|
||
</Function>
|
||
|
||
### add\_qubits
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.add_qubits" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L183-L192" signature="add_qubits(qubits)">
|
||
Add individual qubit wires.
|
||
</Function>
|
||
|
||
### copy
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.copy" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L508-L524" signature="copy()">
|
||
Function to copy a DAGDependency object. :returns: a copy of a DAGDependency object. :rtype: DAGDependency
|
||
</Function>
|
||
|
||
### depth
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.depth" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L175-L181" signature="depth()">
|
||
Return the circuit depth. :returns: the circuit depth :rtype: int
|
||
</Function>
|
||
|
||
### direct\_predecessors
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.direct_predecessors" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L333-L343" signature="direct_predecessors(node_id)">
|
||
Direct predecessors id of a given node as sorted list.
|
||
|
||
**Parameters**
|
||
|
||
**node\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of considered node.
|
||
|
||
**Returns**
|
||
|
||
direct predecessors id as a sorted list
|
||
|
||
**Return type**
|
||
|
||
List
|
||
</Function>
|
||
|
||
### direct\_successors
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.direct_successors" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L321-L331" signature="direct_successors(node_id)">
|
||
Direct successors id of a given node as sorted list.
|
||
|
||
**Parameters**
|
||
|
||
**node\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of considered node.
|
||
|
||
**Returns**
|
||
|
||
direct successors id as a sorted list
|
||
|
||
**Return type**
|
||
|
||
List
|
||
</Function>
|
||
|
||
### draw
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.draw" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L526-L544" signature="draw(scale=0.7, filename=None, style='color')">
|
||
Draws the DAGDependency graph.
|
||
|
||
This function needs pydot \<[https://github.com/erocarrera/pydot](https://github.com/erocarrera/pydot)>, which in turn needs Graphviz \<[https://www.graphviz.org/](https://www.graphviz.org/)>\` to be installed.
|
||
|
||
**Parameters**
|
||
|
||
* **scale** ([*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.13)")) – scaling factor
|
||
* **filename** ([*str*](https://docs.python.org/3/library/stdtypes.html#str "(in Python v3.13)")) – file path to save image to (format inferred from name)
|
||
* **style** ([*str*](https://docs.python.org/3/library/stdtypes.html#str "(in Python v3.13)")) – ‘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
|
||
</Function>
|
||
|
||
### get\_all\_edges
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.get_all_edges" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L283-L295" signature="get_all_edges()">
|
||
Enumeration of all edges.
|
||
|
||
**Returns**
|
||
|
||
corresponding to the label.
|
||
|
||
**Return type**
|
||
|
||
List
|
||
</Function>
|
||
|
||
### get\_edges
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.get_edges" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L270-L281" signature="get_edges(src_id, dest_id)">
|
||
Edge enumeration between two nodes through method get\_all\_edge\_data.
|
||
|
||
**Parameters**
|
||
|
||
* **src\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of the first node.
|
||
* **dest\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of the second node.
|
||
|
||
**Returns**
|
||
|
||
corresponding to all edges between the two nodes.
|
||
|
||
**Return type**
|
||
|
||
List
|
||
</Function>
|
||
|
||
### get\_in\_edges
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.get_in_edges" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L297-L307" signature="get_in_edges(node_id)">
|
||
Enumeration of all incoming edges for a given node.
|
||
|
||
**Parameters**
|
||
|
||
**node\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of considered node.
|
||
|
||
**Returns**
|
||
|
||
corresponding incoming edges data.
|
||
|
||
**Return type**
|
||
|
||
List
|
||
</Function>
|
||
|
||
### get\_node
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.get_node" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L248-L256" signature="get_node(node_id)">
|
||
**Parameters**
|
||
|
||
**node\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of considered node.
|
||
|
||
**Returns**
|
||
|
||
corresponding to the label.
|
||
|
||
**Return type**
|
||
|
||
node
|
||
</Function>
|
||
|
||
### get\_nodes
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.get_nodes" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L241-L246" signature="get_nodes()">
|
||
**Returns**
|
||
|
||
iterator over all the nodes.
|
||
|
||
**Return type**
|
||
|
||
generator([dict](https://docs.python.org/3/library/stdtypes.html#dict "(in Python v3.13)"))
|
||
</Function>
|
||
|
||
### get\_out\_edges
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.get_out_edges" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L309-L319" signature="get_out_edges(node_id)">
|
||
Enumeration of all outgoing edges for a given node.
|
||
|
||
**Parameters**
|
||
|
||
**node\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of considered node.
|
||
|
||
**Returns**
|
||
|
||
corresponding outgoing edges data.
|
||
|
||
**Return type**
|
||
|
||
List
|
||
</Function>
|
||
|
||
### predecessors
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.predecessors" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L357-L367" signature="predecessors(node_id)">
|
||
Predecessors id of a given node as sorted list.
|
||
|
||
**Parameters**
|
||
|
||
**node\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of considered node.
|
||
|
||
**Returns**
|
||
|
||
all predecessors id as a sorted list
|
||
|
||
**Return type**
|
||
|
||
List
|
||
</Function>
|
||
|
||
### replace\_block\_with\_op
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.replace_block_with_op" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L546-L617" signature="replace_block_with_op(node_block, op, wire_pos_map, cycle_check=True)">
|
||
Replace a block of nodes with a single node.
|
||
|
||
This is used to consolidate a block of DAGDepNodes into a single operation. A typical example is a block of CX and SWAP gates consolidated into a LinearFunction. This function is an adaptation of a similar function from DAGCircuit.
|
||
|
||
It is important that such consolidation preserves commutativity assumptions present in DAGDependency. As an example, suppose that every node in a block \[A, B, C, D] commutes with another node E. Let F be the consolidated node, F = A o B o C o D. Then F also commutes with E, and thus the result of replacing \[A, B, C, D] by F results in a valid DAGDependency. That is, any deduction about commutativity in consolidated DAGDependency is correct. On the other hand, suppose that at least one of the nodes, say B, does not commute with E. Then the consolidated DAGDependency would imply that F does not commute with E. Even though F and E may actually commute, it is still safe to assume that they do not. That is, the current implementation of consolidation may lead to suboptimal but not to incorrect results.
|
||
|
||
**Parameters**
|
||
|
||
* **node\_block** (*List\[*[*DAGDepNode*](qiskit.dagcircuit.DAGDepNode "qiskit.dagcircuit.DAGDepNode")*]*) – A list of dag nodes that represents the node block to be replaced
|
||
* **op** ([*qiskit.circuit.Operation*](qiskit.circuit.Operation "qiskit.circuit.Operation")) – The operation to replace the block with
|
||
* **wire\_pos\_map** (*Dict\[*[*Qubit*](circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")*,* [*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")*]*) – The dictionary mapping the qarg to the position. This is necessary to reconstruct the qarg order over multiple gates in the combined single op node.
|
||
* **cycle\_check** ([*bool*](https://docs.python.org/3/library/functions.html#bool "(in Python v3.13)")) – When set to True this method will check that replacing the provided `node_block` with a single node would introduce a cycle (which would invalidate the `DAGDependency`) and will raise a `DAGDependencyError` if a cycle would be introduced. This checking comes with a run time penalty. If you can guarantee that your input `node_block` is a contiguous block and won’t introduce a cycle when it’s contracted to a single node, this can be set to `False` to improve the runtime performance of this method.
|
||
|
||
**Raises**
|
||
|
||
[**DAGDependencyError**](dagcircuit#qiskit.dagcircuit.DAGDependencyError "qiskit.dagcircuit.DAGDependencyError") – if `cycle_check` is set to `True` and replacing the specified block introduces a cycle or if `node_block` is empty.
|
||
</Function>
|
||
|
||
### size
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.size" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L171-L173" signature="size()">
|
||
Returns the number of gates in the circuit
|
||
</Function>
|
||
|
||
### successors
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.successors" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L345-L355" signature="successors(node_id)">
|
||
Successors id of a given node as sorted list.
|
||
|
||
**Parameters**
|
||
|
||
**node\_id** ([*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")) – label of considered node.
|
||
|
||
**Returns**
|
||
|
||
all successors id as a sorted list
|
||
|
||
**Return type**
|
||
|
||
List
|
||
</Function>
|
||
|
||
### to\_retworkx
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.to_retworkx" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L167-L169" signature="to_retworkx()">
|
||
Returns the DAGDependency in retworkx format.
|
||
</Function>
|
||
|
||
### topological\_nodes
|
||
|
||
<Function id="qiskit.dagcircuit.DAGDependency.topological_nodes" github="https://github.com/Qiskit/qiskit/tree/stable/1.2/qiskit/dagcircuit/dagdependency.py#L369-L380" signature="topological_nodes()">
|
||
Yield nodes in topological order.
|
||
|
||
**Returns**
|
||
|
||
node in topological order.
|
||
|
||
**Return type**
|
||
|
||
generator([DAGNode](qiskit.dagcircuit.DAGNode "qiskit.dagcircuit.DAGNode"))
|
||
</Function>
|
||
</Class>
|
||
|