Path: blob/main/notebooks/ch-algorithms/quantum-counting.ipynb
3328 views
Quantum Counting
To understand this algorithm, it is important that you first understand both Grover’s algorithm and the quantum phase estimation algorithm. Whereas Grover’s algorithm attempts to find a solution to the Oracle, the quantum counting algorithm tells us how many of these solutions there are. This algorithm is interesting as it combines both quantum search and quantum phase estimation.
1. Overview
1.1 Intuition
In quantum counting, we simply use the quantum phase estimation algorithm to find an eigenvalue of a Grover search iteration. You will remember that an iteration of Grover’s algorithm, , rotates the state vector by in the , basis:
The percentage number of solutions in our search space affects the difference between and . For example, if there are not many solutions, will be very close to and will be very small. It turns out that the eigenvalues of the Grover iterator are , and we can extract this using quantum phase estimation (QPE) to estimate the number of solutions ().
1.2 A Closer Look
In the , basis we can write the Grover iterator as the matrix:
The matrix has eigenvectors:
With the aforementioned eigenvalues . Fortunately, we do not need to prepare our register in either of these states, the state is in the space spanned by , , and thus is a superposition of the two vectors.
As a result, the output of the QPE algorithm will be a superposition of the two phases, and when we measure the register we will obtain one of these two values! We can then use some simple maths to get our estimate of .
In this guide will choose to ‘count’ on the first 4 qubits on our circuit (we call the number of counting qubits , so ), and to 'search' through the last 4 qubits (). With this in mind, we can start creating the building blocks of our circuit.
2.2 The Controlled-Grover Iteration
We have already covered Grover iterations in the Grover’s algorithm section. Here we'll use the circuit library to quickly create a circuit that does one iteration of Grover's algorithm. The oracle we'll use has 5 solutions () of 16 states ().
Notice the python function takes no input and returns a Gate
object with 4 qubits. Later in this page, we'll use the .control()
method to create a controlled gate from a Gate
.
Again, note we have chosen to return another QuantumCircuit
object, this is so we can easily invert the gate. We create the gate with t = 4 qubits as this is the number of counting qubits we have chosen in this guide:
We can see two values stand out, having a much higher probability of measurement than the rest. These two values correspond to and , but we can’t see the number of solutions yet. We need to little more processing to get this information, so first let us get our output into something we can work with (an int
).
We will get the string of the most probable result from our output data (although either will do):
Let us now store this as an integer:
4. Finding the Number of Solutions (M)
We will create a function, calculate_M()
that takes as input the decimal integer output of our register, the number of counting qubits () and the number of searching qubits ().
First we want to get from measured_int
. You will remember that QPE gives us a measured from the eigenvalue , so to get we need to do:
Or, in code:
You may remember that we can get the angle can from the inner product of and :
And that (a uniform superposition of computational basis states) can be written in terms of and as:
The inner product of and is:
From this, we can use some trigonometry and algebra to show:
And in code:
And we can see we have (approximately) the correct answer! We can approximately calculate the error in this answer using:
Explaining the error calculation is outside the scope of this article, but an explanation can be found in [1].
Finally, here is the finished function calculate_M()
: