Skip to content

DAG -- DAGNode, DAGQCircuit

API reference for Directed Acyclic Graph (DAG) representations of quantum circuits. The DAG structure enables circuit analysis, optimization, and transpilation by representing gate dependencies as a graph.

DAGNode

A node in the DAG representing a single quantum gate, with links to its predecessor and successor nodes.

Signature

python
class DAGNode:
    def __init__(gate: QGate, index: int) -> None

Parameters

ParameterTypeDescription
gateQGateThe quantum gate associated with this node.
indexintThe index of this node in the DAG.

Methods

DAGNode.get_qgate

python
DAGNode.get_qgate() -> QGate

Returns the quantum gate associated with this node.

DAGNode.get_index

python
DAGNode.get_index() -> int

Returns the index of this node in the DAG.

DAGNode.get_pre_nodes

python
DAGNode.get_pre_nodes() -> list[DAGNode]

Returns the list of predecessor nodes (gates that must execute before this one).

DAGNode.get_post_nodes

python
DAGNode.get_post_nodes() -> list[DAGNode]

Returns the list of successor nodes (gates that depend on this one).

DAGNode.add_pre_node

python
DAGNode.add_pre_node(node: DAGNode) -> None

Adds a predecessor node.

ParameterTypeDescription
nodeDAGNodeThe node to add as a predecessor.

DAGNode.add_post_node

python
DAGNode.add_post_node(node: DAGNode) -> None

Adds a successor node.

ParameterTypeDescription
nodeDAGNodeThe node to add as a successor.

DAGNode.remove_pre_node

python
DAGNode.remove_pre_node(node: DAGNode) -> None
DAGNode.remove_pre_node(node_index: int) -> None

Removes a predecessor node, either by reference or by index.

ParameterTypeDescription
nodeDAGNodeThe predecessor node to remove.
node_indexintThe index of the predecessor node to remove.

DAGNode.remove_post_node

python
DAGNode.remove_post_node(node: DAGNode) -> None
DAGNode.remove_post_node(node_index: int) -> None

Removes a successor node, either by reference or by index.

ParameterTypeDescription
nodeDAGNodeThe successor node to remove.
node_indexintThe index of the successor node to remove.

DAGNode.remove_edges

python
DAGNode.remove_edges() -> None

Removes all edges (predecessor and successor connections) from this node.


DAGQCircuit

A directed acyclic graph representation of a quantum circuit. Each vertex corresponds to a gate, and edges encode qubit dependency ordering.

Signature

python
class DAGQCircuit:
    def __init__() -> None
    def __init__(other: DAGQCircuit) -> None
    def __init__(circuit: QCircuit) -> None

Constructors

ConstructorDescription
DAGQCircuit()Creates an empty DAG circuit.
DAGQCircuit(other)Copy constructor.
DAGQCircuit(circuit)Constructs a DAG from a QCircuit.

Vertex Operations

DAGQCircuit.add_vertex

python
DAGQCircuit.add_vertex(gate: QGate) -> int

Adds a quantum gate as a new vertex in the DAG.

ParameterTypeDescription
gateQGateThe quantum gate to add.

Returns: The index of the added vertex.

DAGQCircuit.add_vertexs

python
DAGQCircuit.add_vertexs(gates: list[QGate], is_remove_barrier: bool = False) -> list[int]

Adds multiple quantum gates as vertices.

ParameterTypeDescription
gateslist[QGate]Vector of quantum gates to add.
is_remove_barrierboolIf True, skip barrier gates. Defaults to False.

Returns: A list of indices for the added vertices.

DAGQCircuit.remove_vertex

python
DAGQCircuit.remove_vertex(node: DAGNode) -> None
DAGQCircuit.remove_vertex(node_index: int) -> None

Removes a vertex from the DAG.

ParameterTypeDescription
nodeDAGNodeThe node to remove.
node_indexintThe index of the vertex to remove.

DAGQCircuit.get_vertex

python
DAGQCircuit.get_vertex(node_index: int) -> DAGNode

Returns the vertex at the given index.

ParameterTypeDescription
node_indexintIndex of the vertex.

Returns: The DAGNode at the specified index.

DAGQCircuit.get_vertex_list

python
DAGQCircuit.get_vertex_list() -> list[DAGNode]

