Skip to content

DAG -- DAGNode, DAGQCircuit

量子线路的有向无环图(DAG, Directed Acyclic Graph)表示的 API 参考。DAG 结构通过将门依赖关系表示为图来实现线路分析、优化和转译。

DAGNode

DAG 中表示单个量子门的节点,具有到其前驱和后继节点的链接。

签名

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

参数

参数类型说明
gateQGate与此节点关联的量子门。
indexint此节点在 DAG 中的索引。

方法

DAGNode.get_qgate

python
DAGNode.get_qgate() -> QGate

返回与此节点关联的量子门。

DAGNode.get_index

python
DAGNode.get_index() -> int

返回此节点在 DAG 中的索引。

DAGNode.get_pre_nodes

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

返回前驱节点列表(必须在此门之前执行的门)。

DAGNode.get_post_nodes

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

返回后继节点列表(依赖于此门的门)。

DAGNode.add_pre_node

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

添加一个前驱节点。

参数类型说明
nodeDAGNode要添加为前驱的节点。

DAGNode.add_post_node

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

添加一个后继节点。

参数类型说明
nodeDAGNode要添加为后继的节点。

DAGNode.remove_pre_node

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

按引用或索引移除一个前驱节点。

参数类型说明
nodeDAGNode要移除的前驱节点。
node_indexint要移除的前驱节点的索引。

DAGNode.remove_post_node

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

按引用或索引移除一个后继节点。

参数类型说明
nodeDAGNode要移除的后继节点。
node_indexint要移除的后继节点的索引。

DAGNode.remove_edges

python
DAGNode.remove_edges() -> None

移除此节点的所有边(前驱和后继连接)。


DAGQCircuit

量子线路的有向无环图表示。每个顶点对应一个门,边编码量子比特依赖顺序。

签名

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

构造函数

构造函数说明
DAGQCircuit()创建空 DAG 线路。
DAGQCircuit(other)拷贝构造函数。
DAGQCircuit(circuit)QCircuit 构造 DAG。

顶点操作

DAGQCircuit.add_vertex

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

将量子门作为新顶点添加到 DAG。

参数类型说明
gateQGate要添加的量子门。

返回: 添加的顶点的索引。

DAGQCircuit.add_vertexs

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

将多个量子门作为顶点添加。

参数类型说明
gateslist[QGate]要添加的量子门向量。
is_remove_barrierbool如果为 True,跳过屏障门。默认为 False

返回: 添加的顶点的索引列表。

DAGQCircuit.remove_vertex

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

从 DAG 中移除一个顶点。

参数类型说明
nodeDAGNode要移除的节点。
node_indexint要移除的顶点的索引。

DAGQCircuit.get_vertex

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

返回给定索引处的顶点。

参数类型说明
node_indexint顶点的索引。

返回: 指定索引处的 DAGNode

DAGQCircuit.get_vertex_list

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

返回 DAG 中的完整顶点列表。

DAGQCircuit.get_vertex_vec

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

返回用于索引访问的顶点指针向量。


边操作

DAGQCircuit.add_edge

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

在两个节点之间添加有向边,建立依赖关系。

参数类型说明
src_node / src_indexDAGNodeint源(前驱)节点。
target_node / target_indexDAGNodeint目标(后继)节点。

DAGQCircuit.remove_edge

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

移除两个节点之间的有向边。

参数类型说明
src_node / src_indexDAGNodeint源节点。
target_node / target_indexDAGNodeint目标节点。

图查询

DAGQCircuit.in_edges

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

返回指定节点的入边。

DAGQCircuit.out_edges

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

返回指定节点的出边。

DAGQCircuit.in_neighbors

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

返回前驱节点的索引。

DAGQCircuit.out_neighbors

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

返回后继节点的索引。

DAGQCircuit.in_degree

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

返回指定节点的入边数量。

DAGQCircuit.out_degree

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

返回指定节点的出边数量。


线路转换

DAGQCircuit.from_circuit

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

QCircuit 填充 DAG。

参数类型说明
circuitQCircuit要转换的线路。
using_only_q2_gatebool如果为 True,仅包含双量子比特门。默认为 False

DAGQCircuit.from_qprog

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

QProg 填充 DAG。

参数类型说明
progQProg要转换的量子程序。
using_only_q2_gatebool如果为 True,仅包含双量子比特门。默认为 False
is_pilotboolPilot 转译模式标志。默认为 False

DAGQCircuit.to_qprog

python
DAGQCircuit.to_qprog() -> QProg

将 DAG 转换回 QProg

返回: 从 DAG 重建的 QProg

DAGQCircuit.to_circuit

python
DAGQCircuit.to_circuit() -> QCircuit

返回 DAG 的 QCircuit 表示。

返回: 对应 DAG 的 QCircuit


线路度量

DAGQCircuit.get_num_gates

python
DAGQCircuit.get_num_gates() -> int

返回 DAG 中的门总数。

DAGQCircuit.get_depth

python
DAGQCircuit.get_depth() -> int

返回线路的深度(最长路径)。

DAGQCircuit.get_gate

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

返回指定索引处的门。

参数类型说明
gate_indexint要获取的门的索引。

返回: 给定索引处的 QGate


图结构查询

DAGQCircuit.layers

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

返回线路的门层。每个内部列表包含可并行执行的节点索引。

DAGQCircuit.gate_list

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

返回 DAG 中所有门的扁平列表。

DAGQCircuit.gates

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

返回图中所有门的列表。

DAGQCircuit.nodes

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

返回图中所有节点索引的列表。

DAGQCircuit.edges

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

返回所有边作为 (源, 目标) 索引对的列表。

DAGQCircuit.two_qubit_gates

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

仅返回线路中的双量子比特门。

DAGQCircuit.two_qubit_gate_nodes

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

返回与双量子比特门关联的节点索引。

DAGQCircuit.get_initial_front_layer_gates

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

返回初始前沿层的门(没有前驱、可以立即执行的门)。

DAGQCircuit.longest_path

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

返回 DAG 中最长路径上的节点索引。


变更操作

DAGQCircuit.append

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

向 DAG 追加一个门。

DAGQCircuit.insert

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

在指定索引处插入门。

参数类型说明
indexint插入位置。
gateslist[QGate]要插入的门。

DAGQCircuit.build

python
DAGQCircuit.build() -> None

构建 DAG 结构,重新建立所有依赖边。

DAGQCircuit.clear

python
DAGQCircuit.clear() -> None

移除 DAG 中的所有顶点和边。

DAGQCircuit.reallocate_index

python
DAGQCircuit.reallocate_index() -> None

为 DAG 中的所有节点重新分配顺序索引。

DAGQCircuit.remove_back

python
DAGQCircuit.remove_back() -> None

移除 DAG 中的最后一个节点。

DAGQCircuit.get_back_node

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

返回 DAG 中最后一个节点使用的量子比特索引。

DAGQCircuit.reserve_size

python
DAGQCircuit.reserve_size() -> None

为大量节点预分配内存。适用于构建非常大的 DAG。


运算符

DAGQCircuit.lshift

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

使用 << 运算符追加一个门。

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

示例

从线路构造 DAG

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

检查层和依赖关系

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}")

# 检查特定节点的邻居
predecessors = dag.in_neighbors(2)
successors = dag.out_neighbors(2)
print(f"Node 2 predecessors: {predecessors}")
print(f"Node 2 successors: {successors}")

增量构建 DAG 并转换回线路

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

# 转换回线路
circ = dag.to_circuit()
print("Circuit depth:", circ.depth())

分析双量子比特门

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]}")

另见

Released under the MIT License.