dwave.preprocessing.composites.__init__.SpinReversalTransformComposite.sample#

SpinReversalTransformComposite.sample(bqm: BinaryQuadraticModel, *, srts: ndarray | None = None, num_spin_reversal_transforms: int | None = None, **kwargs)[source]#

Sample from the binary quadratic model.

Parameters:
  • bqm – Binary quadratic model to be sampled from.

  • srts – A boolean NumPy array with shape (num_spin_reversal_transforms, bqm.num_variables). True indicates a flip and False indicates no flip; applied to in the order given by bqm.variables. If this is not specified as an input values are generated uniformly at random from the class pseudo-random number generator.

  • num_spin_reversal_transforms – Number of spin reversal transform runs. A value of 0 will not transform the problem. If you specify a nonzero value, each spin reversal transform will result in an independent run of the child sampler. If srts is set then num_spin_reversal_transforms is inferred by the shape, otherwise the default is 1.

Returns:

A sample set. Note that for a sampler that returns num_reads samples, the sample set will contain num_reads*num_spin_reversal_transforms samples.

Raises:
  • ValueError – If srts is inconsistent with

  • num_spin_reversal_transforms` or the binary quadratic model

Examples

This example runs 10 spin reversals applied to an unfrustrated chain of length 6.

Using the lowest energy (ground) state returned, you can define a special SRT that transforms all programmed couplers to be ferromagnetic (ground state to all 1).

>>> from dimod import ExactSolver
>>> import numpy as np
>>> from dwave.preprocessing.composites import SpinReversalTransformComposite
>>> base_sampler = ExactSolver()
>>> composed_sampler = SpinReversalTransformComposite(base_sampler)
...
>>> num_var = 6
>>> num_spin_reversal_transforms = 10
>>> J = {(i, i+1): np.random.choice([-1,1]) for i in range(num_var-1)}
>>> h = {i: 0 for i in range(num_var)}
>>> response = composed_sampler.sample_ising(h, J,
...               num_spin_reversal_transforms=num_spin_reversal_transforms)
>>> len(response) == 2**num_var * num_spin_reversal_transforms
True
>>> srts = np.array([[response.first.sample[i] != 1 for i in range(num_var)]])
>>> response = composed_sampler.sample_ising(h, J,
...               srts=srts, num_reads=1)
>>> sum(response.record.num_occurrences) == 2**num_var
True