Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 0 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
allow-prereleases: true
- run: uv sync --group=test
- name: Run tests
# TODO: #8818 Re-enable quantum tests
run: uv run --with=pytest-run-parallel pytest
--iterations=8 --parallel-threads=auto
--ignore=computer_vision/cnn_classification.py
Expand All @@ -30,7 +29,6 @@ jobs:
--ignore=machine_learning/lstm/lstm_prediction.py
--ignore=neural_network/input_data.py
--ignore=project_euler/
--ignore=quantum/q_fourier_transform.py
--ignore=scripts/validate_solutions.py
--ignore=web_programming/current_stock_price.py
--ignore=web_programming/fetch_anime_and_play.py
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies = [
"opencv-python>=4.10.0.84",
"pandas>=2.2.3",
"pillow>=11.3",
"qiskit>=2",
"rich>=13.9.4",
"scikit-learn>=1.5.2",
"scipy>=1.16.2",
Expand Down
53 changes: 35 additions & 18 deletions quantum/q_fourier_transform.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
"""
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 quantum Fourier transform (QFT) for a desired
number of qubits using the Qiskit framework.

This circuit can be used as a building block to design
Shor's algorithm in quantum computing, as well as
quantum phase estimation, among others.

The circuit is simulated with Qiskit's built-in, pure-Python
``BasicSimulator`` (no compiled ``qiskit-aer`` backend required),
so it runs anywhere Qiskit itself installs.

References:
https://en.wikipedia.org/wiki/Quantum_Fourier_transform
https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html
https://quantum.cloud.ibm.com/docs/en/api/qiskit/qiskit.circuit.library.QFT
"""

import math

import numpy as np
import qiskit
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, transpile
from qiskit.providers.basic_provider import BasicSimulator


def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts.Counts:
"""
# >>> quantum_fourier_transform(2)
# {'00': 2500, '01': 2500, '11': 2500, '10': 2500}
Build and simulate the quantum Fourier transform applied to the all-zero
state ``|0...0>``. The QFT maps ``|0...0>`` to a uniform superposition, so
every computational-basis outcome is (up to shot noise) equally likely.

# quantum circuit for number_of_qubits = 3:
┌───┐
qr_0: ──────■──────────────────────■───────┤ H ├─X─
Expand All @@ -31,13 +38,20 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts
qr_2: ┤ H ├─■────────■───────────────────────────X─
└───┘
cr: 3/═════════════════════════════════════════════

Args:
n : number of qubits
number_of_qubits : number of qubits

Returns:
qiskit.result.counts.Counts: distribute counts.
qiskit.result.counts.Counts: measurement counts over 10000 shots.
Comment thread
cclauss marked this conversation as resolved.
Outdated

>>> quantum_fourier_transform(2)
{'00': 2500, '01': 2500, '10': 2500, '11': 2500}
The simulation is seeded, so the set of observed outcomes is reproducible:

>>> counts = quantum_fourier_transform(2)
>>> sorted(counts)
['00', '01', '10', '11']
>>> sum(counts.values())
10000
>>> quantum_fourier_transform(-1)
Traceback (most recent call last):
...
Expand Down Expand Up @@ -82,9 +96,12 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts

# measure all the qubits
quantum_circuit.measure(qr, cr)
# simulate with 10000 shots
backend = Aer.get_backend("qasm_simulator")
job = execute(quantum_circuit, backend, shots=10000)

# simulate with 10000 shots on the pure-Python BasicSimulator; seed the run
# so the observed outcomes are reproducible for the doctest above.
backend = BasicSimulator()
transpiled_circuit = transpile(quantum_circuit, backend)
job = backend.run(transpiled_circuit, shots=10000, seed_simulator=42)
Comment thread
cclauss marked this conversation as resolved.
Outdated

return job.result().get_counts(quantum_circuit)

Expand Down