dimod.binary.Spins

Spins(labels: Union[int, Iterable[Hashable]], dtype: Union[numpy.dtype, None, type, numpy.typing._dtype_like._SupportsDType[numpy.dtype], str, Tuple[Any, int], Tuple[Any, Union[typing_extensions.SupportsIndex, Sequence[typing_extensions.SupportsIndex]]], List[Any], numpy.typing._dtype_like._DTypeDict, Tuple[Any, Any]] = None) Iterator[dimod.binary.binary_quadratic_model.BinaryQuadraticModel][source]

Yield binary quadratic models, each with a single spin variable.

Parameters
  • labels – Either an iterable of variable labels or a number. If a number labels are generated using uuid.UUID.

  • dtype – Data type for the returned binary quadratic models.

Yields

A BinaryQuadraticModel for each spin-valued binary variable.

Examples

This example generates a BQM that represents the constraint \(s_1 = s_2\) as a penalty model, \(-\frac{1}{2}s_1s_2\), described in the system documentation’s Getting Started with D-Wave Solvers guide. The output of the brute-force solver, ExactSolver, shows that best solutions are for eqaul assignments of the variables’ values.

>>> s1, s2 = dimod.Spins(["s1", "s2"])
>>> bqm = -0.5*s1*s2
>>> print(bqm.to_polystring())
-0.5*s1*s2
>>> print(dimod.ExactSolver().sample(bqm).lowest())
  s1 s2 energy num_oc.
0 -1 -1   -0.5       1
1 +1 +1   -0.5       1
['SPIN', 2 rows, 2 samples, 2 variables]