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 (BQM).
- 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¶
The number of interactions in the model. |
|
The number of variables in the model. |
|
The constant energy offset associated with the model. |
|
A 2-tuple of |
|
Variables of the binary quadratic model. |
|
The vartype of the binary quadratic model. |
Views¶
Quadratic biases as a nested dict of dicts. |
|
Linear biases as a mapping. |
|
Quadratic biases as a flat mapping. |
|
Binary-valued version of the binary quadratic model. |
|
Spin-valued version of the binary quadratic model. |
Methods¶
|
Add the specified value to the offset of a binary quadratic model. |
|
Add an interaction and/or quadratic bias to a binary quadratic model. |
|
Add interactions and/or quadratic biases to a binary quadratic model. |
|
Add a variable to the binary quadratic model. |
|
Add variables and/or linear biases to a binary quadratic model. |
|
Return a binary quadratic model with the specified vartype. |
|
Enforce u, v being the same variable in a binary quadratic model. |
|
Return a copy. |
|
Return degree of the specified variable. |
|
Return the degrees of a binary quadratic model’s variables. |
|
Create a new empty binary quadratic model. |
|
Determine the energies of the given samples. |
|
Determine the energy of the given sample. |
|
Fix the value of the variables and remove them. |
Flip variable v in a binary quadratic model. |
|
|
Deserialize a binary quadratic model from a COOrdinate format encoding. |
|
Create a binary quadratic model from an Ising problem. |
|
Create a binary quadratic model from a NetworkX graph. |
|
Create a binary quadratic model from a NumPy array. |
|
Create a binary quadratic model from vectors. |
|
Create a binary quadratic model from a QUBO problem. |
|
Get the linear bias of the specified variable. |
|
Get the quadratic bias of the specified interaction. |
|
Return True if v is a variable in the binary quadratic model. |
Iterate over the interactions of the binary quadratic model. |
|
Iterate over the linear biases of the binary quadratic model. |
|
Iterate over neighbors of a variable in the binary quadratic model. |
|
|
Iterate over the quadratic biases of the binary quadratic model. |
Iterate over the variables of the binary quadratic model. |
|
|
Normalizes the biases of the binary quadratic model to fall in the provided range(s), and adjusts the offset appropriately. |
|
Relabel variables of a binary quadratic model as specified by mapping. |
|
Relabel the variables of the binary quadratic model to integers. |
|
Remove the interaction between the specified variables. |
|
Remove the given interactions from the binary quadratic model. |
Set the binary quadratic model’s offset to zero. |
|
|
Remove a variable and its associated interactions. |
|
Remove the given variables from the binary quadratic model. |
|
Multiply all the biases by the specified scalar. |
|
Set the linear biase of the specified variable. |
|
Set the quadratic bias of an interaction specified by its variables. |
Returns True if the binary quadratic model is shapeable. |
|
|
Serialize the binary quadratic model to a COOrdinate format encoding. |
|
Converts a binary quadratic model to Ising format. |
|
Convert a binary quadratic model to NetworkX graph format. |
|
Convert a binary quadratic model to NumPy 2D array. |
|
Convert binary quadratic model to NumPy vectors. |
|
Convert a binary quadratic model to QUBO format. |
|
Update the binary quadratic model, adding biases from another. |