Returns the full list of vertices in the DAG.

DAGQCircuit.get_vertex_vec

python
DAGQCircuit.get_vertex_vec() -> list[DAGNode]

Returns a vector of vertex pointers for indexed access.


Edge Operations

DAGQCircuit.add_edge

python
DAGQCircuit.add_edge(src_node: DAGNode, target_node: DAGNode) -> None
DAGQCircuit.add_edge(src_index: int, target_index: int) -> None

Adds a directed edge between two nodes, establishing a dependency.

ParameterTypeDescription
src_node / src_indexDAGNode or intThe source (predecessor) node.
target_node / target_indexDAGNode or intThe target (successor) node.

DAGQCircuit.remove_edge

python
DAGQCircuit.remove_edge(src_node: DAGNode, target_node: DAGNode) -> None
DAGQCircuit.remove_edge(src_index: int, target_index: int) -> None

Removes a directed edge between two nodes.

ParameterTypeDescription
src_node / src_indexDAGNode or intThe source node.
target_node / target_indexDAGNode or intThe target node.

Graph Queries

DAGQCircuit.in_edges

python
DAGQCircuit.in_edges(node_index: int) -> list[list[int]]

Returns the incoming edges for the specified node.

DAGQCircuit.out_edges

python
DAGQCircuit.out_edges(node_index: int) -> list[list[int]]

Returns the outgoing edges for the specified node.

DAGQCircuit.in_neighbors

python
DAGQCircuit.in_neighbors(node_index: int) -> list[int]

Returns the indices of predecessor nodes.

DAGQCircuit.out_neighbors

python
DAGQCircuit.out_neighbors(node_index: int) -> list[int]

Returns the indices of successor nodes.

DAGQCircuit.in_degree

python
DAGQCircuit.in_degree(node_index: int) -> int

Returns the number of incoming edges for the specified node.

DAGQCircuit.out_degree

python
DAGQCircuit.out_degree(node_index: int) -> int

Returns the number of outgoing edges for the specified node.


Circuit Conversion

DAGQCircuit.from_circuit

python
DAGQCircuit.from_circuit(circuit: QCircuit, using_only_q2_gate: bool = False) -> None

Populates the DAG from a QCircuit.

ParameterTypeDescription
circuitQCircuitThe circuit to convert.
using_only_q2_gateboolIf True, only include two-qubit gates. Defaults to False.

DAGQCircuit.from_qprog

python
DAGQCircuit.from_qprog(prog: QProg, using_only_q2_gate: bool = False, is_pilot: bool = False) -> None

Populates the DAG from a QProg.

ParameterTypeDescription
progQProgThe quantum program to convert.
using_only_q2_gateboolIf True, only include two-qubit gates. Defaults to False.
is_pilotboolFlag for Pilot transpile mode. Defaults to False.

DAGQCircuit.to_qprog

python
DAGQCircuit.to_qprog() -> QProg

Converts the DAG back to a QProg.

Returns: A QProg reconstructed from the DAG.

DAGQCircuit.to_circuit

python
DAGQCircuit.to_circuit() -> QCircuit

Returns the QCircuit representation of the DAG.

Returns: A QCircuit corresponding to the DAG.


Circuit Metrics

DAGQCircuit.get_num_gates

python
DAGQCircuit.get_num_gates() -> int

Returns the total number of gates in the DAG.

DAGQCircuit.get_depth

python
DAGQCircuit.get_depth() -> int

Returns the depth (longest path) of the circuit.

DAGQCircuit.get_gate

python
DAGQCircuit.get_gate(gate_index: int) -> QGate

Returns the gate at the specified index.

ParameterTypeDescription
gate_indexintIndex of the gate to retrieve.

Returns: The QGate at the given index.


Graph Structure Queries

DAGQCircuit.layers

python
DAGQCircuit.layers() -> list[list[int]]

Returns the gate layers of the circuit. Each inner list contains node indices that can execute in parallel.

DAGQCircuit.gate_list

python
DAGQCircuit.gate_list() -> list[QGate]

Returns a flat list of all gates in the DAG.

DAGQCircuit.gates

python
DAGQCircuit.gates() -> list[QGate]

Returns the list of all gates in the graph.

DAGQCircuit.nodes

