openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
openjij.sampler.sa_sampler.SASampler Class Reference
Inheritance diagram for openjij.sampler.sa_sampler.SASampler:
Collaboration diagram for openjij.sampler.sa_sampler.SASampler:

Public Member Functions

 parameters (self)
 __init__ (self)
"oj.sampler.response.Response" sample (self, Union["openj.model.model.BinaryQuadraticModel", dimod.BinaryQuadraticModel] bqm, Optional[float] beta_min=None, Optional[float] beta_max=None, Optional[int] num_sweeps=None, Optional[int] num_reads=None, Optional[list] schedule=None, Optional[Union[list, dict]] initial_state=None, Optional[str] updater=None, Optional[bool] sparse=None, Optional[bool] reinitialize_state=None, Optional[int] seed=None)
 sample_hubo (self, dict[tuple, float] J, Optional[str] vartype=None, int num_sweeps=1000, int num_reads=1, int num_threads=1, Optional[float] beta_min=None, Optional[float] beta_max=None, str updater="METROPOLIS", str random_number_engine="XORSHIFT", Optional[int] seed=None, str temperature_schedule="GEOMETRIC")
"oj.sampler.response.Response" sample_quio (self, dict[tuple, float] J, dict[Any, tuple[int, int]] bound_list, int num_sweeps=1000, int num_reads=1, int num_threads=1, Optional[float] beta_min=None, Optional[float] beta_max=None, str updater="OPT_METROPOLIS", str random_number_engine="XORSHIFT", Optional[int] seed=None, str temperature_schedule="GEOMETRIC", bool log_history=False)
"oj.sampler.response.Response" sample_huio (self, dict[tuple, float] J, dict[Any, tuple[int, int]] bound_list, int num_sweeps=1000, int num_reads=1, int num_threads=1, Optional[float] beta_min=None, Optional[float] beta_max=None, str updater="OPT_METROPOLIS", str random_number_engine="XORSHIFT", Optional[int] seed=None, str temperature_schedule="GEOMETRIC", bool log_history=False)
Public Member Functions inherited from openjij.sampler.sampler.BaseSampler
 sample_ising (self, h, J, **parameters)
 sample_qubo (self, Q, **parameters)

Public Attributes

dict schedule_info
 index_list = sorted(index_set, key=lambda x: (isinstance(x, str), x))
 num_variables = len(self.index_list)
dict index_map = {index: i for i, index in enumerate(self.index_list)}
Public Attributes inherited from openjij.sampler.sampler.BaseSampler
 sample_ising
 sample_qubo

Protected Member Functions

 _convert_validation_schedule (self, schedule)
"openjij.sampler.response.Response" _sample_hubo_old (self, Union[dict, "openj.model.model.BinaryPolynomialModel", cimod.BinaryPolynomialModel] J, Optional[str] vartype=None, Optional[float] beta_min=None, Optional[float] beta_max=None, Optional[int] num_sweeps=None, Optional[int] num_reads=None, Optional[list] schedule=None, Optional[Union[list, dict]] initial_state=None, Optional[str] updater=None, Optional[bool] reinitialize_state=None, Optional[int] seed=None)
"oj.sampler.response.Response" _base_integer_sampler (self, dict[tuple, float] J, dict[Any, tuple[int, int]] bound_list, bool include_higher_order, int num_sweeps=1000, int num_reads=1, int num_threads=1, Optional[float] beta_min=None, Optional[float] beta_max=None, str updater="OPT_METROPOLIS", str random_number_engine="XORSHIFT", Optional[int] seed=None, str temperature_schedule="GEOMETRIC", bool log_history=False)
Protected Member Functions inherited from openjij.sampler.sampler.BaseSampler
 _set_params (self, **kwargs)
 _sampling (self, **kwargs)
 _cxxjij_sampling (self, model, init_generator, algorithm, system, reinitialize_state=None, seed=None, offset=None, *, read_seeds=None)
 _get_result (self, system, model)

Protected Attributes

dict _params = self._default_params.copy()
dict _make_system
dict _algorithm
Protected Attributes inherited from openjij.sampler.sampler.BaseSampler
 _default_params

Additional Inherited Members

Static Public Attributes inherited from openjij.sampler.sampler.BaseSampler
 properties = dict()

Detailed Description

Sampler with Simulated Annealing (SA).

Args:
    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.

Constructor & Destructor Documentation

◆ __init__()

openjij.sampler.sa_sampler.SASampler.__init__ ( self)

Member Function Documentation

◆ _base_integer_sampler()

"oj.sampler.response.Response" openjij.sampler.sa_sampler.SASampler._base_integer_sampler ( self,
dict[tuple, float] J,
dict[Any, tuple[int, int]] bound_list,
bool include_higher_order,
int num_sweeps = 1000,
int num_reads = 1,
int num_threads = 1,
Optional[float] beta_min = None,
Optional[float] beta_max = None,
str updater = "OPT_METROPOLIS",
str random_number_engine = "XORSHIFT",
Optional[int] seed = None,
str temperature_schedule = "GEOMETRIC",
bool log_history = False )
protected

