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/intro/intro_symbolic_math.html
Comments
Please sign in to leave a comment.