JijZeptLab Server-Side Script
In the Getting Started chapter we have learned how to submit a request using function .submit_func(...)
or .submit_file(...)
by specifying the script that works only on server-side as an argument.
An example of the usage of .submit_func(...)
is:
def script_on_server_side():
import jijzeptlab as jzl
# [code server-side script here]
import jijzeptlab as jzl
client = jzl.client.JijZeptLabClient(config="./config.toml")
# define problem and instance_data
input_data = jzl.client.InputData(problem, instance_data)
result = client.submit_func(script_on_server_side, ["final_solution"], input_data)
print(result.variable)
In the case of .submit_file(...)
:
import jijzeptlab as jzl
client = jzl.client.JijZeptLabClient(config="./config.toml")
# define problem and instance_data
input_data = jzl.client.InputData(problem, instance_data)
result = client.submit_file("/path/to/server_side_script.py", ["final_solution"], input_data)
print(result.variable)
In this chapter we will learn how to code the server-side script.
Pre-defined variables
The following variables are pre-defined in the server-side script if you specify jijzeptlab.InputData
object in .submit_func(...)
or .submit_file(...)
:
Variable | Type | Description |
---|---|---|
problem | jm.Problem | JijModeling problem object |
instance_data | dict | Instance data information for JijModeling problem object. The format is the same as JijZept one |
fixed_varibles | dict | Fixed variables information for JijModeling problem object (link) |
Initialize jijzeptlab.CompiledInstance
object
The first step of the server-side script is to initialize jijzeptlab.CompiledInstance
object. jijzeptlab.compile_model
function returns jijzeptlab.CompiledInstance
object from the specified problem
, instance_data
, and (optional) fixed_variables
. The following code is an example of the server-side script.
import jijzeptlab as jzl
import jijmodeling as jm
# pre-defined variables
problem: jm.Problem
instance_data: dict
# Initialize jijzeptlab.Compile object
compiled_model = jzl.compile_model(problem, instance_data)
This function generates jijzeptlab.CompiledInstance
object that is an intermediate representation of the JijModeling object.
Solve with MIP solver
To solve the problem with MIP solver, use the following procedure:
- create MIP model by using
mip.create_model
fromjijzeptlab.CompiledInstance
object - solve the model by using
mip.solve
Below is an example of the server-side script using MIP solver.
import jijzeptlab as jzl
import jijmodeling as jm
# we are going to solve knapsack problem using MIP solver in this tutorial
# of course we can switch to quantum Ising sampler
import jijzeptlab.solver.mip as mip
# problem, instance_data are pre-defined
problem: jm.Problem
instance_data: dict
# compile the problem to generate intermediate representation
compiled_model = jzl.compile_model(problem, instance_data)
# initialize MIP instance
mip_model = mip.create_model(compiled_model)
# solve
mip_result = mip.solve(mip_model)
# convert the solution to `jm.SampleSet`。
final_solution = mip_result.to_sample_set()
You can also use jijzeptlab.solve.nlp
module if you woule like to deal with nonlinear model.
Please refer the Solver document for more details.
Sample with Ising sampler using QUBO form
To sample the problem with Ising sampler, use the following procedure:
- create Ising model by using
jijzeptlab.sampler.*
modules fromjijzeptlab.CompiledInstance
object - sample the model by using
.sample
Below is an example of the server-side script using Ising sampler using D-Wave Leap.
import jijzeptlab as jzl
import jijmodeling as jm
# we are going to solve knapsack problem using MIP solver in this tutorial
# of course we can switch to quantum Ising sampler
import jijzeptlab.solver.mip as mip
# problem, instance_data are pre-defined
problem: jm.Problem
instance_data: dict
# compile the problem to generate intermediate representation
compiled_model = jzl.compile_model(problem,instance_data)
# initialize D-Wave Leap model
leap_model = dwaveleap.create_model(compiled_instance)
# sample
leap_option = dwaveleap.DwaveLeapSamplerOption(time_limit=5)
leap_result = dwaveleap.sample(leap_model, token="...", option=leap_option)
# convert the solution to `jm.SampleSet`。
final_solution = mip_result.to_sample_set()
Please refer the Sampler document for more details.