ConstrainedQuadraticModel.add_constraint_from_iterable(iterable: Iterable, sense: Sense | str, rhs: float | floating | integer = 0, label: Hashable | None = None, weight: float | None = None, penalty: str = 'linear') Hashable[source]#

Add a constraint from an iterable of tuples.

  • iterable – Iterable of terms as tuples. The variables must have already been added to the object.

  • sense – One of <=, >=, ==.

  • rhs – The 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.

  • weight – Weight for a soft constraint. Must be a positive number. If None or float('inf'), the constraint is hard. In feasible solutions, all the model’s hard constraints must be met, while soft constraints might be violated to achieve overall good solutions.

  • penalty – Penalty type for a soft constraint (a constraint with its weight parameter set). Supported values are 'linear' and 'quadratic'. Ignored if weight is None. 'quadratic' is supported for a constraint with binary variables only.


Label of the added constraint.


>>> from dimod import ConstrainedQuadraticModel, Integer, Binary
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.add_variable('INTEGER', 'i')
>>> cqm.add_variable('INTEGER', 'j')
>>> cqm.add_variable('BINARY', 'x')
>>> cqm.add_variable('BINARY', 'y')
>>> label1 = cqm.add_constraint_from_iterable([('x', 'y', 1), ('i', 2), ('j', 3),
...                                           ('i', 'j', 1)], '<=', rhs=1)
>>> print(cqm.constraints[label1].to_polystring())
2*i + 3*j + y*x + i*j <= 1.0