dimod.AdjArrayBQM

class AdjArrayBQM(vartype=None, *args)

A binary quadratic model structured as two C++ vectors.

Can be created in several ways:

AdjArrayBQM(vartype)

Creates an empty binary quadratic model.

AdjArrayBQM(bqm)

Creates a BQM from another BQM. See copy and cls kwargs below.

AdjArrayBQM(bqm, vartype)

Creates a BQM from another BQM, changing to the specified vartype if necessary.

AdjArrayBQM(n, vartype)

Creates a BQM with n variables, indexed linearly from zero, setting all biases to zero.

AdjArrayBQM(quadratic, vartype)

Creates a BQM from quadratic biases given as a square array_like or a dictionary of the form {(u, v): b, …}. Note that when formed with SPIN-variables, biases on the diagonal are added to the offset.

AdjArrayBQM(linear, quadratic, vartype)

Creates a BQM from linear and quadratic biases, where linear is a one-dimensional array_like or a dictionary of the form {v: b, …}, and quadratic is a square array_like or a dictionary of the form {(u, v): b, …}. Note that when formed with SPIN-variables, biases on the diagonal are added to the offset.

AdjArrayBQM(linear, quadratic, offset, vartype)

Creates a BQM from linear and quadratic biases, where linear is a one-dimensional array_like or a dictionary of the form {v: b, …}, and quadratic is a square array_like or a dictionary of the form {(u, v): b, …}, and offset is a numerical offset. Note that when formed with SPIN-variables, biases on the diagonal are added to the offset.

Notes

The AdjArrayBQM is implemented using two C++ vectors. The first vector contains the linear biases and the index of the start of each variable’s neighborhood in the second vector. The second vector contains the neighboring variables and their associated quadratic biases. The vectors, once initialized, are not resized.

Advantages:

  • Very fast iteration over the biases

Disadvantages:

  • Does not support incremental construction

  • Only supports float64 biases

Intended Use:

  • When performance is important and the BQM can be treated as read-only

Examples

>>> import numpy as np
>>> from dimod import AdjArrayBQM

Construct from a NumPy array.

>>> AdjArrayBQM(np.triu(np.ones((2, 2))), 'BINARY')
AdjArrayBQM({0: 1.0, 1: 1.0}, {(0, 1): 1.0}, 0.0, 'BINARY')

Construct from dicts.

>>> AdjArrayBQM({'a': -1}, {('a', 'b'): 1}, 'SPIN')
AdjArrayBQM({a: -1.0, b: 0.0}, {('a', 'b'): 1.0}, 0.0, 'SPIN')

Attributes

dtype

Data type of the linear biases, float64.

itype

Data type of the indices, uint32.

ntype

Data type of the neighborhood indices, varies by platform.

num_interactions

Number of interactions in the model.

num_variables

Number of variables in the model.

offset

The constant energy offset associated with the model.

shape

A 2-tuple of num_variables and num_interactions.

variables

Variables of the binary quadratic model.

vartype

Variable type, Vartype.SPIN or Vartype.BINARY.

Views

adj

Quadratic biases as a nested dict of dicts.

linear

Linear biases as a mapping.

quadratic

Quadratic biases as a flat mapping.

binary

Binary-valued version of the binary quadratic model.

spin

Spin-valued version of the binary quadratic model.

Methods

add_offset(offset)

Add the specified value to the offset of a binary quadratic model.

change_vartype(self, vartype[, inplace])

Return a binary quadratic model with the specified vartype.

copy([deep])

Return a copy.

degree(self, v)

Return degree of the specified variable.

degrees([array, dtype])

Return the degrees of a binary quadratic model’s variables.

empty(vartype)

Create a new empty binary quadratic model.

energies(self, samples[, dtype])

Determine the energies of the given samples.

energy(sample[, dtype])

Determine the energy of the given sample.

flip_variable(v)

Flip variable v in a binary quadratic model.

from_coo(obj[, vartype])

Deserialize a binary quadratic model from a COOrdinate format encoding.

from_file(type cls, file_like)

Construct a binary quadratic model from a file-like object.

from_ising(h, J[, offset])

Create a binary quadratic model from an Ising problem.

from_networkx_graph(G[, vartype, …])

Create a binary quadratic model from a NetworkX graph.

from_numpy_matrix(mat[, variable_order, …])

Create a binary quadratic model from a NumPy array.

from_numpy_vectors(type cls, linear, …[, …])

Create a binary quadratic model from vectors.

from_qubo(Q[, offset])

Create a binary quadratic model from a QUBO problem.

get_linear(self, v)

Get the linear bias of the specified variable.

get_quadratic(self, u, v[, default])

Get the quadratic bias of the specified interaction.

has_variable(v)

Return True if v is a variable in the binary quadratic model.

iter_interactions()

Iterate over the interactions of the binary quadratic model.

iter_linear(self)

Iterate over the linear biases of the binary quadratic model.

iter_neighbors(u)

Iterate over neighbors of a variable in the binary quadratic model.

iter_quadratic(self[, variables])

Iterate over the quadratic biases of the binary quadratic model.

iter_variables()

Iterate over the variables of the binary quadratic model.

normalize([bias_range, quadratic_range, …])

Normalizes the biases of the binary quadratic model to fall in the provided range(s), and adjusts the offset appropriately.

relabel_variables(self, mapping[, inplace])

Relabel variables of a binary quadratic model as specified by mapping.

relabel_variables_as_integers(self[, inplace])

Relabel as integers the variables of a binary quadratic model.

scale(scalar[, ignored_variables, …])

Multiply all the biases by the specified scalar.

set_linear(self, v, Bias b)

Set the linear bias of a variable.

set_quadratic(self, u, v, Bias b)

Set the quadratic bias of an interaction specified by its variables.

shapeable()

Returns True if the binary quadratic model is shapeable.

to_coo([fp, vartype_header])

Serialize the binary quadratic model to a COOrdinate format encoding.

to_file(self)

View the BQM as a file-like object.

to_ising()

Converts a binary quadratic model to Ising format.

to_networkx_graph([node_attribute_name, …])

Convert a binary quadratic model to NetworkX graph format.

to_numpy_matrix([variable_order])

Convert a binary quadratic model to NumPy 2D array.

to_numpy_vectors(self[, variable_order, …])

Convert binary quadratic model to NumPy vectors.

to_qubo()

Convert a binary quadratic model to QUBO format.

remove_offset()

Set the binary quadratic model’s offset to zero.