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
class DAGNode:
def __init__(gate: QGate, index: int) -> NoneParameters
| Parameter | Type | Description |
|---|---|---|
| gate | QGate | The quantum gate associated with this node. |
| index | int | The index of this node in the DAG. |
Methods
DAGNode.get_qgate
DAGNode.get_qgate() -> QGateReturns the quantum gate associated with this node.
DAGNode.get_index
DAGNode.get_index() -> intReturns the index of this node in the DAG.
DAGNode.get_pre_nodes
DAGNode.get_pre_nodes() -> list[DAGNode]Returns the list of predecessor nodes (gates that must execute before this one).
DAGNode.get_post_nodes
DAGNode.get_post_nodes() -> list[DAGNode]Returns the list of successor nodes (gates that depend on this one).
DAGNode.add_pre_node
DAGNode.add_pre_node(node: DAGNode) -> NoneAdds a predecessor node.
| Parameter | Type | Description |
|---|---|---|
| node | DAGNode | The node to add as a predecessor. |
DAGNode.add_post_node
DAGNode.add_post_node(node: DAGNode) -> NoneAdds a successor node.
| Parameter | Type | Description |
|---|---|---|
| node | DAGNode | The node to add as a successor. |
DAGNode.remove_pre_node
DAGNode.remove_pre_node(node: DAGNode) -> None
DAGNode.remove_pre_node(node_index: int) -> NoneRemoves a predecessor node, either by reference or by index.
| Parameter | Type | Description |
|---|---|---|
| node | DAGNode | The predecessor node to remove. |
| node_index | int | The index of the predecessor node to remove. |
DAGNode.remove_post_node
DAGNode.remove_post_node(node: DAGNode) -> None
DAGNode.remove_post_node(node_index: int) -> NoneRemoves a successor node, either by reference or by index.
| Parameter | Type | Description |
|---|---|---|
| node | DAGNode | The successor node to remove. |
| node_index | int | The index of the successor node to remove. |
DAGNode.remove_edges
DAGNode.remove_edges() -> NoneRemoves 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
class DAGQCircuit:
def __init__() -> None
def __init__(other: DAGQCircuit) -> None
def __init__(circuit: QCircuit) -> NoneConstructors
| Constructor | Description |
|---|---|
DAGQCircuit() | Creates an empty DAG circuit. |
DAGQCircuit(other) | Copy constructor. |
DAGQCircuit(circuit) | Constructs a DAG from a QCircuit. |
Vertex Operations
DAGQCircuit.add_vertex
DAGQCircuit.add_vertex(gate: QGate) -> intAdds a quantum gate as a new vertex in the DAG.
| Parameter | Type | Description |
|---|---|---|
| gate | QGate | The quantum gate to add. |
Returns: The index of the added vertex.
DAGQCircuit.add_vertexs
DAGQCircuit.add_vertexs(gates: list[QGate], is_remove_barrier: bool = False) -> list[int]Adds multiple quantum gates as vertices.
| Parameter | Type | Description |
|---|---|---|
| gates | list[QGate] | Vector of quantum gates to add. |
| is_remove_barrier | bool | If True, skip barrier gates. Defaults to False. |
Returns: A list of indices for the added vertices.
DAGQCircuit.remove_vertex
DAGQCircuit.remove_vertex(node: DAGNode) -> None
DAGQCircuit.remove_vertex(node_index: int) -> NoneRemoves a vertex from the DAG.
| Parameter | Type | Description |
|---|---|---|
| node | DAGNode | The node to remove. |
| node_index | int | The index of the vertex to remove. |
DAGQCircuit.get_vertex
DAGQCircuit.get_vertex(node_index: int) -> DAGNodeReturns the vertex at the given index.
| Parameter | Type | Description |
|---|---|---|
| node_index | int | Index of the vertex. |
Returns: The DAGNode at the specified index.
DAGQCircuit.get_vertex_list
DAGQCircuit.get_vertex_list() -> list[DAGNode]Returns the full list of vertices in the DAG.
DAGQCircuit.get_vertex_vec
DAGQCircuit.get_vertex_vec() -> list[DAGNode]Returns a vector of vertex pointers for indexed access.
Edge Operations
DAGQCircuit.add_edge
DAGQCircuit.add_edge(src_node: DAGNode, target_node: DAGNode) -> None
DAGQCircuit.add_edge(src_index: int, target_index: int) -> NoneAdds a directed edge between two nodes, establishing a dependency.
| Parameter | Type | Description |
|---|---|---|
| src_node / src_index | DAGNode or int | The source (predecessor) node. |
| target_node / target_index | DAGNode or int | The target (successor) node. |
DAGQCircuit.remove_edge
DAGQCircuit.remove_edge(src_node: DAGNode, target_node: DAGNode) -> None
DAGQCircuit.remove_edge(src_index: int, target_index: int) -> NoneRemoves a directed edge between two nodes.
| Parameter | Type | Description |
|---|---|---|
| src_node / src_index | DAGNode or int | The source node. |
| target_node / target_index | DAGNode or int | The target node. |
Graph Queries
DAGQCircuit.in_edges
DAGQCircuit.in_edges(node_index: int) -> list[list[int]]Returns the incoming edges for the specified node.
DAGQCircuit.out_edges
DAGQCircuit.out_edges(node_index: int) -> list[list[int]]Returns the outgoing edges for the specified node.
DAGQCircuit.in_neighbors
DAGQCircuit.in_neighbors(node_index: int) -> list[int]Returns the indices of predecessor nodes.
DAGQCircuit.out_neighbors
DAGQCircuit.out_neighbors(node_index: int) -> list[int]Returns the indices of successor nodes.
DAGQCircuit.in_degree
DAGQCircuit.in_degree(node_index: int) -> intReturns the number of incoming edges for the specified node.
DAGQCircuit.out_degree
DAGQCircuit.out_degree(node_index: int) -> intReturns the number of outgoing edges for the specified node.
Circuit Conversion
DAGQCircuit.from_circuit
DAGQCircuit.from_circuit(circuit: QCircuit, using_only_q2_gate: bool = False) -> NonePopulates the DAG from a QCircuit.
| Parameter | Type | Description |
|---|---|---|
| circuit | QCircuit | The circuit to convert. |
| using_only_q2_gate | bool | If True, only include two-qubit gates. Defaults to False. |
DAGQCircuit.from_qprog
DAGQCircuit.from_qprog(prog: QProg, using_only_q2_gate: bool = False, is_pilot: bool = False) -> NonePopulates the DAG from a QProg.
| Parameter | Type | Description |
|---|---|---|
| prog | QProg | The quantum program to convert. |
| using_only_q2_gate | bool | If True, only include two-qubit gates. Defaults to False. |
| is_pilot | bool | Flag for Pilot transpile mode. Defaults to False. |
DAGQCircuit.to_qprog
DAGQCircuit.to_qprog() -> QProgConverts the DAG back to a QProg.
Returns: A QProg reconstructed from the DAG.
DAGQCircuit.to_circuit
DAGQCircuit.to_circuit() -> QCircuitReturns the QCircuit representation of the DAG.
Returns: A QCircuit corresponding to the DAG.
Circuit Metrics
DAGQCircuit.get_num_gates
DAGQCircuit.get_num_gates() -> intReturns the total number of gates in the DAG.
DAGQCircuit.get_depth
DAGQCircuit.get_depth() -> intReturns the depth (longest path) of the circuit.
DAGQCircuit.get_gate
DAGQCircuit.get_gate(gate_index: int) -> QGateReturns the gate at the specified index.
| Parameter | Type | Description |
|---|---|---|
| gate_index | int | Index of the gate to retrieve. |
Returns: The QGate at the given index.
Graph Structure Queries
DAGQCircuit.layers
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
DAGQCircuit.gate_list() -> list[QGate]Returns a flat list of all gates in the DAG.
DAGQCircuit.gates
DAGQCircuit.gates() -> list[QGate]Returns the list of all gates in the graph.
DAGQCircuit.nodes
DAGQCircuit.nodes() -> list[int]Returns a list of all node indices in the graph.
DAGQCircuit.edges
DAGQCircuit.edges() -> list[tuple[int, int]]Returns a list of all edges as (source, target) index pairs.
DAGQCircuit.two_qubit_gates
DAGQCircuit.two_qubit_gates() -> list[QGate]Returns only the two-qubit gates in the circuit.
DAGQCircuit.two_qubit_gate_nodes
DAGQCircuit.two_qubit_gate_nodes() -> list[int]Returns the node indices associated with two-qubit gates.
DAGQCircuit.get_initial_front_layer_gates
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
DAGQCircuit.longest_path() -> list[int]Returns the node indices along the longest path in the DAG.
Mutation Operations
DAGQCircuit.append
DAGQCircuit.append(gate: QGate) -> NoneAppends a gate to the DAG.
DAGQCircuit.insert
DAGQCircuit.insert(index: int, gates: list[QGate]) -> NoneInserts gates at the specified index.
| Parameter | Type | Description |
|---|---|---|
| index | int | The position at which to insert. |
| gates | list[QGate] | The gates to insert. |
DAGQCircuit.build
DAGQCircuit.build() -> NoneBuilds the DAG structure, re-establishing all dependency edges.
DAGQCircuit.clear
DAGQCircuit.clear() -> NoneRemoves all vertices and edges from the DAG.
DAGQCircuit.reallocate_index
DAGQCircuit.reallocate_index() -> NoneReassigns sequential indices to all nodes in the DAG.
DAGQCircuit.remove_back
DAGQCircuit.remove_back() -> NoneRemoves the last node in the DAG.
DAGQCircuit.get_back_node
DAGQCircuit.get_back_node() -> list[int]Returns the qubit indices used by the last node in the DAG.
DAGQCircuit.reserve_size
DAGQCircuit.reserve_size() -> NonePre-allocates memory for a large number of nodes. Useful before building very large DAGs.
Operator
DAGQCircuit.lshift
DAGQCircuit.__lshift__(gate: QGate) -> DAGQCircuitAppends a gate using the << operator.
dag << H(0) << CNOT(0, 1)Examples
Construct a DAG from a circuit
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
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
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
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]}")