User:Omrika/sandbox/QIP/Quantum counting

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  Grover's search algorithm.

The ability to perform quantum counting efficiently unties a major issue in Grover's search algorithm that arises from the relation between the number of iterations to be done and the number of existing solutions. Moreover, the algorithm can be used to solve a quantum existence problem.

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

The problem
Consider a finite set $$ \{0,1\}^n $$ in the size of $$N=2^n$$ and $$B$$ a set of solutions.

Let $$ f : \left\{0,1\right\}^n \rightarrow \left\{0,1\right\}$$ such that $$ f(x) = \begin{cases} 1 & \text{if } x \in B \\ 0  & \text{if } x \notin B \end{cases} $$.

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

Classical solution
Without any prior knowledge on the data structure or any other exploits in the problem, the classical solution can't perform better than $$O(N)$$ (consider a case where the last element to be inspected is a solution).

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

Create superposition
The initial state of the system is $$ |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$$\frac{1}{2^{p/2}}(|0\rangle + |1\rangle)^{\otimes p}$$ and the state of the second register is $$\frac{1}{2^{n/2}}(|0\rangle + |1\rangle)^{\otimes n} = \frac{1}{\sqrt{N}}\sum_{x=0}^{N-1}|x\rangle$$ - equal superposition state in the computational basis.

Grover operator
For an $$ N $$ size search space with $$ M $$ solutions $$ x \in B$$, we can define the following normalized states $$ |\alpha\rangle = \frac{1}{\sqrt{N-M}} \sum_{x \notin B}{|x\rangle}$$ and $$|\beta\rangle = \frac{1}{\sqrt{M}} \sum_{x \in B}{|x\rangle} $$.

Note that $$ \sqrt{\frac{N-M}{N}}|\alpha\rangle + \sqrt{\frac{M}{N}}|\beta\rangle = \frac{1}{\sqrt{N}}\sum_{x=0}^{N-1}{x}$$ i.e. the state of the second register after Hadamard transform.

Geometric visualization of Grover's algorithm shows that in the two dimensional space spanned by $$|\alpha\rangle$$ and $$|\beta\rangle$$, the Grover operator is a  counterclockwise rotation ,hence, it can be expressed as $$ G = \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta\end{bmatrix} $$ in the basis of $$|\alpha\rangle$$ and $$|\beta\rangle$$.

Using the trigonometric identity $$ \cos^2 \theta + \sin^2 \theta = 1 $$ we see that $$GG^{\dagger} = I$$, meaning $$G$$ is unitary with eigenvalues  of $$e^{\pm i \theta}$$.

Estimating $$\theta$$'s value
From here onwards, we can follow the quantum phase estimation algorithm  scheme - apply  controlled Grover  operations followed by inverse  quantum fourier transform  and we will measure the correct value of $$\theta$$ with probability larger than $$\frac{4}{\pi^2}$$.

Analysis
Assuming that we've doubled the search space so that $$ M \leq \frac{N}{2} $$, we have $$ \sin {\frac{\theta}{2}} = \sqrt{\frac{M}{2N}}$$ as a result from Grover's algorithm analysis.

The error $$ \frac{\vert \Delta M \vert}{2N} = \Bigg \vert \sin^2 \left( \frac{\theta + \Delta \theta}{2} \right)  - \sin^2 \left( \frac{\theta}{2} \right) \Bigg \vert $$ is determined by the error in $$\theta$$'s estimation, meaning that for a $$ p $$ large enough we will have $$\Delta \theta \approx 0$$ hence $$ \vert \Delta M \vert \approx 0 $$.

A known result of Grover's search algorithm is that the number of iterations that should be done can be calculated by $$\frac{\pi}{4}\sqrt{\frac{N}{M}}$$.

If $$ N $$ is known and $$\theta$$ is evaluated correctly, using the result of the Quantum counting algorithm $$ M $$, it is possible to determine how many iterations should be done in Grover's search algorithm.

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

An Hamiltonian cycle  is a simple cycle that visits all vertices in a graph. The Hamiltonian cycle problem is determining whether a graph $$G=(V,E)$$ has an Hamiltonian cycle. This problem is NP-complete.

A simple solution to the Hamiltonian cycle problem is to check for each ordering of $$G$$'s vertices whether it is an hamiltonian cycle or not until such ordering is found. Searching through all the possible combinations 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.

Quantum existence problem
Quantum existence problem is a special case of quantum counting where we don't want to calculate $$M$$'s value but we only wish to decide whether $$ M \neq 0 $$ or not. Trivially it is possible to use the quantum counting algorithm and if it yields $$M \neq 0$$ then we have an answer to the existence problem. This approach involves some overhead information because we are not interested in the value of $$M$$.

A setup with little amount of qubits in the upper register won't be able to estimate $$\theta$$'s value with high accuracy, but it can be shown that the algorithm will yield a good enough result for $$\theta$$ to know whether $$M$$ equals zero or not.