Referenced by sample_huio(), and sample_quio().

Here is the caller graph for this function:

◆ _convert_validation_schedule()

openjij.sampler.sa_sampler.SASampler._convert_validation_schedule ( self,
schedule )
protected
Checks if the schedule is valid and returns cxxjij schedule.

Referenced by openjij.sampler.sqa_sampler.SQASampler._annealing_schedule_setting(), and openjij.sampler.sqa_sampler.SQASampler.sample().

Here is the caller graph for this function:

◆ _sample_hubo_old()

"openjij.sampler.response.Response" openjij.sampler.sa_sampler.SASampler._sample_hubo_old ( self,
Union[ dict, "openj.model.model.BinaryPolynomialModel", cimod.BinaryPolynomialModel ] J,
Optional[str] vartype = None,
Optional[float] beta_min = None,
Optional[float] beta_max = None,
Optional[int] num_sweeps = None,
Optional[int] num_reads = None,
Optional[list] schedule = None,
Optional[Union[list, dict]] initial_state = None,
Optional[str] updater = None,
Optional[bool] reinitialize_state = None,
Optional[int] seed = None )
protected
Sampling from higher order unconstrainted binary optimization.

Args:
    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.

Returns:
    :class:`openjij.sampler.response.Response`: results

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")

References openjij.sampler.sampler.BaseSampler._cxxjij_sampling(), openjij.sampler.csqa_sampler.CSQASampler._params, _params, openjij.sampler.sampler.BaseSampler._set_params(), openjij.sampler.sa_sampler.geometric_hubo_beta_schedule(), and schedule_info.

Referenced by sample_hubo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parameters()

openjij.sampler.sa_sampler.SASampler.parameters ( self)

◆ sample()

"oj.sampler.response.Response" openjij.sampler.sa_sampler.SASampler.sample ( self,
Union[ "openj.model.model.BinaryQuadraticModel", dimod.BinaryQuadraticModel ] bqm,
Optional[float] beta_min = None,
Optional[float] beta_max = None,
Optional[int] num_sweeps = None,
Optional[int] num_reads = None,
Optional[list] schedule = None,
Optional[Union[list, dict]] initial_state = None,
Optional[str] updater = None,
Optional[bool] sparse = None,
Optional[bool] reinitialize_state = None,
Optional[int] seed = None )
Sample Ising model.

Automatic temperatures for single-spin updates use coefficient
quantiles and can include a colder tail. The reported ``beta_max``
is the final inverse temperature, including the tail when present.

Args:
    bqm (openjij.model.model.BinaryQuadraticModel) binary quadratic model
    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 reproducible batches with a distinct random seed per read
Returns:
    :class:`openjij.sampler.response.Response`: results

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)

Reimplemented from openjij.sampler.sampler.BaseSampler.

References openjij.sampler.csqa_sampler.CSQASampler._params, _params, openjij.sampler.sampler.BaseSampler._set_params(), and openjij.sampler.sa_sampler.geometric_ising_beta_schedule().

Referenced by openjij.sampler.sampler.BaseSampler.sample_ising(), and openjij.sampler.sampler.BaseSampler.sample_qubo().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sample_hubo()

openjij.sampler.sa_sampler.SASampler.sample_hubo ( self,
dict[tuple, float] J,
Optional[str] vartype = None,
int num_sweeps = 1000,
int num_reads = 1,
int num_threads = 1,
Optional[float] beta_min = None,
Optional[float] beta_max = None,
str updater = "METROPOLIS",
str random_number_engine = "XORSHIFT",
Optional[int] seed = None,
str temperature_schedule = "GEOMETRIC" )
Sampling from higher order unconstrained binary optimization.

Args:
    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:
    :class:`openjij.sampler.response.Response`: results

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 = openjij.SASampler()
        >>> J = {(0,): -1, (0, 1): -1, (0, 1, 2): 1}
        >>> response = sampler.sample_hubo(J, "BINARY")

References _sample_hubo_old().

Here is the call graph for this function:

◆ sample_huio()

"oj.sampler.response.Response" openjij.sampler.sa_sampler.SASampler.sample_huio ( self,
dict[tuple, float] J,
dict[Any, tuple[int, int]] bound_list,
int num_sweeps = 1000,
int num_reads = 1,
int num_threads = 1,
Optional[float] beta_min = None,
Optional[float] beta_max = None,
str updater = "OPT_METROPOLIS",
str random_number_engine = "XORSHIFT",
Optional[int] seed = None,
str temperature_schedule = "GEOMETRIC",
bool log_history = False )
Sampling from higher-order unconstrained integer optimization (HUIO).
This method solves integer optimization problems that can include variable interactions of any order (linear, quadratic, cubic, and higher).

