昇思量子计算系列教程-龙算法

从运行的结果看到,我们成功地搜索出 ∣ 5 ⟩ |5\rangle ∣5⟩​​和 ∣ 11 ⟩ |11\rangle ∣11⟩​​​​​态。

至此,我们介绍了Grover搜索算法的基本原理,以及通过两个具体的小例子来展示如何利用MindSpore Quantum实现该算法!赶紧动手体验一下量子编程的乐趣吧!

龙算法

除了在规模为4的数据库中找1个数据的场景,Grover算法不能够精确的搜索出所标记态。清华大学龙桂鲁教授在Grover算法基础之上提出量子精确搜索算法龙算法[3],能够以准确率为1的概率在所有场景中搜索出目标态。其主要思想是将Grover算子改写为如下的算子,

L = − H ⊗ n R 0 H ⊗ n R τ L = -H^{\otimes n} R_0 H^{\otimes n} R_\tau L=−H⊗nR0H⊗nRτ

其中: R 0 = ( I + ( e i θ − 1 ) ∣ 0 > < 0 ∣ ) R_0 = (I+(e^{i\theta}-1)\left|0\right>\left<0\right|) R0=(I+(eiθ−1)∣0⟩⟨0∣), R τ = ( I + ( e i θ − 1 ) ∣ τ > < τ ∣ ) R_\tau = (I+(e^{i\theta}-1)\left|\tau\right>\left<\tau\right|) Rτ=(I+(eiθ−1)∣τ⟩⟨τ∣)。在满足相位匹配条件时,

θ = 2 arcsin ⁡ ( sin ⁡ β sin ⁡ ( π 4 J s + 6 ) ) \theta = 2\arcsin\left(\sin\beta\sin\left(\frac{\pi}{4J_s+6}\right)\right) θ=2arcsin(sinβsin(4Js+6π))

我们只需作用 J s + 1 J_s+1 Js+1次龙算子,就可以以概率1找到目标态,这里 β = arcsin ⁡ M / N \beta=\arcsin{\sqrt{M/N}} β=arcsinM/N , M M M为标记态个数, N N N为数据库大小, J s > = [ ( ( π / 2 ) − β ) / β ] J_s>=[((\pi/2)-\beta)/\beta] Js>=[((π/2)−β)/β]。下面我们用MindSpore Quantum来实现。

一般角度相位转动线路

借助于辅助比特,我们搭建某个计算基矢一般角度相位转动线路。

python 复制代码
from mindquantum.core.gates import X, PhaseShift
from mindquantum.core.circuit import Circuit
def change_phase_with_anclia(which, n_qubits, phase):
    c = Circuit()
    which_bit = bin(which)[2:].zfill(n_qubits)[::-1]
    polarity_circ = Circuit()
    for idx, bit in enumerate(which_bit):
        if bit == "0":
            polarity_circ += X.on(idx)
    c += polarity_circ
    c += PhaseShift(phase).on(n_qubits, list(range(n_qubits)))
    c += polarity_circ
    return c

搭建龙算子

python 复制代码
from mindquantum.core.gates import BARRIER, Z

def L(which, n_qubits, theta, phi):
    U = UN(H, n_qubits)
    R0 = change_phase_with_anclia(0, n_qubits, theta)
    R_t = change_phase_with_anclia(which, n_qubits, phi)
    g_ops = R_t + BARRIER + U + BARRIER + R0 + BARRIER + U + BARRIER
    g_ops += Z.on(n_qubits)
    return g_ops

完成量子精确搜索算法:龙算法

这里我们以3比特数据库中搜索 ∣ 2 > \left|2\right> ∣2⟩态为例,完成龙算法。

python 复制代码
import numpy as np
from mindquantum.core.gates import H
from mindquantum.core.circuit import UN
n_qubits = 3
will_find = 2
beta = np.arcsin(np.sqrt(1 / 2**n_qubits))
Js = int((np.pi / 2 - beta) / 2 / beta)
theta = 2 * np.arcsin(np.sin(np.pi / (4 * Js + 6)) / np.sin(beta))
phi = theta

g = L(will_find, n_qubits, theta, phi)            # 构建用于精确搜索的龙算子

circ = UN(H, n_qubits) + X.on(n_qubits)
for i in range(Js + 1):
    circ += g
circ.svg()

接下来,我们计算线路的量子态。发现,除去相位,我们可以精确的得到目标态。通过采样,我们也可以得到如下类似的结果。

python 复制代码
from mindquantum.simulator import Simulator
from mindquantum.core.gates import Measure

sim = Simulator('mqvector', circ.n_qubits)
res = sim.sampling(circ + UN(Measure(), circ.n_qubits), shots=100)
res.svg()
python 复制代码
from mindquantum.utils.show_info import InfoTable

InfoTable('mindquantum', 'scipy', 'numpy')
`
``
相关推荐
至简行远2 分钟前
路由器全局配置DHCP实验简述
linux·网络·数据结构·python·算法·华为·智能路由器
liuyang-neu2 分钟前
力扣 16.最接近的三数之和
java·数据结构·算法·leetcode
碧海蓝天20226 分钟前
_Array类,类似于Vector,其实就是_string
数据结构·c++·算法
至简行远8 分钟前
路由器接口配置DHCP实验简述
java·服务器·网络·数据结构·python·算法·智能路由器
Antonio91522 分钟前
【高级数据结构】并查集
数据结构·c++·算法
肥猪猪爸41 分钟前
sklearn特征选取之RFE
数据结构·人工智能·python·算法·机器学习·数据挖掘·sklearn
木向1 小时前
leetcode第十一题:盛最多水的容器
算法·leetcode·职场和发展
繁依Fanyi1 小时前
828华为云征文|华为Flexus云服务器搭建Cloudreve私人网盘
开发语言·人工智能·pytorch·python·算法·华为云·php
DA树聚1 小时前
Transformer模型-7- Decoder
人工智能·深度学习·神经网络·算法·目标检测·语言模型·transformer
子午2 小时前
水果识别系统Python+卷积神经网络算法+人工智能+深度学习+计算机课设项目+TensorFlow+模型训练
人工智能·python·深度学习