Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Grover's algorithm

Grover's algorithm, proposed by Lov Grover in 1996, is a quantum algorithm for searching
an unsorted database, offering a quadratic speedup compared to classical algorithms.
Grover’s algorithm is a quantum algorithm that solves the unstructured search problem. In an
unstructured search problem, we are given a set of N elements and we want to find a single
marked element. A classical computer would need to search through all N elements in order
to find the marked element, which would take time O(N). Grover’s algorithm, on the other
hand, can find the marked element in time O(√ N).

Algorithm:

The algorithm works by applying a series of quantum operations to the input state, which is
initialized as a superposition of all possible search states. The key idea behind Grover’s
algorithm is to amplify the amplitude of the marked state (i.e., the state containing the item
that we are searching for) by iteratively applying a quantum operation known as the Grover
operator.

The Grover operator has two quantum operations:

 The reflection on the mean


 The inversion of the marked state.

Here's a simplified explanation of how it works:

1. Initialization: Represent the items in the database as quantum states. Create a


superposition of all possible states using Hadamard gates to put each qubit into a
superposition of its 0 and 1 states.

where ∣x⟩ is the state corresponding to the element x.

2. Oracle: Apply an oracle gate, which marks the solution(s) in the superposition. This
oracle flips the sign of the amplitude of the solution state(s).
3. Amplitude Amplification: Apply a series of operations involving the oracle and a
diffuser gate. This step amplifies the amplitude of the solution state(s) while
decreasing the amplitude of the other states. It involves applying the oracle, followed
by a reflection operation about the mean amplitude, achieved by the diffuser gate.
4. Repeat: Steps 2 and 3 are repeated a certain number of times (roughly √N times) to
increase the probability of measuring the solution(s) state.
5. Measurement: Finally, measure the quantum state. The solution(s) will be obtained
with a higher probability compared to the other states.
By repeating the steps of oracle and amplitude amplification, Grover's algorithm can increase
the probability of measuring the correct solution, achieving a quadratic speedup compared to
classical algorithms.

Pseudo Code:
Input: N: number of items in the list, oracle(x): a function that returns true if x is the target
item, and false otherwise
Step 1: Initialize state
 Hadamard transform on all qubits
Step 2: Iterate over Grover’s algorithm
for k = 1 to sqrt(N) do
# Step 2a: Apply the oracle
 Apply the oracle to the state
# Step 2b: Apply the diffusion operator
 Hadamard transform on all qubits
 Apply an X gate on all qubits
 Apply a multi-controlled Z gate (which flips the sign of the state only if all
qubits are in the state |1>)
 Apply an X gate on all qubits
 Hadamard transform on all qubits
end for
Step 3: Measure the state and output the result
 Measure the state and output the result

from qiskit import QuantumCircuit, Aer, execute


from qiskit.visualization import plot_histogram
import numpy as np

# Function to create the oracle gate


def oracle(circuit, qubits):
# Apply X gate to target qubits
circuit.x(qubits[0])
circuit.x(qubits[2])
# Apply controlled-Z gate
circuit.h(qubits[2])
circuit.ccx(qubits[0], qubits[1], qubits[2])
circuit.h(qubits[2])
# Apply X gate again to revert the state of target qubits
circuit.x(qubits[0])
circuit.x(qubits[2])

# Function to create the diffusion operator


def diffusion(circuit, qubits):
# Apply H gates to all qubits
circuit.h(qubits)
# Apply X gates to all qubits
circuit.x(qubits)
# Apply controlled-Z gate (equivalent to multi-controlled X gate)
circuit.h(qubits[2])
circuit.cx(qubits[0], qubits[2])
circuit.h(qubits[2])
# Apply X gates to all qubits
circuit.x(qubits)
# Apply H gates to all qubits
circuit.h(qubits)

# Number of qubits
n=3

# Create quantum circuit


qc = QuantumCircuit(n)

# Apply Hadamard gates to create superposition


qc.h(range(n))

# Number of Grover iterations (adjust as needed)


num_iterations = 2

# Perform Grover iterations


for _ in range(num_iterations):
# Apply oracle gate
oracle(qc, [0, 1, 2])
# Apply diffusion operator
diffusion(qc, range(n))

# Measure the qubits


qc.measure_all()

# Draw the circuit


print("Quantum Circuit:")
print(qc.draw())

# Execute the circuit on a simulator


backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1024)
result = job.result()
counts = result.get_counts()

# Plot histogram of measurement outcomes


print("\nMeasurement results:")
plot_histogram(counts)

*************************************************************

You might also like