Args:
    J (dict): Interactions. Keys are tuples of variable indices, values are interaction coefficients.
    bound_list (dict): Variable bounds. Keys are variable indices, values are tuples of (lower_bound, upper_bound) for integer variables.
    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", "OPT_METROPOLIS", "HEAT_BATH", and "SUWA_TODO". Defaults to "OPT_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".
    log_history (bool, optional): If True, logs the energy and temperature history. Defaults to False.

Returns:
    :class:`openjij.sampler.response.Response`: results

Examples::
    To solve f(x) = -x_0 - x_0*x_1 + x_0*x_1*x_2 with bounds:
    x_0 in [-10, 10], x_1 in [3, 10], x_2 in [-2, -1], use the following code:
    >>> import openjij
    >>> sampler = openjij.SASampler()
    >>> J = {(0,): -1, (0, 1): -1, (0, 1, 2): 1}
    >>> bound_list = {0: (-10, 10), 1: (3, 10), 2: (-2, -1)}
    >>> response = sampler.sample_huio(J, bound_list)

References _base_integer_sampler().

Here is the call graph for this function:

◆ sample_quio()

"oj.sampler.response.Response" openjij.sampler.sa_sampler.SASampler.sample_quio ( self,
dict[tuple, float] J,
dict[Any, tuple[int, int]] bound_list,
int num_sweeps = 1000,
int num_reads = 1,
int num_threads = 1,
Optional[float] beta_min = None,
Optional[float] beta_max = None,
str updater = "OPT_METROPOLIS",
str random_number_engine = "XORSHIFT",
Optional[int] seed = None,
str temperature_schedule = "GEOMETRIC",
bool log_history = False )
Sampling from quadratic unconstrained integer optimization (QUIO).
This method solves integer optimization problems with interactions up to quadratic order (linear and quadratic terms only).

Args:
    J (dict): Interactions. Keys are tuples of variable indices, values are interaction coefficients.
    bound_list (dict): Variable bounds. Keys are variable indices, values are tuples of (lower_bound, upper_bound) for integer variables.
    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", "OPT_METROPOLIS", "HEAT_BATH", and "SUWA_TODO". Defaults to "OPT_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".
    log_history (bool, optional): If True, logs the energy and temperature history. Defaults to False.

Returns:
    :class:`openjij.sampler.response.Response`: results

Examples::
    To solve f(x) = -x_0 - x_0*x_1 + x_1*x_2 with bounds:
    x_0 in [-10, 10], x_1 in [3, 10], use the following code:
    >>> import openjij
    >>> sampler = openjij.SASampler()
    >>> J = {(0,): -1, (0, 1): -1, (1, 2): 1}
    >>> bound_list = {0: (-10, 10), 1: (3, 10), 2: (-2, -1)}
    >>> response = sampler.sample_quio(J, bound_list)

References _base_integer_sampler().

Here is the call graph for this function:

Member Data Documentation

◆ _algorithm

dict openjij.sampler.sa_sampler.SASampler._algorithm
protected
Initial value:
= {
"singlespinflip": cxxjij.algorithm.Algorithm_SingleSpinFlip_run,
"singlespinflippolynomial": cxxjij.algorithm.Algorithm_SingleSpinFlip_run,
"swendsenwang": cxxjij.algorithm.Algorithm_SwendsenWang_run,
}

Referenced by openjij.sampler.sqa_sampler.SQASampler.sample().

◆ _make_system

dict openjij.sampler.sa_sampler.SASampler._make_system
protected
Initial value:
= {
"singlespinflip": cxxjij.system.make_classical_ising,
"singlespinflippolynomial": cxxjij.system.make_classical_ising_polynomial,
"swendsenwang": cxxjij.system.make_classical_ising,
}

Referenced by openjij.sampler.sqa_sampler.SQASampler.sample().

◆ _params

◆ index_list

openjij.sampler.sa_sampler.SASampler.index_list = sorted(index_set, key=lambda x: (isinstance(x, str), x))

◆ index_map

dict openjij.sampler.sa_sampler.SASampler.index_map = {index: i for i, index in enumerate(self.index_list)}

◆ num_variables

openjij.sampler.sa_sampler.SASampler.num_variables = len(self.index_list)

◆ schedule_info

dict openjij.sampler.sa_sampler.SASampler.schedule_info
Initial value:
= {
"beta_max": beta_range[0],
"beta_min": beta_range[1],
"num_sweeps": self._params["num_sweeps"],
}

Referenced by openjij.sampler.sqa_sampler.SQASampler._annealing_schedule_setting(), _sample_hubo_old(), and openjij.sampler.csqa_sampler.CSQASampler.sample_ising().


The documentation for this class was generated from the following file: