openjij.sampler.sa_sampler#

Module Contents#

Classes#

SASampler

Sampler with Simulated Annealing (SA).

Functions#

geometric_hubo_beta_schedule(sa_system, beta_max, ...)

geometric_ising_beta_schedule(model[, beta_max, ...])

Make geometric cooling beta schedule.

class openjij.sampler.sa_sampler.SASampler(beta_min: Optional[float] = None, beta_max: Optional[float] = None, num_sweeps: Optional[int] = None, num_reads: Optional[int] = None, schedule: Optional[list] = None)[source]#

Bases: openjij.sampler.sampler.BaseSampler

Inheritance diagram of openjij.sampler.sa_sampler.SASampler

Sampler with Simulated Annealing (SA).

Parameters
  • beta_min (float) – Minmum beta (inverse temperature). You can overwrite in methods .sample_*.

  • beta_max (float) – Maximum beta (inverse temperature). You can overwrite in methods .sample_*.

  • num_reads (int) – number of sampling (algorithm) runs. defaults None. You can overwrite in methods .sample_*.

  • num_sweeps (int) – number of MonteCarlo steps during SA. defaults None. You can overwrite in methods .sample_*.

  • schedule_info (dict) – Information about an annealing schedule.

  • schedule (Optional[list]) –

Raises
  • ValueError – If schedules or variables violate as below.

  • - not list or numpy.array.

  • - not list of tuple (beta – float, step_length : int).

  • - beta is less than zero.

properties#
property parameters#

Parameters as a dict, where keys are keyword parameters accepted by the sampler methods and values are lists of the properties relevent to each parameter.

remove_unknown_kwargs(**kwargs) Dict[str, Any]#

Remove with warnings any keyword arguments not accepted by the sampler.

Parameters

**kwargs – Keyword arguments to be validated.

Return type

Dict[str, Any]

Returns: Updated kwargs dict.

Examples

>>> import warnings
>>> sampler = dimod.RandomSampler()
>>> with warnings.catch_warnings():
...     warnings.filterwarnings('ignore')
...     try:
...         sampler.remove_unknown_kwargs(num_reads=10, non_param=3)
...     except dimod.exceptions.SamplerUnknownArgWarning:
...        pass
{'num_reads': 10}
sample(bqm: Union[openj.model.model.BinaryQuadraticModel, dimod.BinaryQuadraticModel], beta_min: Optional[float] = None, beta_max: Optional[float] = None, num_sweeps: Optional[int] = None, num_reads: Optional[int] = None, schedule: Optional[list] = None, initial_state: Optional[Union[list, dict]] = None, updater: Optional[str] = None, sparse: Optional[bool] = None, reinitialize_state: Optional[bool] = None, seed: Optional[int] = None) openjij.sampler.response.Response[source]#

Sample Ising model.

Parameters
  • bqm (openjij.model.model.BinaryQuadraticModel) –

  • beta_min (float) – minimal value of inverse temperature

  • beta_max (float) – maximum value of inverse temperature

  • num_sweeps (int) – number of sweeps

  • num_reads (int) – number of reads

  • schedule (list) – list of inverse temperature

  • initial_state (dict) – initial state

  • updater (str) – updater algorithm

  • reinitialize_state (bool) – if true reinitialize state for each run

  • seed (int) – seed for Monte Carlo algorithm

  • sparse (Optional[bool]) –

Returns

results

Return type

openjij.sampler.response.Response

Examples

for Ising case:

>>> h = {0: -1, 1: -1, 2: 1, 3: 1}
>>> J = {(0, 1): -1, (3, 4): -1}
>>> sampler = openj.SASampler()
>>> res = sampler.sample_ising(h, J)

for QUBO case:

>>> Q = {(0, 0): -1, (1, 1): -1, (2, 2): 1, (3, 3): 1, (4, 4): 1, (0, 1): -1, (3, 4): 1}
>>> sampler = openj.SASampler()
>>> res = sampler.sample_qubo(Q)
sample_hubo(J: Union[dict, openj.model.model.BinaryPolynomialModel, cimod.BinaryPolynomialModel], vartype: Optional[str] = None, beta_min: Optional[float] = None, beta_max: Optional[float] = None, num_sweeps: Optional[int] = None, num_reads: Optional[int] = None, schedule: Optional[list] = None, initial_state: Optional[Union[list, dict]] = None, updater: Optional[str] = None, reinitialize_state: Optional[bool] = None, seed: Optional[int] = None) openjij.sampler.response.Response[source]#

Sampling from higher order unconstrainted binary optimization.

Parameters
  • J (dict) – Interactions.

  • vartype (str, openjij.VarType) – “SPIN” or “BINARY”.

  • beta_min (float, optional) – Minimum beta (initial inverse temperature). Defaults to None.

  • beta_max (float, optional) – Maximum beta (final inverse temperature). Defaults to None.

  • schedule (list, optional) – schedule list. Defaults to None.

  • num_sweeps (int, optional) – number of sweeps. Defaults to None.

  • num_reads (int, optional) – number of reads. Defaults to 1.

  • init_state (list, optional) – initial state. Defaults to None.

  • reinitialize_state (bool) – if true reinitialize state for each run

  • seed (int, optional) – seed for Monte Carlo algorithm. Defaults to None.

  • initial_state (Optional[Union[list, dict]]) –

  • updater (Optional[str]) –

Returns

results

Return type

openjij.sampler.response.Response

Examples::
for Ising case::
>>> sampler = openjij.SASampler()
>>> J = {(0,): -1, (0, 1): -1, (0, 1, 2): 1}
>>> response = sampler.sample_hubo(J, "SPIN")
for Binary case::
>>> sampler = ooenjij.SASampler()
>>> J = {(0,): -1, (0, 1): -1, (0, 1, 2): 1}
>>> response = sampler.sample_hubo(J, "BINARY")
sample_ising(h, J, **parameters)#

Sample from an Ising model using the implemented sample method.

Parameters
  • h (dict) – Linear biases

  • J (dict) – Quadratic biases

Returns

results

Return type

openjij.sampler.response.Response

sample_qubo(Q, **parameters)#

Sample from a QUBO model using the implemented sample method.

Parameters

Q (dict or numpy.ndarray) – Coefficients of a quadratic unconstrained binary optimization

Returns

results

Return type

openjij.sampler.response.Response

openjij.sampler.sa_sampler.geometric_hubo_beta_schedule(sa_system, beta_max, beta_min, num_sweeps)[source]#
openjij.sampler.sa_sampler.geometric_ising_beta_schedule(model: openjij.model.model.BinaryQuadraticModel, beta_max=None, beta_min=None, num_sweeps=1000)[source]#

Make geometric cooling beta schedule.

Parameters
  • model (openjij.model.BinaryQuadraticModel) –

  • beta_max (float, optional) – [description]. Defaults to None.

  • beta_min (float, optional) – [description]. Defaults to None.

  • num_sweeps (int, optional) – [description]. Defaults to 1000.

Returns

list of cxxjij.utility.ClassicalSchedule, list of beta range [max, min]