dimod.ConstrainedQuadraticModel.add_discrete_from_comparison¶
- ConstrainedQuadraticModel.add_discrete_from_comparison(comp: dimod.sym.Comparison, label: Optional[Hashable] = None, copy: 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.- Parameters
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.
- Returns
Label of the added constraint.
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'