Why do I get an error while adding symbolic math variables to a Quadratic Model?

If you try to use the add_variable() method to add an object created using symbolic math to a model as in the following code snippet:

>>> from dimod import Binary, BQM
>>> x = Binary('x')
>>> bqm = BQM('BINARY')
>>> bqm.add_variable(x, 1)

You will get the following error:

TypeError: unhashable type: 'BinaryQuadraticModel'

This is because the object x instantiated above is a binary quadratic model with a single variable labeled 'x':

>>> x = Binary('x')
BinaryQuadraticModel({'x': 1.0}, {}, 0.0, 'BINARY')

You can add the single-variable BQM, x to another BQM which basically adds the variable labeled 'x' to the other model as shown in the following example:

>>> bqm1 = BQM('BINARY')
>>> bqm1 += x
>>> bqm1
BinaryQuadraticModel({'x': 1.0}, {}, 0.0, 'BINARY')

Alternatively, you can fix the error above by adding a variable 'x' using the add_variable() method as below:

>>> bqm2 = BQM('BINARY')
>>> bqm2.add_variable('x', 1)
BinaryQuadraticModel({'x': 1.0}, {}, 0.0, 'BINARY')


See also: https://docs.ocean.dwavesys.com/en/stable/docs_dimod/reference/symbolic.html

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.