dimod.BinaryQuadraticModel.to_numpy_matrix¶
-
BinaryQuadraticModel.to_numpy_matrix(variable_order=None)[source]¶ Convert a binary quadratic model to NumPy 2D array.
Parameters: variable_order (list, optional) – If provided, indexes the rows/columns of the NumPy array. If variable_order includes any variables not in the binary quadratic model, these are added to the NumPy array. Returns: The binary quadratic model as a NumPy 2D array. Note that the binary quadratic model is converted to BINARYvartype.Return type: numpy.ndarrayNotes
The matrix representation of a binary quadratic model only makes sense for binary models. For a binary sample x, the energy of the model is given by:
\[E(x) = x^T Q x\]The offset is dropped when converting to a NumPy array.
Examples
This example converts a binary quadratic model to NumPy array format while ordering variables and adding one (‘d’).
>>> 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']) array([[ 0. , 0. , 0. , 0. ], [ 0. , 0.5, 1.5, 0. ], [ 0. , 0. , -1. , 0.5], [ 0. , 0. , 0. , 1. ]])