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

Public Member Functions

 parameters (self)
 __init__ (self)
"openjij.sampler.response.Response" sample (self, Union["openjij.model.model.BinaryQuadraticModel", dimod.BinaryQuadraticModel] bqm, Optional[float] beta=None, Optional[float] gamma=None, Optional[int] num_sweeps=None, Optional[list] schedule=None, Optional[int] trotter=None, Optional[int] num_reads=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, *, Optional[float] beta_min=None, Optional[float] beta_max=None, Optional[float] coupling_min=None, Optional[float] coupling_max=None, Optional[int] num_threads=None)
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 = {"schedule": "custom schedule"}
Public Attributes inherited from openjij.sampler.sampler.BaseSampler
 sample_ising
 sample_qubo

Protected Member Functions

 _convert_validation_schedule (self, schedule, beta)
 _get_result (self, system, model)
 _parallel_sampling (self, model, ising_graph, init_generator, make_system, algorithm, gamma, reinitialize_state, read_seeds, num_threads)
 _annealing_schedule_setting (self, model, beta=None, gamma=None, num_sweeps=None, schedule=None)
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)

Protected Attributes

 _params = dict(self._default_params)
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 Quantum Annealing (SQA).

Inherits from :class:`openjij.sampler.sampler.BaseSampler`.
Hamiltonian

.. math::

    H(s) = s H_p + \\Gamma (1-s)\\sum_i \\sigma_i^x

where :math:`H_p` is the problem Hamiltonian we want to solve.
The quantum Monte Carlo simulation uses the Suzuki-Trotter
decomposition with ``trotter`` slices. Every slice feels the problem
Hamiltonian at inverse temperature :math:`s\\beta/M` and neighbouring
slices are coupled ferromagnetically with
:math:`J_\\perp = -\\frac12\\ln\\tanh(\\beta\\Gamma(1-s)/M)`.

Unless ``schedule``, ``beta`` or ``gamma`` is given, the annealing
schedule is generated by :func:`geometric_sqa_schedule`: the classical
inverse temperature of every slice follows the automatic schedule of
:class:`openjij.SASampler` (estimated from the coefficients of the
problem), and the transverse field decreases such that
:math:`J_\\perp` grows geometrically. Both ends of :math:`J_\\perp` are
chosen from the energy landscape (:func:`trotter_coupling_range`): weak
for frustrated spin glasses, where nearly independent replicas work
best, and stronger for problems with rigid local structure or tall and
narrow barriers such as global constraints. The transverse field stays
finite at the end of the annealing.

Reads run in parallel on ``num_threads`` threads.

Args:
    beta (float): Inverse temperature (legacy schedule).
    gamma (float): Amplitude of quantum fluctuation (legacy schedule).
    trotter (int): Trotter number.
    num_sweeps (int): number of sweeps
    schedule (list): schedule list
    num_reads (int): Number of iterations.
    schedule_info (dict): Information about a annealing schedule.

Raises:
    ValueError: If the schedule violates as below.
    - not list or numpy.array.
    - schedule range is '0 <= s <= 1'.

Constructor & Destructor Documentation

◆ __init__()

openjij.sampler.sqa_sampler.SQASampler.__init__ ( self)

Member Function Documentation

◆ _annealing_schedule_setting()

openjij.sampler.sqa_sampler.SQASampler._annealing_schedule_setting ( self,
model,
beta = None,
gamma = None,
num_sweeps = None,
schedule = None )
protected

◆ _convert_validation_schedule()

openjij.sampler.sqa_sampler.SQASampler._convert_validation_schedule ( self,
schedule,
beta )
protected

Referenced by _annealing_schedule_setting(), and sample().

Here is the caller graph for this function:

◆ _get_result()

openjij.sampler.sqa_sampler.SQASampler._get_result ( self,
system,
model )
protected

Reimplemented from openjij.sampler.sampler.BaseSampler.

Reimplemented in openjij.sampler.csqa_sampler.CSQASampler.

References _get_result().

Referenced by _get_result().

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

◆ _parallel_sampling()

openjij.sampler.sqa_sampler.SQASampler._parallel_sampling ( self,
model,
ising_graph,
init_generator,
make_system,
algorithm,
gamma,
reinitialize_state,
read_seeds,
num_threads )
protected
Run the reads on concurrent systems and collect the results.

Each read owns a random seed, so the samples do not depend on
``num_threads``. With ``reinitialize_state=False`` every read continues
from the final state of the previous one, so the reads run one by one.

References openjij.sampler.sqa_sampler._concurrent_systems(), openjij.sampler.csqa_sampler.CSQASampler._get_result(), openjij.sampler.sampler.BaseSampler._get_result(), openjij.sampler.csqa_sampler.CSQASampler._params, openjij.sampler.sa_sampler.SASampler._params, and _params.

