dimod.BinaryQuadraticModel.add_variable

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

Add variable v and/or its bias to a binary quadratic model.

Parameters:
  • v (variable) – The variable to add to the model. Can be any python object that is a valid dict key.
  • bias (bias) – Linear bias associated with v. If v is already in the model, this value is added to its current linear 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, and adds to the linear biases of the initial two.

>>> import dimod
...
>>> bqm = dimod.BinaryQuadraticModel({0: 0.0, 1: 1.0}, {(0, 1): 0.5}, -0.5, dimod.SPIN)
>>> len(bqm.linear)
2
>>> bqm.add_variable(2, 2.0, vartype=dimod.SPIN)        # Add a new variable
>>> bqm.add_variable(1, 0.33, vartype=dimod.SPIN)
>>> bqm.add_variable(0, 0.33, vartype=dimod.BINARY)     # Binary value is converted to spin value
>>> len(bqm.linear)
3
>>> bqm.linear[1]
1.33