1.7K Star 2.7K Fork 4.1K

GVPMindSpore/mindquantum

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
quantum_fourier.py 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
# 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.
# ============================================================================
"""Quantum fourier transform."""
import numpy as np
from mindquantum.core.circuit import Circuit, SwapParts
from mindquantum.core.gates import H, PhaseShift
from mindquantum.utils.type_value_check import _check_input_type
def _rn(k):
return PhaseShift(2 * np.pi / 2**k)
def _qft_unit(qubits):
circ = Circuit(H.on(qubits[0]))
for index, ctrl_qubit in enumerate(qubits[1:]):
circ += _rn(index + 2).on(qubits[0], ctrl_qubit)
return circ
def qft(qubits):
"""
Quantum fourier transform (QFT).
The function of the quantum Fourier transform is similar to that of the classical Fourier transform.
Note:
Please refer to Nielsen, M., & Chuang, I. (2010) for more information.
Args:
qubits (list[int]): Qubits you want to apply quantum fourier transform.
Examples:
>>> from mindquantum.algorithm.library import qft
>>> print(qft([0, 1]).get_qs(ket=True))
1/2¦00⟩
1/2¦01⟩
1/2¦10⟩
1/2¦11⟩
Returns:
Circuit, circuit that can do fourier transform.
"""
_check_input_type('qubits', (list, range), qubits)
circuit = Circuit()
n_qubits = len(qubits)
for i in range(n_qubits):
circuit += _qft_unit(qubits[i:])
if n_qubits > 1:
part1 = []
part2 = []
for j in range(n_qubits // 2):
part1.append(qubits[j])
part2.append(qubits[n_qubits - 1 - j])
circuit += SwapParts(part1, part2)
return circuit
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/mindspore/mindquantum.git
git@gitee.com:mindspore/mindquantum.git
mindspore
mindquantum
mindquantum
r0.9

搜索帮助

0d507c66 1850385 C8b1a773 1850385