Here is the call graph for this function:

◆ parameters()

openjij.sampler.sqa_sampler.SQASampler.parameters ( self)

◆ sample()

"openjij.sampler.response.Response" openjij.sampler.sqa_sampler.SQASampler.sample ( self,
Union[ "openjij.model.model.BinaryQuadraticModel", dimod.BinaryQuadraticModel ] bqm,
Optional[float] beta = None,
Optional[float] gamma = None,
Optional[int] num_sweeps = None,
Optional[list] schedule = None,
Optional[int] trotter = None,
Optional[int] num_reads = 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,
* ,
Optional[float] beta_min = None,
Optional[float] beta_max = None,
Optional[float] coupling_min = None,
Optional[float] coupling_max = None,
Optional[int] num_threads = None )
Sampling from the Ising model.

The annealing schedule is chosen as follows.

- ``schedule`` is given: the custom schedule.
- ``beta`` or ``gamma`` is given: the legacy schedule, i.e. fixed
  ``beta`` (default 5) and ``s`` following
  :func:`quartic_ising_schedule` from 0 to 1.
- otherwise: :func:`geometric_sqa_schedule`. ``beta_min`` and
  ``beta_max`` override the estimated range of the classical inverse
  temperature of each Trotter slice, and ``coupling_min`` and
  ``coupling_max`` the range of the Trotter coupling chosen from the
  energy landscape (:func:`trotter_coupling_range`).

Args:
    bqm (openjij.BinaryQuadraticModel) binary quadratic model
    beta (float, optional): inverse temperature of the legacy schedule.
    gamma (float, optional): strength of transverse field. Defaults to 1 for custom and legacy schedules.
    num_sweeps (int, optional): number of sweeps. Defaults to 1000.
    schedule (list[list[float, int]], optional): List of annealing parameter. Defaults to None.
    trotter (int): Trotter number. Defaults to 8.
    num_reads (int, optional): number of sampling. Defaults to 1.
    initial_state (list[int], optional): Initial state. Defaults to None.
    updater (str, optional): update method, "single spin flip" (default)
        or "trotter cluster flip". The latter flips segments of Trotter
        copies at once and keeps the replicas moving when the Trotter
        coupling is strong (e.g. near s = 1 in reverse annealing).
    sparse (bool): use sparse matrix or not.
    reinitialize_state (bool, optional): Re-initilization at each sampling. Defaults to True.
        If False, reads run sequentially, each starting from the previous final state.
    seed (int, optional): seed for reproducible batches with a distinct random seed per read.
    beta_min (float, optional): initial classical inverse temperature per Trotter slice
        of the automatic schedule.
    beta_max (float, optional): final classical inverse temperature per Trotter slice
        of the automatic schedule.
    coupling_min (float, optional): initial Trotter coupling of the automatic schedule.
    coupling_max (float, optional): final Trotter coupling of the automatic schedule.
    num_threads (int, optional): number of threads that run reads in parallel.
        Defaults to ``OMP_NUM_THREADS`` if set, otherwise the number of CPUs.
        Results do not depend on the number of threads.

Raises:
    ValueError:

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 = openjij.SQASampler()
        >>> 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 = openjij.SQASampler()
        >>> res = sampler.sample_qubo(Q)

Reimplemented from openjij.sampler.sampler.BaseSampler.

References openjij.sampler.sa_sampler.SASampler._algorithm, _algorithm, openjij.sampler.sa_sampler.SASampler._convert_validation_schedule(), _convert_validation_schedule(), openjij.sampler.sa_sampler.SASampler._make_system, _make_system, openjij.sampler.csqa_sampler.CSQASampler._params, openjij.sampler.sa_sampler.SASampler._params, _params, openjij.sampler.sqa_sampler._resolve_num_threads(), and openjij.sampler.sampler.BaseSampler._set_params().

Here is the call graph for this function:

Member Data Documentation

◆ _algorithm

dict openjij.sampler.sqa_sampler.SQASampler._algorithm
protected
Initial value:
= {
"singlespinflip": cxxjij.algorithm.Algorithm_SingleSpinFlip_run_parallel,
"trotterclusterflip": cxxjij.algorithm.Algorithm_TrotterClusterFlip_run_parallel,
}

Referenced by sample().

◆ _make_system

dict openjij.sampler.sqa_sampler.SQASampler._make_system
protected
Initial value:
= {
"singlespinflip": cxxjij.system.make_transverse_ising,
"trotterclusterflip": cxxjij.system.make_transverse_ising,
}

Referenced by sample().

◆ _params

openjij.sampler.sqa_sampler.SQASampler._params = dict(self._default_params)
protected

◆ schedule_info

dict openjij.sampler.sqa_sampler.SQASampler.schedule_info = {"schedule": "custom schedule"}

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