# qiskit.dagcircuit.DAGCircuit¶

class DAGCircuit[소스]

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.

Create an empty circuit.

__init__()[소스]

Create an empty circuit.

Methods

 Create an empty circuit. add_calibration(gate, qubits, schedule[, params]) Register a low-level, custom pulse definition for the given gate. add_clbits(clbits) Add individual clbit wires. add_creg(creg) Add all wires in a classical register. add_qreg(qreg) Add all wires in a quantum register. add_qubits(qubits) Add individual qubit wires. ancestors(node) Returns set of the ancestors of a node as DAGNodes. apply_operation_back(op[, qargs, cargs, …]) Apply an operation to the output of the circuit. apply_operation_front(op, qargs, cargs[, …]) Apply an operation to the input of the circuit. bfs_successors(node) Returns an iterator of tuples of (DAGNode, [DAGNodes]) where the DAGNode is the current node and [DAGNode] is its successors in BFS order. Return a set of non-conditional runs of 1q “op” nodes. collect_runs(namelist) Return a set of non-conditional runs of “op” nodes with the given names. compose(other[, edge_map, qubits, clbits, …]) Compose the other circuit onto the output of this circuit. compose_back(input_circuit[, edge_map]) DEPRECATED: use DAGCircuit.compose() instead. Count the occurrences of operation names. Count the occurrences of operation names on the longest path. Return the circuit depth. descendants(node) Returns set of the descendants of a node as DAGNodes. draw([scale, filename, style]) Draws the dag circuit. edges([nodes]) Iterator for edge values and source and dest node extend_back(dag[, edge_map]) DEPRECATED: Add dag at the end of self, using edge_map. from_networkx(graph) Take a networkx MultiDigraph and create a new DAGCircuit. Return a list of op nodes in the first layer of this dag. Get the list of gate nodes in the dag. Return True if the dag has a calibration defined for the node operation. idle_wires([ignore]) Return idle wires. is_predecessor(node, node_pred) Checks if a second node is in the predecessors of node. is_successor(node, node_succ) Checks if a second node is in the successors of node. Yield a shallow view on a layer of this DAGCircuit for all d layers of this circuit. Returns the longest path in the dag as a list of DAGNodes. Get list of 3+ qubit operations. Yield layers of the multigraph. named_nodes(*names) Get the set of “op” nodes with the given name. node(node_id) Get the node in the dag. Iterator for node values. nodes_on_wire(wire[, only_ops]) Iterator for nodes that affect a given wire. Return the total number of classical bits used by the circuit. Return the total number of qubits used by the circuit. Compute how many components the circuit can decompose into. op_nodes([op, include_directives]) Get the list of “op” nodes in the dag. predecessors(node) Returns iterator of the predecessors of a node as DAGNodes. Return a dictionary of circuit properties. Returns iterator of the predecessors of a node that are connected by a quantum edge as DAGNodes. Returns iterator of the successors of a node that are connected by a quantum edge as DAGNodes. remove_all_ops_named(opname) Remove all operation nodes with the given name. Remove all of the ancestor operation nodes of node. Remove all of the descendant operation nodes of node. Remove all of the non-ancestors operation nodes of node. Remove all of the non-descendants operation nodes of node. remove_op_node(node) Remove an operation node n. Reverse the operations in the self circuit. Yield a layer for all gates of this circuit. Return the number of operations. substitute_node(node, op[, inplace]) Replace a DAGNode with a single instruction. substitute_node_with_dag(node, input_dag[, …]) Replace one node with dag. successors(node) Returns iterator of the successors of a node as DAGNodes. Get list of 3-or-more-qubit gates: (id, data). Returns a copy of the DAGCircuit in networkx format. Yield nodes in topological order. Yield op nodes in topological order. Get list of 2-qubit gates. Get list of 2 qubit operations. Return the total number of qubits + clbits used by the circuit.

Attributes

 calibrations Return calibration dictionary. global_phase Return the global phase of the circuit. node_counter Returns the number of nodes in the dag. wires Return a list of the wires in order.
add_calibration(gate, qubits, schedule, params=None)[소스]

Register a low-level, custom pulse definition for the given gate.

