Skip to main content
IBM Quantum Platform

Description of Grover's algorithm

In this section, we'll describe Grover's algorithm. We'll begin by discussing phase query gates and how to build them, followed by the description of Grover's algorithm itself. Finally, we'll briefly discuss how this algorithm is naturally applied to searching.


Phase query gates

Grover's algorithm makes use of operations known as phase query gates. In contrast to an ordinary query gate Uf,U_f, defined for a given function ff in the usual way described previously, a phase query gate for the function ff is defined as

Zfx=(1)f(x)xZ_f \vert x\rangle = (-1)^{f(x)} \vert x\rangle

for every string xΣn.x\in\Sigma^n.

The operation ZfZ_f can be implemented using one query gate UfU_f as this diagram suggests:

A quantum circuit implementing a Z_f gate using one query gate together with the phase kickback phenomenon

This implementation makes use of the phase kickback phenomenon, and requires that one workspace qubit, initialized to a \vert -\rangle state, is made available. This qubit remains in the \vert - \rangle state after the implementation has completed, and can be reused (to implement subsequent ZfZ_f gates, for instance) or simply discarded.

In addition to the operation Zf,Z_f, we will also make use of a phase query gate for the nn-bit OR function, which is defined as follows for each string xΣn.x\in\Sigma^n.

OR(x)={0x=0n1x0n\mathrm{OR}(x) = \begin{cases} 0 & x = 0^n\\[0.5mm] 1 & x \neq 0^n \end{cases}

Explicitly, the phase query gate for the nn-bit OR function operates like this:

ZORx={xx=0nxx0n.Z_{\mathrm{OR}} \vert x\rangle = \begin{cases} \vert x\rangle & x = 0^n \\[0.5mm] - \vert x\rangle & x \neq 0^n. \end{cases}

To be clear, this is how ZORZ_{\mathrm{OR}} operates on standard basis states; its behavior on arbitrary states is determined from this expression by linearity.

The operation ZORZ_{\mathrm{OR}} can be implemented as a quantum circuit by beginning with a Boolean circuit for the OR function, then constructing a UORU_{\mathrm{OR}} operation (i.e., a standard query gate for the nn-bit OR function) using the procedure described in the Quantum algorithmic foundations lesson, and finally a ZORZ_{\mathrm{OR}} operation using the phase kickback phenomenon as described above. Notice that the operation ZORZ_{\mathrm{OR}} has no dependence on the function ff and can therefore be implemented by a quantum circuit having no query gates.


Description of the algorithm

Now that we have the two operations ZfZ_f and ZOR,Z_{\mathrm{OR}}, we can describe Grover's algorithm.

The algorithm refers to a number t,t, which is the number of iterations it performs (and therefore the number of queries to the function ff it requires). This number tt isn't specified by Grover's algorithm as we're describing it, and we'll discuss later in the lesson how it can be chosen.

Grover's algorithm

  1. Initialize an nn qubit register Q\mathsf{Q} to the all-zero state 0n\vert 0^n \rangle and then apply a Hadamard operation to each qubit of Q.\mathsf{Q}.
  2. Apply tt times the unitary operation G=HnZORHnZfG = H^{\otimes n} Z_{\mathrm{OR}} H^{\otimes n} Z_f to the register Q\mathsf{Q}
  3. Measure the qubits of Q\mathsf{Q} with respect to standard basis measurements and output the resulting string.

The operation G=HnZORHnZfG = H^{\otimes n} Z_{\mathrm{OR}} H^{\otimes n} Z_f iterated in step 2 will be called the Grover operation throughout the remainder of this lesson. Here is a quantum circuit representation of the Grover operation when n=7 ⁣:n=7\!:

A quantum circuit representation of the Grover operation

In this diagram, the ZfZ_f operation is depicted as being larger than ZORZ_{\mathrm{OR}} as an informal visual clue to suggest that it is likely to be the more costly operation. In particular, when we're working within the query model, ZfZ_f requires one query while ZORZ_{\mathrm{OR}} requires no queries. If instead we have a Boolean circuit for the function f,f, and then convert it to a quantum circuit for Zf,Z_f, we can reasonably expect that the resulting quantum circuit will be larger and more complicated than one for ZOR.Z_{\mathrm{OR}}.

Here's a diagram of a quantum circuit for the entire algorithm when n=7n=7 and t=3.t=3. For larger values of tt we can simply insert additional instances of the Grover operation immediately before the measurements.

A quantum circuit for Grover's algorithm when t=3

Grover's algorithm can be applied to the Search problem as follows:

  • Choose the number tt in step 2. (This is discussed later in the lesson.)
  • Run Grover's algorithm on the function f,f, using whatever choice we made for t,t, to obtain a string xΣn.x\in\Sigma^n.
  • Query the function ff on the string xx to see if it's a valid solution:
    • If f(x)=1,f(x) = 1, then we have found a solution, so we can stop and output x.x.
    • Otherwise, if f(x)=0,f(x) = 0, then we can either run the procedure again, possibly with a different choice for t,t, or we can decide to give up and output "no solution."

Once we've analyzed how Grover's algorithm works, we'll see that by taking t=O(N),t = O(\sqrt{N}), we obtain a solution to our search problem (if one exists) with high probability.

Was this page helpful?
Report a bug or request content on GitHub.