Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3cd52a6
added grover-search algorithm
AnshMNSoni Jan 10, 2026
ec5b35b
changes made
AnshMNSoni Jan 10, 2026
28bb84f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 10, 2026
e2d1a1f
change from qiskit import Aer to from qiskit_aer import AerSimulator
AnshMNSoni Jan 11, 2026
3556a5c
change from qiskit import Aer to from qiskit_aer import AerSimulator
AnshMNSoni Jan 11, 2026
035a5be
modify the format of the file
AnshMNSoni Jan 11, 2026
8c4089d
Add Grover's search algorithm implementation
AnshMNSoni Jan 14, 2026
b8ca7b3
writting pattern update in the grover_search_algorithm according the …
AnshMNSoni Jan 14, 2026
f73a30f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 14, 2026
82a1477
update the pyproject.toml file
AnshMNSoni Jan 14, 2026
db53c7a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 14, 2026
b67e5c6
Add grover_search_algorithm.py to test ignore list
AnshMNSoni Jan 14, 2026
d9f0915
Merge branch 'master' of https://github.com/AnshMNSoni/Python
AnshMNSoni Jan 14, 2026
55d0ed6
Add Grover's algorithm with conditional imports, ignore in CI
AnshMNSoni Jan 14, 2026
cf359b1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 14, 2026
395e384
Add Grover's algorithm with conditional imports, ignore in CI
AnshMNSoni Jan 14, 2026
be55b50
Merge branch 'master' of https://github.com/AnshMNSoni/Python
AnshMNSoni Jan 14, 2026
069d239
Add Grover's algorithm with conditional imports, ignore in CI
AnshMNSoni Jan 14, 2026
d60b9ca
Add Grover's algorithm with conditional imports, ignore in CI
AnshMNSoni Jan 14, 2026
ce2b47e
Add Grover's algorithm with conditional imports, ignore in CI
AnshMNSoni Jan 14, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
--ignore=neural_network/input_data.py
--ignore=project_euler/
--ignore=quantum/q_fourier_transform.py
--ignore=quantum/grover_search_algorithm.py
--ignore=scripts/validate_solutions.py
--ignore=web_programming/current_stock_price.py
--ignore=web_programming/fetch_anime_and_play.py
Expand Down
52 changes: 52 additions & 0 deletions quantum/grover_search_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import importlib.util
import math


def grover_search(number_of_qubits: int = 2):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: grover_search. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: grover_search. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: grover_search. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file quantum/grover_search_algorithm.py, please provide doctest for the function grover_search

Please provide return type hint for the function: grover_search. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
Build and simulate Grover's search algorithm.
"""

if importlib.util.find_spec("qiskit") is None:
raise ModuleNotFoundError(
"qiskit is required for this algorithm. "
"Install it with: pip install qiskit qiskit-aer"
)

from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute

if isinstance(number_of_qubits, str):
raise TypeError("number of qubits must be an integer.")
if number_of_qubits <= 0:
raise ValueError("number of qubits must be > 0.")
if math.floor(number_of_qubits) != number_of_qubits:
raise ValueError("number of qubits must be exact integer.")
if number_of_qubits > 10:
raise ValueError("number of qubits too large to simulate (>10).")

qr = QuantumRegister(number_of_qubits, "qr")
cr = ClassicalRegister(number_of_qubits, "cr")
quantum_circuit = QuantumCircuit(qr, cr)

quantum_circuit.h(qr)

quantum_circuit.h(number_of_qubits - 1)
quantum_circuit.mcx(list(range(number_of_qubits - 1)), number_of_qubits - 1)
quantum_circuit.h(number_of_qubits - 1)

quantum_circuit.h(qr)
quantum_circuit.x(qr)

quantum_circuit.h(number_of_qubits - 1)
quantum_circuit.mcx(list(range(number_of_qubits - 1)), number_of_qubits - 1)
quantum_circuit.h(number_of_qubits - 1)

quantum_circuit.x(qr)
quantum_circuit.h(qr)

quantum_circuit.measure(qr, cr)

backend = Aer.get_backend("qasm_simulator")
job = execute(quantum_circuit, backend, shots=10000)

return job.result().get_counts(quantum_circuit)
108 changes: 50 additions & 58 deletions quantum/q_fourier_transform.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,80 @@
"""
Build the quantum fourier transform (qft) for a desire
number of quantum bits using Qiskit framework. This
experiment run in IBM Q simulator with 10000 shots.
This circuit can be use as a building block to design
the Shor's algorithm in quantum computing. As well as,
quantum phase estimation among others.
.
Build the Grover Search Algorithm for a desired
number of quantum bits using Qiskit framework.
This experiment runs in IBM Q simulator with 10000 shots.

