Search

Grover’s Algorithm

illustration

Overview

Secondary School

Mathematics, Computer Science

Quantum Computing

English

Overview

Keywords: quantum algorithms, search, complexity
Age group: 16-18 years
Required knowledge/skills: quantum states, Hadamard gate, quantum composer
Time frame: 60-90 minutes lesson

Author: Holger Vogts (DE)

Content

Lesson plan
Student exercises and teacher notes
The search problem
Classical solution
Implementing the search function using quantum gates
Grover‘s Algorithm: Quantum Solution
Conclusions

Summary

The goal of this lesson is to get an understanding of the Grover algorithm for search in unstructured data. Grover’s algorithm is doing the search in N steps rather than N steps as a classical algorithm. Therefore this algorithm makes “quantum speed up” clear. The full mathematical description is quite hard for students. So, we focus on a geometrical description, in which an understanding of the algorithm is possible.

Teaser Icon Quantum Algorithms

Lesson plan
 

Timing
in minutes
Lesson phasesActivityResources
0–5IntroductionRemember linear search from lesson 1.Worksheet
5–10Exercise 1/2Find the classical solution (compare with lesson 1) and write the numbers in binary representation.Worksheet
10–20Understanding the oracle gatesThe Toffoli gate as oracle is introduced and compared to the CNOT gates used in Bernstein Vazirani. Depending on the student group, this can be done by themselves by reading the chapter or by explanation through the teacher.Worksheet (page 2 and 3)
20–40Exercise 3Students do the Grover algorithm in a geometrical approach. They read the chapter “Quantum solution” and transfer the algorithm in task 3 to different numbers.
The results should be reviewed before continuing with the next phase.
Worksheet
40–50Exercise 4Students reflect their solution from the geometrical approach.
This is a crucial point in the lesson to understand how Grover’s algorithm works and to compare it to the other algorithms from the previous lessons.
Worksheet
50–60SummaryThe results from the previous phase are discussed in plenum. The teacher should focus on the comparison to other quantum algorithms as well as to the classical case.Worksheet
60–endExercise 5Optional:
The algorithm is tested in the quantum composer. The probabilities can be compared with the geometrical approach.
Worksheet, quantum composer

Student exercises and teacher notes

Imagine you want to crack a safe with a combination lock that has four digits ranging from 0 to 9. You have no clue which numbers might be correct. If you're very lucky, you might hit the right combination on your first try. How many attempts would you need if things go badly? How many attempts would you need on average?

Remember the search in unstructured data in lesson 1 (programming and non-programming versions).

Worksheet

The following tasks are available as worksheet for download as pdf and docx.

There is a list of N numbers, in which exactly one of the numbers is assigned to 1 (this is the right number) and all other numbers are assigned to 0. The task is to find the position of the number which is assigned to 1 with the least possible number of questions.

The mathematical description of this problem can be done in terms of a function f with the following definition. There exists exactly one r from 1 to N with:

