1.7K Star 2.7K Fork 4.1K

GVPMindSpore/mindquantum

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
swap_related.py 3.93 KB
一键复制 编辑 原始数据 按行查看 历史
donghufeng 提交于 2023-12-24 22:33 . new text style for circuit
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""SWAP gate related decompose rule."""
from mindquantum.core import gates
from mindquantum.core.circuit import Circuit
from mindquantum.utils.type_value_check import _check_control_num, _check_input_type
def swap_decompose(gate: gates.SWAPGate):
"""
Decompose :class:`~.core.gates.SWAPGate` gate.
Args:
gate (:class:`~.core.gates.SWAPGate`): A :class:`~.core.gates.SWAPGate` gate.
Returns:
List[:class:`~.core.circuit.Circuit`], All possible decompose solution.
Examples:
>>> from mindquantum.algorithm.compiler.decompose import swap_decompose
>>> from mindquantum.core.circuit import Circuit
>>> from mindquantum.core.gates import SWAP
>>> swap = SWAP.on([1, 0])
>>> origin_circ = Circuit() + swap
>>> decomposed_circ = swap_decompose(swap)[0]
>>> origin_circ
q0: ──╳───
q1: ──╳───
>>> decomposed_circ
┏━━━┓ ┏━━━┓
q0: ──┨╺╋╸┠───■───┨╺╋╸┠───
┗━┳━┛ ┃ ┗━┳━┛
┃ ┏━┻━┓ ┃
q1: ────■───┨╺╋╸┠───■─────
┗━━━┛
"""
_check_input_type('gate', gates.SWAPGate, gate)
_check_control_num(gate.obj_qubits, 2)
circuit = Circuit()
q0 = gate.obj_qubits[0]
q1 = gate.obj_qubits[1]
circuit += gates.X.on(q1, q0)
circuit += gates.X.on(q0, q1)
circuit += gates.X.on(q1, q0)
return [circuit]
def cswap_decompose(gate: gates.SWAPGate):
"""
Decompose controlled :class:`~.core.gates.SWAPGate` gate.
Args:
gate (:class:`~.core.gates.SWAPGate`): a :class:`~.core.gates.SWAPGate` with
one control qubit.
Returns:
List[:class:`~.core.circuit.Circuit`], all possible decompose solution.
Examples:
>>> from mindquantum.algorithm.compiler.decompose import cswap_decompose
>>> from mindquantum.core import Circuit, SWAP
>>> cswap = SWAP.on([1, 2], 0)
>>> origin_circ = Circuit() + cswap
>>> decomposed_circ = cswap_decompose(cswap)[0]
>>> origin_circ
q0: ──■───
q1: ──╳───
q2: ──╳───
>>> decomposed_circ
q0: ──────────■───────────
┏━━━┓ ┃ ┏━━━┓
q1: ──┨╺╋╸┠───■───┨╺╋╸┠───
┗━┳━┛ ┃ ┗━┳━┛
┃ ┏━┻━┓ ┃
q2: ────■───┨╺╋╸┠───■─────
┗━━━┛
"""
_check_input_type('gate', gates.SWAPGate, gate)
_check_control_num(gate.ctrl_qubits, 1)
circuit = Circuit()
q0 = gate.ctrl_qubits[0]
q1 = gate.obj_qubits[0]
q2 = gate.obj_qubits[1]
circuit += gates.X.on(q1, q2)
circuit += gates.X.on(q2, [q0, q1])
circuit += gates.X.on(q1, q2)
return [circuit]
decompose_rules = ['swap_decompose', 'cswap_decompose']
__all__ = decompose_rules
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/mindspore/mindquantum.git
git@gitee.com:mindspore/mindquantum.git
mindspore
mindquantum
mindquantum
master

搜索帮助