ConstrainedQuadraticModel.add_discrete(data, *args, **kwargs) Hashable[source]#

A convenience wrapper for other methods that add one-hot constraints.

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 one such constraint must not be used in others in the model.

Constraints added by the methods wrapped by add_discrete() are guaranteed to be satisfied in solutions returned by the LeapHybridCQMSampler hybrid sampler.


>>> cqm = dimod.ConstrainedQuadraticModel()

Add a discrete constraint over variables x, y, z from an iterable.

>>> iterable = ['x', 'y', 'z']
>>> for v in iterable:
...      cqm.add_variable('BINARY', v)
>>> cqm.add_discrete(iterable, label='discrete-xyz')

Add a discrete constraint over variables a, b, c from a model.

>>> a, b, c = dimod.Binaries(['a', 'b', 'c'])
>>> cqm.add_discrete(sum([a, b, c]), label='discrete-abc')

Add a discrete constraint over variables d, e, f from a comparison.

>>> d, e, f = dimod.Binaries(['d', 'e', 'f'])
>>> cqm.add_discrete(d + e + f == 1, label='discrete-def')