to_numpy_matrix example fails
Description
The example given at https://docs.ocean.dwavesys.com/projects/dimod/en/stable/reference/bqm/generated/dimod.BinaryQuadraticModel.to_numpy_matrix.html fails to run.
Steps To Reproduce
Simply enter the code from the to_numpy_matrix example:
$ python
Python 3.8.3 (default, Jul 2 2020, 16:21:59)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dimod
>>> import numpy as np
>>> model = dimod.BinaryQuadraticModel({'a': 1, 'b': -1, 'c': .5},
... {('a', 'b'): .5, ('b', 'c'): 1.5},
... 1.4,
... dimod.BINARY)
>>> model.to_numpy_matrix(variable_order=['d', 'c', 'b', 'a'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pakin/.anaconda3/lib/python3.8/site-packages/dimod/core/bqm.py", line 1073, in to_numpy_matrix
raise ValueError("variable_order does not include all variables")
ValueError: variable_order does not include all variables
Expected Behavior
I expected to see
array([[ 0. , 0. , 0. , 0. ],
[ 0. , 0.5, 1.5, 0. ],
[ 0. , 0. , -1. , 0.5],
[ 0. , 0. , 0. , 1. ]])
Environment
- OS: Ubuntu 20.10
- Python version: 3.8.3
- dimod version: 0.9.13
Looks to me that it needs the , 'd': 0 in the model creation line:
>>> model = dimod.BinaryQuadraticModel({'a': 1, 'b': -1, 'c': .5, 'd': 0},
{('a', 'b'): .5, ('b', 'c'): 1.5},
1.4,
dimod.BINARY)
>>> model.to_numpy_matrix(variable_order=['d', 'c', 'b', 'a'])
array([[0, 0.0, 0.0, 0.0],
[0, 0.5, 1.5, 0.0],
[0, 0, -1.0, 0.5],
[0, 0, 0, 1.0]], dtype=object)
Sorry, just noticed that the problem is that it is not adding the d by itself, once it is specified in the order, as should be expected.
Sorry, just noticed that the problem is that it is not adding the
dby itself, once it is specified in the order, as should be expected.
Right. If you remove the d from variable_order or add it, as you did, to the model, the example works. However, the documentation claims that the d should be added automatically:
This example converts a binary quadratic model to NumPy array format while ordering variables and adding one (‘d’).