11"""
2- Grover's Search Algorithm implementation using Qiskit.
2+ Build the Grover Search Algorithm for a desired
3+ number of quantum bits using Qiskit framework.
4+ This experiment runs in IBM Q simulator with 10000 shots.
35
4- Grover's algorithm is a quantum algorithm for searching an unsorted database
5- with quadratic speedup over classical algorithms.
6+ This circuit demonstrates amplitude amplification
7+ and can be used as a building block for quantum
8+ search and optimization problems.
69
7- Wikipedia :
10+ References :
811https://en.wikipedia.org/wiki/Grover%27s_algorithm
12+ https://qiskit.org/textbook/ch-algorithms/grover.html
913"""
1014
11- from qiskit import QuantumCircuit, transpile
12- from qiskit_aer import AerSimulator
15+ import math
1316
17+ import qiskit
18+ from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute
1419
15- def grover_search(shots: int = 1024) -> dict[str, int]:
20+
21+ def grover_search(number_of_qubits: int = 2) -> qiskit.result.counts.Counts:
1622 """
17- Runs Grover's search algorithm for 2 qubits and returns measurement results .
23+ Build and simulate Grover's search algorithm.
1824
19- The oracle marks the |11> state.
25+ The oracle marks the |11...1 > state.
2026
2127 Args:
22- shots (int): Number of simulation shots.
28+ number_of_qubits (int): number of qubits
2329
2430 Returns:
25- dict[str, int]: Measurement counts.
31+ qiskit.result.counts.Counts: distribution counts.
32+
33+ Raises:
34+ TypeError: if input is not integer
35+ ValueError: if invalid number of qubits
2636
27- Example:
28- >>> result = grover_search(100)
29- >>> isinstance(result, dict)
37+ >>> counts = grover_search(2)
38+ >>> isinstance(counts, dict)
3039 True
40+ >>> sum(counts.values())
41+ 10000
3142 """
43+ if isinstance(number_of_qubits, str):
44+ raise TypeError("number of qubits must be an integer.")
45+ if number_of_qubits <= 0:
46+ raise ValueError("number of qubits must be > 0.")
47+ if math.floor(number_of_qubits) != number_of_qubits:
48+ raise ValueError("number of qubits must be exact integer.")
49+ if number_of_qubits > 10:
50+ raise ValueError("number of qubits too large to simulate (>10).")
51+
52+ # Create registers
53+ qr = QuantumRegister(number_of_qubits, "qr")
54+ cr = ClassicalRegister(number_of_qubits, "cr")
55+ quantum_circuit = QuantumCircuit(qr, cr)
56+
57+ # Step 1: Initialize superposition
58+ quantum_circuit.h(qr)
3259
33- n = 2
34- qc = QuantumCircuit(n, n)
60+ # -------- Oracle (mark |11...1>) --------
61+ quantum_circuit.h(number_of_qubits - 1)
62+ quantum_circuit.mcx(list(range(number_of_qubits - 1)), number_of_qubits - 1)
63+ quantum_circuit.h(number_of_qubits - 1)
3564
36- # Initialize superposition
37- qc.h(range(n))
65+ # -------- Diffuser --------
66+ quantum_circuit.h(qr)
67+ quantum_circuit.x(qr)
3868
39- # Oracle marking |11>
40- qc.cz(0, 1)
69+ quantum_circuit.h(number_of_qubits - 1)
70+ quantum_circuit.mcx(list(range(number_of_qubits - 1)), number_of_qubits - 1)
71+ quantum_circuit.h(number_of_qubits - 1)
4172
42- # Diffuser
43- qc.h(range(n))
44- qc.x(range(n))
45- qc.h(1)
46- qc.cx(0, 1)
47- qc.h(1)
48- qc.x(range(n))
49- qc.h(range(n))
73+ quantum_circuit.x(qr)
74+ quantum_circuit.h(qr)
5075
51- # Measurement
52- qc .measure(range(n), range(n) )
76+ # Measure all qubits
77+ quantum_circuit .measure(qr, cr )
5378
54- # Run on simulator
55- backend = AerSimulator()
56- compiled = transpile(qc, backend)
57- result = backend.run(compiled, shots=shots).result()
58- counts = result.get_counts()
79+ # Run simulator with 10000 shots
80+ backend = Aer.get_backend("qasm_simulator")
81+ job = execute(quantum_circuit, backend, shots=10000)
5982
60- return counts
83+ return job.result().get_counts(quantum_circuit)
6184
6285
6386if __name__ == "__main__":
64- print(grover_search() )
87+ print(f"Total count for Grover search state is: { grover_search(3)}" )
0 commit comments