# Grover's algorithm

**Grover's algorithm** is a quantum algorithm that finds with high probability the unique input to a black box function that produces a particular output value, using just evaluations of the function, where is the size of the function's domain. It was devised by Lov Grover in 1996.

The analogous problem in classical computation cannot be solved in fewer than evaluations (because, in the worst case, the -th member of the domain might be the correct member). At roughly the same time that Grover published his algorithm, Bennett, Bernstein, Brassard, and Vazirani proved that any quantum solution to the problem needs to evaluate the function times, so Grover's algorithm is asymptotically optimal.[1]

It has been shown that a non-local hidden variable quantum computer could implement a search of an -item database in at most steps. This is faster than the steps taken by Grover's algorithm. Neither search method will allow quantum computers to solve NP-Complete problems in polynomial time.[2]

Unlike other quantum algorithms, which may provide exponential speedup over their classical counterparts, Grover's algorithm provides only a quadratic speedup. However, even quadratic speedup is considerable when is large. Grover's algorithm could brute-force a 128-bit symmetric cryptographic key in roughly 2^{64} iterations, or a 256-bit key in roughly 2^{128} iterations. As a result, it is sometimes suggested[3] that symmetric key lengths be doubled to protect against future quantum attacks.

Like many quantum algorithms, Grover's algorithm is probabilistic in the sense that it gives the correct answer with a probability of less than 1. Though there is technically no upper bound on the number of repetitions that might be needed before the correct answer is obtained, the expected number of repetitions is a constant factor that does not grow with . Grover's original paper described the algorithm as a database search algorithm, and this description is still common. The database in this analogy is a table of all of the function's outputs, indexed by the corresponding input.

## Applications

Although the purpose of Grover's algorithm is usually described as "searching a database", it may be more accurate to describe it as "inverting a function". In fact since the oracle for an unstructured database requires at least linear complexity, the algorithm cannot be used for actual databases.[4] Roughly speaking, if a function can be evaluated on a quantum computer, Grover's algorithm calculates when given . Inverting a function is related to the searching of a database because we could come up with a function that produces one particular value of ("true", for instance) if matches a desired entry in a database, and another value of ("false") for other values of .

Grover's algorithm can also be used for estimating the mean and median of a set of numbers. The algorithm can be further optimized if there is more than one matching entry and the number of matches is known beforehand.

## Setup

Consider an unsorted database with entries. The algorithm requires an -dimensional state space *H*, which can be supplied by *n* = log_{2} *N* qubits. Consider the problem of determining the index of the database entry that satisfies some search criterion. Let *f* be the function that maps database entries to 0 or 1, where *f*(*x*) = 1 if and only if *x* satisfies the search criterion (*x* = *ω*). We are provided with (quantum black box) access to a subroutine in the form of a unitary operator *U _{ω}* that acts as follows:

An alternative definition of *U _{ω}* may be encountered assuming the presence of an ancillary qubit system (like in the quantum circuit depicted below). The operation then represents a conditional inversion (

*NOT*gate) conditioned by the value of

*f*(

*x*) on the main system:

or briefly,

This is a natural way to realize a binary operation using the method of uncomputation. Note that if the ancillary qubit is prepared in the state , the effective operation of this controlled *NOT* gate becomes equivalent to the original form, leaving the ancillary system disentangled from the main system:

In either setting, our goal is to identify the index .

## Algorithm steps

The steps of Grover's algorithm are given as follows. Let denote the uniform superposition over all states

Then the operator

is known as the Grover diffusion operator.

Here is the algorithm:

- Initialize the system to the state

. - Perform the following "Grover iteration" times. The function , which is asymptotically , is described below.
- Apply the operator .
- Apply the operator .

- Perform the measurement Ω. The measurement result will be eigenvalue
*λ*with probability approaching 1 for_{ω}*N*≫ 1. From*λ*_{ω},*ω*may be obtained.

## The first iteration

A preliminary observation, in parallel with our definition

is that can be expressed in an alternate way:

In other words, both transformations are of Householder transformation type. To prove this it suffices to check how acts on basis states:

The following computations show what happens in the first iteration:

It is worth noting the special case of *N* = 4 with a single marked state. This has , meaning that in a single application of the Grover iterator the marked state is returned.

After application of the operators and , the square amplitude of the queried element has increased from to .

## Description of

Grover's algorithm requires a "quantum oracle" operator , which can recognize solutions to the search problem and give them a negative sign. In order to keep the search algorithm general, we will leave the inner workings of the oracle as a black box, but will explain how the sign is flipped. The oracle contains a function that returns if is a solution to the search problem and otherwise. The oracle is a unitary operator operating on two qubits:

where is the index qubit and is the oracle qubit.

As usual, denotes addition modulo 2. The operation flips the oracle qubit if and leaves it unchanged otherwise. In Grover's algorithm we want to flip the sign of the state if it labels a solution. This is achieved by setting the oracle qubit in the state , which is flipped to if is a solution:

We regard as flipped, thus the oracle qubit is not changed, so by convention the oracle qubits are usually not mentioned in the specification of Grover's algorithm. Thus the operation of the oracle is simply written as

## Geometric proof of correctness

Consider the plane spanned by and ; equivalently, the plane spanned by and the perpendicular ket . We will consider the first iteration, acting on the initial ket . Since is one of the basis vectors in the overlap is

In geometric terms, the angle between and is given by

The operator is a reflection at the hyperplane orthogonal to for vectors in the plane spanned by and , i.e. it acts as a reflection across . The operator is a reflection through . Therefore, the state vector remains in the plane spanned by and after each application of the operators and , and it is straightforward to check that the operator of each Grover iteration step rotates the state vector by an angle of .

We need to stop when the state vector passes close to ; after this, subsequent iterations rotate the state vector *away* from , reducing the probability of obtaining the correct answer. The exact probability of measuring the correct answer is

where *r* is the (integer) number of Grover iterations. The earliest time that we get a near-optimal measurement is therefore .

## Algebraic proof of correctness

To complete the algebraic analysis, we need to find out what happens when we repeatedly apply . A natural way to do this is by eigenvalue analysis of a matrix. Notice that during the entire computation, the state of the algorithm is a linear combination of and . We can write the action of and in the space spanned by as:

So in the basis (which is neither orthogonal nor a basis of the whole space) the action of applying followed by is given by the matrix

This matrix happens to have a very convenient Jordan form. If we define , it is

- where

It follows that *r*-th power of the matrix (corresponding to *r* iterations) is

Using this form, we can use trigonometric identities to compute the probability of observing *ω* after *r* iterations mentioned in the previous section,

Alternatively, one might reasonably imagine that a near-optimal time to distinguish would be when the angles 2*rt* and −2*rt* are as far apart as possible, which corresponds to , or . Then the system is in state

A short calculation now shows that the observation yields the correct answer *ω* with error *O*(1/*N*).

## Extension to space with multiple targets

If, instead of 1 matching entry, there are *k* matching entries, the same algorithm works, but the number of iterations must be instead of .

There are several ways to handle the case if *k* is unknown. For example, one could run Grover's algorithm several times, with

iterations. For any *k*, one of the iterations will find a matching entry with a sufficiently high probability. The total number of iterations is at most

which is still . It can be shown that this can be improved. If the number of marked items is *k*, where *k* is unknown, there is an algorithm that finds the solution in queries. This fact is used in order to solve the collision problem.[5][6]

## Quantum partial search

A modification of Grover's algorithm called quantum partial search was described by Grover and Radhakrishnan in 2004.[7] In partial search, one is not interested in finding the exact address of the target item, only the first few digits of the address. Equivalently, we can think of "chunking" the search space into blocks, and then asking "in which block is the target item?". In many applications, such a search yields enough information if the target address contains the information wanted. For instance, to use the example given by L. K. Grover, if one has a list of students organized by class rank, we may only be interested in whether a student is in the lower 25%, 25–50%, 50–75% or 75–100% percentile.

To describe partial search, we consider a database separated into blocks, each of size . The partial search problem is easier. Consider the approach we would take classically – we pick one block at random, and then perform a normal search through the rest of the blocks (in set theory language, the complement). If we don't find the target, then we know it's in the block we didn't search. The average number of iterations drops from to .

Grover's algorithm requires iterations. Partial search will be faster by a numerical factor that depends on the number of blocks . Partial search uses global iterations and local iterations. The global Grover operator is designated and the local Grover operator is designated .

The global Grover operator acts on the blocks. Essentially, it is given as follows:

- Perform standard Grover iterations on the entire database.
- Perform local Grover iterations. A local Grover iteration is a direct sum of Grover iterations over each block.
- Perform one standard Grover iteration.

The optimal values of and are discussed in the paper by Grover and Radhakrishnan. One might also wonder what happens if one applies successive partial searches at different levels of "resolution". This idea was studied in detail by Korepin and Xu, who called it binary quantum search. They proved that it is not in fact any faster than performing a single partial search.

## Optimality

It is known that Grover's algorithm is optimal. That is, any algorithm that accesses the database only by using the operator *U _{ω}* must apply

*U*at least as many times as Grover's algorithm.[1] This result is important in understanding the limits of quantum computation.

_{ω}If the Grover's search problem was solvable with log^{c} *N* applications of *U _{ω}*, that would imply that NP is contained in BQP, by transforming problems in NP into Grover-type search problems. The optimality of Grover's algorithm suggests (but does not prove) that NP is not contained in BQP.

The number of iterations for *k* matching entries, *π*(*N*/*k*)^{1/2}/4, is also optimal.[5]