매개변수
• gate (Union[Gate, str]) – Gate information.

• qubits (Union[int, Tuple[int]]) – List of qubits to be measured.

• schedule (Schedule) – Schedule information.

• params (Optional[List[Union[float, Parameter]]]) – A list of parameters.

예외

Exception – if the gate is of type string and params is None.

add_clbits(clbits)[소스]

add_creg(creg)[소스]

Add all wires in a classical register.

add_qreg(qreg)[소스]

Add all wires in a quantum register.

add_qubits(qubits)[소스]

ancestors(node)[소스]

Returns set of the ancestors of a node as DAGNodes.

apply_operation_back(op, qargs=None, cargs=None, condition=None)[소스]

Apply an operation to the output of the circuit.

매개변수
• op (qiskit.circuit.Instruction) – the operation associated with the DAG node

• qargs (list[Qubit]) – qubits that op will be applied to

• cargs (list[Clbit]) – cbits that op will be applied to

• condition (tuple or None) – DEPRECATED optional condition (ClassicalRegister, int)

반환값

the current max node

반환 형식

DAGNode

예외

DAGCircuitError – if a leaf node is connected to multiple outputs

apply_operation_front(op, qargs, cargs, condition=None)[소스]

Apply an operation to the input of the circuit.

매개변수
• op (qiskit.circuit.Instruction) – the operation associated with the DAG node

• qargs (list[Qubit]) – qubits that op will be applied to

• cargs (list[Clbit]) – cbits that op will be applied to

• condition (tuple or None) – DEPRECATED optional condition (ClassicalRegister, int)

반환값

the current max node

반환 형식

DAGNode

예외

DAGCircuitError – if initial nodes connected to multiple out edges

bfs_successors(node)[소스]

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

property calibrations

Return calibration dictionary.

The custom pulse definition of a given gate is of the form

{‘gate_name’: {(qubits, params): schedule}}

collect_1q_runs()[소스]

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

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(other, edge_map=None, qubits=None, clbits=None, front=False, inplace=True)[소스]

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.

매개변수
• other (DAGCircuit) – circuit to compose with self

• edge_map (dict) – DEPRECATED - a {Bit: Bit} map from input wires of other to output wires of self (i.e. rhs->lhs). The key, value pairs can be either Qubit or Clbit mappings.

• qubits (list[Qubit|int]) – qubits of self to compose onto.

• clbits (list[Clbit|int]) – clbits of self to compose onto.

• front (bool) – If True, front composition will be performed (not implemented yet)

• inplace (bool) – If True, modify the object. Otherwise return composed circuit.

반환값

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

반환 형식

DAGCircuit

예외

DAGCircuitError – if other is wider or there are duplicate edge mappings.

compose_back(input_circuit, edge_map=None)[소스]

count_ops()[소스]

Count the occurrences of operation names.

Returns a dictionary of counts keyed on the operation name.

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()[소스]

Return the circuit depth. :returns: the circuit depth :rtype: int

예외

DAGCircuitError – if not a directed acyclic graph

descendants(node)[소스]

Returns set of the descendants of a node as DAGNodes.

draw(scale=0.7, filename=None, style='color')[소스]

Draws the dag circuit.

This function needs pydot, which in turn needs Graphviz to be installed.

매개변수
• 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

반환값

if in Jupyter notebook and not saving to file, otherwise None.

반환 형식

Ipython.display.Image

edges(nodes=None)[소스]

Iterator for edge values and source and dest node

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

매개변수

nodes (DAGNode|list(DAGNode) – 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 in the same format as out_edges the tuple

(source node, destination node, edge data)

extend_back(dag, edge_map=None)[소스]

DEPRECATED: Add dag at the end of self, using edge_map.

classmethod from_networkx(graph)[소스]

Take a networkx MultiDigraph and create a new DAGCircuit.

매개변수

graph (networkx.MultiDiGraph) – The graph to create a DAGCircuit object from. The format of this MultiDiGraph format must be in the same format as returned by to_networkx.

반환값

The dagcircuit object created from the networkx

MultiDiGraph.

반환 형식

DAGCircuit

예외
front_layer()[소스]

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

gate_nodes()[소스]

Get the list of gate nodes in the dag.

반환값

the list of DAGNodes that represent gates.

반환 형식

list[DAGNode]

property global_phase

Return the global phase of the circuit.

has_calibration_for(node)[소스]

Return True if the dag has a calibration defined for the node operation. In this case, the operation does not need to be translated to the device basis.

idle_wires(ignore=None)[소스]

Return idle wires.

매개변수

ignore (list(str)) – List of node names to ignore. Default: []

Yields

Bit – Bit in idle wire.

is_predecessor(node, node_pred)[소스]

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

is_successor(node, node_succ)[소스]

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

layers()[소스]

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) DAGNodes. 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()[소스]

