dimod.generators.constraints.combinations¶
-
combinations(n, k, strength=1, vartype=<Vartype.BINARY: frozenset([0, 1])>)[source]¶ Generate a bqm that is minimized when k of n variables are selected.
More fully, we wish to generate a binary quadratic model which is minimized for each of the k-combinations of its variables.
The energy for the binary quadratic model is given by \((\sum_{i} x_i - k)^2\).
Parameters: - n (int/list/set) – If n is an integer, variables are labelled [0, n-1]. If n is list or set then the variables are labelled accordingly.
- k (int) – The generated binary quadratic model will have 0 energy when any k of the variables are 1.
- strength (number, optional, default=1) – The energy of the first excited state of the binary quadratic model.
- vartype (
Vartype/str/set) –Variable type for the binary quadratic model. Accepted input values:
Vartype.SPIN,'SPIN',{-1, 1}Vartype.BINARY,'BINARY',{0, 1}
Returns: 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