## Applicability and limitations

When applications of Grover's algorithm are considered, it should be emphasized that the database is not represented explicitly. Instead, an oracle is invoked to evaluate an item by its index. Reading a full data-base item by item and converting it into such a representation may take a lot longer than Grover's search. To account for such effects, Grover's algorithm can be viewed as solving an equation or satisfying a constraint. In such applications, the oracle is a way to check the constraint and is not related to the search algorithm. This separation usually prevents algorithmic optimizations, whereas conventional search algorithms often rely on such optimizations and avoid exhaustive search. These and other considerations about using Grover's algorithm are discussed in a paper by Viamontes, Markov and Hayes.[8]

## See also

- Amplitude amplification
- Shor's algorithm (for factorization)
- Brassard-Høyer-Tapp algorithm (for solving the collision problem)

## Notes

- Bennett C.H.; Bernstein E.; Brassard G.; Vazirani U. (1997). "The strengths and weaknesses of quantum computation".
*SIAM Journal on Computing*.**26**(5): 1510–1523. arXiv:quant-ph/9701001. doi:10.1137/s0097539796300933. - Aaronson, Scott. "Quantum Computing and Hidden Variables" (PDF).
- Daniel J. Bernstein (2010-03-03). "Grover vs. McEliece" (PDF). Cite journal requires
`|journal=`

(help) - https://web.eecs.umich.edu/~imarkov/pubs/jour/cise05-grov.pdf
- Michel Boyer; Gilles Brassard; Peter Høyer; Alain Tapp (1998), "Tight Bounds on Quantum Searching",
*Fortsch. Phys.*,**46**: 493–506, arXiv:quant-ph/9605034, Bibcode:1998ForPh..46..493B, doi:10.1002/3527603093.ch10, ISBN 9783527603091 - Andris Ambainis (2004), "Quantum search algorithms",
*SIGACT News*,**35**(2): 22–35, arXiv:quant-ph/0504012, Bibcode:2005quant.ph..4012A, doi:10.1145/992287.992296 - L.K. Grover; J. Radhakrishnan (2005-02-07). "Is partial quantum search of a database any easier?". arXiv:quant-ph/0407122v4.
- Viamontes G.F.; Markov I.L.; Hayes J.P. (2005), "Is Quantum Search Practical?" (PDF),
*Computing in Science and Engineering*,**7**(3): 62–70, arXiv:quant-ph/0405001, doi:10.1109/mcse.2005.53

## References

- Grover L.K.:
*A fast quantum mechanical algorithm for database search*, Proceedings, 28th Annual ACM Symposium on the Theory of Computing, (May 1996) p. 212 - Grover L.K.:
*From Schrödinger's equation to quantum search algorithm*, American Journal of Physics, 69(7): 769-777, 2001. Pedagogical review of the algorithm and its history. - Grover L.K.: QUANTUM COMPUTING: How the weird logic of the subatomic world could make it possible for machines to calculate millions of times faster than they do today
*The Sciences*, July/August 1999, pp. 24–30. - Nielsen, M.A. and Chuang, I.L.
*Quantum computation and quantum information*. Cambridge University Press, 2000. Chapter 6. - What's a Quantum Phone Book?, Lov Grover, Lucent Technologies
- Grover's Algorithm: Quantum Database Search, C. Lavor, L.R.U. Manssur, R. Portugal
- Grover's algorithm on arxiv.org

## External links

Wikiquote has quotations related to: Grover's algorithm |

- Davy Wybiral. "Quantum Circuit Simulator". Archived from the original on 2017-01-16. Retrieved 2017-01-13.
- Craig Gidney (2013-03-05). "Grover's Quantum Search Algorithm".
- François Schwarzentruber (2013-05-18). "Grover's algorithm".
- Alexander Prokopenya. "Quantum Circuit Implementing Grover's Search Algorithm". Wolfram Alpha.
- Hazewinkel, Michiel, ed. (2001) [1994], "Quantum computation, theory of",
*Encyclopedia of Mathematics*, Springer Science+Business Media B.V. / Kluwer Academic Publishers, ISBN 978-1-55608-010-4 - Roberto Maestre (2018-05-11). "Grover's Algorithm implemented in R and C".