Loading [MathJax]/jax/output/HTML-CSS/jax.js

Grover’s Algorithm

Carsten Urbach

The Algorithm

Grover’s quantum search algorithm is defined via the two following unitary operations U = 12|xsxs|,V = 12|ψψ|. Here |ψ = 1Nx|x, with states |x in the computational basis and N=2n with n the number of qubits. xs is the index of the element sougth for.

The unitary operator U is implemented via an oracle function f performing the following action |x|q  |x|qf(x) with f(x) = {1x=xs,0ortherwise. Thus, the qubit q is flipped, if f(x)=1.

The quantum circuit for U looks as follows

For V it looks like

Example case N=4

The case n=2 and N=22=4 can be implemented as follows: assume xs=2, thus we need a function f(x)=1 for x=2 and f(x)=0 otherwise. This is achieved as follows:

## oracle for n=2 and x_s=2
oracle <- function(x) {
  x <- X(1) * (CCNOT(c(1,2,3)) *(X(1) * x))
  return(x)
}

The following test should return 1 only for x=xs

## case |00>=0
x <- oracle(qstate(3))
measure(x, 3)$value
[1] 0
## case |01>=1
x <- oracle(X(1)*qstate(3))
measure(x, 3)$value
[1] 0
## case |10>=2
x <- oracle(X(2)*qstate(3))
measure(x, 3)$value
[1] 1
## case |11>=3
x <- oracle(X(2)*(X(1)*qstate(3)))
measure(x, 3)$value
[1] 0

The unitaries U and V for the n=2 can then be implemented as follows

U <- function(x) {
  x <- oracle(x)
  x <- Z(3) * x
  x <- oracle(x)
  return(x)
}
V <- function(x) {
  for(i in c(1:2)) {
    x <- H(i) * x
  }
  x <- X(1) * (X(2) * x)
  x <- CCNOT(c(1,2,3)) * x
  x <- Z(3) * x
  x <- CCNOT(c(1,2,3)) * x
  x <- X(1) * (X(2) * x)
  for(i in c(1:2)) {
    x <- H(i) * x
  }
  return(x)
}

One application of VU looks as follows in the quantum circuit picture

N=4 is the special case where the algorithms returns the correct result with certainty after only a single application of VU. This is demonstrated in the following example

## prepare psi
psi <- H(1) * ( H(2) * qstate(3))
## apply VU
x <- U(psi)
x <- V(x)
x
   ( -1 )   * |010> 

As expected, the first two qubits (the two rightmost ones) of x are equal to xs in binary representation. (The phase is not observable.)