python
DAGQCircuit.nodes() -> list[int]

Returns a list of all node indices in the graph.

DAGQCircuit.edges

python
DAGQCircuit.edges() -> list[tuple[int, int]]

Returns a list of all edges as (source, target) index pairs.

DAGQCircuit.two_qubit_gates

python
DAGQCircuit.two_qubit_gates() -> list[QGate]

Returns only the two-qubit gates in the circuit.

DAGQCircuit.two_qubit_gate_nodes

python
DAGQCircuit.two_qubit_gate_nodes() -> list[int]

Returns the node indices associated with two-qubit gates.

DAGQCircuit.get_initial_front_layer_gates

python
DAGQCircuit.get_initial_front_layer_gates() -> list[QGate]

Returns the gates in the initial front layer (gates with no predecessors that can execute immediately).

DAGQCircuit.longest_path

python
DAGQCircuit.longest_path() -> list[int]

Returns the node indices along the longest path in the DAG.


Mutation Operations

DAGQCircuit.append

python
DAGQCircuit.append(gate: QGate) -> None

Appends a gate to the DAG.

DAGQCircuit.insert

python
DAGQCircuit.insert(index: int, gates: list[QGate]) -> None

Inserts gates at the specified index.

ParameterTypeDescription
indexintThe position at which to insert.
gateslist[QGate]The gates to insert.

DAGQCircuit.build

python
DAGQCircuit.build() -> None

Builds the DAG structure, re-establishing all dependency edges.

DAGQCircuit.clear

python
DAGQCircuit.clear() -> None

Removes all vertices and edges from the DAG.

DAGQCircuit.reallocate_index

python
DAGQCircuit.reallocate_index() -> None

Reassigns sequential indices to all nodes in the DAG.

DAGQCircuit.remove_back

python
DAGQCircuit.remove_back() -> None

Removes the last node in the DAG.

DAGQCircuit.get_back_node

python
DAGQCircuit.get_back_node() -> list[int]

Returns the qubit indices used by the last node in the DAG.

DAGQCircuit.reserve_size

python
DAGQCircuit.reserve_size() -> None

Pre-allocates memory for a large number of nodes. Useful before building very large DAGs.


Operator

DAGQCircuit.lshift

python
DAGQCircuit.__lshift__(gate: QGate) -> DAGQCircuit

Appends a gate using the << operator.

python
dag << H(0) << CNOT(0, 1)

Examples

Construct a DAG from a circuit

python
from pyqpanda3.core import QCircuit, DAGQCircuit, H, CNOT, X

circ = QCircuit()
circ << H(0) << CNOT(0, 1) << X(2)

dag = DAGQCircuit(circ)
print("Gates:", dag.get_num_gates())
print("Depth:", dag.get_depth())

Inspect layers and dependencies

python
from pyqpanda3.core import QCircuit, DAGQCircuit, H, CNOT, X

circ = QCircuit()
circ << H(0) << H(1) << CNOT(0, 1) << X(2)

dag = DAGQCircuit(circ)
layers = dag.layers()
for i, layer in enumerate(layers):
    print(f"Layer {i}: nodes {layer}")

# Check neighbors of a specific node
predecessors = dag.in_neighbors(2)
successors = dag.out_neighbors(2)
print(f"Node 2 predecessors: {predecessors}")
print(f"Node 2 successors: {successors}")

Build a DAG incrementally and convert back

python
from pyqpanda3.core import DAGQCircuit, H, CNOT, RX

dag = DAGQCircuit()
dag << H(0)
dag << RX(1, 1.57)
dag << CNOT(0, 1)
dag.build()

# Convert back to a circuit
circ = dag.to_circuit()
print("Circuit depth:", circ.depth())

Analyze two-qubit gates

python
from pyqpanda3.core import QCircuit, DAGQCircuit, H, CNOT, CZ, X

circ = QCircuit()
circ << H(0) << CNOT(0, 1) << X(1) << CZ(1, 2)

dag = DAGQCircuit(circ)
two_q = dag.two_qubit_gates()
print(f"Number of 2-qubit gates: {len(two_q)}")

front = dag.get_initial_front_layer_gates()
print(f"Front layer gates: {[g.name() for g in front]}")

See Also

Released under the MIT License.