Grover’s algorithm¶
We are now in a good place to discuss our first quantum algorithms and subroutines. Let’s begin with Grover’s search algorithm and the amplitude amplification trick.
You have likely heard that one of the many advantages a quantum computer has over a classical computer is its superior speed searching databases. Grover’s algorithm demonstrates this capability. This algorithm can speed up an unstructured search problem quadratically, but its uses extend beyond that; it can serve as a general trick or subroutine to obtain quadratic run time improvements for a variety of other algorithms. This is called the amplitude amplification trick. But before we start the simulations, let’s look at the unstructured search problem.
Unstructured search¶
Suppose you are given a large list of items. Among these items is one item with a unique property that we wish to locate. We will call this one the winner, . Think of each item in the list as a box of a particular color. Say all items in the list are gray except the winner , which is red.
To find the red box – the marked item – using classical computation, one would have to check on average of these boxes, and in the worst case, all of them. On a quantum computer, however, we can find the marked item in roughly steps with Grover’s amplitude amplification trick. It was proven (even before Grover’s algorithm was discovered!) that this speedup is in fact the most we can hope for [Bennett, 1997]. A quadratic speedup is indeed a substantial time-saver for finding marked items in long lists. Additionally, the algorithm does not use the list’s internal structure, which makes it generic; this is why it immediately provides a quadratic quantum speed-up for many classical problems.
The Oracle¶
How will the list items be provided to the quantum computer? For the examples in this topic, our ‘database’ is comprised of all the possible computational basis states our qubits can be in. For example, if we have 3 qubits, our list is the states (i.e the states ).
Grover’s algorithm solves oracles that add a negative phase to the solution states. That is, for any state in the computational basis:
This oracle will be a diagonal matrix, where the entry that correspond to the marked item will have a negative phase. For example, if we have three qubits and , our oracle will have the matrix:
What makes Grover’s algorithm so powerful is how easy it is to convert a problem to an oracle of this form. There are many computational problems in which it’s difficult to find a solution, but relatively easy to verify a solution. For example, we can easily verify a solution to a sudoku by checking all the rules are satisfied. For these problems, we can create a function that takes a proposed solution and returns if is not a solution (), and for a valid solution (). Our oracle can then be described as:
and the oracle’s matrix will be a diagonal matrix of the form:
Circuit construction of a Grover oracle
If we have our classical function , we can convert it to a reversible circuit of the form:
If we initialise the ‘output’ qubit in the state , the phase kickback effect turns this into a Grover oracle (similar to the workings of the Deutsch-Jozsa oracle):
We then ignore the auxiliary () qubit.
For the next part of this chapter, we aim to teach the core concepts of the algorithm. We will create example oracles where we know beforehand, and not worry ourselves with whether these oracles are useful or not.
Amplitude amplification¶
So how does the algorithm work? Before looking at the list of items, we have no idea where the marked item is. Therefore, any guess of its location is as good as any other, which can be expressed in terms of a quantum state called a uniform superposition:
If at this point we were to measure in the standard basis , this superposition would collapse, according to the fifth quantum law, to any one of the basis states with the same probability of . Our chances of guessing the right value is therefore in , as could be expected. Hence, on average we would need to try about times to guess the correct item.
Enter the amplitude amplification procedure, which is how a quantum computer significantly enhances this probability. This procedure stretches out (amplifies) the amplitude of the marked item, which shrinks the other items’ amplitudes, so that measuring the final state will return the right item with near certainty.
This algorithm has a nice geometrical interpretation in terms of two reflections, which generate a rotation in a two-dimensional plane. The only two special states we need to consider are the winner and the uniform superposition . These two vectors span a two-dimensional plane in the vector space They are not quite perpendicular because occurs in the superposition with amplitude as well. We can, however, introduce an additional state that is in the span of these two vectors, is perpendicular to , and is obtained from by removing and rescaling.
Step 1 The amplitude amplification procedure starts out in the uniform superposition . (The uniform superposition is easily constructed from , as was shown in Creating superpositions and quantum interference.)
The left graphic corresponds to the two-dimensional plane spanned by perpendicular vectors and , which allows us to express the initial state as , where . The right graphic is a bar graph of the amplitudes of the state .
Step 2 We apply the oracle reflection to the state .
Geometrically this corresponds to a reflection of the state about . This transformation means that the amplitude in front of the state becomes negative, which in turn means that the average amplitude (indicated by a dashed line) has been lowered.
Step 3 We now apply an additional reflection about the state : . This transformation maps the state to and completes the transformation.
Two reflections always correspond to a rotation. The transformation rotates the initial state closer toward the winner . The action of the reflection in the amplitude bar diagram can be understood as a reflection about the average amplitude. Since the average amplitude has been lowered by the first reflection, this transformation boosts the negative amplitude of to roughly three times its original value, while it decreases the other amplitudes. We then go to Step 2 to repeat the application. This procedure will be repeated several times to focus in on the winner.
After steps, the state will have transformed to , where How many times do we need to apply the rotation? It turns out that roughly rotations suffice. This becomes clear when looking at the amplitudes of the state . We can see that the amplitude of grows linearly with the number of applications . However, since we are dealing with amplitudes and not probabilities, the vector space’s dimension enters as a square root. Therefore it is the amplitude, and not just the probability, that is being amplified in this procedure.
In the case that there are multiple solutions, , it can be shown that roughly rotations will suffice.
Example: two qubits¶
Let us now examine a simple example. The smallest circuit for which this can be implemented involves two qubits, that is, . First, we will determine how many rotations are required to rotate the initial state to the winner .
Following the previously described steps, for the case , we have
After steps, we have
where
In order to obtain , we need . Substituting into the above gives us . Therefore, after rotation, the desired element is found.
Oracle for ¶
We will review the case . The oracle in this case acts as follows:
or:
which you may recognise as the controlled-Z gate. Thus, for this example, our oracle is simply the controlled-Z gate:
Reflection ¶
To complete the circuit, we need to implement the additional reflection . Since this is a reflection about , we want to add a negative phase to every state orthogonal to .
One way to do this is to use the operation that transforms the state , which we know is the Hadamard gate applied to each qubit:
Then we apply a circuit that adds a negative phase to the states orthogonal to :
One way of implementing is the following circuit:
Finally, we perform the operation that transforms the state (the Hadamard gate) again:
The complete circuit for looks like this:
Full Circuit for ¶
Since in the case of , only one rotation is required, we can combine the above components to build the full circuit for Grover’s algorithm for the case :
Use IBM Quantum Composer to explore the oracles¶
There are four possible oracles , one for each choice of the winner. In each of the following examples, we first create the uniform superposition, then tag the state with , and finally perform .
Grover
Grover
Grover
- Grover