dwavesystems / dimod

A shared API for QUBO/Ising samplers.
https://docs.ocean.dwavesys.com/en/stable/docs_dimod/
Apache License 2.0
124 stars 81 forks source link

TypeError: No matching signature found when building Ising model from numpy arrays #1024

Open bernalde opened 3 years ago

bernalde commented 3 years ago

Description As detailed in the title and MWE example, this code is failing.

Steps To Reproduce This is a MWE

import dimod
import numpy as np

N = 10  # Number of variables
np.random.seed(42)  # Fixing the random seed to get the same result
J = np.random.rand(N, N)
# We only consider upper triangular matrix ignoring the diagonal
J = np.triu(J, 1)
h = np.random.rand(N, 1)

print(h.dtype)
print(J.dtype)
model_random = dimod.BinaryQuadraticModel.from_ising(h, J, offset=0.0)

Which prints:

 float64
float64
Traceback (most recent call last):
  File "/home/bernalde/scratch.py", line 14, in <module>
    model_random = dimod.BinaryQuadraticModel.from_ising(h, J, offset=0.0)
  File "/home/bernalde/miniconda3/envs/quipml/lib/python3.9/site-packages/dimod/binary/binary_quadratic_model.py", line 1191, in from_ising
    return cls(h, J, offset, Vartype.SPIN)
  File "/home/bernalde/miniconda3/envs/quipml/lib/python3.9/site-packages/dimod/binary/binary_quadratic_model.py", line 193, in __init__
    self._init_components(*args, dtype=dtype)
  File "/home/bernalde/miniconda3/envs/quipml/lib/python3.9/site-packages/dimod/binary/binary_quadratic_model.py", line 259, in _init_components
    self.add_linear_from_array(linear)
  File "/home/bernalde/miniconda3/envs/quipml/lib/python3.9/site-packages/dimod/binary/binary_quadratic_model.py", line 783, in add_linear_from_array
    self.data.add_linear_from_array(np.asarray(ldata))
  File "dimod/binary/cybqm/cybqm_template.pyx.pxi", line 223, in dimod.binary.cybqm.cybqm_float64.cyBQM_template.__pyx_fused_cpdef
TypeError: No matching signature found

I have debugged it down to line 223 in the cybqm_template.pyx.pxi cpdef Py_ssize_t add_linear_from_array(self, ConstNumeric[:] linear) except -1:...

Expected Behavior In previous versions of Python, it works well, so my guess is that Python 3.9 makes some type assumptions that are leading to this error. Something along this post in Stack overflow.

Environment

Additional Context Add any other background information about the problem.

bernalde commented 3 years ago

I reinstalled an environment with python 3.8 and it does not solve the issue, so the problem might be somewhere else.

mcfarljm commented 3 years ago

Hi, I suspect the problem is with the shape of h. I think the BQM constructor is looking for a sequence as opposed to a 2D array:

h = np.random.rand(N)

FYI, I believe you can also construct the model directly using:

model_random = dimod.BinaryQuadraticModel(h, J, 'SPIN')
arcondello commented 3 years ago

@mcfarljm is correct. Though I think we could add a better error message. I'll make a PR to do so.

bernalde commented 3 years ago

Thank you for looking into this. The solution works just fine, but I wanted to make you aware of this since it worked on a previous version of dimod.

arcondello commented 3 years ago

Ah, interesting, I didn't realize it was a backwards compatibility break (though I should have from your original post). Ok, I'll see if we can restore the behavior. Worst case we can still just raise a better error message.