author | description | ms.author | ms.date | ms.service | ms.subservice | ms.topic | title | uid |
---|---|---|---|---|---|---|---|---|
george-moussa |
Reference for azure.quantum.optimization.Problem |
georgenm |
02/01/2021 |
azure-quantum |
optimization |
reference |
Quantum optimization problem |
microsoft.quantum.optimization.problem |
from azure.quantum.optimization import Problem
To create a Problem
object, you specify the following information:
name
: A friendly name for your problem. No uniqueness constraints.- [optional]
terms
: A list ofTerm
objects to add to the problem. - [optional]
problem_type
: The type of problem. Must be eitherProblemType.ising
orProblemType.pubo
. Default isProblemType.ising
. - [optional]
init_config
: A dictionary of variable ids to value if user wants to specify an initial configuration for the problem.
terms = [
Term(c=-9, indices=[0]),
Term(c=-3, indices=[1,0]),
Term(c=5, indices=[2,0])
]
problem = Problem(name="My Difficult Problem", terms=terms)
# with initial configuration set
config = {'0': 1, '1': 1, '2': 0}
problem2 = Problem(name="Problem with Initial Configuration", terms=terms, init_config=config)
Adds a single term to the problem. It takes a coefficient for the term and the indices of variables that appear in the term.
coefficient = 0.13
problem.add_term(c=coefficient, indices=[2,0])
Adds multiple terms to the problem using a list of Terms
.
problem.add_terms([
Term(c=-9, indices=[0]),
Term(c=-3, indices=[1,0]),
Term(c=5, indices=[2,0]),
Term(c=9, indices=[2,1]),
Term(c=2, indices=[3,0]),
Term(c=-4, indices=[3,1]),
Term(c=4, indices=[3,2])
])
Serializes a problem to a json string.
problem = Problem("My Problem", [Term(c=1, indices=[0,1])])
problem.serialize()
> {"cost_function": {"version": "1.0", "type": "ising", "terms": [{"c": 1, "ids": [0, 1]}]}}
Problem data can be explicitly uploaded to an Azure storage account using its
upload
method that receives as a parameter the Workspace
instance:
problem.upload(workspace=workspace)
Once a problem is explicitly uploaded, it will not be automatically uploaded during submission unless its terms change.
Once a problem has been defined, the user can evaluate the problem on any configuration they supply. The configuration should be supplied as a dictionary of variable ids to values.
problem = Problem("My Problem", [Term(c=1, indices=[0,1])])
problem.evaluate({0:1, 1:1})
> 1
problem.evaluate({0:1, 1:0})
> 0
During experimentation, the user may want to set a variable (or a group of variables) to a particular value. Calling set_fixed_variables will return a new Problem object representing the modified problem after such variables have been fixed.
fixed_var = {'1': 1, '2': 1}
problem = Problem("My Problem", [Term(c=1, indices=[0,1]), Term(c=11, indices=[1,2]), Term(c=5, indices=[])])
new_problem = problem.set_fixed_variables(fixed_var)
new_problem.terms
> [{'c': 1, 'ids': [0]}, {'c': 16, 'ids': []}]
To piece back the fixed variables with the solution on the reduced problem:
result = solver.optimize(new_problem)
result_config = json.loads(result)['configuration']
result_config.update(fixed_var) # join the fixed variables with the result
The user can get the terms in which a variable exists using this function.
problem = Problem("My problem" , [Term(c=1, indices=[0,1]), Term(c=11, indices=[1,2]), Term(c=5, indices=[1])])
terms = problem.get_terms(id = 1)
terms
> [{'c': 11, 'ids': [1,2]}, {'c': 5, 'ids': [1]}]
The StreamingProblem class can handle large problems that exceeds local memory limits. Unlike with the Problem class, terms in the StreamingProblem are uploaded directly to blob and are not kept in memory.
The StreamingProblem class uses the same interface as the Problem class.
There are some features that are not yet supported on the StreamingProblem class due to its streaming nature:
- Problem.set_fixed_variables()
- Problem.evaluate()
The OnlineProblem class creates a problem from the url of the blob storage where an optimization problem has been uploaded. It is essentially used to reuse already submitted problems.
It does not support client side analysis, e.g. the evaluate
and set_fixed_variables
functions. It allows you to download the problem from the blob storage as an instance of the Problem class to do any of the client side operations.
For an example of how to use the OnlineProblem class, have a look at reusing problem definitions.