dimod.BinaryQuadraticModel.to_numpy_matrix

BinaryQuadraticModel.to_numpy_matrix(variable_order=None)[source]

Convert a binary quadratic model to NumPy matrix format.

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 matrix.
Returns:The binary quadratic model as a NumPy matrix. The matrix has binary vartype.
Return type:numpy.matrix

Notes

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 matrix.

Examples

This example converts a binary quadratic model to NumPy matrix 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'])
matrix([[ 0. ,  0. ,  0. ,  0. ],
        [ 0. ,  0.5,  1.5,  0. ],
        [ 0. ,  0. , -1. ,  0.5],
        [ 0. ,  0. ,  0. ,  1. ]])