From 1225f9f8804a7872f54d02298ad9a24cc1b53cb1 Mon Sep 17 00:00:00 2001 From: Priya Sundaram Date: Sun, 30 Aug 2026 07:29:30 +0000 Subject: [PATCH 1/3] 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. --- .github/workflows/build.yml | 2 -- pyproject.toml | 1 + quantum/q_fourier_transform.py | 53 ++++++++++++++++++++++------------ 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 40e6a0515cd0..a40f32c2afaf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 1a55feda3cbb..17651335e567 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/quantum/q_fourier_transform.py b/quantum/q_fourier_transform.py index 762ac408190e..46f43a6aa766 100644 --- a/quantum/q_fourier_transform.py +++ b/quantum/q_fourier_transform.py @@ -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─ @@ -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. - >>> 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): ... @@ -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) return job.result().get_counts(quantum_circuit) From 2cca4d4cf881de9439922316428794d9a9a67838 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Aug 2026 10:21:32 +0200 Subject: [PATCH 2/3] Update quantum/q_fourier_transform.py --- quantum/q_fourier_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/q_fourier_transform.py b/quantum/q_fourier_transform.py index 46f43a6aa766..59a44d6d9c34 100644 --- a/quantum/q_fourier_transform.py +++ b/quantum/q_fourier_transform.py @@ -43,7 +43,7 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts number_of_qubits : number of qubits Returns: - qiskit.result.counts.Counts: measurement counts over 10000 shots. + qiskit.result.counts.Counts: measurement counts over 10,000 shots. The simulation is seeded, so the set of observed outcomes is reproducible: From ed6a19b2c338baabc8a88f0bac4bf2b3809373fa Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Aug 2026 10:28:44 +0200 Subject: [PATCH 3/3] Apply suggestion from @cclauss --- quantum/q_fourier_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/q_fourier_transform.py b/quantum/q_fourier_transform.py index 59a44d6d9c34..276b52d5dfb3 100644 --- a/quantum/q_fourier_transform.py +++ b/quantum/q_fourier_transform.py @@ -101,7 +101,7 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts # 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) + job = backend.run(transpiled_circuit, shots=10_000, seed_simulator=42) return job.result().get_counts(quantum_circuit)