Skip to content

Commit

Permalink
Merge pull request #65 from arcondello/numpy-2.0
Browse files Browse the repository at this point in the history
Support NumPy 2.0
  • Loading branch information
arcondello authored Jul 5, 2024
2 parents 99801b8 + 596e3b9 commit 132d19f
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 41 deletions.
21 changes: 11 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ jobs:
parameters:
python-version:
type: string
dimod-version:
dependency-versions:
type: string
only-binary:
type: string

docker:
Expand All @@ -230,9 +232,9 @@ jobs:
command: |
python -m venv env
. env/bin/activate
pip install -r requirements.txt
pip install << parameters.dependency-versions >> --upgrade --only-binary << parameters.only-binary >>
pip install dwave-samplers --no-index -f dist/ --no-deps --force-reinstall
pip install 'dimod<< parameters.dimod-version >>' --only-binary=dimod -U
- run: &unix-run-tests
name: run tests
command: |
Expand Down Expand Up @@ -310,17 +312,16 @@ workflows:
- test-codecov
- test-doctest
- test-dependencies:
name: test-dependencies-dimod<< matrix.dimod-version >>-py<< matrix.python-version >>
name: test-dependencies-python<< matrix.python-version >>-dependencies=<< matrix.dependency-versions >>
requires:
- build-and-test-linux
matrix:
parameters:
# test the lowest and highest of each minor for the dependencies
dimod-version: [==0.12.0, ~=0.12.0]
python-version: *python-versions
exclude:
- dimod-version: ==0.12.0 # Does not support Python 3.12
python-version: 3.12.0
dependency-versions: [dimod==0.12.13 oldest-supported-numpy networkx==3.0, dimod numpy networkx]
# Skip some of the intermediate Python versions for speed. 3.8 gets a special callout because
# Numpy 2 does not support it, so we build it differently
python-version: ["3.8", "3.9", "3.12"]
only-binary: ["numpy,dimod,networkx"]
- test-linux-cpp11
- test-sdist:
requires:
Expand Down
1 change: 0 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pydata-sphinx-theme==0.14.3
sphinx==7.3.7
reno[sphinx]==3.3.0
networkx==2.6.3
1 change: 0 additions & 1 deletion dwave/samplers/greedy/descent.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ cimport numpy as np

cimport dwave.samplers.greedy.decl as decl


