openjij.sampler package

Submodules

openjij.sampler.csqa_sampler module

class openjij.sampler.csqa_sampler.CSQASampler(beta=5.0, gamma=1.0, num_sweeps=1000, schedule=None, num_reads=1)[source]

Bases: openjij.sampler.sqa_sampler.SQASampler

Sampler with continuous-time simulated quantum annealing (CSQA) using Hamiltonian

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

where \(H_p\) is the problem Hamiltonian we want to solve.

Parameters
  • beta (float) – Inverse temperature.

  • gamma (float) – Amplitude of quantum fluctuation.

  • schedule (list) – schedule list

  • step_num (int) – Number of Monte Carlo step.

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

  • num_reads (int) – Number of iterations.

  • num_sweeps (int) – number of sweeps

  • num_reads – Number of iterations.

  • schedule_info – Information about a annealing schedule.

sample_ising(h, J, beta=None, gamma=None, num_sweeps=None, schedule=None, num_reads=None, initial_state=None, updater='swendsenwang', reinitialize_state=True, seed=None)[source]

Sampling from the Ising model.

Parameters
  • h (dict) – linear biases

  • J (dict) – quadratic biases

  • beta (float, optional) – inverse temperature

  • gamma (float, optional) – strength of transverse field

  • num_sweeps (int, optional) – number of sampling.

  • schedule (list, optional) – schedule list

  • num_reads (int, optional) – number of iterations

  • initial_state (optional) – initial state of spins

  • updater (str, optional) – updater algorithm

  • reinitialize_state (bool, optional) – Re-initilization at each sampling. Defaults to True.

  • seed (int, optional) – Sampling seed.

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

openjij.sampler.response module

OpenJij response class.
The usage is almost the same as dimod.SampleSet.
class openjij.sampler.response.Response(record, variables, info, vartype)[source]

Bases: dimod.sampleset.SampleSet

property energies
property indices
property min_samples
property states

openjij.sampler.sa_sampler module

class openjij.sampler.sa_sampler.SASampler(beta_min=None, beta_max=None, num_sweeps=1000, schedule=None, num_reads=1)[source]

Bases: openjij.sampler.sampler.BaseSampler

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

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

sample(bqm, beta_min=None, beta_max=None, num_sweeps=None, num_reads=None, schedule=None, initial_state=None, updater='single spin flip', sparse=False, reinitialize_state=True, seed=None)[source]

sample Ising model.

Parameters
  • bqm (oj.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

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 = oj.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 = oj.SASampler()
>>> res = sampler.sample_qubo(Q)
sample_hubo(J, vartype, beta_min=None, beta_max=None, num_sweeps=None, num_reads=None, schedule=None, initial_state=None, updater=None, reinitialize_state=True, seed=None)[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.

Returns

results

Return type

openjij.sampler.response.Response

Examples::
for Ising case::
>>> sampler = oj.SASampler()
>>> J = {(0,): -1, (0, 1): -1, (0, 1, 2): 1}
>>> response = sampler.sample_hubo(J, "SPIN")
for Binary case::
>>> sampler = oj.SASampler()
>>> J = {(0,): -1, (0, 1): -1, (0, 1, 2): 1}
>>> response = sampler.sample_hubo(J, "BINARY")
openjij.sampler.sa_sampler.geometric_hubo_beta_schedule(sa_system: cxxjij.system.PyCapsule.make_classical_ising_polynomial, beta_max=None, beta_min=None, num_sweeps=1000)[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.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]

openjij.sampler.sampler module

This module defines the abstract sampler (BaseSampler).

class openjij.sampler.sampler.BaseSampler[source]

Bases: dimod.core.sampler.Sampler

Base sampler class of python wrapper for cxxjij simulator

parameters = {}
properties = {}
sample(bqm, **parameters)[source]

Sample from a binary quadratic model. :param bqm: Binary Qudratic Model :type bqm: openjij.BinaryQuadraticModel :param **parameters: See the implemented sampling for additional keyword definitions.

Returns

results

Return type

openjij.sampler.response.Response

sample_ising(h, J, **parameters)[source]

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)[source]

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.sampler.measure_time(func)[source]

Decorator for measuring calculation time.

Parameters

func – decorator function

openjij.sampler.sqa_sampler module

class openjij.sampler.sqa_sampler.SQASampler(beta=5.0, gamma=1.0, num_sweeps=1000, schedule=None, trotter=4, num_reads=1)[source]

Bases: openjij.sampler.sampler.BaseSampler

Sampler with Simulated Quantum Annealing (SQA).

Inherits from openjij.sampler.sampler.BaseSampler. Hamiltonian

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

where \(H_p\) is the problem Hamiltonian we want to solve.

Parameters
  • beta (float) – Inverse temperature.

  • gamma (float) – Amplitude of quantum fluctuation.

  • 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'.

property parameters

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

sample(bqm, beta=None, gamma=None, num_sweeps=None, schedule=None, trotter=None, num_reads=None, initial_state=None, updater='single spin flip', sparse=False, reinitialize_state=True, seed=None)[source]

Sampling from the Ising model

Parameters
  • bqm (oj.BinaryQuadraticModel) –

  • beta (float, optional) – inverse tempareture.

  • gamma (float, optional) – strangth of transverse field. Defaults to None.

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

  • schedule (list[list[float, int]], optional) – List of annealing parameter. Defaults to None.

  • trotter (int) – Trotter number.

  • 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. Defaults to ‘single spin flip’.

  • reinitialize_state (bool, optional) – Re-initilization at each sampling. Defaults to True.

  • seed (int, optional) – Sampling seed. Defaults to None.

Raises

ValueError

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 = oj.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 = oj.SQASampler()
>>> res = sampler.sample_qubo(Q)
openjij.sampler.sqa_sampler.linear_ising_schedule(model, beta, gamma, num_sweeps)[source]

Generate linear ising schedule.

Parameters
Returns

generated schedule

openjij.sampler.sqa_sampler.quartic_ising_schedule(model, beta, gamma, num_sweeps)[source]

Generate quartic ising schedule based on S. Morita and H. Nishimori, Journal of Mathematical Physics 49, 125210 (2008).

Parameters
Returns

generated schedule

Module contents