dimod.BinaryQuadraticModel.add_interactions_from

BinaryQuadraticModel.add_interactions_from(quadratic, vartype=None)[source]

Add interactions and/or quadratic biases to a binary quadratic model.

Parameters:
  • quadratic (dict[(variable, variable), bias]/iterable[(variable, variable, bias)]) – A collection of variables that have an interaction and their quadratic bias to add to the model. If a dict, keys are 2-tuples of variables in the binary quadratic model and values are their corresponding bias. Alternatively, an iterable of 3-tuples. Each interaction in quadratic should be unique; that is, if (u, v) is a key, (v, u) should not be. Variables can be any python object that is a valid dict key. Many methods and functions expect the biases to be numbers but this is not explicitly checked.
  • vartype (Vartype, optional, default=None) – Vartype of the given bias. If None, the vartype of the binary quadratic model is used. Valid values are Vartype.SPIN or Vartype.BINARY.

Examples

This example creates creates an empty Ising model, adds an interaction for two variables, adds to its bias while adding a new variable, then adds another interaction.

>>> import dimod
...
>>> bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)
>>> bqm.add_interactions_from({('a', 'b'): -.5})
>>> bqm.quadratic[('a', 'b')]
-0.5
>>> bqm.add_interactions_from({('a', 'b'): -.5, ('a', 'c'): 2})
>>> bqm.add_interactions_from({('b', 'c'): 2}, vartype=dimod.BINARY)   # Binary value is converted to spin value
>>> len(bqm.quadratic)
3
>>> bqm.quadratic[('a', 'b')]
-1.0