dimod.BinaryQuadraticModel¶

class
BinaryQuadraticModel
(*args, **kwargs)[source]¶ Encodes a binary quadratic model.
Binary quadratic model is the superclass that contains the Ising model and the QUBO.
Parameters:  linear (dict[variable, bias]) – Linear biases as a dict, where keys are the variables of the binary quadratic model and values the linear biases associated with these variables. A variable can be any python object that is valid as a dictionary key. Biases are generally numbers but this is not explicitly checked.
 quadratic (dict[(variable, variable), bias]) – Quadratic biases as a dict, where keys are 2tuples of variables and values the quadratic biases associated with the pair of variables (the interaction). A variable can be any python object that is valid as a dictionary key. Biases are generally numbers but this is not explicitly checked. Interactions that are not unique are added.
 offset (number) – Constant energy offset associated with the binary quadratic model.
Any input type is allowed, but many applications assume that offset is a number.
See
BinaryQuadraticModel.energy()
.  vartype (
Vartype
/str/set) –Variable type for the binary quadratic model. Accepted input values:
Vartype.SPIN
,'SPIN'
,{1, 1}
Vartype.BINARY
,'BINARY'
,{0, 1}
 **kwargs – Any additional keyword parameters and their values are stored in
BinaryQuadraticModel.info
.
Notes
The
BinaryQuadraticModel
class does not enforce types on biases and offsets, but most applications that use this class assume that they are numeric.Examples
This example creates a binary quadratic model with three spin variables.
>>> bqm = dimod.BinaryQuadraticModel({0: 1, 1: 1, 2: .5}, ... {(0, 1): .5, (1, 2): 1.5}, ... 1.4, ... dimod.Vartype.SPIN)
This example creates a binary quadratic model with nonnumeric variables (variables can be any hashable object).
>>> bqm = dimod.BQM({'a': 0.0, 'b': 1.0, 'c': 0.5}, ... {('a', 'b'): 1.0, ('b', 'c'): 1.5}, ... 1.4, ... dimod.SPIN) >>> len(bqm) 3 >>> 'b' in bqm True

linear
¶ Linear biases as a dict, where keys are the variables of the binary quadratic model and values the linear biases associated with these variables.
Type: dict[variable, bias]

quadratic
¶ Quadratic biases as a dict, where keys are 2tuples of variables, which represent an interaction between the two variables, and values are the quadratic biases associated with the interactions.
Type: dict[(variable, variable), bias]

offset
¶ The energy offset associated with the model. Same type as given on instantiation.
Type: number

variables
¶ The variables in the binary quadratic model as a dictionary keys view object.
Type: keysview

adj
¶ The model’s interactions as nested dicts. In graphic representation, where variables are nodes and interactions are edges or adjacencies, keys of the outer dict (adj) are all the model’s nodes (e.g. v) and values are the inner dicts. For the inner dict associated with outerkey/node ‘v’, keys are all the nodes adjacent to v (e.g. u) and values are quadratic biases associated with the pair of inner and outer keys (u, v).
Type: dict
Examples
This example creates an instance of the
BinaryQuadraticModel
class for the K4 complete graph, where the nodes have biases set equal to their sequential labels and interactions are the concatenations of the node pairs (e.g., 23 for u,v = 2,3).>>> linear = {1: 1, 2: 2, 3: 3, 4: 4} >>> quadratic = {(1, 2): 12, (1, 3): 13, (1, 4): 14, ... (2, 3): 23, (2, 4): 24, ... (3, 4): 34} >>> offset = 0.0 >>> vartype = dimod.BINARY >>> bqm_k4 = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype) >>> len(bqm_k4.adj[2]) # Adjacencies for node 2 3 >>> bqm_k4.adj[2][3] # Show the quadratic bias for nodes 2,3 23