TheQuantum counting algorithm is aquantum algorithm for efficiently counting the number of solutions for a given search problem.The algorithm is based on thequantum phase estimation algorithm and onGrover's search algorithm.
Counting problems are common in diverse fields such as statistical estimation, statistical physics, networking, etc. As forquantum computing, the ability to perform quantum counting efficiently is needed in order to use Grover's search algorithm (because running Grover's search algorithm requires knowing how many solutions exist). Moreover, this algorithm solves the quantum existence problem (namely, deciding whetherany solution exists) as a special case.
The algorithm was devised byGilles Brassard, Peter Høyer and Alain Tapp in 1998.
Consider a finite set of size and a set of "solutions" (that is a subset of). Define:
In other words, is theindicator function of.
Calculate the number of solutions.[1]
Without any prior knowledge on the set of solutions (or the structure of the function), a classical deterministic solution cannot perform better than, because all the elements of must be inspected (consider a case where the last element to be inspected is a solution).

The input consists of tworegisters (namely, two parts): the upper qubits comprise thefirst register, and the lower qubits are thesecond register.
The initial state of the system is. After applying multiple bitHadamard gate operation on each of the registers separately, the state of thefirst register is
and the state of thesecond register is
an equalsuperposition state in the computational basis.
Because the size of the space is and the number of solutions is, we can define the normalized states:[2]: 252
Note that
which is the state of thesecond register after the Hadamard transform.
Geometric visualization of Grover's algorithm shows that in the two-dimensional space spanned by and, the Grover operator is acounterclockwise rotation; hence, it can be expressed as
in theorthonormal basis.[2]: 252 [3]: 149
From theproperties of rotation matrices we know that is aunitary matrix with the twoeigenvalues.[2]: 253
From here onwards, we follow thequantum phase estimation algorithm scheme: we applycontrolled Grover operations followed by inversequantum Fourier transform; and according to theanalysis, we will find the best-bit approximation to thereal number (belonging to the eigenvalues of the Grover operator) with probability higher than.[4]: 348 [3]: 157
Note that the second register is actually in asuperposition of theeigenvectors of the Grover operator (while in the original quantum phase estimation algorithm, the second register is the required eigenvector). This means that with some probability, we approximate, and with some probability, we approximate; those two approximations are equivalent.[2]: 224–225
Assuming that the size of the space is at least twice the number of solutions (namely, assuming that), a result of the analysis of Grover's algorithm is:[2]: 254
Thus, if we find, we can also find the value of (because is known).
The error
is determined by the error within estimation of the value of. The quantum phase estimation algorithm finds, with high probability, the best-bit approximation of; this means that if is large enough, we will have, hence.[2]: 263
In Grover's search algorithm, the number of iterations that should be done is.[2]: 254 [3]: 150
Thus, if is known and is calculated by the quantum counting algorithm, the number of iterations for Grover's algorithm is easily calculated.
The quantum counting algorithm can be used to speed up solution to problems which areNP-complete.
An example of an NP-complete problem is theHamiltonian cycle problem, which is the problem of determining whether agraph has aHamiltonian cycle.
A simple solution to the Hamiltonian cycle problem is checking, for each ordering of the vertices of, whether it is a Hamiltonian cycle or not. Searching through all the possible orderings of the graph's vertices can be done with quantum counting followed by Grover's algorithm, achieving a speedup of the square root, similar to Grover's algorithm.[2]: 264 This approach finds a Hamiltonian cycle (if exists); for determining whether a Hamiltonian cycle exists, the quantum counting algorithm itself is sufficient (and even the quantum existence algorithm, described below, is sufficient).
Quantum existence problem is a special case of quantum counting where we do not want to calculate the value of, but we only wish to know whether or not.[5]: 147
A trivial solution to this problem is directly using the quantum counting algorithm: the algorithm yields, so by checking whether we get the answer to the existence problem. This approach involves some overhead information because we are not interested in the value of. Quantum phase estimation can be optimized to eliminate this overhead.[5]: 148
If you are not interested in the control of error probability then using a setup with small number of qubits in the upper register will not produce an accurate estimation of the value of, but will suffice to determine whether equals zero or not.[2]: 263
Quantum relation testing. is an extension of quantum existence testing, it decides whether at least one entry can be found in the data base which fulfils the relation to a certain reference value.[6] E.g. gives back YES if the data base contains any value larger than 5 else it returns NO. Quantum relation testing combined with classical logarithmic search forms an efficient quantum min/max searching algorithm.[5]: 152 [7]
{{cite book}}: CS1 maint: multiple names: authors list (link)