dimod.generators.combinations#

combinations(n: int | Collection[Hashable], k: int, strength: float = 1, vartype: Vartype | str | frozenset = Vartype.BINARY) 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 – Variable labels. If n is an integer, variables are labelled [0, n).

  • k – The number of selected variables (variables assigned value 1) that minimizes the generated BQM, resulting in an energy of 0.

  • strength – Energy of the first excited state of the BQM. The first excited state occurs when the number of selected variables is \(k \pm 1\).

  • 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