author | description | ms.author | ms.date | ms.service | ms.subservice | ms.topic | title | uid |
---|---|---|---|---|---|---|---|---|
KittyYeungQ |
Reference for azure.quantum.optimization |
kitty |
02/01/2021 |
azure-quantum |
optimization |
reference |
Azure.Quantum.Optimization |
microsoft.quantum.reference.python-sdk.azure.quantum.optimization |
from azure.quantum.optimization import Job
Retrieves the job result (that is, the computed solution and energy). If the job has not yet finished, blocks until it has.
Returns a boolean value indicating whether the job has finished (for example, the job is in a final state).
job = workspace.get_job(jobId)
job.refresh()
print(job.has_completed())
> False
Refreshes the job's details by querying the Workspace.
job = workspace.get_job(jobId)
job.refresh()
print(job.id)
> 5d2f9cd70f55f149e3ed3aef
Keeps refreshing the job's details until it reaches a final state. For more information on job states, see Azure Quantum Overview.
job = workspace.get_job(jobId)
job.wait_until_completed()
print(job.has_completed())
> True
from azure.quantum.optimization import Problem
To create a Problem
object, you must 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
Adds a single term to the problem. It takes a weight for the term and the indices of variables that appear in the term.
weight = 0.13
problem.add_term(w=5, indices=[2,0])
Adds multiple terms to the problem using a list of Terms
.
problem.add_terms([
Term(w=-9, indices=[0]),
Term(w=-3, indices=[1,0]),
Term(w=5, indices=[2,0]),
Term(w=9, indices=[2,1]),
Term(w=2, indices=[3,0]),
Term(w=-4, indices=[3,1]),
Term(w=4, indices=[3,2])
])
Serializes a problem to a json string.
import json
# Create a problem definition
problem = Problem("My Problem", [Term(w=1, indices=[0,1])])
# Save problem definition to a .json file
with open('myfile.json', 'w', encoding ='utf8') as json_file:
json_file.write(problem.serialize())
# Open json file
with open('myfile.json', 'r', encoding ='utf8') as json_file:
problem = json.load(json_file)
print("Version: " + problem["cost_function"]["version"])
print("Type: " + problem["cost_function"]["type"])
print("Terms: " + str(problem["cost_function"]["terms"]))
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.
from azure.quantum.optimization import ProblemType
The ProblemType
enum allows you to specify the type of optimization problem
you would like to solve.
A polynomial unconstrained binary optimization problem (PUBO) is a problem of the form:
Where H is the cost function, also known as the Hamiltonian. It is called k-local if the maximum degree of the polynomial is k.
An Ising Model is a cost function of the form:
It is called k-local if the maximum degree of the polynomial is k.