dwavebinarycsp.Constraint.from_func#

classmethod Constraint.from_func(func, variables, vartype, name=None)[source]#

Construct a constraint from a validation function.

Parameters:
  • func (function) – Function that evaluates True when the variables satisfy the constraint.

  • variables (iterable) – Iterable of variable labels.

  • vartype (Vartype/str/set) –

    Variable type for the constraint. Accepted input values:

  • name (string, optional, default='Constraint') – Name for the constraint.

Examples

This example creates a constraint that binary variables a and b are not equal.

>>> import operator
>>> const = dwavebinarycsp.Constraint.from_func(operator.ne, ['a', 'b'], 'BINARY')
>>> print(const.name)
Constraint
>>> (0, 1) in const.configurations
True

This example creates a constraint that \(out = NOT(x)\) for spin variables.

>>> def not_(y, x):  # y=NOT(x) for spin variables
...     return (y == -x)
...
>>> const = dwavebinarycsp.Constraint.from_func(
...               not_,
...               ['out', 'in'],
...               {1, -1},
...               name='not_spin')
>>> print(const.name)
not_spin
>>> (1, -1) in const.configurations
True