Grover’s Search using amplitude amplification and an oracle that finds a marked element.
Grover’s Search Implementation
We are given \(N=2^n\) elements, and one element is marked. The task is to find this marked element.
We are going to use \(n\) qubits. At the beginning we apply Hadamard to each qubit, so we put our quantum state into superposition. The amplitude of each basis state is set to 1/\(\sqrt{N}\) . After that we iterate the following algorithm for several times:
Make a query: apply a query oracle operator to qubits - it flips the sign of the amplitude of the state that corresponds to the marked element.
Inversion: apply a diffusion matrix - the amplitude of each state is reflected over the mean of all amplitudes.
Query operation
Oracle
Suppose that there exists a function \(f:\{0,1\}^n \rightarrow \{0,1\}\) with the following properties:
\[\begin{align*}
f(x)&=1 &\mbox{ if $x$ is marked}\\
f(x)&=0 &\mbox{ otherwise}
\end{align*}\]
Grover’s algorithm does not actually search a list of elements, but given function \(f\) with the above properties, it finds the element \(x\) such that \(f(x)=1\).
f is often called as the oracle or blackbox. In this case element 11 is marked.
Sign flip using Phase kickback
We use phase kickback to flip the sign of the marked element:
Create a circuit with 3 qubits, the last qubit is the ancilla.
A quantum circuit requires a number of ancilla qubits to store partial results; the ancilla qubits are initially in a well-defined state, usually |0〉, and must be returned to the same state at the end of the computation.
Set output qubit (qreg[2]) to |−⟩ by applying X and H.
<qiskit.circuit.instructionset.InstructionSet at 0x7f975e698730>
job = execute(mycircuit,Aer.get_backend('unitary_simulator'))u=job.result().get_unitary(mycircuit,decimals=3).datafor i inrange(4): s=""for j inrange(4): val =str(u[i][j].real)while(len(val)<5): val =" "+val s = s + valprint(s)mycircuit.draw(output='mpl')
In a list, inversion around the mean is achieved through (-element_value + 2* mean of all elements) in the list.
How amplitude amplification works in principle?
We start with a vector X = [10, 10, 10, 10, 10], whose mean is 10. The difference between the fourth element and the rest is: 0.
Phase inversion: [10, 10, 10, -10, 10].
Mean inversion: The mean now is X_bar = 6. The rule for mean inversion is: -X_i + 2X_bar. Thus, we get: [2, 2, 2, 22, 2]. The difference between the fourth element and the rest is: 20 - it increased!
Phase inversion: [2, 2, 2, -22, 2].
Mean inversion: The mean now is X_bar = -2.8. Then, we get: [-7.6, -7.6, -7.6, 16.4, -7.6,]. The difference between the fourth element and the rest is: 24 - it increased!
How amplitude amplification works in Grover Search?
To implement the inversion (diffusion) operation, we will need additional (ancilla) qubit. This is how we implement the inversion operator:
Set the ancilla qubit to |-> by applying X and H.
Apply H to all qubits other than the ancilla.
Apply X to all qubits other than the ancilla.
Apply multiple controlled NOT operator, where the ancilla qubit is target and all other qubits are used for controlling.