dimod.binary.Spins#

Spins(labels: int | Iterable[Hashable], dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] = None) Iterator[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]