dimod.BinaryQuadraticModel.add_variables_from¶
-
BinaryQuadraticModel.add_variables_from(linear, vartype=None)[source]¶ Add variables and/or linear biases to a binary quadratic model.
Parameters: - linear (dict[variable, bias]/iterable[(variable, bias)]) – A collection of variables and their linear biases to add to the model. If a dict, keys are variables in the binary quadratic model and values are biases. Alternatively, an iterable of (variable, bias) pairs. 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. If any variable already exists in the model, its bias is added to the variable’s current linear bias.
- vartype (
Vartype, optional, default=None) – Vartype of the given bias. If None, the vartype of the binary quadratic model is used. Valid values areVartype.SPINorVartype.BINARY.
Examples
This example creates creates an empty Ising model, adds two variables, and subsequently adds to the bias of the one while adding a new, third, variable.
>>> import dimod ... >>> bqm = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN) >>> len(bqm.linear) 0 >>> bqm.add_variables_from({'a': .5, 'b': -1.}) >>> 'b' in bqm True >>> bqm.add_variables_from({'b': -1., 'c': 2.0}) >>> bqm.linear['b'] -2.0