Grover’s quantum search algorithm is defined via the two following unitary operations U = 1−2|xs⟩⟨xs|,V = 1−2|ψ⟩⟨ψ|. Here |ψ⟩ = 1√N∑x|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⟩|q⊕f(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
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
function(x) {
oracle <- X(1) * (CCNOT(c(1,2,3)) *(X(1) * x))
x <-return(x)
}
The following test should return 1
only for x=xs
## case |00>=0
oracle(qstate(3))
x <-measure(x, 3)$value
[1] 0
## case |01>=1
oracle(X(1)*qstate(3))
x <-measure(x, 3)$value
[1] 0
## case |10>=2
oracle(X(2)*qstate(3))
x <-measure(x, 3)$value
[1] 1
## case |11>=3
oracle(X(2)*(X(1)*qstate(3)))
x <-measure(x, 3)$value
[1] 0
The unitaries U and V for the n=2 can then be implemented as follows
function(x) {
U <- oracle(x)
x <- Z(3) * x
x <- oracle(x)
x <-return(x)
} function(x) {
V <-for(i in c(1:2)) {
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)
x <-for(i in c(1:2)) {
H(i) * x
x <-
}return(x)
}
One application of V⋅U 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 V⋅U. This is demonstrated in the following example
## prepare psi
H(1) * ( H(2) * qstate(3))
psi <-## apply VU
U(psi)
x <- V(x)
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.)