# Grover's algorithm

Short description: Quantum search algorithm
In quantum computing, Grover's algorithm, also known as the quantum search algorithm, refers to a quantum algorithm for unstructured search that finds with high probability the unique input to a black box function that produces a particular output value, using just $\displaystyle{ O(\sqrt{N}) }$ evaluations of the function, where $\displaystyle{ N }$ 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 $\displaystyle{ O(N) }$ evaluations (because, on average, one has to check half of the domain to get a 50% chance of finding the right input). Charles H. Bennett, Ethan Bernstein, Gilles Brassard, and Umesh Vazirani proved that any quantum solution to the problem needs to evaluate the function $\displaystyle{ \Omega(\sqrt{N}) }$ times, so Grover's algorithm is asymptotically optimal. Since classical algorithms for NP-complete problems require exponentially many steps, and Grover's algorithm provides at most a quadratic speedup over the classical solution for unstructured search, this suggests that Grover's algorithm by itself will not provide polynomial-time solutions for NP-complete problems (as the square root of an exponential function is an exponential, not polynomial, function).

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 $\displaystyle{ N }$ is large, and Grover's algorithm can be applied to speed up broad classes of algorithms. Grover's algorithm could brute-force a 128-bit symmetric cryptographic key in roughly 264 iterations, or a 256-bit key in roughly 2128 iterations. As a result, it is sometimes suggested that symmetric key lengths be doubled to protect against future quantum attacks.

## Applications and limitations

Grover's algorithm, along with variants like amplitude amplification, can be used to speed up a broad range of algorithms. In particular, algorithms for NP-complete problems generally contain exhaustive search as a subroutine, which can be sped up by Grover's algorithm. The current best algorithm for 3SAT is one such example. Generic constraint satisfaction problems also see quadratic speedups with Grover. These algorithms do not require that the input be given in the form of an oracle, since Grover's algorithm is being applied with an explicit function, e.g. the function checking that a set of bits satisfies a 3SAT instance.

Grover's algorithm can also give provable speedups for black-box problems in quantum query complexity, including element distinctness and the collision problem (solved with the Brassard–Høyer–Tapp algorithm). In these types of problems, one treats the oracle function f as a database, and the goal is to use the quantum query to this function as few times as possible.

### Cryptography

Grover's algorithm essentially solves the task of function inversion. Roughly speaking, if we have a function $\displaystyle{ y = f(x) }$ that can be evaluated on a quantum computer, Grover's algorithm allows us to calculate $\displaystyle{ x }$ when given $\displaystyle{ y }$. Consequently, Grover's algorithm gives broad asymptotic speed-ups to many kinds of brute-force attacks on symmetric-key cryptography, including collision attacks and pre-image attacks. However, this may not necessarily be the most efficient algorithm since, for example, the parallel rho algorithm is able to find a collision in SHA2 more efficiently than Grover's algorithm.

### Limitations

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. However, this database is not represented explicitly. Instead, an oracle is invoked to evaluate an item by its index. Reading a full database 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.

The major barrier to instantiating a speedup from Grover's algorithm is that the quadratic speedup achieved is too modest to overcome the large overhead of near-term quantum computers. However, later generations of fault-tolerant quantum computers with better hardware performance may be able to realize these speedups for practical instances of data.

## Problem description

As input for Grover's algorithm, suppose we have a function $\displaystyle{ f\colon \{0,1,\ldots,N-1\} \to \{0,1\} }$. In the "unstructured database" analogy, the domain represent indices to a database, and f(x) = 1 if and only if the data that x points to satisfies the search criterion. We additionally assume that only one index satisfies f(x) = 1, and we call this index ω. Our goal is to identify ω.

We can access f with a subroutine (sometimes called an oracle) in the form of a unitary operator Uω that acts as follows:

$\displaystyle{ \begin{cases} U_\omega |x\rang = -|x\rang & \text{for } x = \omega \text{, that is, } f(x) = 1, \\ U_\omega |x\rang = |x\rang & \text{for } x \ne \omega \text{, that is, } f(x) = 0. \end{cases} }$

This uses the $\displaystyle{ N }$-dimensional state space $\displaystyle{ \mathcal{H} }$, which is supplied by a register with $\displaystyle{ n = \lceil \log_{2} N \rceil }$ qubits. This is often written as

$\displaystyle{ U_\omega|x\rang = (-1)^{f(x)}|x\rang. }$