f ( x ) = { 1 if x = r 0 for all other values of x

Classical solution


Exercise 1: Classical solution

Assuming you don’t know what r is, determine the average number of guesses of x you will input into the function f to find r. What is your strategy for doing this?

Use linear search (compare to Exercise 1 in lesson 1 – programming and non-programming versions), and O(N) steps.

Exercise 2: Binary representation

We will use the binary representation of the numbers from 1 to N. So N = 2 n and r is represented as a string of 0s and 1s of length n.

Find the binary representation of r = 5 in the case N = 16.

The solution is: r = 0101

Implementing the search function using quantum gates

In order to implement the search function, we need to introduce a new gate, known as the Toffoli gate. This is very similar to a CNOT gate, but acts on multiple input qubits. For example:

Toffoli gate


This Toffoli gate will perform a NOT operation on the output qubit q[2] only when both input qubits, q[0] and q[1], are in state | 1 . With the output qubit initially set to state | 0 , this structure has the same effect as the equivalent to the AND operation in classical computing.

The following table shows the final state of the output qubit, q[2], for all combinations of the two input qubits:

q[0]

 

| 0

 

| 0

 

| 1

 

| 1
q[1]

 

| 0

 

| 1

 

| 0

 

| 1
q [2] (final)

 

| 0

 

| 0

 

| 0

 

| 1


To build the hidden function for the Grover search, we use a combination of an n-input Toffoli gate and single qubit NOT gates.

For our implementation of the Grover Algorithm, we will use a 4-qubit implementation where we are searching for the hidden string 0101. To build the circuit for this, we need a 4-qubit Toffoli gate and NOT gates for the qubits that correspond to 0s in the hidden string. The NOT gates flip these two inputs to | 1 so that the Toffoli gate flips the state of the output qubit to | 1 only when all of the input states match the hidden string. After the Toffoli gate the corresponding states have to flipped back by NOT gates.

Gate structure for hidden string 0101

gate structure

The difference between Grover and Bernstein-Vazirani

In the previous lesson, we saw how the Bernstein-Vazirani hidden function could be found in a single step. However, the Bernstein-Vazirani function had the property that it could be constructed using only CNOT gates, and this was the reason why the application of Hadamards led immediately to the discovery of the hidden function.

The objective of Grover is to solve the more realistic problem of identifying whether a particular data item exists within an unstructured set of data. The requirement to use Toffoli gates to do this means that the Bernstein-Vazirani approach does not give us the desired result. However, as in both Deutsch and Bernstein-Vazirani, we begin by putting all input qubits into state | 0 , the output qubit into state | 1 , and applying Hadamards to all qubits:

qbits

 

The following table shows the effect on the input qubits happens of this structure:
 

table with states and amplitudes

 

The application of the Hadamards creates a superposition of 16 different states (all the binary combinations of 0s and 1s from 0 to 15). Note that the probability of being in any of a measurement resulting in any particular state is 1/16 and the initial amplitude of each is 1/4. This is the same starting point as in all of our previous algorithms.

However, the application of the hidden function then has a curious effect. All of the amplitudes of the incorrect guesses remain the same, but the coefficient of the correct combination is flipped to have a negative sign.

It is important to understand that this alone will not allow us to find the hidden string. The probability of being in each state is the square of the amplitude, so this state still has the same probability as all others and measurement at this stage would not give us any better chance of finding the hidden string.

However, if we apply further operations to this superposition state, we are able to amplify the magnitude of the state of the hidden string (whilst shrinking all of the others). The following part shows how Grover’s Algorithm does this.

Grover‘s Algorithm: Quantum Solution

Grover’s algorithm tackles our problem using only N number of questions, thanks to the power of quantum computing. The algorithm is implemented in the following steps, which we will understand in a geometric picture:

Step I:

Prepare n qubits in state |0 and apply a Hadamard gate to all of them. This gives a superposition of all possible states on the n qubits.

This state |ψ can be described in a composition of the desired (right) state |r and the (normalized) sum of all other (wrong) states |w , which are perpendicular to |r .

alpha degree angle

Example:

In the case of N=4 we will get 
|ψ = 12 ( |00 + |01 + |10 + |11 ) .

If we assume that the desired number is r = 2 10 , this can be written as 
|ψ = 1 2 |r + 3 2 |w

with 
|w = 1 3 ( |00 + |01 + |11 )  .

The angle α can be calculated by the arcsine of the factor of the desired state: 
α = sin−1 ( 12 ) = 30°

Step II:

Apply the oracle gate Zf to |ψ . This gate does exactly one query on the function f.

As defined in the previous section, it adds a minus to the state |r and does not change any other state, which means that the vector |ψ is reflected at the vector |w .

two alpha degree angles

Step III:

Apply a combination of gates, which lead to a reflection of the vector Zf |ψ at the vector |ψ .

The combination of step II and step III is called Grover operation G. This means that the resulting vector is rotated by 2α in direction of the desired state in comparison to the initial state |ψ .

In the example N = 4 we have reached the desired state exactly with one step of the Grover operation. In general, the Grover operation has to be repeated as in the next step.

several alpha degree angles

Step IV:

Repeat the Grover operation (step II and step III) subsequently t times (so the number of queries on f is t) until the state vector is at most parallel to |r and do a measurement on all n qubits.

Exercise 3: Understanding Grover’s Algorithm step by step

Now, you will delve into each step of the algorithm and get an understanding of how often the Grover operation must be repeated.

In this case we set n=4 and N = 24 = 16 .

an alpha angle

Step I:

Applying the Hadamard operation on all four qubits of the state |0000 gives 
|ψ = 1 24 ( |0000 + |0001 + + |1111 ) .

Let us assume that r = 5 0101 . So 
|ψ = 1 24 |0101 + a |w ,

whereby a is some normalization value.

Calculate the angle α from the factor 
1 24 = 14

and draw the corresponding vector |ψ in the picture.

Step II: Draw the state vector Zf |ψ by reflection of |ψ at |w .

Step III: Draw the state vector G |ψ by reflection of Zf |ψ at |ψ .

Step IV: Repeat step II and step III (so t=2).

The angle can be calculated by

α = sin−1 ( 14 ) 14.5° .

After one Grover operation the angle is approximately 44,5°; after the next Grover operation 72,5°.

Exercise 4: Reflecting on your solution

a. Do you think that two repetitions of the Grover operation are sufficient? What would happen if you did more repetitions?

After two repetitions the state is not exactly the desired state, but it has already a large amplitude in the correct direction. Doing a further repetition would change the angle to approximately 101,5°, so the state would correspond a little bit better to the desired state. Doing more repetitions would make the result worse, since the state would remove from the desires direction.
 

b. The Grover algorithm gives the right answer with high probability and not necessarily exact. Explain this with your solution.

An exact answer of the search problem would require that the quantum state is exactly the desired state. In the case of n=4 this is not possible due to the angles which occur with these numbers. But after two or three Grover operations the probability of getting the right answer in the measurement is much larger than getting the wrong answer since the amplitude is much higher in the right direction rather than in the wrong direction (see conclusions in the worksheet how to handle this problem).
 

c. How does the angle α behave if n increases? What does this mean for the number of repetitions?

The angle will become smaller if n increases. This means that the Grover operation must be repeated more often to get closer to the desired direction.

(In addition: This will also mean that you will get closer to the desired direction since you can manage the number of repetitions such that you are not more than α away from the desired direction.)

Conclusions


Why does Grover only need O ( N ) steps?

In general, the angle α is equal to α = sin−1 ( 1N ) , 
which is approximately equal to 1N .

After one Grover operation the current angle is 3α, after the second Grover operation the angle is 5α. For t Grover operations the angle is ( 2t +1 ) α .

To get the right state with a high probability this angle should be close to π2 .

So with ( 2t +1 ) 1N π2 
we get t 12 ( N π2 1 ) ,

which is the optimal number of iterations in the Grover algorithm.

How can I be sure to get the right answer?

The Grover algorithm will in most cases give the result with a high probability and not necessarily exact. To be sure, to get the right answer, it is easy to check the result just by putting the result in the oracle. If the result is not correct, you can just run the Grover operation a second time to get another result to check.

Exercise 5

The circuit shows the 4bit Gover operation with the desired state | r = | 0101 .

circuit
 

  1. Implement this circuit in the composer.
  2. The circuit just implements one Grover operation. Repeat this step in the circuit and compare with your results from task 2.

For a repetition of the Grover operation the oracle and the reflection at | ψ must be repeated:

circuit


This will result in a probability of the desired state of approximately 90,2 %:

table showing probabilities with a peak at 1010


Doing a third repetition will result in a probability of 95,8%.

Close search