dimod.BinaryQuadraticModel.add_interaction

BinaryQuadraticModel.add_interaction(u, v, bias, vartype=None)[source]

Add an interaction and/or quadratic bias to a binary quadratic model.

Parameters:
  • v (variable) – One of the pair of variables to add to the model. Can be any python object that is a valid dict key.
  • u (variable) – One of the pair of variables to add to the model. Can be any python object that is a valid dict key.
  • bias (bias) – Quadratic bias associated with u, v. If u, v is already in the model, this value is added to the current quadratic bias. Many methods and functions expect bias to be a number 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 an Ising model with two variables, adds a third, adds to the bias of the initial interaction, and creates a new interaction.

>>> import dimod
...
>>> bqm = dimod.BinaryQuadraticModel({0: 0.0, 1: 1.0}, {(0, 1): 0.5}, -0.5, dimod.SPIN)
>>> len(bqm.quadratic)
1
>>> bqm.add_interaction(0, 2, 2)        # Add new variable 2
>>> bqm.add_interaction(0, 1, .25)
>>> bqm.add_interaction(1, 2, .25, vartype=dimod.BINARY)     # Binary value is converted to spin value
>>> len(bqm.quadratic)
3
>>> bqm.quadratic[(0, 1)]
0.75