---
title: DAGCircuit (latest version)
description: API reference for qiskit.dagcircuit.DAGCircuit in the latest version of qiskit
source: https://quantum.cloud.ibm.com/docs/en/api/qiskit/qiskit.dagcircuit.DAGCircuit
---

# DAGCircuit

*class* `qiskit.dagcircuit.DAGCircuit`

Bases: [`object`](https://docs.python.org/3/library/functions.html#object)

Quantum circuit as a directed acyclic graph.

There are 3 types of nodes in the graph: inputs, outputs, and operations. The nodes are connected by directed edges that correspond to qubits and bits.

## Attributes

### clbits

Returns the current sequence of registered [`Clbit`](/docs/api/qiskit/circuit#qiskit.circuit.Clbit "qiskit.circuit.Clbit") instances as a list.

> **Warning**
>
> Do not modify this list yourself. It will invalidate the [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit") data structures.

**Returns**

The current sequence of registered clbits.

**Return type**

list([`Clbit`](/docs/api/qiskit/circuit#qiskit.circuit.Clbit "qiskit.circuit.Clbit"))

### cregs

Returns the dict containing the ClassicalRegisters in the circuit

### duration

Returns the total duration of the circuit, set by a scheduling transpiler pass. Its unit is specified by [`unit`](#qiskit.dagcircuit.DAGCircuit.unit "qiskit.dagcircuit.DAGCircuit.unit")

DEPRECATED since Qiskit 1.3.0 and will be removed in Qiskit 3.0.0

### global\_phase

Return the global phase of the circuit.

### input\_map

### metadata

Circuit metadata

### name

Circuit name. Generally, this corresponds to the name of the QuantumCircuit from which the DAG was generated.

### node\_counter

Returns the number of nodes in the dag.

### num\_captured\_stretches

Number of captured stretches tracked by the circuit.

### num\_captured\_vars

Number of captured classical variables tracked by the circuit.

### num\_declared\_stretches

Number of declared local stretches tracked by the circuit.

### num\_declared\_vars

Number of declared local classical variables tracked by the circuit.

### num\_input\_vars

Number of input classical variables tracked by the circuit.

### num\_stretches

Total number of stretches tracked by the circuit.

### num\_vars

Total number of classical variables tracked by the circuit.

### output\_map

### qregs

Returns the dict containing the QuantumRegisters in the circuit

### qubits

Returns the current sequence of registered [`Qubit`](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit") instances as a list.

> **Warning**
>
> Do not modify this list yourself. It will invalidate the [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit") data structures.

**Returns**

The current sequence of registered qubits.

**Return type**

list([`Qubit`](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit"))

### unit

Returns the unit that duration is specified in.

DEPRECATED since Qiskit 1.3.0 and will be removed in Qiskit 3.0.0

### wires

Return a list of the wires in order.

## Methods

### add\_captured\_stretch

`add_captured_stretch(stretch)`

Add a captured stretch to the circuit.

**Parameters**

**stretch** – the stretch to add.

### add\_captured\_var

`add_captured_var(var)`

Add a captured variable to the circuit.

**Parameters**

**var** – the variable to add.

### add\_clbits

`add_clbits(clbits)`

Add individual clbit wires.

### add\_creg

`add_creg(creg)`

Add all wires in a classical register.

### add\_declared\_stretch

`add_declared_stretch(stretch)`

Add a declared stretch to the circuit.

**Parameters**

**stretch** – the stretch to add.

### add\_declared\_var

`add_declared_var(var)`

Add a declared local variable to the circuit.

**Parameters**

**var** – the variable to add.

### add\_input\_var

`add_input_var(var)`

Add an input variable to the circuit.

**Parameters**

**var** – the variable to add.

### add\_qreg

`add_qreg(qreg)`

Add all wires in a quantum register.

### add\_qubits

`add_qubits(qubits)`

Add individual qubit wires.

### ancestors

`ancestors(node)`

Returns set of the ancestors of a node as [`DAGOpNode`](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")s and [`DAGInNode`](/docs/api/qiskit/qiskit.dagcircuit.DAGInNode "qiskit.dagcircuit.DAGInNode")s.

The ancestors are the set of all nodes that can reach the target node. Whereas the [`DAGCircuit.predecessors()`](#qiskit.dagcircuit.DAGCircuit.predecessors "qiskit.dagcircuit.DAGCircuit.predecessors") only contains the immediate predecessors, the ancestors recursively contain the predecessors of each predecessor.

### apply\_operation\_back

`apply_operation_back(op, qargs=None, cargs=None, *, check=True)`

Apply an operation to the output of the circuit.

**Parameters**

- **op** ([*qiskit.circuit.Operation*](/docs/api/qiskit/qiskit.circuit.Operation "qiskit.circuit.Operation")) – the operation associated with the DAG node
- **qargs** ([*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple)*\[*[*Qubit*](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")*]*) – qubits that op will be applied to
- **cargs** ([*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple)*\[*[*Clbit*](/docs/api/qiskit/circuit#qiskit.circuit.Clbit "qiskit.circuit.Clbit")*]*) – cbits that op will be applied to
- **check** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If `True` (default), this function will enforce that the [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit") data-structure invariants are maintained (all `qargs` are [`Qubit`](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")\ s, all are in the DAG, etc). If `False`, the caller *must* uphold these invariants itself, but the cost of several checks will be skipped. This is most useful when building a new DAG from a source of known-good nodes.

**Returns**

the node for the op that was added to the dag

**Return type**

[DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if a leaf node is connected to multiple outputs

### apply\_operation\_front

`apply_operation_front(op, qargs=None, cargs=None, *, check=True)`

Apply an operation to the input of the circuit.

**Parameters**

- **op** ([*qiskit.circuit.Operation*](/docs/api/qiskit/qiskit.circuit.Operation "qiskit.circuit.Operation")) – the operation associated with the DAG node
- **qargs** ([*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple)*\[*[*Qubit*](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")*]*) – qubits that op will be applied to
- **cargs** ([*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple)*\[*[*Clbit*](/docs/api/qiskit/circuit#qiskit.circuit.Clbit "qiskit.circuit.Clbit")*]*) – cbits that op will be applied to
- **check** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If `True` (default), this function will enforce that the [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit") data-structure invariants are maintained (all `qargs` are [`Qubit`](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")\ s, all are in the DAG, etc). If `False`, the caller *must* uphold these invariants itself, but the cost of several checks will be skipped. This is most useful when building a new DAG from a source of known-good nodes.

**Returns**

the node for the op that was added to the dag

**Return type**

[DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if initial nodes connected to multiple out edges

### bfs\_successors

`bfs_successors(node)`

Returns an iterator of tuples of `(DAGNode, [DAGNodes])` where the `DAGNode` is the current node and `[DAGNodes]` is a list of the successors in BFS order.

### classical\_predecessors

`classical_predecessors(node)`

Returns iterator of the predecessors of a node that are connected by a classical edge as DAGOpNodes and DAGInNodes.

### classical\_successors

`classical_successors(node)`

Returns iterator of the successors of a node that are connected by a classical edge as DAGOpNodes and DAGOutNodes.

### collect\_1q\_runs

`collect_1q_runs()`

Return a set of non-conditional runs of 1q “op” nodes.

### collect\_2q\_runs

`collect_2q_runs()`

Return a set of non-conditional runs of 2q “op” nodes.

### collect\_runs

`collect_runs(namelist)`

Return a set of non-conditional runs of “op” nodes with the given names.

For example, “… h q\[0]; cx q\[0],q\[1]; cx q\[0],q\[1]; h q\[1]; ..” would produce the tuple of cx nodes as an element of the set returned from a call to collect\_runs(\[“cx”]). If instead the cx nodes were “cx q\[0],q\[1]; cx q\[1],q\[0];”, the method would still return the pair in a tuple. The namelist can contain names that are not in the circuit’s basis.

Nodes must have only one successor to continue the run.

### compose

`compose(other, qubits=None, clbits=None, front=False, inplace=True, *, inline_captures=False)`

Compose the `other` circuit onto the output of this circuit.

A subset of input wires of `other` are mapped to a subset of output wires of this circuit.

`other` can be narrower or of equal width to `self`.

**Parameters**

- **other** ([*DAGCircuit*](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")) – circuit to compose with self
- **qubits** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)*\[*[*Qubit*](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")*|*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – qubits of self to compose onto.
- **clbits** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)*\[*[*Clbit*](/docs/api/qiskit/circuit#qiskit.circuit.Clbit "qiskit.circuit.Clbit")*|*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – clbits of self to compose onto.
- **front** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If True, front composition will be performed (not implemented yet)
- **inplace** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If True, modify the object. Otherwise return composed circuit.
- **inline\_captures** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If `True`, variables marked as “captures” in the `other` DAG will be inlined onto existing uses of those same variables in `self`. If `False`, all variables in `other` are required to be distinct from `self`, and they will be added to `self`.

**Returns**

the composed dag (returns None if inplace==True).

**Return type**

[DAGCircuit](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if `other` is wider or there are duplicate edge mappings.

### control\_flow\_op\_nodes

`control_flow_op_nodes()`

Get a list of “op” nodes in the dag that contain control flow instructions.

**Returns**

The list of dag nodes containing control flow ops.

**Return type**

[list](https://docs.python.org/3/library/stdtypes.html#list)\[[DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")]

### copy\_empty\_like

`copy_empty_like(*, vars_mode=Ellipsis)`

Return a copy of self with the same structure but empty.

**That structure includes:**

- name and other metadata
- global phase
- duration
- all the qubits and clbits, including the registers.

**Returns**

An empty copy of self.

**Return type**

[DAGCircuit](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")

### count\_ops

`count_ops(*, recurse=True)`

Count the occurrences of operation names.

**Parameters**

**recurse** – if `True` (default), then recurse into control-flow operations. In all cases, this counts only the number of times the operation appears in any possible block; both branches of if-elses are counted, and for- and while-loop blocks are only counted once.

**Returns**

a mapping of operation names to the number of times it appears.

**Return type**

Mapping\[[str](https://docs.python.org/3/library/stdtypes.html#str), [int](https://docs.python.org/3/library/functions.html#int)]

### count\_ops\_longest\_path

`count_ops_longest_path()`

Count the occurrences of operation names on the longest path.

Returns a dictionary of counts keyed on the operation name.

### depth

`depth(*, recurse=False)`

Return the circuit depth. If there is control flow present, this count may only be an estimate, as the complete control-flow path cannot be statically known.

**Parameters**

**recurse** – if `True`, then recurse into control-flow operations. For loops with known-length iterators are counted as if the loop had been manually unrolled (*i.e.* with each iteration of the loop body written out explicitly). If-else blocks take the longer case of the two branches. While loops are counted as if the loop body runs once only. Defaults to `False` and raises [`DAGCircuitError`](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") if any control flow is present, to avoid silently returning a nonsensical number.

**Returns**

the circuit depth

**Return type**

[int](https://docs.python.org/3/library/functions.html#int)

**Raises**

- [**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if not a directed acyclic graph
- [**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if unknown control flow is present in a recursive call, or any control flow is present in a non-recursive call.

### descendants

`descendants(node)`

Returns set of the descendants of a node as [`DAGOpNode`](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")s and [`DAGOutNode`](/docs/api/qiskit/qiskit.dagcircuit.DAGOutNode "qiskit.dagcircuit.DAGOutNode")s.

The descendants are the set of all nodes that can be reached from the target node. In comparison, [`DAGCircuit.successors()`](#qiskit.dagcircuit.DAGCircuit.successors "qiskit.dagcircuit.DAGCircuit.successors") is an iterator over the immediate successors, whereas this method contains all the successors’ successors.

### draw

`draw(scale=0.7, filename=None, style='color')`

Draws the dag circuit.

This function needs [Graphviz](https://www.graphviz.org/) to be installed. Graphviz is not a python package and can’t be pip installed (the `graphviz` package on PyPI is a Python interface library for Graphviz and does not actually install Graphviz). You can refer to [the Graphviz documentation](https://www.graphviz.org/download/) on how to install it.

> **Warning**
>
> This function will call the system Graphviz tool on a file involving user-controllable strings (such as gate labels or register names). It is recommended to only call this function on trusted input.

**Parameters**

- **scale** ([*float*](https://docs.python.org/3/library/functions.html#float)) – scaling factor
- **filename** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)) – file path to save image to (format inferred from name)
- **style** ([*str*](https://docs.python.org/3/library/stdtypes.html#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

### edges

`edges(nodes=None)`

Iterator for edge values with source and destination node.

This works by returning the outgoing edges from the specified nodes. If no nodes are specified all edges from the graph are returned.

**Parameters**

**nodes** ([*DAGOpNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")*,* [*DAGInNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGInNode "qiskit.dagcircuit.DAGInNode")*, or* [*DAGOutNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGOutNode "qiskit.dagcircuit.DAGOutNode")*|*[*list*](https://docs.python.org/3/library/stdtypes.html#list)*(*[*DAGOpNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")*,* [*DAGInNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGInNode "qiskit.dagcircuit.DAGInNode")*, or* [*DAGOutNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGOutNode "qiskit.dagcircuit.DAGOutNode")) – Either a list of nodes or a single input node. If none is specified, all edges are returned from the graph.

**Yields**

*edge* –

**the edge as a tuple with the format**

(source node, destination node, edge wire)

### find\_bit

`find_bit(bit)`

Finds locations in the circuit, by mapping the Qubit and Clbit to positional index BitLocations is defined as: BitLocations = namedtuple(“BitLocations”, (“index”, “registers”))

**Parameters**

**bit** ([*Bit*](/docs/api/qiskit/circuit#qiskit.circuit.Bit "qiskit.circuit.Bit")) – The bit to locate.

**Returns**

**A 2-tuple. The first element (`index`)**

contains the index at which the `Bit` can be found (in either [`qubits`](#qiskit.dagcircuit.DAGCircuit.qubits "qiskit.dagcircuit.DAGCircuit.qubits"), [`clbits`](#qiskit.dagcircuit.DAGCircuit.clbits "qiskit.dagcircuit.DAGCircuit.clbits"), depending on its type). The second element (`registers`) is a list of `(register, index)` pairs with an entry for each `Register` in the circuit which contains the `Bit` (and the index in the `Register` at which it can be found).

**Return type**

namedtuple(int, List\[Tuple(Register, int)])

**Raises:**

DAGCircuitError: If the supplied `Bit` was of an unknown type. DAGCircuitError: If the supplied `Bit` could not be found on the circuit.

### front\_layer

`front_layer()`

Return a list of op nodes in the first layer of this dag.

### gate\_nodes

`gate_nodes()`

Get the list of gate nodes in the dag.

**Returns**

the list of DAGOpNodes that represent gates.

**Return type**

[list](https://docs.python.org/3/library/stdtypes.html#list)\[[DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")]

### has\_identifier

`has_identifier(var)`

Is this identifier in the DAG?

**Parameters**

**var** – the identifier or name to check.

### has\_stretch

`has_stretch(stretch)`

Is this stretch in the DAG?

**Parameters**

**var** – the stretch or name to check.

### has\_var

`has_var(var)`

Is this realtime variable in the DAG?

**Parameters**

**var** – the variable or name to check.

### idle\_wires

`idle_wires(ignore=None)`

Return idle wires.

**Parameters**

**ignore** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)*(*[*str*](https://docs.python.org/3/library/stdtypes.html#str)*)*) – List of node names to ignore. Default: \[]

**Yields**

*Bit* – Bit in idle wire.

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – If the DAG is invalid

### is\_empty

`is_empty()`

Return true if there are no operation nodes in the graph.

### is\_predecessor

`is_predecessor(node, node_pred)`

Checks if a second node is in the predecessors of node.

### is\_successor

`is_successor(node, node_succ)`

Checks if a second node is in the successors of node.

### iter\_captured\_stretches

`iter_captured_stretches()`

Iterable over the captured stretches tracked by the circuit.

### iter\_captured\_vars

`iter_captured_vars()`

Iterable over the captured classical variables tracked by the circuit.

### iter\_captures

`iter_captures()`

Iterable over all captured identifiers tracked by the circuit.

### iter\_declared\_stretches

`iter_declared_stretches()`

Iterable over the declared stretches tracked by the circuit.

### iter\_declared\_vars

`iter_declared_vars()`

Iterable over the declared classical variables tracked by the circuit.

### iter\_input\_vars

`iter_input_vars()`

Iterable over the input classical variables tracked by the circuit.

### iter\_stretches

`iter_stretches()`

Iterable over all the stretches tracked by the circuit.

### iter\_vars

`iter_vars()`

Iterable over all the classical variables tracked by the circuit.

### layers

`layers(*, vars_mode=Ellipsis)`

Yield a shallow view on a layer of this DAGCircuit for all d layers of this circuit.

A layer is a circuit whose gates act on disjoint qubits, i.e., a layer has depth 1. The total number of layers equals the circuit depth d. The layers are indexed from 0 to d-1 with the earliest layer at index 0. The layers are constructed using a greedy algorithm. Each returned layer is a dict containing \{“graph”: circuit graph, “partition”: list of qubit lists}.

The returned layer contains new (but semantically equivalent) DAGOpNodes, DAGInNodes, and DAGOutNodes. These are not the same as nodes of the original dag, but are equivalent via DAGNode.semantic\_eq(node1, node2).

TODO: Gates that use the same cbits will end up in different layers as this is currently implemented. This may not be the desired behavior.

### longest\_path

`longest_path()`

Returns the longest path in the dag as a list of DAGOpNodes, DAGInNodes, and DAGOutNodes.

### make\_physical

`make_physical(num_qubits=None)`

Put `self` into the canonical physical form, with the given number of qubits.

This acts in place, and does not need to traverse the DAG. It is intended for use when the DAG is known to already represent a physical circuit, and we just need to assert that it is canonical physical form.

This erases any information about virtual qubits in the [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit"); if using this yourself, you may need to ensure you have created and stored a suitable [`Layout`](/docs/api/qiskit/qiskit.transpiler.Layout "qiskit.transpiler.Layout"). Effectively, this applies the “trivial” layout mapping virtual qubit 0 to physical qubit 0, and so on.

**Parameters**

**num\_qubits** – if given, the total number of physical qubits in the output; it must be at least as large as the number of qubits in the DAG. If not given, the number of qubits is unchanged.

### multi\_qubit\_ops

`multi_qubit_ops()`

Get list of 3+ qubit operations. Ignore directives like snapshot and barrier.

### multigraph\_layers

`multigraph_layers()`

Yield layers of the multigraph.

### named\_nodes

`named_nodes(*names)`

Get the set of “op” nodes with the given name.

### node

`node(node_id)`

Get the node in the dag.

**Parameters**

**node\_id** ([*int*](https://docs.python.org/3/library/functions.html#int)) – Node identifier.

**Returns**

the node.

**Return type**

node

### nodes

`nodes()`

Iterator for node values.

**Yields**

*node* – the node.

### nodes\_on\_wire

`nodes_on_wire(wire, only_ops=False)`

Iterator for nodes that affect a given wire.

**Parameters**

- **wire** ([*Bit*](/docs/api/qiskit/circuit#qiskit.circuit.Bit "qiskit.circuit.Bit")) – the wire to be looked at.
- **only\_ops** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – True if only the ops nodes are wanted; otherwise, all nodes are returned.

**Yields**

*Iterator* – the successive nodes on the given wire

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if the given wire doesn’t exist in the DAG

### num\_blocks

`num_blocks()`

Return the number of basic blocks in this circuit.

### num\_clbits

`num_clbits()`

Return the total number of classical bits used by the circuit.

### num\_ops

`num_ops()`

Get the number of op nodes in the DAG.

### num\_qubits

`num_qubits()`

Return the total number of qubits used by the circuit. num\_qubits() replaces former use of width(). DAGCircuit.width() now returns qubits + clbits for consistency with Circuit.width() \[qiskit-terra #2564].

### num\_tensor\_factors

`num_tensor_factors()`

Compute how many components the circuit can decompose into.

### op\_nodes

`op_nodes(op=None, include_directives=True)`

Get the list of “op” nodes in the dag.

**Parameters**

- **op** ([*Type*](/docs/api/qiskit/circuit_classical#qiskit.circuit.classical.types.Type "qiskit.circuit.classical.types.Type")) – [`qiskit.circuit.Operation`](/docs/api/qiskit/qiskit.circuit.Operation "qiskit.circuit.Operation") subclass op nodes to return. If None, return all op nodes.
- **include\_directives** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – include barrier, snapshot etc.

**Returns**

the list of dag nodes containing the given op.

**Return type**

[list](https://docs.python.org/3/library/stdtypes.html#list)\[[DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")]

### op\_predecessors

`op_predecessors(node)`

Returns the iterator of “op” predecessors of a node in the dag.

### op\_successors

`op_successors(node)`

Returns iterator of “op” successors of a node in the dag.

### predecessors

`predecessors(node)`

Returns iterator of the predecessors of a node as [`DAGOpNode`](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")s and [`DAGInNode`](/docs/api/qiskit/qiskit.dagcircuit.DAGInNode "qiskit.dagcircuit.DAGInNode")s.

### properties

`properties()`

Return a dictionary of circuit properties.

### quantum\_causal\_cone

`quantum_causal_cone(qubit)`

Returns causal cone of a qubit.

A qubit’s causal cone is the set of qubits that can influence the output of that qubit through interactions, whether through multi-qubit gates or operations. Knowing the causal cone of a qubit can be useful when debugging faulty circuits, as it can help identify which wire(s) may be causing the problem.

This method does not consider any classical data dependency in the `DAGCircuit`, classical bit wires are ignored for the purposes of building the causal cone.

**Parameters**

**qubit** ([*Qubit*](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")) – The output qubit for which we want to find the causal cone.

**Returns**

The set of qubits whose interactions affect `qubit`.

**Return type**

Set\[[*Qubit*](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")]

### quantum\_predecessors

`quantum_predecessors(node)`

Returns iterator of the predecessors of a node that are connected by a quantum edge as DAGOpNodes and DAGInNodes.

### quantum\_successors

`quantum_successors(node)`

Returns iterator of the successors of a node that are connected by a quantum edge as DAGOpNodes and DAGOutNodes.

### remove\_all\_ops\_named

`remove_all_ops_named(opname)`

Remove all operation nodes with the given name.

### remove\_ancestors\_of

`remove_ancestors_of(node)`

Remove all of the ancestor operation nodes of node.

### remove\_clbits

`remove_clbits(*clbits)`

Remove classical bits from the circuit. All bits MUST be idle. Any registers with references to at least one of the specified bits will also be removed.

> **Warning**
>
> This method is rather slow, since it must iterate over the entire DAG to fix-up bit indices.

**Parameters**

**clbits** (*List\[*[*Clbit*](/docs/api/qiskit/circuit#qiskit.circuit.Clbit "qiskit.circuit.Clbit")*]*) – The bits to remove.

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – a clbit is not a [`Clbit`](/docs/api/qiskit/circuit#qiskit.circuit.Clbit "qiskit.circuit.Clbit"), is not in the circuit, or is not idle.

### remove\_cregs

`remove_cregs(*cregs)`

Remove classical registers from the circuit, leaving underlying bits in place.

**Raises**

- [**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – a creg is not a ClassicalRegister, or is not in
- **the circuit.** –

### remove\_descendants\_of

`remove_descendants_of(node)`

Remove all of the descendant operation nodes of node.

### remove\_nonancestors\_of

`remove_nonancestors_of(node)`

Remove all of the non-ancestors operation nodes of node.

### remove\_nondescendants\_of

`remove_nondescendants_of(node)`

Remove all of the non-descendants operation nodes of node.

### remove\_op\_node

`remove_op_node(node)`

Remove an operation node n.

Add edges from predecessors to successors.

### remove\_qregs

`remove_qregs(*qregs)`

Remove quantum registers from the circuit, leaving underlying bits in place.

**Raises**

- [**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – a qreg is not a QuantumRegister, or is not in
- **the circuit.** –

### remove\_qubits

`remove_qubits(*qubits)`

Remove quantum bits from the circuit. All bits MUST be idle. Any registers with references to at least one of the specified bits will also be removed.

> **Warning**
>
> This method is rather slow, since it must iterate over the entire DAG to fix-up bit indices.

**Parameters**

**qubits** (*List\[*[*Qubit*](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")*]*) – The bits to remove.

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – a qubit is not a [`Qubit`](/docs/api/qiskit/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit"), is not in the circuit, or is not idle.

### replace\_block\_with\_op

`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 DAGOpNodes into a single operation. A typical example is a block of gates being consolidated into a single `UnitaryGate` representing the unitary matrix of the block.

**Parameters**

- **node\_block** (*List\[*[*DAGNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGNode "qiskit.dagcircuit.DAGNode")*]*) – A list of dag nodes that represents the node block to be replaced
- **op** ([*qiskit.circuit.Operation*](/docs/api/qiskit/qiskit.circuit.Operation "qiskit.circuit.Operation")) – The operation to replace the block with
- **wire\_pos\_map** (*Dict\[*[*Bit*](/docs/api/qiskit/circuit#qiskit.circuit.Bit "qiskit.circuit.Bit")*,* [*int*](https://docs.python.org/3/library/functions.html#int)*]*) – The dictionary mapping the bits to their positions in the output `qargs` or `cargs`. This is necessary to reconstruct the arg order over multiple gates in the combined single op node. If a [`Bit`](/docs/api/qiskit/circuit#qiskit.circuit.Bit "qiskit.circuit.Bit") is not in the dictionary, it will not be added to the args; this can be useful when dealing with control-flow operations that have inherent bits in their `condition` or `target` fields.
- **cycle\_check** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – 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 `DAGCircuit`) and will raise a `DAGCircuitError` 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**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if `cycle_check` is set to `True` and replacing the specified block introduces a cycle or if `node_block` is empty.

**Returns**

The op node that replaces the block.

**Return type**

[DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")

### reverse\_ops

`reverse_ops()`

Reverse the operations in the `self` circuit.

**Returns**

the reversed dag.

**Return type**

[DAGCircuit](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")

### separable\_circuits

`separable_circuits(remove_idle_qubits=False, *, vars_mode=Ellipsis)`

Decompose the circuit into sets of qubits with no gates connecting them.

**Parameters**

**remove\_idle\_qubits** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – Flag denoting whether to remove idle qubits from the separated circuits. If `False`, each output circuit will contain the same number of qubits as `self`.

**Returns**

**The circuits resulting from separating `self` into sets**

of disconnected qubits

**Return type**

List\[[DAGCircuit](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")]

Each [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit") instance returned by this method will contain the same number of clbits as `self`. The global phase information in `self` will not be maintained in the subcircuits returned by this method.

### serial\_layers

`serial_layers(*, vars_mode=Ellipsis)`

Yield a layer for all gates of this circuit.

A serial layer is a circuit with one gate. The layers have the same structure as in layers().

### size

`size(*, recurse=False)`

Return the number of operations. If there is control flow present, this count may only be an estimate, as the complete control-flow path cannot be statically known.

**Parameters**

**recurse** – if `True`, then recurse into control-flow operations. For loops with known-length iterators are counted unrolled. If-else blocks sum both of the two branches. While loops are counted as if the loop body runs once only. Defaults to `False` and raises [`DAGCircuitError`](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") if any control flow is present, to avoid silently returning a mostly meaningless number.

**Returns**

the circuit size

**Return type**

[int](https://docs.python.org/3/library/functions.html#int)

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if an unknown [`ControlFlowOp`](/docs/api/qiskit/qiskit.circuit.ControlFlowOp "qiskit.circuit.ControlFlowOp") is present in a call with `recurse=True`, or any control flow is present in a non-recursive call.

### structurally\_equal

`structurally_equal(other)`

Are these two DAGs structurally equal?

This function returns true iff the graph structures are precisely the same as each other, including the valid node indices, edge orders, and so on. This is a much stricter check than graph equivalence, and is mostly useful for testing if two [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit") instances have been constructed and manipulated in the exact same ways. For example, this method can be used to test whether a sequence of manipulations of a DAG is deterministic.

This method does not consider tracking metadata such as [`metadata`](#qiskit.dagcircuit.DAGCircuit.metadata "qiskit.dagcircuit.DAGCircuit.metadata") or [`name`](#qiskit.dagcircuit.DAGCircuit.name "qiskit.dagcircuit.DAGCircuit.name"), but does consider many low-level implementation details of the internal representation, many of which do not change the semantics of the circuit.

This method should, in general, be much faster than graph-equivalence checks, but will return `False` in many more situations. This method should never return `True` when a graph-equivalence check would return `False`.

> **Note**
>
> This currently does not handle control flow, because of technical limitations in the internal representation of control flow, and will return false if any control-flow operation is present, even if they are individually equal.

> **See also**
>
> **The `==` operator**
>
> [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit") implements `__eq__()` between itself and other [`DAGCircuit`](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit") instances (this same method also powers [`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit "qiskit.circuit.QuantumCircuit")’s equality check). This implements a semantic data-flow equality check, which is less sensitive to the order operations were defined. This is typically what a user cares about with respect to equality.

### substitute\_node

`substitute_node(node, op, inplace=False, propagate_condition=None)`

Replace a DAGOpNode with a single operation. qargs, cargs and conditions for the new operation will be inferred from the node to be replaced. The new operation will be checked to match the shape of the replaced operation.

**Parameters**

- **node** ([*DAGOpNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")) – Node to be replaced
- **op** ([*qiskit.circuit.Operation*](/docs/api/qiskit/qiskit.circuit.Operation "qiskit.circuit.Operation")) – The [`qiskit.circuit.Operation`](/docs/api/qiskit/qiskit.circuit.Operation "qiskit.circuit.Operation") instance to be added to the DAG
- **inplace** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – Optional, default False. If True, existing DAG node will be modified to include op. Otherwise, a new DAG node will be used.
- **propagate\_condition** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – DEPRECATED a legacy option that used to control the behavior of handling control flow. It has no effect anymore, left it for backwards compatibility. Will be removed in Qiskit 3.0.

**Returns**

the new node containing the added operation.

**Return type**

[DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")

**Raises**

- [**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – If replacement operation was incompatible with
- **location**\*\* of \*\***target node.** –

### substitute\_node\_with\_dag

`substitute_node_with_dag(node, input_dag, wires=None, propagate_condition=None)`

Replace one node with dag.

**Parameters**

- **node** ([*DAGOpNode*](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")) – node to substitute
- **input\_dag** ([*DAGCircuit*](#qiskit.dagcircuit.DAGCircuit "qiskit.dagcircuit.DAGCircuit")) – circuit that will substitute the node
- **wires** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)*\[*[*Bit*](/docs/api/qiskit/circuit#qiskit.circuit.Bit "qiskit.circuit.Bit")*] | Dict\[*[*Bit*](/docs/api/qiskit/circuit#qiskit.circuit.Bit "qiskit.circuit.Bit")*,* [*Bit*](/docs/api/qiskit/circuit#qiskit.circuit.Bit "qiskit.circuit.Bit")*]*) – gives an order for (qu)bits in the input circuit. If a list, then the bits refer to those in the `input_dag`, and the order gets matched to the node wires by qargs first, then cargs, then conditions. If a dictionary, then a mapping of bits in the `input_dag` to those that the `node` acts on.
- **propagate\_condition** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – DEPRECATED a legacy option that used to control the behavior of handling control flow. It has no effect anymore, left it for backwards compatibility. Will be removed in Qiskit 3.0.

**Returns**

maps node IDs from input\_dag to their new node incarnations in self.

**Return type**

[dict](https://docs.python.org/3/library/stdtypes.html#dict)

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if met with unexpected predecessor/successors

### successors

`successors(node)`

Returns iterator of the successors of a node as [`DAGOpNode`](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode")s and [`DAGOutNode`](/docs/api/qiskit/qiskit.dagcircuit.DAGOutNode "qiskit.dagcircuit.DAGOutNode")s.

### swap\_nodes

`swap_nodes(node1, node2)`

Swap connected nodes e.g. due to commutation.

**Parameters**

- **node1** (*OpNode*) – predecessor node
- **node2** (*OpNode*) – successor node

**Raises**

[**DAGCircuitError**](/docs/api/qiskit/dagcircuit#qiskit.dagcircuit.DAGCircuitError "qiskit.dagcircuit.DAGCircuitError") – if either node is not an OpNode or nodes are not connected

### to\_circuit

`to_circuit(*, copy_operations=True)`

Convert this DAG to a [`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit "qiskit.circuit.QuantumCircuit").

This is a simple wrapper around [`dag_to_circuit()`](/docs/api/qiskit/converters#qiskit.converters.dag_to_circuit "qiskit.converters.dag_to_circuit").

**Parameters**

**copy\_operations** – whether to deep copy the individual instructions. If set to `False`, the operation is cheaper but mutations to the instructions in the circuit will affect the original circuit.

**Returns**

a [`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit "qiskit.circuit.QuantumCircuit") representing this same DAG.

### topological\_nodes

`topological_nodes(key=None, reverse=False)`

Yield nodes in topological order.

**Parameters**

- **key** (*Callable*) – A callable which will take a DAGNode object and return a string sort key. If not specified the bit qargs and cargs of a node will be used for sorting.
- **reverse** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If True, yield nodes in reverse topological order.

**Returns**

node in topological order

**Return type**

generator([DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode"), [DAGInNode](/docs/api/qiskit/qiskit.dagcircuit.DAGInNode "qiskit.dagcircuit.DAGInNode"), or [DAGOutNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOutNode "qiskit.dagcircuit.DAGOutNode"))

### topological\_op\_nodes

`topological_op_nodes(key=None, reverse=False)`

Yield op nodes in topological order.

Allowed to pass in specific key to break ties in top order

**Parameters**

- **key** (*Callable*) – A callable which will take a DAGNode object and return a string sort key. If not specified the qargs and cargs of a node will be used for sorting.
- **reverse** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – If True, yield op nodes in reverse topological order.

**Returns**

op node in topological order

**Return type**

generator([DAGOpNode](/docs/api/qiskit/qiskit.dagcircuit.DAGOpNode "qiskit.dagcircuit.DAGOpNode"))

### two\_qubit\_ops

`two_qubit_ops()`

Get list of 2 qubit operations. Ignore directives like snapshot and barrier.

### width

`width()`

Return the total number of qubits + clbits used by the circuit. This function formerly returned the number of qubits by the calculation return len(self.\_wires) - self.num\_clbits() but was changed by issue #2564 to return number of qubits + clbits with the new function DAGCircuit.num\_qubits replacing the former semantic of DAGCircuit.width().
