dimod.ConstrainedQuadraticModel.add_constraint_from_model¶
- ConstrainedQuadraticModel.add_constraint_from_model(qm: Union[dimod.binary.binary_quadratic_model.BinaryQuadraticModel, dimod.quadratic.quadratic_model.QuadraticModel], sense: Union[dimod.sym.Sense, str], rhs: Union[float, numpy.floating, numpy.integer] = 0, label: Optional[Hashable] = None, copy: bool = True) Hashable [source]¶
Add a constraint from a quadratic model.
- Parameters
qm – Quadratic model or binary quadratic model.
sense – One of <=, >=, ==.
rhs – Right hand side of the constraint.
label – Label for the constraint. Must be unique. If no label is provided, then one is generated using
uuid
.copy – If True, model
qm
is copied. This can be set to False to improve performance, but subsequently mutatingqm
can cause issues.
- Returns
Label of the added constraint.
Examples
This example adds a constraint from the single-variable binary quadratic model
x
.>>> from dimod import ConstrainedQuadraticModel, Binary >>> cqm = ConstrainedQuadraticModel() >>> x = Binary('x') >>> cqm.add_constraint_from_model(x, '>=', 0, 'Min x') 'Min x' >>> print(cqm.constraints["Min x"].to_polystring()) x >= 0
Adding a constraint without copying the model requires caution:
>>> cqm.add_constraint_from_model(x, "<=", 3, "Risky constraint", copy=False) 'Risky constraint' >>> x *= 2 >>> print(cqm.constraints["Risky constraint"].to_polystring()) 2*x <= 3