0% found this document useful (0 votes)
1 views

08-Grover-algorithm

This document discusses Grover's algorithm, a quantum algorithm designed for unstructured search problems where the goal is to find a binary string x such that f(x) = 1. It outlines the algorithm's process, which includes initializing qubits, iterating the Grover operation, and measuring the output to find a solution. The document also explains the mechanics of phase query gates and the action of the Grover operation on solution and non-solution states.

Uploaded by

richardditlin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

08-Grover-algorithm

This document discusses Grover's algorithm, a quantum algorithm designed for unstructured search problems where the goal is to find a binary string x such that f(x) = 1. It outlines the algorithm's process, which includes initializing qubits, iterating the Grover operation, and measuring the output to find a solution. The document also explains the mechanics of phase query gates and the action of the Grover operation on solution and non-solution states.

Uploaded by

richardditlin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Understanding

quantum information
and computation
By John Watrous

Lesson 8
Grover’s algorithm

© Copyright IBM Corp. 2024, 2025


Unstructured search
Let Σ = {0, 1} denote the binary alphabet (throughout the lesson).
Suppose we’re given a function

n
f∶Σ →Σ

that we can compute efficiently.


Our goal is to find a solution, which is a binary string x ∈ Σ for which f(x) = 1.
n

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

Hereafter let us write

N=2
n

n
By iterating through all x ∈ Σ and evaluating f on each one, we can solve Search with N
queries.

This is the best we can do with a deterministic algorithm.

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

The phase query gate for f operates like this:

Zf ∶ ∣x⟩ ↦ (−1) ∣x⟩ (for all x ∈ Σ )


f(x) n

∣x⟩ (−1)
f(x)
∣x⟩
Uf

∣−⟩ ∣−⟩

Zf

Exercise: show how to build a Uf operation using a controlled Zf operation.


Phase query gates
The phase query gate for f operates like this:

Zf ∶ ∣x⟩ ↦ (−1) ∣x⟩ (for all x ∈ Σ )


f(x) n

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.

The Grover operation is defined like this:

⊗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.

The Grover operation is defined like this:

⊗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.

A typical way that Grover’s algorithm can be applied:


1. Choose the number of iterations t (next section).
2. Run Grover’s algorithm with t iterations to get a candidate solution x.
3. Check the solution. If f(x) = 1 then output x, otherwise either run Grover’s algorithm
again (possibly with a different t) or report “no solutions.”
Solutions and non-solutions
We’ll refer to the n qubits being used for Grover’s algorithm as a register Q.

∣0 ⟩ and the Grover


⊗n
We’re interested in what happens when Q is initialized to the state H
n

operation G is performed iteratively.

⊗n ⊗n
G=H ZOR H Zf

These are the sets of non-solutions and solutions:

A0 = {x ∈ Σ ∶ f(x) = 0}
n

A1 = {x ∈ Σ ∶ f(x) = 1}
n

We will be interested in uniform superpositions over these sets:

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

The register Q is first initialized to this state:

1
∣u⟩ = H ∣0 ⟩ = √ ∑ ∣x⟩
⊗n n
N x∈Σn

This state is contained in the subspace spanned by ∣A0 ⟩ and ∣A1 ⟩:

√ √
∣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

1. Recall that Zf is defined like this:

Zf ∣x⟩ = (−1) ∣x⟩ (for all x ∈ Σ )


f(x) n

Its action on ∣A0 ⟩ and ∣A1 ⟩ is simple:

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

2. The operation ZOR is defined like this:

