Skip to content

Haimasker/Grover-search-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Grover's search algorithm (GSA)

Simple C++ implementation of Grover's search algorithm with Qubit class.

GitHub code size in bytes Number of lines of code Code language count GitHub top language


Preamble

The purpose of this project is to create functions that implements Grover's quantum search algorithm.
Qubit quantum register is presented via Qubit class.

GSA is the quantum algorithm that finds root $x$ of some function $f(x)$ among the values $1...N$.
Usage of a quantum register allows not to perform a brute force to find root of the function.
There are two types of this algorithm: $f(x)$ has only one root and $f(x)$ has at least one root (singleGSA() and multipleGSA respectively).


Description

First of all, I recommend you to read some theoretical information about GSA and understand how it works here and here.
Let's tale a closer look to phase Oracle and Grover's diffusion operators (query and diffusion matrices in programm respectively).

  • Phase Oracle matrix has the following form:
        [ 1 0 ........ 0 ]
        [ 0 1 0 ...... 0 ]
query = [ .............. ]
        [ 0 ... -1 ... 0 ]
        [ 0 0 ........ 0 ]

So, the phase Oracle matrix is the square diagonal matrix with ones, but there is -1 at the row the number of which is equal to the root of the function.
The phase Oracle applies a conditional phase shift of −1 for the solution item.
Query matrix for the multiple roots case defines similarly.

  • Diffusion matrix has the following form:
            [ 2/N-1 2/N ........ 2/N ]
            [ 2/N   2/N-1 ...... 2/N ]
diffusion = [ ...................... ]
            [ 2/N ........ 2/N-1 2/N ]
            [ 2/N ........ 2/N 2/N-1 ]

The Grover's diffusion operator can be geometrically interpreted as a reflection in the vector space about the uniform superposition state.


Functions

  • singleGSA - takes qubit register size and solution item index.
    If index is equal to 0 then it determines randomly.
void singleGSA(unsigned, unsigned = 0);
  • multipleGSA - takes qubit register size and vector of solution items indices.
    If vector is empty then it determines randomly.
void multipleGSA(unsigned, std::set<unsigned> = {});