Skip to content

Commit 75e2109

Browse files
Add support for Python 3.13 (#836)
* Add support for `Python 3.13` * Add note for tutorial tests * Add support for `Python 3.13` * Add note for tutorial tests * Update .github/workflows/main.yml Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> * Update `def __eq__` in optimizers * Implement the convention `def __eq__(self, other: object) -> bool:` * Fix typos * Fix shape in steppable optimizer --------- Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
1 parent de2cf2c commit 75e2109

File tree

10 files changed

+84
-18
lines changed

10 files changed

+84
-18
lines changed

.github/workflows/main.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,17 @@ jobs:
112112
fail-fast: false
113113
matrix:
114114
os: [ubuntu-latest]
115-
python-version: [3.9, '3.10', 3.11, 3.12]
115+
python-version: [3.9, '3.10', 3.11, 3.12, 3.13]
116116
include:
117117
# macos-latest is an Arm64 image
118118
- os: macos-latest
119119
python-version: 3.9
120120
- os: macos-latest
121-
python-version: 3.12
121+
python-version: 3.13
122122
- os: windows-latest
123123
python-version: 3.9
124124
- os: windows-latest
125-
python-version: 3.12
125+
python-version: 3.13
126126
steps:
127127
- uses: actions/checkout@v4
128128
- uses: actions/setup-python@v5
@@ -184,7 +184,7 @@ jobs:
184184
fail-fast: false
185185
matrix:
186186
os: [ubuntu-latest]
187-
python-version: [3.9, 3.12]
187+
python-version: [3.9, 3.13]
188188
steps:
189189
- uses: actions/checkout@v4
190190
with:
@@ -282,28 +282,32 @@ jobs:
282282
with:
283283
name: ubuntu-latest-3.12
284284
path: /tmp/u312
285+
- uses: actions/download-artifact@v4
286+
with:
287+
name: ubuntu-latest-3.13
288+
path: /tmp/u313
285289
- uses: actions/download-artifact@v4
286290
with:
287291
name: macos-latest-3.9
288292
path: /tmp/m39
289293
- uses: actions/download-artifact@v4
290294
with:
291-
name: macos-latest-3.12
292-
path: /tmp/m312
295+
name: macos-latest-3.13
296+
path: /tmp/m313
293297
- uses: actions/download-artifact@v4
294298
with:
295299
name: windows-latest-3.9
296300
path: /tmp/w39
297301
- uses: actions/download-artifact@v4
298302
with:
299-
name: windows-latest-3.12
300-
path: /tmp/w312
303+
name: windows-latest-3.13
304+
path: /tmp/w313
301305
- name: Install Dependencies
302306
run: pip install -U coverage coveralls diff-cover
303307
shell: bash
304308
- name: Combined Deprecation Messages
305309
run: |
306-
sort -f -u /tmp/u39/ml.dep /tmp/u310/ml.dep /tmp/u311/ml.dep /tmp/u312/ml.dep /tmp/m39/ml.dep /tmp/m312/ml.dep /tmp/w39/ml.dep /tmp/w312/ml.dep || true
310+
sort -f -u /tmp/u39/ml.dep /tmp/u310/ml.dep /tmp/u311/ml.dep /tmp/u312/ml.dep /tmp/u313/ml.dep /tmp/m39/ml.dep /tmp/m313/ml.dep /tmp/w39/ml.dep /tmp/w313/ml.dep || true
307311
shell: bash
308312
- name: Coverage combine
309313
run: coverage3 combine /tmp/u39/ml.dat

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
55
[tool.black]
66
line-length = 100
77

8-
target-version = ['py39', 'py310', 'py311', 'py312']
8+
target-version = ['py39', 'py310', 'py311', 'py312', 'py313']
99

1010
[tool.pylint.main]
1111
extension-pkg-allow-list = [

qiskit_machine_learning/optimizers/gradient_descent.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2021, 2024.
3+
# (C) Copyright IBM 2021, 2025.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -42,6 +42,22 @@ class GradientDescentState(OptimizerState):
4242
4343
"""
4444

45+
# See parent class for a comment on having custom equals. I needed this
46+
# too as it does not appear to use super by default and without this failed
47+
# the exact same way. Note it does not include learning rate as that field
48+
# is not included in the compare as per the field decorator. The __eq__
49+
# method is supposed to accept any object. If you update the version of
50+
# mypy you're using, it'll print out a note recommending this code
51+
# structure.
52+
def __eq__(self, other: object) -> bool:
53+
if not isinstance(other, GradientDescentState):
54+
# If we return NotImplemented, Python will automatically try
55+
# running other.__eq__(self), in case 'other' knows what to do with
56+
# Person objects.
57+
return NotImplemented
58+
59+
return super().__eq__(other) and self.stepsize == other.stepsize
60+
4561

4662
class GradientDescent(SteppableOptimizer):
4763
r"""The gradient descent minimization routine.

qiskit_machine_learning/optimizers/steppable_optimizer.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2022, 2024.
3+
# (C) Copyright IBM 2022, 2025.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -73,6 +73,35 @@ class OptimizerState:
7373
nit: int | None
7474
"""Number of optimization steps performed so far in the optimization."""
7575

76+
# Under Python 3.13 the auto-generated equal fails with an error around
77+
# using numpy all or any. See https://github.com/qiskit-community/qiskit-algorithms/pull/225
78+
# for further information. Hence, this custom function was added. The __eq__
79+
# method is supposed to accept any object. If you update the version of
80+
# mypy you're using, it'll print out a note recommending this code structure.
81+
def __eq__(self, other: object) -> bool:
82+
if not isinstance(other, OptimizerState):
83+
# If we return NotImplemented, Python will automatically try
84+
# running other.__eq__(self), in case 'other' knows what to do with
85+
# Person objects.
86+
return NotImplemented
87+
88+
return (
89+
(
90+
self.x == other.x
91+
if isinstance(self.x, float) and isinstance(other.x, float)
92+
else (
93+
False
94+
if isinstance(self.x, float) or isinstance(other.x, float)
95+
else self.x.shape == other.x.shape and (self.x == other.x).all()
96+
)
97+
)
98+
and self.fun == other.fun
99+
and self.jac == other.jac
100+
and self.nfev == other.nfev
101+
and self.njev == other.njev
102+
and self.nit == other.nit
103+
)
104+
76105

77106
class SteppableOptimizer(Optimizer):
78107
"""

releasenotes/notes/0.8/py38_end_of_support-fa1fdea6ea02b502.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
upgrade:
33
- |
44
Removed support for using Qiskit Machine Learning with Python 3.8 to reflect
5-
the EOL of Python 3.8 in October 2024 (PEP 569). To continue using Qiskit Machine Learning, you
6-
must upgrade to a Python: 3.9 or above if you are using older versions of Python.
5+
the EOL of Python 3.8 in October 2024 (`PEP 569 <https://peps.python.org/pep-0569/>`__). To
6+
continue using Qiskit Machine Learning, you must upgrade to a Python: 3.9 or above if you are
7+
using older versions of Python.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
22
features:
33
- |
4-
Added support for using Qiskit Machine Learning with Python 3.12.
4+
Added support for using Qiskit Machine Learning with Python 3.12
5+
(`PEP 693 <https://peps.python.org/pep-0693/>`__).
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
upgrade:
3+
- |
4+
Removed support for using Qiskit Machine Learning with Python 3.8 to reflect
5+
the EOL of Python 3.8 in October 2024 (`PEP 569 <https://peps.python.org/pep-0569/>`__). To
6+
continue using Qiskit Machine Learning, you must upgrade to a Python: 3.9 or above if you are
7+
using older versions of Python.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
features:
3+
- |
4+
Added support for using Qiskit Machine Learning with Python 3.13 following the release of
5+
Python 3.13 (final) in October 2024 (`PEP 719 <https://peps.python.org/pep-0719/>`__). To
6+
access the latest features of Python 3.13, you may update your Qiskit Machine Learning
7+
environment.

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2021, 2024.
3+
# (C) Copyright IBM 2021, 2025.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -65,7 +65,8 @@
6565
"Programming Language :: Python :: 3.10",
6666
"Programming Language :: Python :: 3.11",
6767
"Programming Language :: Python :: 3.12",
68-
"Topic :: Scientific/Engineering",
68+
"Programming Language :: Python :: 3.13",
69+
"Topic :: Scientific/Engineering"
6970
],
7071
keywords="qiskit sdk quantum machine learning ml",
7172
packages=setuptools.find_packages(

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tox]
22
# Sets this min.version because of differences with env_tmp_dir env.
33
minversion = 4.0.2
4-
envlist = py39, py310, py311, py312, lint, gpu, gpu-amd
4+
envlist = py39, py310, py311, py312, py313, lint, gpu, gpu-amd
55
skipsdist = True
66

77
[testenv]

0 commit comments

Comments
 (0)