# Quantum counting algorithm

Quantum counting algorithm is a quantum algorithm for efficiently counting the number of solutions for a given search problem. The algorithm is based on the quantum phase estimation algorithm and on Grover's search algorithm.

Counting problems are common in diverse fields such as statistical estimation, statistical physics, networking, etc. As for quantum 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 whether any solution exists) as a special case.

The algorithm was devised by Gilles Brassard, Peter Høyer and Alain Tapp in 1998.

## The problem

Consider a finite set ${\displaystyle \{0,1\}^{n}}$ of size ${\displaystyle N=2^{n}}$ and a set ${\displaystyle B}$ of "solutions" (that is a subset of ${\displaystyle \{0,1\}^{n}}$). Define:

${\displaystyle {\begin{cases}f:\left\{0,1\right\}^{n}\to \{0,1\}\\f(x)={\begin{cases}1&x\in B\\0&x\notin B\end{cases}}\end{cases}}}$

In other words, ${\displaystyle f}$ is the indicator function of ${\displaystyle B}$.

Calculate the number of solutions ${\displaystyle M=\left\vert f^{-1}(1)\right\vert =\vert B\vert }$.[1]

### Classical solution

Without any prior knowledge on the set of solutions ${\displaystyle B}$ (or the structure of the function ${\displaystyle f}$), a classical deterministic solution cannot perform better than ${\displaystyle \Omega (N)}$, because all the ${\displaystyle N}$ elements of ${\displaystyle \{0,1\}^{n}}$ must be inspected (consider a case where the last element to be inspected is a solution).

## The algorithm

Quantum counting circuit

### Setup

The input consists of two registers (namely, two parts): the upper ${\displaystyle p}$ qubits comprise the first register, and the lower ${\displaystyle n}$ qubits are the second register.

### Create superposition

The initial state of the system is ${\displaystyle |0\rangle ^{\otimes p}|0\rangle ^{\otimes n}}$. After applying multiple bit Hadamard gate operation on each of the registers separately, the state of the first register is

${\displaystyle {\frac {1}{2^{p/2}}}(|0\rangle +|1\rangle )^{\otimes p}}$

and the state of the second register is

${\displaystyle {\frac {1}{2^{n/2}}}(|0\rangle +|1\rangle )^{\otimes n}={\frac {1}{\sqrt {N}}}\sum _{x=0}^{N-1}|x\rangle }$

an equal superposition state in the computational basis.

### Grover operator

Because the size of the space is ${\displaystyle \left\vert \{0,1\}^{n}\right\vert =2^{n}=N}$ and the number of solutions is ${\displaystyle \left\vert B\right\vert =M}$, we can define the normalized states:[2]:252

${\displaystyle |\alpha \rangle ={\frac {1}{\sqrt {N-M}}}\sum _{x\notin B}{|x\rangle },\qquad {\text{and}}\qquad |\beta \rangle ={\frac {1}{\sqrt {M}}}\sum _{x\in B}{|x\rangle }.}$

Note that

${\displaystyle {\sqrt {\frac {N-M}{N}}}|\alpha \rangle +{\sqrt {\frac {M}{N}}}|\beta \rangle ={\frac {1}{\sqrt {N}}}\sum _{x=0}^{N-1}{|x\rangle },}$

which is the state of the second register after the Hadamard transform.

Geometric visualization of Grover's algorithm shows that in the two-dimensional space spanned by ${\displaystyle |\alpha \rangle }$ and ${\displaystyle |\beta \rangle }$, the Grover operator is a counterclockwise rotation; hence, it can be expressed as

${\displaystyle G={\begin{bmatrix}\cos \theta &-\sin \theta \\\sin \theta &\cos \theta \end{bmatrix}}}$

in the orthonormal basis ${\displaystyle \{|\alpha \rangle ,|\beta \rangle \}}$.[2]:252[3]:149

From the properties of rotation matrices we know that ${\displaystyle G}$ is a unitary matrix with the two eigenvalues ${\displaystyle e^{\pm i\theta }}$.[2]:253

### Estimating the value of ${\displaystyle \theta }$

From here onwards, we follow the quantum phase estimation algorithm scheme: we apply controlled Grover operations followed by inverse quantum fourier transform; and according to the analysis, we will find the best ${\displaystyle p}$-bit approximation to the real number ${\displaystyle \theta }$ (belonging to the eigenvalues ${\displaystyle e^{\pm i\theta }}$ of the Grover operator) with probability higher than ${\displaystyle {\frac {4}{\pi ^{2}}}}$.[4]:348[3]:157

Note that the second register is actually in a superposition of the eigenvectors 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 ${\displaystyle \theta }$, and with some probability, we approximate ${\displaystyle 2\pi -\theta }$; those two approximations are equivalent.[2]:224–225

### Analysis

Assuming that the size ${\displaystyle N}$ of the space is at least twice the number of solutions (namely, assuming that ${\displaystyle M\leq {\tfrac {N}{2}}}$), a result of the analysis of Grover's algorithm is:[2]:254

${\displaystyle \sin {\frac {\theta }{2}}={\sqrt {\frac {M}{N}}}.}$

Thus, if we find ${\displaystyle \theta }$, we can also find the value of ${\displaystyle M}$ (because ${\displaystyle N}$ is known).

The error

${\displaystyle {\frac {\vert \Delta M\vert }{N}}=\left\vert \sin ^{2}\left({\frac {\theta +\Delta \theta }{2}}\right)-\sin ^{2}\left({\frac {\theta }{2}}\right)\right\vert }$

is determined by the error in the estimation of the value of ${\displaystyle \theta }$. The quantum phase estimation algorithm finds, with high probability, the best ${\displaystyle p}$-bit approximation of ${\displaystyle \theta }$; this means that if ${\displaystyle p}$ is large enough, we will have ${\displaystyle \Delta \theta \approx 0}$, hence ${\displaystyle \vert \Delta M\vert \approx 0}$.[2]:263

## Uses

### Grover's search algorithm for an initially-unknown number of solutions

In Grover's search algorithm, the number of iterations that should be done is ${\displaystyle {\frac {\pi }{4}}{\sqrt {\frac {N}{M}}}}$.[2]:254 [3]:150

Thus, if ${\displaystyle N}$ is known and ${\displaystyle M}$ is calculated by the quantum counting algorithm, the number of iterations for Grover's algorithm is easily calculated.

### Speeding up NP-complete problems

The quantum counting algorithm can be used to speed up solution to problems which are NP-complete.

An example of an NP-complete problem is the Hamiltonian cycle problem, which is the problem of determining whether a graph ${\displaystyle G=(V,E)}$ has a Hamiltonian cycle.

A simple solution to the Hamiltonian cycle problem is checking, for each ordering of the vertices of ${\displaystyle G}$, 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 speed up of 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

Quantum existence problem is a special case of quantum counting where we do not want to calculate the value of ${\displaystyle M}$, but we only wish to know whether ${\displaystyle M\neq 0}$ or not. A trivial solution to this problem is directly using the quantum counting algorithm: the algorithm yields ${\displaystyle M}$, so by checking whether ${\displaystyle M\neq 0}$ we get the answer to the existence problem. This approach involves some overhead information because we are not interested in the value of ${\displaystyle M}$.

Using a setup with small number of qubits in the upper register will not produce an accurate estimation of the value of ${\displaystyle \theta }$, but will suffice to determine whether ${\displaystyle M}$ equals zero or not.[2]:263