Skip to main content

JijZeptLab Sampler Tutorial

In this tutorial, we will learn how to use JijZeptLab sampler. The sampler includes:

  • SA sampler
    • Supports Quadratic Unconstrained Binary Optimization

Solve the QUBO problem

Let us solve the following QUBO problem with SA sampler.

Problem:QUBOmini=0N1j=0N1ai,jxi,js.t.onehot-col1=0N1xi,1=1i{0,,N1}onehot-row0=0N1x0,j=1j{0,,N1}wherex2-dim binary variable\begin{array}{cccc}\text{Problem:} & \text{QUBO} & & \\& & \min \quad \displaystyle \sum_{i = 0}^{N - 1} \sum_{j = 0}^{N - 1} a_{i, j} \cdot x_{i, j} & \\\text{{s.t.}} & & & \\ & \text{onehot-col} & \displaystyle \sum_{\ast_{1} = 0}^{N - 1} x_{i, \ast_{1}} = 1 & \forall i \in \left\{0,\ldots,N - 1\right\} \\ & \text{onehot-row} & \displaystyle \sum_{\ast_{0} = 0}^{N - 1} x_{\ast_{0}, j} = 1 & \forall j \in \left\{0,\ldots,N - 1\right\} \\\text{{where}} & & & \\& x & 2\text{-dim binary variable}\\\end{array}

import jijmodeling as jm

# set problem
problem = jm.Problem('QUBO')

# define variables
N = jm.Placeholder('N')
a = jm.Placeholder('a', ndim=2)
x = jm.BinaryVar('x', shape=(N,N))
i = jm.Element('i', belong_to=(0, N))
j = jm.Element('j', belong_to=(0, N))


# Objective
problem += jm.sum([i, j], a[i, j]*x[i, j])

# Constriants
problem += jm.Constraint('onehot-row', x[:, j].sum() == 1, forall=j)
problem += jm.Constraint('onehot-col', x[i, :].sum() == 1, forall=i)


# instance data
instance_data = {
"N": 3,
"a": [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
}

Compile the problem

To solve the problem with SA sampler, we need to compile the problem to generate intermediate representation.

import jijzeptlab as jzl

compiled_model = jzl.compile_model(problem, instance_data)

Solve the problem

  1. Create SA model by using sa.create_model
  2. Solve the model by using sa.sample
  3. Convert the result to jm.SampleSet
import jijzeptlab.sampler.sa as sa

sa_model = sa.create_model(compiled_model)
sa_result = sa.sample(sa_model)
print(sa_result.to_sample_set())
SampleSet(record=Record(solution={'x': [(([0, 1, 2], [2, 1, 0]), [1.0, 1.0, 1.0], (3, 3))]}, num_occurrences=[1]), evaluation=Evaluation(energy=[-27.0], objective=[15.0], constraint_violations={"onehot-col": [0.0], "onehot-row": [0.0]}, penalty={}), measuring_time=MeasuringTime(solve=SolvingTime(preprocess=None, solve=None, postprocess=None), system=SystemTime(post_problem_and_instance_data=None, request_queue=None, fetch_problem_and_instance_data=None, fetch_result=None, deserialize_solution=None), total=None), metadata={'system': [], 'sampling_time': 1417.6669938024133, 'execution_time': 1284.2910073231906, 'list_exec_times': array([1284.29100732]), 'schedule': {'beta_max': 1.3157629102823118, 'beta_min': 0.027182242374899815, 'num_sweeps': 1000}})

SASamplerOption

SASamplerOption allows users to specify the following options:

import jijzeptlab.sampler.sa as sa

option = sa.SASamplerOption(
beta_min = 0.1, #set a minimum (initial) inverse temperature
beta_max = 10, #set a maximum (final) inverse temperature
num_sweeps = 2000, #set a number of Monte-Carlo steps
num_reads = 5, #set a number of samples
initial_state = None, #set a an initial state(dict)
updater= "swendsen wang", #set an updater algorithm. Available options are "single spin flip" and "swendsen wang"
sparse = True, #set whether only non-zero matrix elements are stored, which will save memory.
reinitialize_state = False, #set whether to reinitialize state for each run
seed = None, #set a seed for Monte Carlo algorithm
)
#specify other options when compling the model
compiled_model.set_default_parameters(multipliers ={"onehot-row": 10, "onehot-col": 10},needs_square_dict={"onehot-rol": False,"onehot-row": False})
sa_model = sa.create_model(compiled_model)
sa_result = sa.sample(sa_model,option=option)
print(sa_result.to_sample_set())