dimod.generators.combinations

combinations(n: Union[int, Collection[Hashable]], k: int, strength: float = 1, vartype: Union[dimod.vartypes.Vartype, str, frozenset] = Vartype.BINARY) dimod.binary.binary_quadratic_model.BinaryQuadraticModel[source]

Generate a binary quadratic model that is minimized when k of n variables are selected.

More fully, generates a binary quadratic model (BQM) that is minimized for each of the k-combinations of its variables.

The energy for the BQM is given by \((\sum_{i} x_i - k)^2\).

Parameters
  • n – If n is an integer, variables are labelled [0, n). If n is a list or set, variables are labelled accordingly.

  • k – The generated BQM has 0 energy when any k of its variables are 1.

  • strength – The energy of the first excited state of the binary quadratic model.

  • vartype

    Variable type for the BQM. Accepted input values:

    • Vartype.SPIN, 'SPIN', {-1, 1}

    • Vartype.BINARY, 'BINARY', {0, 1}

Returns

A binary quadratic model.

Examples

>>> bqm = dimod.generators.combinations(['a', 'b', 'c'], 2)
>>> bqm.energy({'a': 1, 'b': 0, 'c': 1})
0.0
>>> bqm.energy({'a': 1, 'b': 1, 'c': 1})
1.0
>>> bqm = dimod.generators.combinations(5, 1)
>>> bqm.energy({0: 0, 1: 0, 2: 1, 3: 0, 4: 0})
0.0
>>> bqm.energy({0: 0, 1: 0, 2: 1, 3: 1, 4: 0})
1.0
>>> bqm = dimod.generators.combinations(['a', 'b', 'c'], 2, strength=3.0)
>>> bqm.energy({'a': 1, 'b': 0, 'c': 1})
0.0
>>> bqm.energy({'a': 1, 'b': 1, 'c': 1})
3.0