Returns the longest path in the dag as a list of DAGNodes.

multi_qubit_ops()[소스]

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

multigraph_layers()[소스]

Yield layers of the multigraph.

named_nodes(*names)[소스]

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

node(node_id)[소스]

Get the node in the dag.

매개변수

node_id (int) – Node identifier.

반환값

the node.

반환 형식

node

property node_counter

Returns the number of nodes in the dag.

nodes()[소스]

Iterator for node values.

Yields

node – the node.

nodes_on_wire(wire, only_ops=False)[소스]

Iterator for nodes that affect a given wire.

매개변수
• wire (Bit) – the wire to be looked at.

• only_ops (bool) – True if only the ops nodes are wanted; otherwise, all nodes are returned.

Yields

DAGNode – the successive ops on the given wire

예외

DAGCircuitError – if the given wire doesn’t exist in the DAG

num_clbits()[소스]

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

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()[소스]

Compute how many components the circuit can decompose into.

op_nodes(op=None, include_directives=True)[소스]

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

매개변수
반환값

the list of node ids containing the given op.

반환 형식

list[DAGNode]

predecessors(node)[소스]

Returns iterator of the predecessors of a node as DAGNodes.

properties()[소스]

Return a dictionary of circuit properties.

quantum_predecessors(node)[소스]

Returns iterator of the predecessors of a node that are connected by a quantum edge as DAGNodes.

quantum_successors(node)[소스]

Returns iterator of the successors of a node that are connected by a quantum edge as DAGNodes.

remove_all_ops_named(opname)[소스]

Remove all operation nodes with the given name.

remove_ancestors_of(node)[소스]

Remove all of the ancestor operation nodes of node.

remove_descendants_of(node)[소스]

Remove all of the descendant operation nodes of node.

remove_nonancestors_of(node)[소스]

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

remove_nondescendants_of(node)[소스]

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

remove_op_node(node)[소스]

Remove an operation node n.

Add edges from predecessors to successors.

reverse_ops()[소스]

Reverse the operations in the self circuit.

반환값

the reversed dag.

반환 형식

DAGCircuit

serial_layers()[소스]

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()[소스]

Return the number of operations.

substitute_node(node, op, inplace=False)[소스]

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

매개변수
반환값

the new node containing the added instruction.

반환 형식

DAGNode

예외
• DAGCircuitError – If replacement instruction was incompatible with

• location of target node.

substitute_node_with_dag(node, input_dag, wires=None)[소스]

Replace one node with dag.

매개변수
• node (DAGNode) – node to substitute

• input_dag (DAGCircuit) – circuit that will substitute the node

• wires (list[Bit]) – gives an order for (qu)bits in the input circuit. This order gets matched to the node wires by qargs first, then cargs, then conditions.

예외

DAGCircuitError – if met with unexpected predecessor/successors

successors(node)[소스]

Returns iterator of the successors of a node as DAGNodes.

threeQ_or_more_gates()[소스]

Get list of 3-or-more-qubit gates: (id, data).

to_networkx()[소스]

Returns a copy of the DAGCircuit in networkx format.

topological_nodes()[소스]

Yield nodes in topological order.

반환값

node in topological order

반환 형식

generator(DAGNode)

topological_op_nodes()[소스]

Yield op nodes in topological order.

반환값

op node in topological order

반환 형식

generator(DAGNode)

twoQ_gates()[소스]

Get list of 2-qubit gates. Ignore snapshot, barriers, and the like.

two_qubit_ops()[소스]

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

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().

property wires

Return a list of the wires in order.