With Deutsch-Jozsa, we saw our first fundamentally quantum algorithm that showed how quantum computers could do something better than classical computers. But it doesn't seem like a particularly useful algorithm. Is there a more useful algorithm that we might be able to run on a quantum computer?
We'll answer this question by looking at Grover Search. This algorithm shows how we can use a quantum computer to search through some list of items exponentially faster than a classical computer would be able to.
Let's start by formally defining the problem.
Suppose we have a list of items we would like to search. Perhaps a list of users in our system and we would like to find the user with a particular email address:
User ID | Name | Phone Number | |
---|---|---|---|
34587 | [email protected] | Joe Blogs | 1234556789 |
98263 | [email protected] | Adam Smith | 9373526118 |
76291 | [email protected] | Lara Croft | 6735264123 |
In a classical system, we might solve this problem of lookup using indexes. But this can create overheads in other parts of the system, so it is not uncommon that we might consider consider a linear search.
In a completely unordered list like this, we know that the best we can do is simply scanning over each item until we find our match. This is a linear approach since the amount of time it takes to scan through the list is proportional linearly to the number of items in the list.
This is quite often refered to as the "phone book search" problem. A phone book is an ancient pre-historic device that was used to look up a phone number for a particular person. It was sorted by name so that you could find the person you were looking for quickly. But if you wanted to do the opposite, find the name associated to a particular phone number, the best you could do was simply scan through every entry until you found it.
I hope you're already getting excited thinking about how a quanutm computer could solve this quicker. You should be!
Before we step back into the quantum world, let's translate our problem into some more formal mathematical language.
Let's say our database contains objects. We will define a function that takes an index from to and outputs if the database item at that index satisfies our condition and otherwise.
For example, in our above example, if we're looking for the user with email "[email protected]", we have if the object at index has that email address. If we treat the "User ID" as the index then in this case: .
Again, to re-iterate in the mathemtical language, the best a classical computer could do to find would be to check all values from to until it finds one that satisfies .
Now let's go back to the quantum world.
To start constructing our quantum algorithm, we're going to introduce something called a phase oracle. It will be define as follows:
So it essentially sticks a negative sign in front of a particular state and leaves all others alone. We call this a phase oracle since adding a negative sign is just multiplying by a global phase .
We can represent this oracle in outer-product notation:
We can see this works, in the case when :
We will apply one additional constraint before we construct the algorithm: the database will only have a single entry satisfying the condition i.e. there will only exist a single value of such that . It's possible to generalise to multiple solutions, but we'll keep it simple for now.
Let's define the solution we're looking for as i.e. . Grover's search will then run as follows:
Ok, that's quite a lot to take in. Before we start looking into how this works, let's look at the circuit representation:
The numbers at the bottom represent the corresponding steps in the algorithm listed above. We've also added a bracket over steps 3-6 labelled . We will refer to this part as the Grover iterator. This will be the part that gets repeated several times.
Analysing this algorithm revolves around finding a nice mathematical representation of this operator. Formally, we can see it's defined as:
In fact, we'll split it up more by defining the Grover diffusion operator :
So
We can simplify :
Since the Hadamard gate is its own inverse, the second term here cancels to
The first bracket in the first term we know is just the even superposition of all possible qubit states: . We will need this state a lot, so we're going to denote it as . The second bracket is just the corresponding bra of this ket: . This comes from some maths we haven't discussed yet, but just know that since the two terms in the braket are real (have no imaginary coefficients) the bra can be written this way simply by flipping the ket.
Putting this together we now have:
And thus:
The next step of our journey will be building some geometric intuition for what these operators are really doing.
What we will show now, is that an operator of the form actually represents a geometric reflection.
To see this, imagine we have 2 orthogonal vectors and where, since they are orthogonal, we have . We define a vector in terms of these two: and then see how our operator behaves on it:
As you can see, the operator flipped the component of and left the component of untouched. Thus this represents a reflection or mirroring around the vector .
Now we know this a slight re-arranging of our definition of tells us so much more:
We can see that is in fact a reflection around followed by a reflection around then negated.
If you remember your rules of geometric transforms well, you will know that 2 reflections always make a rotation. So we can see that our Grover iterator is performing a rotation of our vector every time we apply it.
We'd like to be able to visualise this rotation; to see where it's rotating from and where it's rotating to. The problem is that we have qubits and thus different basis states. This means if we have 1000 items to search through, we have a 1000-dimensional vector space! That's not very easy to visualise...
Luckily we have a handy tool up our sleeve: projection. Just in the same way your eyes right now are projecting the 3D world into a 2D image on your retina, we can project our problem into a more manageable number of dimensions.
Surprisingly, we can project it down into just 2!
Our first vector will simply be the solution we are looking for: .
The second vector we will choose will be the even superposition of all states we are NOT looking for i.e. an even superposition of all where . Since we specified there will be only one solution in our case, there are such values of so we can write our second vector as:
The choice to label this vector will become apparent shortly.
It's fairly clear these two vectors are orthogonal, since they don't share any of the same kets. And thus we can plot them in 2D:
Ok, so we've got two basis vectors for our space. Now we need to know where in the space to plot our state vector. Since we've applied all Hadamard gates to the all state, we know that we start in the state .
We also know all our state vectors should be of unit length, that's what the normalisation coefficients are for, so we have to determine whether it is closer to or . We can do this with the inner-product since we know two orthogonal vectors inner-product to 0 and two parallel vectors to 1:
From these results, it is fairly clear that our initial state vector lies closer to than , so let's plot our starting point. We'll denote our starting state as as we have done before:
Now to find we just apply our Grover iterator: . We know, from earlier, that this operation is a reflection around followed by a reflection around then negated.
A nice result from 2D geometry is that a negated reflection around a vector is equal to a reflection around a perpendicular vector.
So we could pull the negative sign of the Grover iterator inside to the reflection around and make it a reflection around
So our Grover iterator is now a reflection around followed by a reflection around .
So this looks like:
Here, the bottom dotted line represents the first reflection around .
So we can see that the Grover iterator is rotating our state away from the vector of non-solutions, , and towards the solution vector !
Once we have got near to the solution we can simply measure the state and we will have a high probability of finding the correct solution. The only question is how many times do we have to rotate? i.e. how many times do we have to apply the Grover iterator?
To answer this, we'll have to look at the angles involved in the rotations. We can see this more easily on the following diagram:
We can see clearly some doubling relations between angles here. The green angle, the result after the initial reflection around , is double the red angle and the blue angle, the result after the reflection around , is double the green angle. And finally, the yellow angle is clearly half the blue angle.
Putting this all together we see that the angle between and is simply double the red angle, the angle between and .
So what is the red angle?
We can use the following geometric result on the inner-product: . In our case and are unit vectors, so the length scales can be ignored: .
But we already calculated this inner-product earlier it's:
So we have:
Using sin/cos relations:
For small we have so:
Now remember every application of the Grover iterator rotates by so after iterations we are at an angle of .
We reach the solution when this angle equals which, when subbing into the previous equation, is when .
Subbing now in our approximation of :
Wow, that was a lot of work! But we're finally done!
We've shown that we require only steps to reach the solution.
Remember a classical algorithm would take steps, so we've found a significant improvement!
> Next Article (Quantum Fourier Transform)