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(cxxgraph[, beta_max, ...])

Make geometric cooling beta schedule.

class openjij.sampler.sa_sampler.SASampler[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.

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.

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.

properties#
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: openj.model.model.BinaryQuadraticModel | dimod.BinaryQuadraticModel, beta_min: float | None = None, beta_max: float | None = None, num_sweeps: int | None = None, num_reads: int | None = None, schedule: list | None = None, initial_state: list | dict | None = None, updater: str | None = None, sparse: bool | None = None, reinitialize_state: bool | None = None, seed: int | None = None) 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

  • sparse (bool) – use sparse matrix or not.

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

  • seed (int) – seed for Monte Carlo algorithm

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: dict[tuple, float], vartype: str | None = None, num_sweeps: int = 1000, num_reads: int = 1, num_threads: int = 1, beta_min: float | None = None, beta_max: float | None = None, updater: str = 'METROPOLIS', random_number_engine: str = 'XORSHIFT', seed: int | None = None, temperature_schedule: str = 'GEOMETRIC')[source]#

Sampling from higher order unconstrainted binary optimization.

Parameters:
  • J (dict) – Interactions.

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

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

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

  • num_threads (int, optional) – The number of threads. Parallelized for each sampling with num_reads > 1. Defaults to 1.

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

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

  • updater (str, optional) – Updater. One can choose “METROPOLIS”, “HEAT_BATH”, or “k-local”. Defaults to “METROPOLIS”.

  • random_number_engine (str, optional) – Random number engine. One can choose “XORSHIFT”, “MT”, or “MT_64”. Defaults to “XORSHIFT”.

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

  • temperature_schedule (str, optional) – Temperature schedule. One can choose “LINEAR”, “GEOMETRIC”. Defaults to “GEOMETRIC”.

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(cxxgraph: Dense | CSRSparse, beta_max=None, beta_min=None, num_sweeps=1000)[source]#

Make geometric cooling beta schedule.

Parameters:
  • cxxgraph (Union[openjij.cxxjij.graph.Dense, openjij.cxxjij.graph.CSRSparse]) – Ising graph, must be either Dense or CSRSparse.

  • 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]