dimod icon indicating copy to clipboard operation
dimod copied to clipboard

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

Open bernalde opened this issue 4 years ago • 5 comments

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

  • OS: Ubuntu 20.04.3 LTS
  • Python version: Python 3.9.7, fresh ocean-sdk pip installation
  • dimod 0.10.7 pypi_0 pypi

Additional Context Add any other background information about the problem.

bernalde avatar Oct 08 '21 15:10 bernalde

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

bernalde avatar Oct 08 '21 15:10 bernalde

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')

mcfarljm avatar Oct 08 '21 22:10 mcfarljm

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

arcondello avatar Oct 12 '21 16:10 arcondello

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.

bernalde avatar Oct 12 '21 16:10 bernalde

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.

arcondello avatar Oct 12 '21 16:10 arcondello