dimod.AdjDictBQM

class AdjDictBQM(*args, vartype=None)[source]

A binary quadratic model structured as a dict-of-dicts.

Can be created in several ways:

AdjDictBQM(vartype)
Creates an empty binary quadratic model.
AdjDictBQM(bqm)
Creates a BQM from another BQM. See copy and cls kwargs below.
AdjDictBQM(bqm, vartype)
Creates a BQM from another BQM, changing to the appropriate vartype if necessary.
AdjDictBQM(n, vartype)
Creates a BQM with n variables, indexed linearly from zero, setting all biases to zero.
AdjDictBQM(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.
AdjDictBQM(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.
AdjDictBQM(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 AdjDictBQM is implemented using a dict-of-dicts structure. The outer dict contains the BQM’s variables as keys and the neighborhoods as values. Each neighborhood dict contains the neighbors as keys and the quadratic biases as values. The linear biases are stored as self-interactions.

Advantages:

  • Pure python implementation
  • Supports arbitrary python types as biases
  • Low complexity for lookup operations
  • Supports incremental construction

Disadvantages:

  • Slow iteration
  • High memory usage

Intended Use:

  • For small problems or when flexibility is important

Examples

The first example constructs a BQM from a dict.

>>> dimod.AdjDictBQM({'a': -1.0}, {('a', 'b'): 1.0}, 'SPIN')
AdjDictBQM({a: -1.0, b: 0.0}, {('a', 'b'): 1.0}, 0.0, 'SPIN')

The next example demonstrates incremental construction:

>>> bqm = dimod.AdjDictBQM('SPIN')
>>> bqm.add_variable('a')
'a'
>>> bqm.add_variable()
1
>>> bqm.set_quadratic('a', 1, 3.0)
>>> bqm
AdjDictBQM({a: 0.0, 1: 0.0}, {('a', 1): 3.0}, 0.0, 'SPIN')

This example shows support for arbitrary types.

>>> import numpy as np
>>> from fractions import Fraction
>>> dimod.AdjDictBQM({('a', 'b'): Fraction(1, 3)}, 'BINARY')
AdjDictBQM({a: 0.0, b: 0.0}, {('a', 'b'): 1/3}, 0.0, 'BINARY')

Attributes

num_interactions The number of interactions in the model.
num_variables The number of variables in the model.
offset The constant energy offset associated with the model.
shape A 2-tuple, the num_variables and num_interactions.
variables The variables of the binary quadratic model.
vartype The vartype of the binary quadratic model.

Views

adj Quadratic biases as a nested dict of dicts.
linear Linear biases as a mapping.
quadratic Quadratic biases as a flat mapping.
binary The binary-valued version of the binary quadratic model.
spin The spin-valued version of the binary quadratic model.

Methods

add_offset(offset) Add specified value to the offset of a binary quadratic model.
add_interaction(u, v, bias) Add an interaction and/or quadratic bias to a binary quadratic model.
add_interactions_from(quadratic) Add interactions and/or quadratic biases to a binary quadratic model.
add_variable([v, bias]) Add a variable to the binary quadratic model.
add_variables_from(linear) Add variables and/or linear biases to a binary quadratic model.
change_vartype(vartype[, inplace]) Return a binary quadratic model with the specified vartype.
contract_variables(u, v) Enforce u, v being the same variable in a binary quadratic model.
copy([deep]) Return a copy.
degree(v) The number of variables sharing an interaction with v.
degrees([array, dtype])
empty(vartype) Create a new empty binary quadratic model.
energies(samples_like[, dtype]) Determine the energies of the given samples.
energy(sample[, dtype])
fix_variables(fixed) Fix the value of the variables and remove them.
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_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(linear, quadratic, …[, …]) Create a binary quadratic model from vectors.
from_qubo(Q[, offset]) Create a binary quadratic model from a QUBO problem.
get_linear(v) Get the linear bias of v.
get_quadratic(u, v[, default]) Get the quadratic bias of (u, v).
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()
iter_neighbors(u) Iterate over neighbors of a variable in the binary quadratic model.
iter_quadratic([variables])
iter_variables() Iterate over the variables of the binary quadratic model.
normalize([bias_range, quadratic_range, …]) Normalizes the biases of the binary quadratic model such that they fall in the provided range(s), and adjusts the offset appropriately.
relabel_variables(mapping[, inplace]) Relabel variables of a binary quadratic model as specified by mapping.
relabel_variables_as_integers([inplace]) Relabel the variables of the BQM to integers.
scale(scalar[, ignored_variables, …]) Multiply all the biases by the specified scalar.
set_linear(v, bias) Set the linear biase of a variable v.
set_quadratic(u, v, bias) Set the quadratic bias of (u, v).
shapeable()
to_coo([fp, vartype_header]) Serialize the binary quadratic model to a COOrdinate format encoding.
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([variable_order, dtype, …]) The BQM as 4 numpy vectors, the offset and a list of variables.
to_qubo() Convert a binary quadratic model to QUBO format.
remove_interaction(u, v) Remove the interaction between variables u and v.
remove_interactions_from(interactions) Remove the given interactions from the binary quadratic model.
remove_offset() Set the binary quadratic model’s offset to zero.
remove_variable([v]) Remove a variable and its associated interactions.
remove_variables_from(variables) Remove the given variables from the binary quadratic model.
update(other) Update the binary quadratic model, adding biases from another.