def steepest_gradient_descent(num_samples,
linear_biases,
coupler_starts, coupler_ends, coupler_weights,
Expand Down
10 changes: 6 additions & 4 deletions dwave/samplers/greedy/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,16 @@ def sample(self, bqm: dimod.BinaryQuadraticModel,
This example samples a simple two-variable Ising model.
>>> import dimod
>>> bqm = dimod.BQM.from_ising({}, {'ab': 1})
>>> bqm = dimod.BQM.from_ising({'a': -1}, {'ab': 1})
...
>>> from dwave.samplers import SteepestDescentSampler
>>> sampler = SteepestDescentSampler()
...
>>> samples = sampler.sample(bqm)
>>> samples.first.energy
-1.0
>>> sampleset = sampler.sample(bqm)
>>> print(sampleset) # doctest: +SKIP
a b energy num_oc. num_st.
0 +1 -1 -2.0 1 0
['SPIN', 1 rows, 1 samples, 2 variables]
Run steepest descent one million times (takes ~150ms on an average
laptop), converging to local minima, each time starting from a
Expand Down
8 changes: 5 additions & 3 deletions dwave/samplers/planar/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ def sample(self,
>>> bqm.add_interaction('b', 'c', +1.0)
>>> bqm.add_interaction('c', 'a', +1.0)
>>> pos = {'a': (0, 0), 'b': (1, 0), 'c': (0, 1)}
>>> sample = PlanarGraphSolver().sample(bqm, pos)
>>> sample.first
Sample(sample={'a': 1, 'b': -1, 'c': -1}, energy=-1.0, num_occurrences=1)
>>> sampleset = PlanarGraphSolver().sample(bqm, pos)
>>> print(sampleset)
a b c energy num_oc.
0 +1 -1 -1 -1.0 1
['SPIN', 1 rows, 1 samples, 3 variables]
"""

Expand Down
3 changes: 1 addition & 2 deletions dwave/samplers/sa/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ def sample(self, bqm: dimod.BinaryQuadraticModel,
>>> # Reuse a seed
>>> a1 = next((sampler.sample(bqm, seed=88)).samples())['a']
>>> a2 = next((sampler.sample(bqm, seed=88)).samples())['a']
>>> a1 == a2
True
>>> assert a1 == a2
.. [#] :math:`\\beta` represents the inverse temperature, :math:`1/(k_B T)`, of a
`Boltzmann distribution <https://en.wikipedia.org/wiki/Boltzmann_distribution>`_
Expand Down
20 changes: 11 additions & 9 deletions dwave/samplers/tabu/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class TabuSampler(dimod.Sampler, dimod.Initialized):
This example solves a two-variable Ising model.
>>> from dwave.samplers import TabuSampler
>>> samples = TabuSampler().sample_ising({'a': -0.5, 'b': 1.0}, {'ab': -1})
>>> list(samples.data()) # doctest: +SKIP
[Sample(sample={'a': -1, 'b': -1}, energy=-1.5, num_occurrences=1)]
>>> samples.first.energy
-1.5
>>> sampleset = TabuSampler().sample_ising({'a': -0.5, 'b': 1.0}, {'ab': -1}, num_restarts=0)
>>> print(sampleset)
a b energy num_oc. num_re.
0 -1 -1 -1.5 1 0
['SPIN', 1 rows, 1 samples, 2 variables]
"""

Expand Down Expand Up @@ -156,14 +156,16 @@ def sample(self, bqm: dimod.BinaryQuadraticModel,
This example samples a simple two-variable Ising model.
>>> import dimod
>>> bqm = dimod.BQM.from_ising({}, {'ab': 1})
>>> bqm = dimod.BQM.from_ising({'a': -1}, {'ab': 1})
>>> from dwave.samplers import TabuSampler
>>> sampler = TabuSampler()
>>> samples = sampler.sample(bqm)
>>> samples.first.energy
-1.0
>>> sampleset = sampler.sample(bqm, num_restarts=0)
>>> print(sampleset)
a b energy num_oc. num_re.
0 +1 -1 -2.0 1 0
['SPIN', 1 rows, 1 samples, 2 variables]
"""

if not bqm:
Expand Down
6 changes: 4 additions & 2 deletions dwave/samplers/tree/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class TreeDecompositionSolver(dimod.Sampler):
Find the ground state.
>>> sampleset = solver.sample_ising(h, J)
>>> sampleset.first.sample
{'a': -1, 'b': -1}
>>> print(sampleset)
a b energy num_oc.
0 -1 -1 -1.1 1
['SPIN', 1 rows, 1 samples, 2 variables]
Take multiple reads to find states of increasing energy.
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ requires = [
"setuptools>=46.4.0", # PEP-420 support, PEP-517/518 support, setup.cfg attr: support
"wheel>=0.30.0", # limited python api support
"cython~=3.0",
'dimod==0.12.13', # Lowest that support Python 3.8 through 3.12
'dimod==0.12.16', # Lowest that support Python 3.8 through 3.12 and NumPy 2.0
'numpy==1.19.0;python_version<"3.9"', # C API for numpy.random
'oldest-supported-numpy;python_version>="3.9"',
'numpy~=2.0;python_version>="3.9"',
]
build-backend = "setuptools.build_meta"

Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/NumPy-2.0-2b5756ee0fcdac2f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- Support, and build with, NumPy 2.0
upgrade:
- Require ``dimod>=0.12.13``.
- Require ``NetworkX>=3.0``.
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cython==3.0.4
dimod==0.12.13
cython==3.0.10
dimod==0.12.16
numpy==1.19.0;python_version<"3.9" # C API for numpy.random
oldest-supported-numpy;python_version>="3.9"
numpy==2.0.0;python_version>="3.9"
reno==3.3.0 # for changelog
setuptools>=46.4.0 # to support setup.cfg getting __version__
networkx==2.6.3
networkx==3.2.1
6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ zip_safe = false
include_package_data = true
python_requires = >=3.8
install_requires =
numpy>=1.19.0,<2.0.0
dimod>=0.12.0,<0.13.0
networkx>=2.4.0
numpy>=1.19.0,<3.0.0
dimod>=0.12.13,<0.13.0 # oldest dimod that supports Python 3.12
networkx>=3.0

0 comments on commit 132d19f

Please sign in to comment.