Grover's algorithm outputs ω with probability at least 1/2 using $\displaystyle{ O(\sqrt{N}) }$ applications of Uω. This probability can be made arbitrarily large by running Grover's algorithm multiple times. If one runs Grover's algorithm until ω is found, the expected number of applications is still $\displaystyle{ O(\sqrt{N}) }$, since it will only be run twice on average.

### Alternative oracle definition

This section compares the above oracle $\displaystyle{ U_\omega }$ with an oracle $\displaystyle{ U_f }$.

Uω is different from the standard quantum oracle for a function f. This standard oracle, denoted here as Uf, uses an ancillary qubit system. The operation then represents an inversion (NOT gate) on the main system conditioned by the value of f(x) from the ancillary system:

$\displaystyle{ \begin{cases} U_f |x\rang |y\rang = |x\rang |\neg y\rang & \text{for } x = \omega \text{, that is, } f(x) = 1, \\ U_f |x\rang |y\rang = |x\rang |y\rang & \text{for } x \ne \omega \text{, that is, } f(x) = 0, \end{cases} }$

or briefly,

$\displaystyle{ U_f |x\rang |y\rang = |x\rang |y \oplus f(x)\rang. }$

These oracles are typically realized using uncomputation.

If we are given Uf as our oracle, then we can also implement Uω, since Uω is Uf when the ancillary qubit is in the state $\displaystyle{ |-\rang = \frac1{\sqrt2}\big(|0\rang - |1\rang\big) = H|1\rang }$:

\displaystyle{ \begin{align} U_f \big( |x\rang \otimes |-\rang \big) &= \frac1{\sqrt2} \left( U_f |x\rang |0\rang - U_f |x\rang |1\rang \right)\\ &= \frac1{\sqrt2} \left(|x\rang |f(x)\rang - |x\rang |1 \oplus f(x)\rang \right)\\ &= \begin{cases} \frac1{\sqrt2} \left(-|x\rang |0\rang + |x\rang |1\rang\right) & \text{if } f(x) = 1, \\ \frac1{\sqrt2} \left( |x\rang |0\rang - |x\rang |1\rang \right) & \text{if } f(x) = 0 \end{cases} \\ &= (U_\omega |x\rang) \otimes |-\rang \end{align} }

So, Grover's algorithm can be run regardless of which oracle is given. If Uf is given, then we must maintain an additional qubit in the state $\displaystyle{ |-\rang }$ and apply Uf in place of Uω.

## Algorithm

The steps of Grover's algorithm are given as follows:

1. Initialize the system to the uniform superposition over all states
$\displaystyle{ |s\rangle = \frac{1}{\sqrt{N}} \sum_{x=0}^{N-1} |x\rangle. }$
2. Perform the following "Grover iteration" $\displaystyle{ r(N) }$ times:
1. Apply the operator $\displaystyle{ U_\omega }$
2. Apply the Grover diffusion operator $\displaystyle{ U_s = 2 \left|s\right\rangle \left\langle s\right| - I }$
3. Measure the resulting quantum state in the computational basis.

For the correctly chosen value of $\displaystyle{ r }$, the output will be $\displaystyle{ |\omega\rang }$ with probability approaching 1 for N ≫ 1. Analysis shows that this eventual value for $\displaystyle{ r(N) }$ satisfies $\displaystyle{ r(N) \leq \Big\lceil\frac{\pi}{4}\sqrt{N}\Big\rceil }$.

Implementing the steps for this algorithm can be done using a number of gates linear in the number of qubits. Thus, the gate complexity of this algorithm is $\displaystyle{ O(\log(N)r(N)) }$, or $\displaystyle{ O(\log(N)) }$ per iteration.

## Geometric proof of correctness Picture showing the geometric interpretation of the first iteration of Grover's algorithm. The state vector $\displaystyle{ |s\rang }$ is rotated towards the target vector $\displaystyle{ |\omega\rang }$ as shown.

