As input for Grover's algorithm, suppose we have a function 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 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 \omega. Our goal is to identify \omega. We can access f with a
subroutine (sometimes called an
oracle) in the form of a
unitary operator U_\omega that acts as follows: \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 N-dimensional
state space \mathcal{H}, which is supplied by a
register with n = \lceil \log_{2} N \rceil
qubits. This is often written as U_\omega|x\rang = (-1)^{f(x)}|x\rang. Grover's algorithm outputs \omega with probability at least 1/2 using O(\sqrt{N}) applications of U_\omega. This probability can be made arbitrarily large by running Grover's algorithm multiple times. If one runs Grover's algorithm until \omega is found, the
expected number of applications is still O(\sqrt{N}), since it will only be run twice on average.
Alternative oracle definition This section compares the above oracle U_\omega with an oracle U_f. U_\omega is different from the standard
quantum oracle for a function f. This standard oracle, denoted here as U_f, 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: \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, U_f |x\rang |y\rang = |x\rang |y \oplus f(x)\rang. These oracles are typically realized using
uncomputation. If we are given U_f as our oracle, then we can also implement U_\omega, since U_\omega is U_f when the ancillary qubit is in the state |-\rang = \frac1{\sqrt2}\big(|0\rang - |1\rang\big) = H|1\rang: \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 |0 \oplus 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 U_f is given, then we must maintain an additional qubit in the state |-\rang and apply U_f in place of U_\omega. == Algorithm ==