dimod.ConstrainedQuadraticModel.add_constraint_from_iterable

ConstrainedQuadraticModel.add_constraint_from_iterable(iterable: Iterable, sense: Union[dimod.sym.Sense, str], rhs: Union[float, numpy.floating, numpy.integer] = 0, label: Optional[Hashable] = None) Hashable[source]

Add a constraint from an iterable of tuples.

Parameters
  • 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.

Returns

Label of the added constraint.

Examples

>>> from dimod import ConstrainedQuadraticModel, Integer, Binary
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.add_variable('INTEGER', 'i')
'i'
>>> cqm.add_variable('INTEGER', 'j')
'j'
>>> cqm.add_variable('BINARY', 'x')
'x'
>>> cqm.add_variable('BINARY', 'y')
'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 + x*y + i*j <= 1