ConstrainedQuadraticModel.add_discrete_from_comparison(comp: Comparison, label: Hashable | None = None, copy: bool = True, check_overlaps: bool = True) Hashable[source]#

Add a one-hot constraint from a comparison.

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.

  • comp – Comparison object. The comparison must be a linear equality constraint with all of the linear biases on the left-hand side equal to one and the right-hand side equal to one.

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

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

  • check_overlaps – If True we perform a variable overlap check. In particular, if the variables already exist, we make sure they’re not used in another discrete constraint.


Label of the added constraint.


>>> 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'