mindquantum.algorithm.compiler.DAGCircuit
- class mindquantum.algorithm.compiler.DAGCircuit(circuit: Circuit)[source]
A Directed Acyclic Graph of a quantum circuit.
- Parameters
circuit (
Circuit
) – the input quantum circuit.
Examples
>>> from mindquantum.algorithm.compiler import DAGCircuit >>> from mindquantum.core.circuit import Circuit >>> circ = Circuit().h(0).x(1, 0) >>> dag_circ = DAGCircuit(circ) >>> dag_circ.head_node[0] q0 >>> dag_circ.head_node[0].child {0: H(0)}
- append_node(node: DAGNode)[source]
Append a quantum gate node.
- Parameters
node (
DAGNode
) – the DAG node you want to append.
Examples
>>> from mindquantum.algorithm.compiler import DAGCircuit, GateNode >>> from mindquantum.core.circuit import Circuit >>> import mindquantum.core.gates as G >>> circ = Circuit().h(0).x(1, 0) >>> circ ┏━━━┓ q0: ──┨ H ┠───■───── ┗━━━┛ ┃ ┏━┻━┓ q1: ────────┨╺╋╸┠─── ┗━━━┛ >>> dag_circ = DAGCircuit(circ) >>> node = GateNode(G.RX('a').on(0, 2)) >>> dag_circ.append_node(node) >>> dag_circ.to_circuit() ┏━━━┓ ┏━━━━━━━┓ q0: ──┨ H ┠───■───┨ RX(a) ┠─── ┗━━━┛ ┃ ┗━━━┳━━━┛ ┏━┻━┓ ┃ q1: ────────┨╺╋╸┠─────╂─────── ┗━━━┛ ┃ ┃ q2: ──────────────────■───────
- depth()[source]
Return the depth of quantum circuit.
Examples
>>> from mindquantum.core.circuit import Circuit >>> from mindquantum.algorithm.compiler import DAGCircuit >>> circ = Circuit().h(0).h(1).x(1, 0) >>> circ ┏━━━┓ q0: ──┨ H ┠───■───── ┗━━━┛ ┃ ┏━━━┓ ┏━┻━┓ q1: ──┨ H ┠─┨╺╋╸┠─── ┗━━━┛ ┗━━━┛ >>> DAGCircuit(circ).depth() 2
- find_all_gate_node()[source]
Find all gate node in this
DAGCircuit
.- Returns
List[
GateNode
], a list of allGateNode
of thisDAGCircuit
.
Examples
>>> from mindquantum.algorithm.compiler import DAGCircuit >>> from mindquantum.core.circuit import Circuit >>> circ = Circuit().h(0).x(1, 0) >>> dag_circ = DAGCircuit(circ) >>> dag_circ.find_all_gate_node() [H(0), X(1 <-: 0)]
- layering()[source]
Layering the quantum circuit.
- Returns
List[
Circuit
], a list of layered quantum circuit.
Examples
>>> from mindquantum.algorithm.compiler import DAGCircuit >>> from mindquantum.utils import random_circuit >>> circ = random_circuit(3, 5, seed=42) >>> circ ┏━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━┓ q0: ──┨ ┠─╳─┨ RY(-6.1944) ┠─────────────────── ┃ ┃ ┃ ┗━━━━━━┳━━━━━━┛ ┃ Rxx(1.2171) ┃ ┃ ┃ ┏━━━━━━━━━━━━━┓ q1: ──┨ ┠─┃────────╂────────┨ ┠─── ┗━━━━━━━━━━━━━┛ ┃ ┃ ┃ ┃ ┏━━━━━━━━━━━━┓ ┃ ┃ ┃ Rzz(-0.552) ┃ q2: ──┨ PS(2.6147) ┠──╳────────■────────┨ ┠─── ┗━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━┛ >>> dag_circ = DAGCircuit(circ) >>> for idx, c in enumerate(dag_circ.layering()): ... print(f"layer {idx}:") ... print(c) layer 0: ┏━━━━━━━━━━━━━┓ q0: ──┨ ┠─── ┃ ┃ ┃ Rxx(1.2171) ┃ q1: ──┨ ┠─── ┗━━━━━━━━━━━━━┛ ┏━━━━━━━━━━━━┓ q2: ──┨ PS(2.6147) ┠──── ┗━━━━━━━━━━━━┛ layer 1: q0: ──╳─── ┃ ┃ q2: ──╳─── layer 2: ┏━━━━━━━━━━━━━┓ q0: ──┨ RY(-6.1944) ┠─── ┗━━━━━━┳━━━━━━┛ ┃ q2: ─────────■────────── layer 3: ┏━━━━━━━━━━━━━┓ q1: ──┨ ┠─── ┃ ┃ ┃ Rzz(-0.552) ┃ q2: ──┨ ┠─── ┗━━━━━━━━━━━━━┛
- static replace_node_with_dag_circuit(node: DAGNode, coming: 'DAGCircuit')[source]
Replace a node with a DAGCircuit.
- Parameters
node (
DAGNode
) – the original DAG node.coming (
DAGCircuit
) – the coming DAG circuit.
Examples
>>> from mindquantum.algorithm.compiler import DAGCircuit >>> from mindquantum.core.circuit import Circuit >>> circ = Circuit().x(1, 0) >>> circ q0: ────■───── ┃ ┏━┻━┓ q1: ──┨╺╋╸┠─── ┗━━━┛ >>> dag_circ = DAGCircuit(circ) >>> node = dag_circ.head_node[0].child[0] >>> node X(1 <-: 0) >>> sub_dag = DAGCircuit(Circuit().h(1).z(1, 0).h(1)) >>> DAGCircuit.replace_node_with_dag_circuit(node, sub_dag) >>> dag_circ.to_circuit() q0: ──────────■─────────── ┃ ┏━━━┓ ┏━┻━┓ ┏━━━┓ q1: ──┨ H ┠─┨ Z ┠─┨ H ┠─── ┗━━━┛ ┗━━━┛ ┗━━━┛
- to_circuit()[source]
Convert
DAGCircuit
to quantum circuit.- Returns
Circuit
, the quantum circuit of this DAG.
Examples
>>> from mindquantum.core.circuit import Circuit >>> from mindquantum.algorithm.compiler import DAGCircuit >>> circ = Circuit().h(0).h(1).x(1, 0) >>> circ ┏━━━┓ q0: ──┨ H ┠───■───── ┗━━━┛ ┃ ┏━━━┓ ┏━┻━┓ q1: ──┨ H ┠─┨╺╋╸┠─── ┗━━━┛ ┗━━━┛ >>> dag_circ = DAGCircuit(circ) >>> dag_circ.to_circuit() ┏━━━┓ q0: ──┨ H ┠───■───── ┗━━━┛ ┃ ┏━━━┓ ┏━┻━┓ q1: ──┨ H ┠─┨╺╋╸┠─── ┗━━━┛ ┗━━━┛