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

从运行的结果看到,我们成功地搜索出 ∣ 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=HnR0HnRτ

其中: 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)00) 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来实现。

一般角度相位转动线路

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

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

搭建龙算子

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态为例,完成龙算法。

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()

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

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()
from mindquantum.utils.show_info import InfoTable

InfoTable('mindquantum', 'scipy', 'numpy')
`
``

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明志刘明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值