ConstrainedQuadraticModel.add_discrete_from_iterable(variables: Iterable[Hashable], label: Optional[Hashable] = None) Hashable[source]

Add a one-hot constraint from an iterable.

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.

  • variables – An iterable of variables.

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


Label of the added constraint.


>>> cqm = dimod.ConstrainedQuadraticModel()
>>> color = ["red", "blue", "green"]
>>> for v in color:
...      cqm.add_variable('BINARY', v)
>>> cqm.add_discrete(color, label='one-color')
>>> print(cqm.constraints['one-color'].to_polystring())
red + blue + green == 1