Bernstein-Vazirani Algorithm
Posted 安徽思远
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bernstein-Vazirani Algorithm相关的知识,希望对你有一定的参考价值。
问题:对于函数,
f
(
{
x
0
,
x
1
,
x
2
,
.
.
.
}
)
→
0
or
1
where
x
n
is
0
or
1
f(\\{x_0,x_1,x_2,...\\}) \\rightarrow 0 \\textrm{ or } 1 \\textrm{ where } x_n \\textrm{ is }0 \\textrm{ or } 1
f({x0,x1,x2,...})→0 or 1 where xn is 0 or 1
该函数的形式为
f
(
x
)
=
s
⋅
x
(mod 2)
f(x) = s \\cdot x \\, \\text{(mod 2)}
f(x)=s⋅x(mod 2),给定x,得到f(x),求s的值。
该算法步骤:
- 初始化 |0⟩⊗n state, and output qubit
to |−⟩ . - 对Input寄存器进行Hardmard变化。
- 对Output进行measure。
对于Hardmard变换,有普遍规律:
∣
a
⟩
→
H
⊗
n
1
2
n
∑
x
∈
{
0
,
1
}
n
(
−
1
)
a
⋅
x
∣
x
⟩
.
|a\\rangle \\xrightarrow{H^{\\otimes n}} \\frac{1}{\\sqrt{2^n}} \\sum_{x\\in \\{0,1\\}^n} (-1)^{a\\cdot x}|x\\rangle.
∣a⟩H⊗n2n1x∈{0,1}n∑(−1)a⋅x∣x⟩.
我们对 ∣ 00 … 0 ⟩ |00\\dots 0\\rangle ∣00…0⟩ 使用n Hadamard gates得到量子叠加态:
∣ 00 … 0 ⟩ → H ⊗ n 1 2 n ∑ x ∈ { 0 , 1 } n ∣ x ⟩ |00\\dots 0\\rangle \\xrightarrow{H^{\\otimes n}} \\frac{1}{\\sqrt{2^n}} \\sum_{x\\in \\{0,1\\}^n} |x\\rangle ∣00…0⟩H⊗n2n1x∈{0,1}n∑∣x⟩
( − 1 ) a ⋅ x = 0 (-1)^{a\\cdot x}=0 (−1)a⋅x=0 , since a = 0 , a=0, a=0, and thus ( − 1 ) a ⋅ x = 1 (-1)^{a\\cdot x} = 1 (−1)a⋅x=1.
之后,我们作用 f s f_{s} fs函数。这里我们的ouput初始状态a为 ∣ − ⟩ |{-}\\rangle ∣−⟩.
- 若f(x) = 1, ∣ a ⊕ f ( x ) ⟩ = − ∣ − ⟩ |a \\oplus f(x)\\rangle = -|{-}\\rangle ∣a⊕f(x)⟩=−∣−⟩
- 若f(x) = 0, ∣ a ⊕ f ( x ) ⟩ = ∣ − ⟩ |a \\oplus f(x)\\rangle = |{-}\\rangle ∣a⊕f(x)⟩=∣−⟩
∣ x ⟩ → f s ( − 1 ) s ⋅ x ∣ x ⟩ |x \\rangle \\xrightarrow{f_s} (-1)^{s\\cdot x} |x \\rangle ∣x⟩fs(−1)s⋅x∣x⟩
∣ 00 … 0 ⟩ → H ⊗ n 1 2 n ∑ x ∈ { 0 , 1 } n ∣ x ⟩ → f a 1 2 n ∑ x ∈ { 0 , 1 } n ( − 1 ) a ⋅ x ∣ x ⟩ |00\\dots 0\\rangle \\xrightarrow{H^{\\otimes n}} \\frac{1}{\\sqrt{2^n}} \\sum_{x\\in \\{0,1\\}^n} |x\\rangle \\xrightarrow{f_a} \\frac{1}{\\sqrt{2^n}} \\sum_{x\\in \\{0,1\\}^n} (-1)^{a\\cdot x}|x\\rangle ∣00…0⟩H⊗n2n1x∈{0,1}n∑∣x⟩fa2n1x∈{0,1}n∑(−1)a⋅x∣x⟩
由于量子计算是可逆的,我们再使用Hardmard Gate便可以得到a。
1 2 n ∑ x ∈ { 0 , 1 } n ( − 1 ) a ⋅ x ∣ x ⟩ → H ⊗ n ∣ a ⟩ \\frac{1}{\\sqrt{2^n}} \\sum_{x\\in \\{0,1\\}^n} (-1)^{a\\cdot x}|x\\rangle \\xrightarrow{H^{\\otimes n}} |a\\rangle 2n1x∈{0,1}n∑(−1)a⋅x∣x⟩H⊗n∣a⟩
Coding
# initialization
import matplotlib.pyplot as plt
import numpy as np
# importing Qiskit
from qiskit import IBMQ, Aer
from qiskit.providers.ibmq import least_busy
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, transpile, assemble
# import basic plot tools
from qiskit.visualization import plot_histogram
n = 3 # number of qubits used to represent s
s = '011' # the hidden binary string
# We need a circuit with n qubits, plus one auxiliary qubit
# Also need n classical bits to write the output to
bv_circuit = QuantumCircuit(n+1, n)
# put auxiliary in state |->
bv_circuit.h(n)
bv_circuit.z(n)
# Apply Hadamard gates before querying the oracle
for i in range(n):
bv_circuit.h(i)
# Apply barrier
bv_circuit.barrier()
# Apply the inner-product oracle
s = s[::-1] # reverse s to fit qiskit's qubit ordering
for q in range(n):
if s[q] == '0':
bv_circuit.i(q)
else:
bv_circuit量子计算求解函数因子:Bernstein-Vazirani Algorithm