mindquantum.algorithm.compiler.GateReplacer

View Source On Gitee
class mindquantum.algorithm.compiler.GateReplacer(ori_example_gate: BasicGate, wanted_example_circ: Circuit)[source]

Replace given gate with given circuit.

Parameters
  • ori_example_gate (BasicGate) – The gate you want to replace. Please note that every gate that belong to given gate together with same length of obj_qubits and ctrl_qubits will be matched.

  • wanted_example_circ (Circuit) – The quantum circuit you want.

Examples

>>> from mindquantum.algorithm.compiler import GateReplacer, compile_circuit
>>> from mindquantum.core.circuit import Circuit
>>> from mindquantum.core.gates import X
>>> circ = Circuit().x(1, 0).h(1).x(1, 2)
>>> circ
q0: ────■─────────────────

      ┏━┻━┓ ┏━━━┓ ┏━━━┓
q1: ──┨╺╋╸┠─┨ H ┠─┨╺╋╸┠───
      ┗━━━┛ ┗━━━┛ ┗━┳━┛

q2: ────────────────■─────
>>> equivalent_cnot = Circuit().h(0).z(0, 1).h(0)
>>> equivalent_cnot
      ┏━━━┓ ┏━━━┓ ┏━━━┓
q0: ──┨ H ┠─┨ Z ┠─┨ H ┠───
      ┗━━━┛ ┗━┳━┛ ┗━━━┛

q1: ──────────■───────────
>>> compiler = GateReplacer(X.on(0, 1), equivalent_cnot)
>>> compiler
GateReplacer<
        ┏━━━┓
  q0: ──┨╺╋╸┠───
        ┗━┳━┛

  q1: ────■─────
   ->
        ┏━━━┓ ┏━━━┓ ┏━━━┓
  q0: ──┨ H ┠─┨ Z ┠─┨ H ┠───
        ┗━━━┛ ┗━┳━┛ ┗━━━┛

  q1: ──────────■───────────
>
>>> compile_circuit(compiler, circ)
q0: ──────────■───────────────────────────────────

      ┏━━━┓ ┏━┻━┓ ┏━━━┓ ┏━━━┓ ┏━━━┓ ┏━━━┓ ┏━━━┓
q1: ──┨ H ┠─┨ Z ┠─┨ H ┠─┨ H ┠─┨ H ┠─┨ Z ┠─┨ H ┠───
      ┗━━━┛ ┗━━━┛ ┗━━━┛ ┗━━━┛ ┗━━━┛ ┗━┳━┛ ┗━━━┛

q2: ──────────────────────────────────■───────────
do(dag_circuit: DAGCircuit)[source]

Do gate replacer rule.

Parameters

dag_circuit (DAGCircuit) – The DAG of quantum circuit you want to compile.