∣x⟩ x = 0
n
ZOR ∣x⟩ = {
n
(for all x ∈ Σ )
−∣x⟩ x ≠ 0
n

Here’s an alternative way to express ZOR :

ZOR = 2∣0 ⟩⟨0 ∣ − 1


n n

⊗n ⊗n
Using this expression, we can write H ZOR H like this:

(2∣0 ⟩⟨0 ∣ − 1)H


⊗n ⊗n ⊗n ⊗n
= 2∣u⟩⟨u∣ − 1
n n
H ZOR H =H
Action of the Grover operation
√ √
Zf ∣A0 ⟩ = ∣A0 ⟩ ∣A0 ∣ ∣A1 ∣
∣u⟩ = ∣A0 ⟩ + ∣A1 ⟩
Zf ∣A1 ⟩ = −∣A1 ⟩ N N

G∣A0 ⟩ = (2∣u⟩⟨u∣ − 1)Zf ∣A0 ⟩

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

G∣A1 ⟩ = (2∣u⟩⟨u∣ − 1)Zf ∣A1 ⟩

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

The action of G on span{∣A0 ⟩, ∣A1 ⟩} can be described by a 2 × 2 matrix:


⎛ ∣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

This is a rotation matrix.

√ √
⎛ ∣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

After the initialization step, this is the state of the register Q:

√ √
∣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θ:

∣u⟩ = cos(θ)∣A0 ⟩ + sin(θ)∣A1 ⟩

G∣u⟩ = cos(3θ)∣A0 ⟩ + sin(3θ)∣A1 ⟩


G ∣u⟩ = cos(5θ)∣A0 ⟩ + sin(5θ)∣A1 ⟩
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

Composing two reflections yields a rotation.

1. Recall that Zf has this action on the ∣A1 ⟩


2-dimensional space spanned by
∣A0 ⟩ and ∣A1 ⟩:

Zf ∣A0 ⟩ = ∣A0 ⟩ ∣ψ⟩

Zf ∣A1 ⟩ = −∣A1 ⟩ L1 ∣A0 ⟩

This is a reflection about the line L1


parallel to ∣A0 ⟩. Zf ∣ψ⟩
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

Composing two reflections yields a rotation.

∣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

Composing two reflections yields a rotation.

When we compose two reflections, we ∣A1 ⟩


G∣ψ⟩
obtain a rotation by twice the angle
between the lines of reflection.
∣ψ⟩

L1 ∣u⟩

θ ∣A0 ⟩
L2

Zf ∣ψ⟩
Setting the target
Consider any quantum state of this form:

α∣A0 ⟩ + β∣A1 ⟩

Measuring yields a solution x ∈ A1 with probability ∣β∣ .


2

α∣A0 ⟩ + β∣A1 ⟩ = √ ∑ ∣x⟩ + √ ∑ ∣x⟩


α β
∣A0 ∣ x∈A0 ∣A1 ∣ x∈A1


⎪ ∣α∣

2

⎪ ∣A0 ∣
p(x) = ⎪
x ∈ A0



⎪ ∣β∣

2

⎩ ∣A1 ∣ x ∈ A1

Pr(outcome is in A1 ) = ∑ p(x) = ∣β∣


2

x∈A1
Setting the target
Consider any quantum state of this form:

α∣A0 ⟩ + β∣A1 ⟩

Measuring yields a solution x ∈ A1 with probability ∣β∣ .


2

The state of Q after t iterations in Grover’s algorithm:


∣A1 ∣
cos((2t + 1)θ)∣A0 ⟩ + sin((2t + 1)θ)∣A1 ⟩ ( )
−1
θ = sin
N

Measuring after t iterations gives an outcome x ∈ A1 with probability

sin ((2t + 1)θ)


2

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

Measuring after t iterations gives an outcome x ∈ A1 with probability

sin ((2t + 1)θ)


2

To make this probability close to 1 and minimize t, we will aim for

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

For Unique search we have s = ∣A1 ∣ = 1 and therefore

√ √
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
π

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θ

Measuring after t iterations gives the (unique) outcome x ∈ A1 with probability

p(N, 1) = sin ((2t + 1)θ)


2

Success probabilities for Unique search

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

It can be proved analytically that p(N, 1) ≥ 1 − 1/N.


Multiple solutions
Example: N = 128, s = 4


( ) = 0.1777⋯
−1 s
θ = sin
N

t=⌊ ⌋ =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?

(x) ≥ x (for every x ∈ [0, 1])


−1
sin
√ √
θ = sin ( )≥
−1 s s
N N

π π N
t≤ ≤
4θ 4 s

)
N
t = O(
s
Unknown number of solutions
What do we do if we don’t know the number of solutions in advance?

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

A more sophisticated approach

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.

You might also like