There is a geometric interpretation of Grover's algorithm, following from the observation that the quantum state of Grover's algorithm stays in a two-dimensional subspace after each step. Consider the plane spanned by $\displaystyle{ |s\rang }$ and $\displaystyle{ |\omega\rang }$; equivalently, the plane spanned by $\displaystyle{ |\omega\rang }$ and the perpendicular ket $\displaystyle{ \textstyle |s'\rang = \frac{1}{\sqrt{N - 1}}\sum_{x \neq \omega} |x\rang }$.

Grover's algorithm begins with the initial ket $\displaystyle{ |s\rang }$, which lies in the subspace. The operator $\displaystyle{ U_{\omega} }$ is a reflection at the hyperplane orthogonal to $\displaystyle{ |\omega\rang }$ for vectors in the plane spanned by $\displaystyle{ |s'\rang }$ and $\displaystyle{ |\omega\rang }$, i.e. it acts as a reflection across $\displaystyle{ |s'\rang }$. This can be seen by writing $\displaystyle{ U_\omega }$ in the form of a Householder reflection:

$\displaystyle{ U_\omega = I - 2|\omega\rangle\langle \omega|. }$

The operator $\displaystyle{ U_s = 2 |s\rangle \langle s| - I }$ is a reflection through $\displaystyle{ |s\rang }$. Both operators $\displaystyle{ U_s }$ and $\displaystyle{ U_{\omega} }$ take states in the plane spanned by $\displaystyle{ |s'\rang }$ and $\displaystyle{ |\omega\rang }$ to states in the plane. Therefore, Grover's algorithm stays in this plane for the entire algorithm.

It is straightforward to check that the operator $\displaystyle{ U_s U_{\omega} }$ of each Grover iteration step rotates the state vector by an angle of $\displaystyle{ \theta = 2\arcsin\tfrac{1}{\sqrt{N}} }$. So, with enough iterations, one can rotate from the initial state $\displaystyle{ |s\rang }$ to the desired output state $\displaystyle{ |\omega\rang }$. The initial ket is close to the state orthogonal to $\displaystyle{ |\omega\rang }$:

$\displaystyle{ \lang s'|s\rang = \sqrt{\frac{N-1}{N}}. }$

In geometric terms, the angle $\displaystyle{ \theta/2 }$ between $\displaystyle{ |s\rang }$ and $\displaystyle{ |s'\rang }$ is given by

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

We need to stop when the state vector passes close to $\displaystyle{ |\omega\rang }$; after this, subsequent iterations rotate the state vector away from $\displaystyle{ |\omega\rang }$, reducing the probability of obtaining the correct answer. The exact probability of measuring the correct answer is

$\displaystyle{ \sin^2\left( \Big( r + \frac{1}{2} \Big)\theta\right), }$

where r is the (integer) number of Grover iterations. The earliest time that we get a near-optimal measurement is therefore $\displaystyle{ r \approx \pi \sqrt{N} / 4 }$.

## Algebraic proof of correctness

To complete the algebraic analysis, we need to find out what happens when we repeatedly apply $\displaystyle{ U_s U_\omega }$. 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 $\displaystyle{ s }$ and $\displaystyle{ \omega }$. We can write the action of $\displaystyle{ U_s }$ and $\displaystyle{ U_\omega }$ in the space spanned by $\displaystyle{ \{|s\rang, |\omega\rang\} }$ as:

$\displaystyle{ U_s : a |\omega \rang + b |s \rang \mapsto [|\omega \rang \, | s \rang] \begin{bmatrix} -1 & 0 \\ 2/\sqrt{N} & 1 \end{bmatrix}\begin{bmatrix}a\\b\end{bmatrix}. }$
$\displaystyle{ U_\omega : a |\omega \rang + b |s \rang \mapsto [|\omega \rang \, | s \rang] \begin{bmatrix} -1 & -2/\sqrt{N} \\ 0 & 1 \end{bmatrix}\begin{bmatrix}a\\b\end{bmatrix}. }$

So in the basis $\displaystyle{ \{ |\omega\rang, |s\rang \} }$ (which is neither orthogonal nor a basis of the whole space) the action $\displaystyle{ U_sU_\omega }$ of applying $\displaystyle{ U_\omega }$ followed by $\displaystyle{ U_s }$ is given by the matrix

$\displaystyle{ U_sU_\omega = \begin{bmatrix} -1 & 0 \\ 2/\sqrt{N} & 1 \end{bmatrix} \begin{bmatrix} -1 & -2/\sqrt{N} \\ 0 & 1 \end{bmatrix} = \begin{bmatrix} 1 & 2/\sqrt{N} \\ -2/\sqrt{N} & 1-4/N \end{bmatrix}. }$

This matrix happens to have a very convenient Jordan form. If we define $\displaystyle{ t = \arcsin(1/\sqrt{N}) }$, it is

$\displaystyle{ U_sU_\omega = M \begin{bmatrix} e^{2it} & 0 \\ 0 & e^{-2it}\end{bmatrix} M^{-1} }$ where $\displaystyle{ M = \begin{bmatrix}-i & i \\ e^{it} & e^{-it} \end{bmatrix}. }$

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

$\displaystyle{ (U_sU_\omega)^r = M \begin{bmatrix} e^{2rit} & 0 \\ 0 & e^{-2rit}\end{bmatrix} M^{-1}. }$

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

$\displaystyle{ \left|\begin{bmatrix}\lang\omega|\omega\rang & \lang\omega|s\rang\end{bmatrix}(U_sU_\omega)^r \begin{bmatrix}0 \\ 1\end{bmatrix} \right|^2 = \sin^2\left( (2r+1)t\right). }$

Alternatively, one might reasonably imagine that a near-optimal time to distinguish would be when the angles 2rt and −2rt are as far apart as possible, which corresponds to $\displaystyle{ 2rt \approx \pi/2 }$, or $\displaystyle{ r = \pi/4t = \pi/4\arcsin(1/\sqrt{N}) \approx \pi\sqrt{N}/4 }$. Then the system is in state

$\displaystyle{ [|\omega \rang \, | s \rang] (U_sU_\omega)^r \begin{bmatrix}0\\1\end{bmatrix} \approx [|\omega \rang \, | s \rang] M \begin{bmatrix} i & 0 \\ 0 & -i\end{bmatrix} M^{-1} \begin{bmatrix}0\\1\end{bmatrix} = | \omega \rang \frac{1}{\cos(t)} - |s \rang \frac{\sin(t)}{\cos(t)}. }$

A short calculation now shows that the observation yields the correct answer ω with error $\displaystyle{ O\left (\frac{1}{N} \right) }$.

## Extensions and variants

### Multiple matching entries

If, instead of 1 matching entry, there are k matching entries, the same algorithm works, but the number of iterations must be $\displaystyle{ \frac{\pi}{4}{\left( \frac{N}{k} \right)^{1/2}} }$instead of $\displaystyle{ \frac{\pi}{4}{N^{1/2}}. }$

There are several ways to handle the case if k is unknown. A simple solution performs optimally up to a constant factor: run Grover's algorithm repeatedly for increasingly small values of k, e.g., taking k = N, N/2, N/4, ..., and so on, taking $\displaystyle{ k = N/2^t }$ for iteration t until a matching entry is found.

With sufficiently high probability, a marked entry will be found by iteration $\displaystyle{ t = \log_2(N/k) + c }$ for some constant c. Thus, the total number of iterations taken is at most

$\displaystyle{ \frac{\pi}{4} \Big(1 + \sqrt{2} + \sqrt{4} + \cdots + \sqrt{\frac{N}{k2^c}}\Big) = O\big(\sqrt{N/k}\big). }$

A version of this algorithm is used in order to solve the collision problem.

### Quantum partial search

A modification of Grover's algorithm called quantum partial search was described by Grover and Radhakrishnan in 2004. 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 $\displaystyle{ K }$ blocks, each of size $\displaystyle{ b = N/K }$. 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 $\displaystyle{ N/2 }$ to $\displaystyle{ (N-b)/2 }$.

Grover's algorithm requires $\displaystyle{ \frac{\pi}{4}\sqrt{N} }$ iterations. Partial search will be faster by a numerical factor that depends on the number of blocks $\displaystyle{ K }$. Partial search uses $\displaystyle{ n_1 }$ global iterations and $\displaystyle{ n_2 }$ local iterations. The global Grover operator is designated $\displaystyle{ G_1 }$ and the local Grover operator is designated $\displaystyle{ G_2 }$.

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

1. Perform $\displaystyle{ j_1 }$ standard Grover iterations on the entire database.
2. Perform $\displaystyle{ j_2 }$ local Grover iterations. A local Grover iteration is a direct sum of Grover iterations over each block.
3. Perform one standard Grover iteration.

The optimal values of $\displaystyle{ j_1 }$ and $\displaystyle{ j_2 }$ 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 Vladimir 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

Grover's algorithm is optimal up to sub-constant factors. That is, any algorithm that accesses the database only by using the operator Uω must apply Uω at least a $\displaystyle{ 1-o(1) }$ fraction as many times as Grover's algorithm. The extension of Grover's algorithm to k matching entries, π(N/k)1/2/4, is also optimal. This result is important in understanding the limits of quantum computation.

If the Grover's search problem was solvable with logc 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 that quantum computers cannot solve NP-Complete problems in polynomial time, and thus NP is not contained in BQP.

It has been shown that a class of non-local hidden variable quantum computers could implement a search of an $\displaystyle{ N }$-item database in at most $\displaystyle{ O(\sqrt{N}) }$ steps. This is faster than the $\displaystyle{ O(\sqrt{N}) }$ steps taken by Grover's algorithm.