Migrating from Previous Versions¶
The Amplify SDK v1 is not fully compatible with v0. If you use an older version, this section explains how to maintain your code so that it continues to work.
Continue using the Amplify SDK v0¶
To keep your current code completely unchanged, you can continue to use the Amplify SDK v0.
$ python3 -m pip install -U 'amplify<1.0.0'
To include additional packages, use the following.
$ python3 -m pip install -U 'amplify[extra]<1.0.0'
Documentation for the Amplify SDK v0 is available at the following URL.
https://amplify.fixstars.com/en/docs/amplify/v0/
Important
The Amplify SDK v0 is not guaranteed to work continuously. Other than critical bug fixes, there will be no feature additions or updates to the supported solvers.
It may also stop working due to changes in the environment. Examples are the release of a new version of Python or changes in solver specifications.
Migrating to the Amplify SDK v1¶
You can use the following guidelines to migrate code written in v0 to v1
Creating decision variables¶
The Amplify SDK v1 replaces BinarySymbolGenerator and IsingSymbolGenerator with VariableGenerator. It also adds integer and real variables. The type of variable is specified by the array(), scalar(), and matrix() method arguments.
Previous writing style:
from amplify import BinarySymbolGenerator, IsingSymbolGenerator
gen = BinarySymbolGenerator()
q = gen.array(10)
gen = IsingSymbolGenerator()
s = gen.array(shape=(4, 4))
New writing style:
from amplify import VariableGenerator
gen = VariableGenerator()
q = gen.array("Binary", 10)
s = gen.array("Ising", shape=(4, 4))
n = gen.array("Integer", shape=(3, 3), bounds=(0, 10))
x = gen.array("Real", shape=(2, 4), bounds=(-0.5, 0.5))
Creating the objective function¶
Once you create the variables described above, you can construct a polynomial as in previous versions. However, dictionary construction is deprecated and will not be available.
Matrix-style objective functions are now constructed using the matrix() method instead of directly creating a matrix class. Also, the Matrix class now includes quadratic, linear, and constant terms of the coefficients and the variables they contain. See “Objective Function with a Coefficient Matrix” for more information.
Previous writing style (currently not working):
from amplify import BinaryMatrix
m = BinaryMatrix(3)
m[0, 0] = -2
m[0, 1] = 1
m[1, 2] = -1
m[2, 2] = 1
New writing style:
from amplify import VariableGenerator
gen = VariableGenerator()
m = gen.matrix("Binary", shape=3)
# Obtain quadratic terms
m.quadratic[0, 0] = -2
m.quadratic[0, 1] = 1
m.quadratic[1, 2] = -1
m.quadratic[2, 2] = 1
# Obtain variables corresponding to the coefficient matrix
q = m.variable_array
Creating constraints¶
We moved the constraint creation functions and classes from the amplify.constraint submodule to the amplify module. In addition, we removed the penalty function, and the users can now define their own penalty functions by constructing Constraint classes. See specifying penalty functions for details. All other helper functions are only available by changing modules.
Previous writing style:
from amplify.constraint import equal_to, penalty
c = equal_to(q.sum(), 1)
p = penalty(q[0] * q[1])
New writing style:
from amplify import Constraint, equal_to
c = equal_to(q.sum(), 1)
p = Constraint(q[0] + q[1], le=1, penalty=q[0] * q[1])
Model creation¶
As before, you can create a model in two ways in the Amplify SDK v1. Construct the model class Model explicitly, or sum the objective function and constraints.
In previous versions, BinaryQuadraticModel and IsingQuadraticModel were the classes that represented the model. They were also responsible for transforming the model into a second-order model based on the type of variables. In Amplify SDK v1, however, the models are no longer bound to variable types and orders. They are compatible with solvers that handle different variable types and orders. Therefore, the Amplify SDK no longer transforms the model when the model is constructed. Instead, the solve() function performs the optimal transformation for the solver. You pass the client for the solver to the solve() function.
Previous writing style:
from amplify import BinaryQuadraticModel
model = BinaryQuadraticModel(q.sum() ** 2, c)
New writing style:
from amplify import Model
model = Model(q.sum() ** 2, c)
Previously, the users could obtain the result of the conversion to a quadratic model from the BinaryQuadraticModel and IsingQuadraticModel attributes. In Amplify SDK v1, you can obtain them from the attribute of the Result class. This class is the return value of the solve() function. You can also explicitly call a model conversion method to check it. See “Variable Conversion and Degree Reduction” for details.
Executing the solver¶
We moved the solver client from the amplify.client submodule to the amplify module. The Solver class is also removed, and the solver is now executed by passing the model and solver client to the solve() function. You can pass all model transformation and graph embedding parameters to the solve() function. You pass them as parameters rather than as attributes of the model or solver class.
FixstarsClient is still available in Amplify SDK v1, but it is recommended to use AmplifyAEClient, which supports the new version of the Amplify AE solver. For details, see “Fixstars Amplify”.
Previous writing style:
from amplify import Solver, QuadratizationMethod
from amplify.client import FixstarsClient
client = FixstarsClient()
# client.token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client.parameters.timeout = 1000
model = BinaryQuadraticModel(q.sum() **2, c, method=QuadratizationMethod.SUBSTITUTION)
solver = Solver(client)
solver.filter_solution = False
result = solver.solve(model)
New writing style:
from amplify import AmplifyAEClient, solve
client = AmplifyAEClient()
# client.token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client.parameters.time_limit_ms = 1000
model = Model(q.sum() **2, c)
result = solve(model, client, quadratization_method="Substitute", filter_solution=False)
Also, the Amplify SDK sets the solution of intermediate models (old logical models) and the result of graph embedding to attributes of the Result class. This class is the return value of the solve() function. See “Variable Conversion and Degree Reduction” and “Graph Embedding” for details.
Using the backward-compatible API¶
We provide the legacy API as a backward-compatible API for v1. It lets code written in v0 work with Amplify SDK v1. The backward-compatible API is not guaranteed to be 100% compatible. However, it maintains compatibility with commonly used features and should work without modification in most cases.
Important
We recommend migrating to the v1 API as soon as possible, as the backward-compatible API may be removed in future releases.
When the backward-compatible API is used, this is output as a warning (DeprecationWarning).
We provide the backward-compatible API with the following policy.
Classes¶
Amplify SDK v0 (link to v1 compatible API) |
Backward-compatible API details |
|---|---|
Subclassed as an alias of |
|
Subclassed as an alias of |
|
|
Not provided |
Function as |
|
|
Not provided |
Subclassed as an alias for |
|
|
Not provided |
These are not created unless you explicitly call the constructor. They serve as v0-compatible models that can be used to obtain quadratic intermediate models (old logical models) of the binary and Ising variables, respectively. However, they cannot be passed to the new |
|
|
Not provided |
Provided the same interface as v0 as a wrapper class for the |
Functions¶
Amplify SDK v0 (link to v1 compatible API) |
Backward-compatible API details |
|---|---|
|
Not provided |
Alias for |
|
|
Not provided |
|
Not provided |
|
Not provided |
|
Not provided |
|
Not provided |
Alias for |
|
|
Not provided |
A function version of |
|
|
Not provided |
Namespace¶
Amplify SDK v0 (link to v1 compatible API) |
Backward-compatible API details |
|---|---|
|
Provided for compatibility |
|
Provided for compatibility |