openjij.sampler package

Submodules

openjij.sampler.cmos_annealer module

class openjij.sampler.cmos_annealer.CMOSAnnealer(token, machine_type='ASIC', beta_min=None, beta_max=None, num_sweeps=10, step_length=100, num_reads=1, **kwargs)[source]

Bases: openjij.sampler.sampler.BaseSampler, dimod.core.structured.Structured

Sampler with CMOS Annealer.

Inherits from openjij.sampler.sampler.BaseSampler.

To get More details about CMOS Annealer API, please access the reference (https://annealing-cloud.com/web-api/reference.html).

Parameters
  • token (str) – API token of COMS Annealer.

  • machine_type (str) – Type of CMOS Annealer: ‘ASIC’ or ‘FPGA’.

  • beta_min (float) – Minimum beta (inverse temperature).

  • beta_max (float) – Maximum beta (inverse temperature).

  • step_length (int) – Length of Monte Carlo step.

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

  • num_reads (int) – Number of iterations.

  • **kwargs – Optional keyword arguments for CMOS Annealer.

cmos_parameters

Parameters of CMOS Annealer.

Type

dict

Raises

ValueError

  • only valid input: h, J, Q, or king graph. - CMOS Annealer API raises error.

property edgelist

King’s graph

Returns

King’s graph

Return type

[list of tuple]

make_json_request(model, token)[source]

Make request for CMOS Annealer API.

Parameters
  • model (list) – A list of 5 integer values representing vertices or interactions of Ising model

  • token (str) – API token of COMS Annealer.

property nodelist

2-D lattice nodes

Returns

2-D lattice nodes

Return type

[list of int]

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)

property properties

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_ising(h, J, num_reads=1, num_sweeps=None, step_length=None, beta_min=None, beta_max=None)[source]

sample Ising model.

Parameters
  • h (dict) – linear biases

  • J (dict) – quadratic biases

  • num_reads (int) – number of reads

  • num_sweeps (int) – number of sweeps

  • step_length (int) – number of Monte Carlo steop for each step

  • beta_min (float) – minimal value of inverse temperature

  • beta_max (float) – maximum value of inverse temperature

class openjij.sampler.cmos_annealer.NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
openjij.sampler.cmos_annealer.defalut_beta_range(cmos_model, beta_max, beta_min)[source]

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=1, initial_state=None, updater='swendsenwang', reinitialize_state=True, seed=None, **kwargs)[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.

  • structure (int, optional) – specify the structure.

  • argument is necessary if the model has a specific structure (This) –

  • must have two types of keys, namely "size" which shows the total size of spins and "dict" which is the map from model index (structure) –

  • kwargs

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.hubo_simulated_annealing module

openjij.sampler.hubo_simulated_annealing.default_schedule(bhom, beta_min, beta_max, num_sweeps)[source]
openjij.sampler.hubo_simulated_annealing.hubo_sa_sampling(bhom, var_type, schedule, schedule_info, num_sweeps=100, num_reads=1, init_state=None, seed=None)[source]
openjij.sampler.hubo_simulated_annealing.hubo_simulated_annealing(bhom: openjij.model.higher_order_model.BinaryHigherOrderModel, state: list, schedule: list, var_type)[source]
openjij.sampler.hubo_simulated_annealing.measure_time(func)[source]

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_sampels
property states
update_ising_states_energies(states, energies)[source]
update_trotter_ising_states_energies(trotter_states, q_energies)[source]

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_hubo(interactions: list, var_type, beta_min=None, beta_max=None, schedule=None, num_sweeps=100, num_reads=1, init_state=None, seed=None)[source]

sampling from higher order unconstrainted binary optimization.

Parameters
  • interactions (list) – ordered by degree of interaction.

  • var_type (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 100.

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

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

  • seed (int, optional) – [description]. Defaults to None.

Returns

results

Return type

openjij.sampler.response.Response

Examples:

>>> sampler = oj.SASampler()
>>> h = {0: -1}
>>> J = {(0, 1): -1}
>>> K = {(0, 1, 2): 1}
>>> response = sampler.sample_hubo([h, J, K], var_type="SPIN")
sample_ising(h, J, beta_min=None, beta_max=None, num_sweeps=None, num_reads=1, schedule=None, initial_state=None, updater='single spin flip', reinitialize_state=True, seed=None, **kwargs)[source]

sample Ising model.

Parameters
  • h (dict) – linear biases

  • J (dict) – quadratic biases

  • 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

  • kwargs

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)
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 **kwargs: 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) – 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_ising(h, J, beta=None, gamma=None, num_sweeps=None, schedule=None, num_reads=1, initial_state=None, updater='single spin flip', reinitialize_state=True, seed=None, structure=None, **kwargs)[source]

Sampling from the Ising model

Parameters
  • h (dict) – Linear term of the target Ising model.

  • J (dict) – Quadratic term of the target Ising model.

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

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

  • structure (dict) – specify the structure.

  • argument is necessary if the model has a specific structure (This) –

  • must have two types of keys, namely "size" which shows the total size of spins and "dict" which is the map from model index (structure) –

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

Module contents