dimod.ConstrainedQuadraticModel.fix_variable

ConstrainedQuadraticModel.fix_variable(v: Hashable, value: float, *, cascade: bool = False) Dict[Hashable, float][source]

Fix the value of a variable in the model.

Note that this function does not test feasibility.

Parameters
  • v – Variable label for a variable in the model.

  • value – Value to assign variable v.

  • cascade

    If True, additional variables may be removed from the model based on the assignment of v. Currently handles the following cases:

    • Discrete constraints (see add_discrete())

      Fixing one of the binary variables to 1 fixes the remaining to 0. Fixing all but one of the binary variables to 0 fixes the remaining to 1.

    • Equality constraint

      Fixing one of two variables fixes the other. For example fixing i to 3 in i + j == 7 also fixes j to 4

Returns

Assignments of any additional variables fixed. For cascade==False, this is always {}. If you set cascade==True, additional variables may be fixed. See above.

Raises

ValueError – If v is not the label of a variable in the model.

Examples

>>> cqm = dimod.ConstrainedQuadraticModel()
>>> r, b, g = dimod.Binaries(["red", "blue", "green"])
>>> cqm.add_discrete_from_comparison(r + b + g == 1, label="One color")
'One color'
>>> cqm.fix_variable("red", 1, cascade=True)
{'green': 0, 'blue': 0}
>>> cqm = dimod.ConstrainedQuadraticModel()
>>> r, b, g = dimod.Binaries(["red", "blue", "green"])
>>> cqm.add_discrete_from_comparison(r + b + g == 1, label="One color")
'One color'
>>> cqm.fix_variable("red", 0, cascade=True)
{}
>>> cqm.fix_variable("blue", 0, cascade=True)
{'green': 1}