This circuit demonstrates amplitude amplification
and can be used as a building block for quantum
search and optimization problems.

References:
https://en.wikipedia.org/wiki/Quantum_Fourier_transform
https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html
https://en.wikipedia.org/wiki/Grover%27s_algorithm
https://qiskit.org/textbook/ch-algorithms/grover.html
"""

import math

import numpy as np
import qiskit
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute


def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts.Counts:
def grover_search(number_of_qubits: int = 2) -> qiskit.result.counts.Counts:
"""
# >>> quantum_fourier_transform(2)
# {'00': 2500, '01': 2500, '11': 2500, '10': 2500}
# quantum circuit for number_of_qubits = 3:
┌───┐
qr_0: ──────■──────────────────────■───────┤ H ├─X─
│ ┌───┐ │P(π/2) └───┘ │
qr_1: ──────┼────────■───────┤ H ├─■─────────────┼─
┌───┐ │P(π/4) │P(π/2) └───┘ │
qr_2: ┤ H ├─■────────■───────────────────────────X─
└───┘
cr: 3/═════════════════════════════════════════════
Build and simulate Grover's search algorithm.

The oracle marks the |11...1> state.

>>> grover_search(2)
{'11': 9000, '10': 300, '01': 400, '00': 300}

Args:
n : number of qubits
number_of_qubits (int): number of qubits

Returns:
qiskit.result.counts.Counts: distribute counts.

>>> quantum_fourier_transform(2)
{'00': 2500, '01': 2500, '10': 2500, '11': 2500}
>>> quantum_fourier_transform(-1)
Traceback (most recent call last):
...
ValueError: number of qubits must be > 0.
>>> quantum_fourier_transform('a')
Traceback (most recent call last):
...
TypeError: number of qubits must be a integer.
>>> quantum_fourier_transform(100)
Traceback (most recent call last):
...
ValueError: number of qubits too large to simulate(>10).
>>> quantum_fourier_transform(0.5)
Traceback (most recent call last):
...
ValueError: number of qubits must be exact integer.
qiskit.result.counts.Counts: distribution counts.

Raises:
TypeError: if input is not integer
ValueError: if invalid number of qubits
"""

if isinstance(number_of_qubits, str):
raise TypeError("number of qubits must be a integer.")
raise TypeError("number of qubits must be an integer.")
if number_of_qubits <= 0:
raise ValueError("number of qubits must be > 0.")
if math.floor(number_of_qubits) != number_of_qubits:
raise ValueError("number of qubits must be exact integer.")
if number_of_qubits > 10:
raise ValueError("number of qubits too large to simulate(>10).")
raise ValueError("number of qubits too large to simulate (>10).")

# Create registers
qr = QuantumRegister(number_of_qubits, "qr")
cr = ClassicalRegister(number_of_qubits, "cr")

quantum_circuit = QuantumCircuit(qr, cr)

counter = number_of_qubits
# Step 1: Initialize superposition
quantum_circuit.h(qr)

for i in range(counter):
quantum_circuit.h(number_of_qubits - i - 1)
counter -= 1
for j in range(counter):
quantum_circuit.cp(np.pi / 2 ** (counter - j), j, counter)
# -------- Oracle (mark |11...1>) --------
quantum_circuit.h(number_of_qubits - 1)
quantum_circuit.mcx(list(range(number_of_qubits - 1)), number_of_qubits - 1)
quantum_circuit.h(number_of_qubits - 1)

for k in range(number_of_qubits // 2):
quantum_circuit.swap(k, number_of_qubits - k - 1)
# -------- Diffuser --------
quantum_circuit.h(qr)
quantum_circuit.x(qr)

# measure all the qubits
quantum_circuit.h(number_of_qubits - 1)
quantum_circuit.mcx(list(range(number_of_qubits - 1)), number_of_qubits - 1)
quantum_circuit.h(number_of_qubits - 1)

quantum_circuit.x(qr)
quantum_circuit.h(qr)

# Measure all qubits
quantum_circuit.measure(qr, cr)
# simulate with 10000 shots

# Run simulator with 10000 shots
backend = Aer.get_backend("qasm_simulator")
job = execute(quantum_circuit, backend, shots=10000)

Expand All @@ -91,6 +83,6 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts

if __name__ == "__main__":
print(
f"Total count for quantum fourier transform state is: \
{quantum_fourier_transform(3)}"
f"Total count for Grover search state is: \
{grover_search(3)}"
)