dwave.samplers.SimulatedAnnealingSampler.sample

SimulatedAnnealingSampler.sample(bqm: dimod.binary.binary_quadratic_model.BinaryQuadraticModel, beta_range: Optional[Union[List[float], Tuple[float, float]]] = None, num_reads: Optional[int] = None, num_sweeps: Optional[int] = None, num_sweeps_per_beta: int = 1, beta_schedule_type: str = 'geometric', seed: Optional[int] = None, interrupt_function=None, beta_schedule: Optional[Union[Sequence[float], numpy.ndarray]] = None, initial_states: Optional[Union[Sequence[Union[float, numpy.floating, numpy.integer]], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Tuple[Sequence[Union[float, numpy.floating, numpy.integer]], Sequence[Hashable]], Tuple[numpy.ndarray, Sequence[Hashable]], numpy.ndarray, Sequence[Sequence[Union[float, numpy.floating, numpy.integer]]], Tuple[Sequence[Sequence[Union[float, numpy.floating, numpy.integer]]], Sequence[Hashable]], Sequence[Union[Sequence[Union[float, numpy.floating, numpy.integer]], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Tuple[Sequence[Union[float, numpy.floating, numpy.integer]], Sequence[Hashable]], Tuple[numpy.ndarray, Sequence[Hashable]], numpy.ndarray]], Iterator[Union[Sequence[Union[float, numpy.floating, numpy.integer]], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Tuple[Sequence[Union[float, numpy.floating, numpy.integer]], Sequence[Hashable]], Tuple[numpy.ndarray, Sequence[Hashable]], numpy.ndarray]]]] = None, initial_states_generator: str = 'random', **kwargs) dimod.sampleset.SampleSet[source]

Sample from a binary quadratic model.

Parameters
  • bqm – Binary quadratic model to be sampled.

  • beta_range – A 2-tuple or list defining the beginning and end of the \(\beta\)1 schedule. The schedule is interpolated within this range according to the value specified by beta_schedule_type. Default range is set based on the total bias associated with each node.

  • num_reads – Number of reads. Each read is generated by one run of the simulated annealing algorithm. If num_reads is not explicitly given, it is selected to match the number of initial states given. If initial states are not provided, only one read is performed.

  • num_sweeps – Number of sweeps used in annealing. If no value is provided and beta_schedule is None, the value defaults to 1000.

  • num_sweeps_per_beta (int, optional, default=1) – Number of sweeps to perform at each \(\beta\). One sweep consists of a sequential Metropolis update of all spins.

  • beta_schedule_type

    \(\beta\) schedule type, or how the \(\beta\) values are interpolated between the given beta_range. Supported values are:

    • ”linear”

    • ”geometric”

    • ”custom”

    ”custom” is recommended for high-performance applications, which typically require optimizing \(\beta\) schedules beyond those of the “linear” and “geometric” options, with bounds beyond those provided by default. num_sweeps_per_beta and beta_schedule fully specify a custom schedule.

  • beta_schedule – Sequence of \(\beta\) values swept. Format must be compatible with numpy.array(beta_schedule, dtype=float). Values should be non-negative.

  • seed – Seed to use for the PRNG. Specifying a particular seed with a constant set of parameters produces identical results. If not provided, a random seed is chosen.

  • initial_states – One or more samples, each defining an initial state for all the problem variables. Initial states are given one per read, but if fewer than num_reads initial states are defined, additional values are generated as specified by initial_states_generator. See as_samples() for a description of “samples-like”.

  • initial_states_generator

    Defines the expansion of initial_states if fewer than num_reads are specified:

    • ”none”:

      If the number of initial states specified is smaller than num_reads, raises ValueError.

    • ”tile”:

      Reuses the specified initial states if fewer than num_reads or truncates if greater.

    • ”random”:

      Expands the specified initial states with randomly generated states if fewer than num_reads or truncates if greater.

  • interrupt_function (function, optional) – A function called with no parameters between each sample of simulated annealing. If the function returns True, simulated annealing terminates and returns with all of the samples and energies found so far.

Examples

This example runs simulated annealing on a binary quadratic model with various input parameters.

>>> import dimod
>>> from dwave.samplers import SimulatedAnnealingSampler
...
>>> sampler = SimulatedAnnealingSampler()
>>> bqm = dimod.BinaryQuadraticModel({'a': .5, 'b': -.5},
...                                  {('a', 'b'): -1}, 0.0,
...                                  dimod.SPIN)
>>> # Run with default parameters
>>> sampleset = sampler.sample(bqm)
>>> # Run with specified parameters
>>> sampleset = sampler.sample(bqm, seed=1234,
...                            beta_range=[0.1, 4.2],
...                            num_sweeps=20,
...                            beta_schedule_type='geometric')
>>> # Reuse a seed
>>> a1 = next((sampler.sample(bqm, seed=88)).samples())['a']
>>> a2 = next((sampler.sample(bqm, seed=88)).samples())['a']
>>> a1 == a2
True
1

\(\beta\) represents the inverse temperature, \(1/(k_B T)\), of a Boltzmann distribution where \(T\) is the thermodynamic temperature in kelvin and \(k_B\) is Boltzmann’s constant.