dimod.BinaryQuadraticModel.from_numpy_matrix

classmethod BinaryQuadraticModel.from_numpy_matrix(mat, variable_order=None, offset=0.0, interactions=None)[source]

Create a binary quadratic model from a NumPy array.

Parameters:
  • mat (numpy.ndarray) – Coefficients of a quadratic unconstrained binary optimization (QUBO) model formatted as a square NumPy 2D array.
  • variable_order (list, optional) – If provided, labels the QUBO variables; otherwise, row/column indices are used. If variable_order is longer than the array, extra values are ignored.
  • offset (optional, default=0.0) – Constant offset for the binary quadratic model.
  • interactions (iterable, optional, default=[]) – Any additional 0.0-bias interactions to be added to the binary quadratic model.
Returns:

Binary quadratic model with vartype set to Vartype.BINARY.

Return type:

BinaryQuadraticModel

Examples

This example creates a binary quadratic model from a QUBO in NumPy format while adding an interaction with a new variable (‘f’), ignoring an extra variable (‘g’), and setting an offset.

>>> import dimod
>>> import numpy as np
>>> Q = np.array([[1, 0, 0, 10, 11],
...               [0, 2, 0, 12, 13],
...               [0, 0, 3, 14, 15],
...               [0, 0, 0, 4, 0],
...               [0, 0, 0, 0, 5]]).astype(np.float32)
>>> model = dimod.BinaryQuadraticModel.from_numpy_matrix(Q,
...         variable_order = ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
...         offset = 2.5,
...         interactions = {('a', 'f')})
>>> model.linear   # doctest: +SKIP
{'a': 1.0, 'b': 2.0, 'c': 3.0, 'd': 4.0, 'e': 5.0, 'f': 0.0}
>>> model.quadratic[('a', 'd')]
10.0
>>> model.quadratic[('a', 'f')]
0.0
>>> model.offset
2.5