08-Grover-algorithm
08-Grover-algorithm
quantum information
and computation
By John Watrous
Lesson 8
Grover’s algorithm
n
f∶Σ →Σ
Search
n
Input: f∶Σ →Σ
satisfying f(x) = 1, or “no solution” if no such
n
Output: a string x ∈ Σ
strings exist
This is unstructured search because f is arbitrary — there’s no promise and we can’t rely on
it having a structure that makes finding solutions easy.
Algorithms for search
Search
n
Input: f∶Σ →Σ
satisfying f(x) = 1, or “no solution” if no such
n
Output: a string x ∈ Σ
strings exist
N=2
n
n
By iterating through all x ∈ Σ and evaluating f on each one, we can solve Search with N
queries.
Probabilistic algorithms offer minor improvements, but still require a number of queries
linear in N.
√
Grover’s algorithm is a quantum algorithm for Search requiring O( N) queries.
Phase query gates
n
We assume that we have access to the function f ∶ Σ → Σ through a query gate:
Uf ∶ ∣a⟩∣x⟩ ↦ ∣a ⊕ f(x)⟩∣x⟩
n
(for all a ∈ Σ and x ∈ Σ )
(We can build a circuit for Uf given a Boolean circuit for f.)
∣x⟩ (−1)
f(x)
∣x⟩
Uf
∣−⟩ ∣−⟩
Zf
We’re also going to need a phase query gate for the n-bit OR function:
⎧
⎪
⎪
⎪ 0 x=0
n
OR(x) = ⎨
⎪
n
(for all x ∈ Σ )
⎪
⎪
⎩
1 x≠0
n
⎧
⎪
⎪
⎪ ∣x⟩ x = 0
n
ZOR ∣x⟩ = ⎨
⎪
n
(for all x ∈ Σ )
⎪
⎪
⎩
−∣x⟩ x ≠ 0
n
Algorithm description
Grover’s algorithm
∣0 ⟩.
⊗n n
1. Initialize: set n qubits to the state H
2. Iterate: apply the Grover operation t times (for t to be specified later).
3. Measure: a standard basis measurement yields a candidate solution.
⊗n ⊗n
G=H ZOR H Zf
Zf is the phase query gate for f and ZOR is the phase query gate for the n-bit OR function.
H H
H H
Zf ZOR
H H
H H
Algorithm description
Grover’s algorithm
∣0 ⟩.
⊗n n
1. Initialize: set n qubits to the state H
2. Iterate: apply the Grover operation t times (for t to be specified later).
3. Measure: a standard basis measurement yields a candidate solution.
⊗n ⊗n
G=H ZOR H Zf
Zf is the phase query gate for f and ZOR is the phase query gate for the n-bit OR function.
⊗n ⊗n
G=H ZOR H Zf
A0 = {x ∈ Σ ∶ f(x) = 0}
n
A1 = {x ∈ Σ ∶ f(x) = 1}
n
1
∣A0 ⟩ = √ ∑ ∣x⟩
∣A0 ∣ x∈A0
1
∣A1 ⟩ = √ ∑ ∣x⟩
∣A1 ∣ x∈A1
Analysis: basic idea
A0 = {x ∈ Σ ∶ f(x) = 0} A1 = {x ∈ Σ ∶ f(x) = 1}
n n
1 1
∣A0 ⟩ = √ ∑ ∣x⟩ ∣A1 ⟩ = √ ∑ ∣x⟩
∣A0 ∣ x∈A0 ∣A1 ∣ x∈A1
1
∣u⟩ = H ∣0 ⟩ = √ ∑ ∣x⟩
⊗n n
N x∈Σn
√ √
∣A0 ∣ ∣A1 ∣
∣u⟩ = ∣A0 ⟩ + ∣A1 ⟩
N N
The state of Q remains in this subspace after every application of the Grover operation G.
Action of the Grover operation
We can better understand the Grover operation by splitting it into two parts:
G = (H )(Zf )
⊗n ⊗n
ZOR H
Zf ∣A0 ⟩ = ∣A0 ⟩
Zf ∣A1 ⟩ = −∣A1 ⟩
Action of the Grover operation
We can better understand the Grover operation by splitting it into two parts:
G = (H )(Zf )
⊗n ⊗n
ZOR H
∣x⟩ x = 0
n
ZOR ∣x⟩ = {
n
(for all x ∈ Σ )
−∣x⟩ x ≠ 0
n
⊗n ⊗n
Using this expression, we can write H ZOR H like this:
= (2∣u⟩⟨u∣ − 1)∣A0 ⟩
√
∣A0 ∣
=2 ∣u⟩ − ∣A0 ⟩
√ √ √
N
∣A0 ∣ ∣A0 ∣ ∣A1 ∣
=2 ( ∣A0 ⟩ + ∣A1 ⟩) − ∣A0 ⟩
√
N N N
∣A0 ∣ − ∣A1 ∣ 2 ∣A0 ∣ ⋅ ∣A1 ∣
= ∣A0 ⟩ + ∣A1 ⟩
N N
Action of the Grover operation
√ √
Zf ∣A0 ⟩ = ∣A0 ⟩ ∣A0 ∣ ∣A1 ∣
∣u⟩ = ∣A0 ⟩ + ∣A1 ⟩
Zf ∣A1 ⟩ = −∣A1 ⟩ N N
√
∣A0 ∣ − ∣A1 ∣ 2 ∣A0 ∣ ⋅ ∣A1 ∣
G∣A0 ⟩ = ∣A0 ⟩ + ∣A1 ⟩
N N
= (1 − 2∣u⟩⟨u∣)∣A1 ⟩
√
∣A1 ∣
= ∣A1 ⟩ − 2 ∣u⟩
√ √ √
N
∣A0 ∣ ∣A0 ∣ ∣A1 ∣
= ∣A1 ⟩ − 2 ( ∣A0 ⟩ + ∣A1 ⟩)
√
N N N
2 ∣A0 ∣ ⋅ ∣A1 ∣ ∣A0 ∣ − ∣A1 ∣
=− ∣A0 ⟩ + ∣A1 ⟩
N N
Action of the Grover operation
√ √
Zf ∣A0 ⟩ = ∣A0 ⟩ ∣A0 ∣ ∣A1 ∣
∣u⟩ = ∣A0 ⟩ + ∣A1 ⟩
Zf ∣A1 ⟩ = −∣A1 ⟩ N N
√
∣A0 ∣ − ∣A1 ∣ 2 ∣A0 ∣ ⋅ ∣A1 ∣
G∣A0 ⟩ = ∣A0 ⟩ + ∣A1 ⟩
√
N N
2 ∣A0 ∣ ⋅ ∣A1 ∣ ∣A0 ∣ − ∣A1 ∣
G∣A1 ⟩ = − ∣A0 ⟩ + ∣A1 ⟩
N N
√
⎛ ∣A0 ∣−∣A1 ∣ 2 ∣A0 ∣⋅∣A1 ∣ ⎞
∣A0 ⟩
⎜ ⎟
−
M= ⎜
⎜
⎜
⎟
⎟
⎟
N N
√
⎜ 2 ∣A0 ∣⋅∣A1 ∣ ∣A0 ∣−∣A1 ∣ ⎟
⎝ N N ⎠ ∣A1 ⟩
∣A0 ⟩ ∣A1 ⟩
Rotation by an angle
The action of G on span{∣A0 ⟩, ∣A1 ⟩} can be described by a 2 × 2 matrix:
√ √ √
⎛ ∣A0 ∣ ∣A1 ∣ ⎞
2
⎛ ∣A0 ∣−∣A1 ∣ 2 ∣A0 ∣⋅∣A1 ∣ ⎞
⎜ ⎟ ⎜ N N ⎟
−
⎟ =⎜ ⎟
−
M= ⎜
⎜ 2√∣A ∣⋅∣A ∣
N N
⎟ ⎜√
⎜ √ ⎟
⎟
⎝ 0
N
1 ∣A0 ∣−∣A1 ∣
N
⎠ ⎝ ∣A1 ∣ ∣A0 ∣
⎠
N N
√ √
⎛ ∣A0 ∣ √
− N1 ⎞ ⎛cos(θ)
∣A ∣
− sin(θ)⎞ ∣A1 ∣
⎜
⎜ ⎟
⎟ ⎜ ⎟ ( )
⎜
⎜√ ∣A ∣ √ ⎟
⎟=⎜ ⎟
N −1
θ = sin
⎝ ∣A0 ∣
⎠ ⎝ sin(θ) cos(θ) ⎠
1
N
N N
⎛cos(2θ) − sin(2θ)⎞
M=⎜
⎜ ⎟
⎟
⎝ sin(2θ) cos(2θ) ⎠
Rotation by an angle
√
⎛cos(2θ) − sin(2θ)⎞ ∣A1 ∣
M=⎜
⎜ ⎟
⎟ ( )
−1
θ = sin
⎝ sin(2θ) cos(2θ) ⎠
N
√ √
∣A0 ∣ ∣A1 ∣
∣u⟩ = ∣A0 ⟩ + ∣A1 ⟩ = cos(θ)∣A0 ⟩ + sin(θ)∣A1 ⟩
N N
Each time the Grover operation G is performed, the state of Q is rotated by an angle 2θ:
⋮
G ∣u⟩ = cos((2t + 1)θ)∣A0 ⟩ + sin((2t + 1)θ)∣A1 ⟩
t
Geometric picture
Main idea
⊗n ⊗n
The operation G = H ZOR H Zf is a composition of two reflections:
⊗n ⊗n
Zf and H ZOR H
⊗n ⊗n
Zf and H ZOR H
∣A1 ⟩
⊗n ⊗n
2. The operation H ZOR H can be
expressed like this:
∣ψ⟩
⊗n ⊗n
H ZOR H
∣u⟩
⊗n ⊗n
H ZOR H = 2∣u⟩⟨u∣ − 1
∣A0 ⟩
Again this is a reflection, this time
about the line L2 parallel to ∣u⟩. ∣ψ⟩
L2
Geometric picture
Main idea
⊗n ⊗n
The operation G = H ZOR H Zf is a composition of two reflections:
⊗n ⊗n
Zf and H ZOR H
L1 ∣u⟩
θ ∣A0 ⟩
L2
Zf ∣ψ⟩
Setting the target
Consider any quantum state of this form:
α∣A0 ⟩ + β∣A1 ⟩
⎧
⎪ ∣α∣
⎪
2
⎪ ∣A0 ∣
p(x) = ⎪
x ∈ A0
⎨
⎪
⎪
⎪ ∣β∣
⎪
2
⎩ ∣A1 ∣ x ∈ A1
x∈A1
Setting the target
Consider any quantum state of this form:
α∣A0 ⟩ + β∣A1 ⟩
√
∣A1 ∣
cos((2t + 1)θ)∣A0 ⟩ + sin((2t + 1)θ)∣A1 ⟩ ( )
−1
θ = sin
N
We wish to maximize this probability — so we may view that ∣A1 ⟩ is our target state.
Setting the target
The state of Q after t iterations in Grover’s algorithm:
√
∣A1 ∣
cos((2t + 1)θ)∣A0 ⟩ + sin((2t + 1)θ)∣A1 ⟩ ( )
−1
θ = sin
N
1
(2t + 1)θ ≈ t=⌊ ⌋
π π closest integer π
⇔ t≈ − ⟶
2 4θ 2 4θ
Important considerations:
• t must be an integer
• θ depends on the number of solutions s = ∣A1 ∣
Unique search
(2t + 1)θ ≈ t=⌊ ⌋
π π
⇐
2 4θ
Unique search
n
Input: f∶Σ →Σ
There is exactly one string z ∈ Σ for which f(z) = 1,
n
Promise:
/z
with f(x) = 0 for all strings x =
Output: The string z
√ √
1 1
( )≈
−1
θ = sin
N N
√
Substituting θ ≈ 1/ N into our expression for t gives
π√ √
t≈⌊ N⌋ ← O( N) queries
4
Unique search
Example: N = 128
1
( √ ) = 0.0885⋯
−1
θ = sin
N
t=⌊ ⌋=8
π
4θ
G ∣u⟩
8
G ∣u⟩
12
∣A1 ⟩
G ∣u⟩
2
G∣u⟩
∣u⟩ = cos(θ)∣A0 ⟩ + sin(θ)∣A1 ⟩
∣A0 ⟩
Unique search
√
1
( ) t=⌊ ⌋
−1 π
θ = sin
N 4θ
N p(N, 1) N p(N, 1)
2 .5 128 .9956199
4 1.0 256 .9999470
8 .9453125 512 .9994480
16 .9613190 1024 .9994612
32 .9991823 2048 .9999968
64 .9965857 4096 .9999453
√
( ) = 0.1777⋯
−1 s
θ = sin
N
t=⌊ ⌋ =4
π
4θ
G ∣u⟩G3 ∣u⟩
4
G ∣u⟩
2
∣A ⟩1
G∣u⟩
G ∣u⟩
8 ∣u⟩ = cos(θ)∣A0 ⟩ + sin(θ)∣A1 ⟩
∣A0 ⟩
Multiple solutions
√
( ) t=⌊ ⌋
−1 s π
θ = sin
N 4θ
For every s ∈ {1, . . . , N}, the probability p(N, s) to find a solution satisfies
, }
s s
p(N, s) ≥ max{1 −
N N
Number of queries
√
( ) t=⌊ ⌋
−1 s π
θ = sin
N 4θ
Each iteration of Grover’s algorithm requires 1 query (or evaluations of f). How does the
number of queries t depend on N and s?
A simple approach
√
Choose the number of iterations t ∈ {1, . . . , ⌊π N/4⌋} uniformly at random.
• The probability to find a solution (if one exists) will be at least 40%.
(Repeat several times to boost success probability.)
√
• The number of queries (or evaluations of f) is O( N).
1. Set T = 1.
2. Run Grover’s algorithm with t ∈ {1, . . . , T } chosen uniformly at random.
3. If a solution is found, output it and stop.
Otherwise, increase T and return to step 2 (or report “no solution”).
• The rate of increase of T must be carefully balanced: slower rates require more
queries, higher rates decrease success probability. T ← ⌈ 54 T ⌉ works.
• If the number √ √ of queries (or evaluations of f)
of solutions is s ≥ 1, then the number
required is O( N/s). If there are no solutions, O( N) queries are required.
Concluding remarks
• Grover’s algorithm is asymptotically optimal.
• Grover’s algorithm is broadly applicable.
• The technique used in Grover’s algorithm can be generalized.