dimod.ConstrainedQuadraticModel.add_discrete_from_model

ConstrainedQuadraticModel.add_discrete_from_model(qm: Union[dimod.binary.binary_quadratic_model.BinaryQuadraticModel, dimod.quadratic.quadratic_model.QuadraticModel], label: Optional[Hashable] = None, copy: bool = True) Hashable[source]

Add a one-hot constraint from a model.

One-hot constraints can represent discrete variables (for example a color variable that has values {"red", "blue", "green"}) by requiring that only one of a set of two or more binary variables is assigned a value of 1.

These constraints support only BINARY variables and must be disjoint; that is, variables in such a constraint must not be used elsewhere in the model.

Constraints added by this method are guaranteed to be satisfied in solutions returned by the LeapHybridCQMSampler hybrid sampler.

Parameters
  • qm – A quadratic model or binary quadratic model. The model must be linear with all of the linear biases on the left-hand side equal to one and the right hand side equal to one.

  • label – A label for the constraint. Must be unique. If no label is provided, one is generated using uuid.

  • copy – If True, the model is copied. You can set to False to improve performance, but subsequently mutating the model can cause issues.

Returns

Label of the added constraint.

Examples

>>> cqm = dimod.ConstrainedQuadraticModel()
>>> r, b, g = dimod.Binaries(["red", "blue", "green"])
>>> cqm.add_discrete(sum([r, g, b]), label="One color")
'One color'
>>> print(cqm.constraints["One color"].to_polystring())
red + green + blue == 1