Deutsch-Jozsa Algorithm
Posted 安徽思远
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Deutsch-Jozsa Algorithm相关的知识,希望对你有一定的参考价值。
Deutsch Algorithm
问题:给定一个函数f(x), x ∈ { 0 , 1 } x \\in \\{0,1\\} x∈{0,1},如何判断f(x)是balanced function还是constant function?
这里constant function指,无论x是0还是1,f(x)的结果相同。而对于balanced function,f(0)!=f(1)。
解法:通过0,1叠加态,构造f(x)叠加态,判断其结果。推导过程如下,
这个方法可以更快的得知,f(0)和f(1)的关系比较,而仅仅需要测量一次。试想,如果我们通过经典计算机,把f(0)和f(1)相减,也可以达到相同的结果,那么这种方法的优势是什么?
答:这里利用叠加态,实际上只调用了f(x)这个函数一次。
接下来用代码实现一下:
# initialization
import numpy as np
# importing Qiskit
from qiskit import IBMQ, Aer
from qiskit.providers.ibmq import least_busy
from qiskit import QuantumCircuit, assemble, transpile
# import basic plot tools
from qiskit.visualization import plot_histogram
qc = QuantumCircuit(2,2)
qc.h(0)
qc.x(1)
qc.h(1)
qc.barrier()
qc.cx(0,1)
qc.barrier()
qc.h(0)
qc.measure(0,0)
qc.draw("mpl")
# use local simulator
qasm_sim = Aer.get_backend('qasm_simulator')
shots = 1024
qobj = assemble(qc, qasm_sim)
results = qasm_sim.run(qobj).result()
answer = results.get_counts()
plot_histogram(answer)
这里我们在中间构造了一个balanced function。
q 0 q 1 q_{0}q_{1} q0q1 | f(x) |
---|---|
00 | 0 |
01 | 1 |
10 | 1 |
11 | 0 |
测量结果为1,所以这是一个balanced function。
Deutsch-Jozsa Algorithm
Deutsch-Jozsa Algorithm将Deutsch Algorithm推广到n qubit。
Using a quantum computer, we can solve this problem with 100% confidence after only one call to the function f(x), provided we have the function f implemented as a quantum oracle, which maps the state ∣ x ⟩ ∣ y ⟩ t o ∣ x ⟩ ∣ y ⊕ f ( x ) ⟩ \\vert x\\rangle \\vert y\\rangle to \\vert x\\rangle \\vert y \\oplus f(x)\\rangle ∣x⟩∣y⟩to∣x⟩∣y⊕f(x)⟩, where ⊕ \\oplus ⊕ is addition modulo 2. Below is the generic circuit for the Deutsch-Jozsa algorithm.
Now, let’s go through the steps of the algorithm:
Prepare two quantum registers. The first is an n-qubit register initialized to |0\\rangle, and the second is a one-qubit register initialized to ∣ 1 ⟩ : ∣ ψ 0 ⟩ = ∣ 0 ⟩ ⊗ n ∣ 1 ⟩ |1\\rangle: \\vert \\psi_0 \\rangle = \\vert0\\rangle^{\\otimes n} \\vert 1\\rangle ∣1⟩:∣ψ0⟩=∣0⟩⊗n∣1⟩
Apply a Hadamard gate to each qubit:
∣
ψ
1
⟩
=
1
2
n
+
1
∑
x
=
0
2
n
−
1
∣
x
⟩
(
∣
0
⟩
−
∣
1
⟩
)
\\vert \\psi_1 \\rangle = \\frac{1}{\\sqrt{2^{n+1}}}\\sum_{x=0}^{2^n-1} \\vert x\\rangle \\left(|0\\rangle - |1 \\rangle \\right)
∣ψ1⟩=2n+11∑x=02n−1∣x⟩(∣0⟩−∣1⟩)
Apply the quantum oracle ∣ x ⟩ ∣ y ⟩ t o ∣ x ⟩ ∣ y ⊕ f ( x ) ⟩ \\vert x\\rangle \\vert y\\rangle to \\vert x\\rangle \\vert y \\oplus f(x)\\rangle ∣x⟩∣y⟩to∣x⟩∣y⊕f(x)⟩:
∣
ψ
2
⟩
=
1
2
n
+
1
∑
x
=
0
2
n
−
1
∣
x
⟩
(
∣
f
(
x
)
⟩
−
∣
1
⊕
f
(
x
)
⟩
)
=
1
2
n
+
1
∑
x
=
0
2
n
−
1
(
−
1
)
f
(
x
)
∣
x
⟩
(
∣
0
⟩
−
∣
1
⟩
)
\\begin{aligned} \\lvert \\psi_2 \\rangle & = \\frac{1}{\\sqrt{2^{n+1}}}\\sum_{x=0}^{2^n-1} \\vert x\\rangle (\\vert f(x)\\rangle - \\vert 1 \\oplus f(x)\\rangle) \\\\ & = \\frac{1}{\\sqrt{2^{n+1}}}\\sum_{x=0}^{2^n-1}(-1)^{f(x)}|x\\rangle ( |0\\rangle - |1\\rangle ) \\end{aligned}
∣ψ2⟩=2n+11x=0∑2n−1∣x⟩(∣f(x)⟩−∣1⊕f(x)⟩)=2n+11x=0∑2n−1(−1)f(x)∣x⟩(∣0⟩−∣1⟩)
since for each x,f(x) is either 0 or 1.
At this point the second single qubit register may be ignored. Apply a Hadamard gate to each qubit in the first register:
∣ ψ 3 ⟩ = 1 2 n ∑ x = 0 2 n − 1 ( − 1 ) f ( x ) [ ∑ y = 0 2 n − 1 ( − 1 ) x ⋅ y ∣ y ⟩ ] = 1 2 n ∑ y = 0 2 n − 1 [ ∑ x = 0 2 n − 1 ( − 1 ) f ( x ) ( − 1 ) x ⋅ y ] ∣ y ⟩ \\begin{aligned} \\lvert \\psi_3 \\rangle & = \\frac{1}{2^n}\\sum_{x=0}^{2^n-1}(-1)^{f(x)} \\left[ \\sum_{y=0}^{2^n-1}(-1)^{x \\cdot y} \\vert y \\rangle \\right] \\\\ & = \\frac{1}{2^n}\\sum_{y=0}^{2^n-1} \\left[ \\sum_{x=0}^{2^n-1}(-1)^{f(x)}(-1)^{x \\cdot y} \\right] \\vert y \\rangle \\end{aligned} ∣ψ3⟩=2n1x=0∑2n−1(−1)f(x)[y=0∑2n−1(−1)x⋅y∣y⟩]=2n1y=0∑2n−1[x=0∑2n−1(−1)f(x)(−1)x⋅y]∣y⟩
where x ⋅ y = x 0 y 0 ⊕ x 1 y 1 ⊕ … ⊕ x n − 1 y n − 1 x \\cdot y = x_0y_0 \\oplus x_1y_1 \\oplus \\ldots \\oplus x_{n-1}y_{n-1} x⋅y=x0y0⊕以上是关于Deutsch-Jozsa Algorithm的主要内容,如果未能解决你的问题,请参考以下文章
删除 '#include <algorithm>' 不会破坏代码
[Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}(代码片