Skip to content

Commit dde1e49

Browse files
quantum: modernize QFT to Qiskit 2.x and re-enable its test (#15120)
* quantum: modernize QFT to Qiskit 2.x + re-enable its test quantum/q_fourier_transform.py used the Aer and execute symbols that were removed from qiskit in the 1.0 API break, so it could never run and was on the pytest --ignore list. Port it to the current API: - Drop 'from qiskit import Aer, execute'. Build the circuit unchanged, then simulate with the pure-Python BasicSimulator via transpile() + backend.run(), so no compiled qiskit-aer backend is needed (qiskit-aer has no Python 3.14 wheels yet; BasicSimulator ships inside qiskit core). - Seed the run (seed_simulator=42) and rewrite the doctest to assert the reproducible, shot-noise-independent facts (the four outcomes appear and the counts sum to the shot total) instead of exact per-state counts, which random sampling can never hit. - Add 'qiskit>=2' to project dependencies and drop the quantum ignore + the stale '# TODO: #8818 Re-enable quantum tests' comment in build.yml. Draft until CI confirms qiskit installs and imports on the repo's Python 3.14. * Update quantum/q_fourier_transform.py --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 7fd91a5 commit dde1e49

3 files changed

Lines changed: 36 additions & 20 deletions

File tree

.github/workflows/build.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
allow-prereleases: true
2222
- run: uv sync --group=test
2323
- name: Run tests
24-
# TODO: #8818 Re-enable quantum tests
2524
run: uv run --with=pytest-run-parallel pytest
2625
--iterations=8 --parallel-threads=auto
2726
--ignore=computer_vision/cnn_classification.py
@@ -30,7 +29,6 @@ jobs:
3029
--ignore=machine_learning/lstm/lstm_prediction.py
3130
--ignore=neural_network/input_data.py
3231
--ignore=project_euler/
33-
--ignore=quantum/q_fourier_transform.py
3432
--ignore=scripts/validate_solutions.py
3533
--ignore=web_programming/current_stock_price.py
3634
--ignore=web_programming/fetch_anime_and_play.py

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies = [
2121
"opencv-python>=4.10.0.84",
2222
"pandas>=2.2.3",
2323
"pillow>=11.3",
24+
"qiskit>=2",
2425
"rich>=13.9.4",
2526
"scikit-learn>=1.5.2",
2627
"scipy>=1.16.2",

quantum/q_fourier_transform.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
"""
2-
Build the quantum fourier transform (qft) for a desire
3-
number of quantum bits using Qiskit framework. This
4-
experiment run in IBM Q simulator with 10000 shots.
5-
This circuit can be use as a building block to design
6-
the Shor's algorithm in quantum computing. As well as,
7-
quantum phase estimation among others.
8-
.
2+
Build the quantum Fourier transform (QFT) for a desired
3+
number of qubits using the Qiskit framework.
4+
5+
This circuit can be used as a building block to design
6+
Shor's algorithm in quantum computing, as well as
7+
quantum phase estimation, among others.
8+
9+
The circuit is simulated with Qiskit's built-in, pure-Python
10+
``BasicSimulator`` (no compiled ``qiskit-aer`` backend required),
11+
so it runs anywhere Qiskit itself installs.
12+
913
References:
1014
https://en.wikipedia.org/wiki/Quantum_Fourier_transform
11-
https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html
15+
https://quantum.cloud.ibm.com/docs/en/api/qiskit/qiskit.circuit.library.QFT
1216
"""
1317

1418
import math
1519

1620
import numpy as np
1721
import qiskit
18-
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute
22+
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, transpile
23+
from qiskit.providers.basic_provider import BasicSimulator
1924

2025

2126
def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts.Counts:
2227
"""
23-
# >>> quantum_fourier_transform(2)
24-
# {'00': 2500, '01': 2500, '11': 2500, '10': 2500}
28+
Build and simulate the quantum Fourier transform applied to the all-zero
29+
state ``|0...0>``. The QFT maps ``|0...0>`` to a uniform superposition, so
30+
every computational-basis outcome is (up to shot noise) equally likely.
31+
2532
# quantum circuit for number_of_qubits = 3:
2633
┌───┐
2734
qr_0: ──────■──────────────────────■───────┤ H ├─X─
@@ -31,13 +38,20 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts
3138
qr_2: ┤ H ├─■────────■───────────────────────────X─
3239
└───┘
3340
cr: 3/═════════════════════════════════════════════
41+
3442
Args:
35-
n : number of qubits
43+
number_of_qubits : number of qubits
44+
3645
Returns:
37-
qiskit.result.counts.Counts: distribute counts.
46+
qiskit.result.counts.Counts: measurement counts over 10,000 shots.
3847
39-
>>> quantum_fourier_transform(2)
40-
{'00': 2500, '01': 2500, '10': 2500, '11': 2500}
48+
The simulation is seeded, so the set of observed outcomes is reproducible:
49+
50+
>>> counts = quantum_fourier_transform(2)
51+
>>> sorted(counts)
52+
['00', '01', '10', '11']
53+
>>> sum(counts.values())
54+
10000
4155
>>> quantum_fourier_transform(-1)
4256
Traceback (most recent call last):
4357
...
@@ -82,9 +96,12 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts
8296

8397
# measure all the qubits
8498
quantum_circuit.measure(qr, cr)
85-
# simulate with 10000 shots
86-
backend = Aer.get_backend("qasm_simulator")
87-
job = execute(quantum_circuit, backend, shots=10000)
99+
100+
# simulate with 10000 shots on the pure-Python BasicSimulator; seed the run
101+
# so the observed outcomes are reproducible for the doctest above.
102+
backend = BasicSimulator()
103+
transpiled_circuit = transpile(quantum_circuit, backend)
104+
job = backend.run(transpiled_circuit, shots=10_000, seed_simulator=42)
88105

89106
return job.result().get_counts(quantum_circuit)
90107

0 commit comments

Comments
 (0)