-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
92 lines (64 loc) · 2.19 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# coding: utf-8
import json
from re import T
from azure.quantum.optimization import Term, Problem, ProblemType
from azure.quantum import Workspace
from azure.quantum.optimization import SimulatedAnnealing # Change this line to match the Azure Quantum Optimization solver type you wish to use
# Copy the settings for your workspace below
with open('appsettings.json') as f:
config = json.load(f)
workspace = Workspace (
subscription_id = config.get("workspace").get("subscription_id"),
resource_group = config.get("workspace").get("resource_group"),
name = config.get("workspace").get("name"),
location = config.get("workspace").get("location")
)
def getProblemData():
with open('problem.json') as f:
problemJson = json.load(f)
problemData = problemJson.get("problem").get("data")
return problemData
# create a cost function based on a problem statement
def createCostFunction(problemData):
print("createCostFunction")
terms = []
terms.append(Term(c = 1, indices = [0]))
return terms
def extractSolution(result):
print("extractSolution")
config = result.get("configuration")
result = {}
return result
def validateSolution(problem, solution):
print("validateSolution")
conflicts = False
return not(conflicts)
def printProblem(problem):
print("printProblem")
print(problem)
def printSolution(solution):
print("printSolution")
print(solution)
# read problem data
problemData = getProblemData()
isValidSolution = False
# create a cost function out of the problem data
terms = createCostFunction(problemData)
# submit the cost function to a solver
problem = Problem(name="problem", problem_type=ProblemType.pubo, terms=terms)
solver = SimulatedAnnealing(workspace, timeout=100, seed=22)
job = solver.submit(problem)
job.refresh()
# get the result
result = job.get_results()
# extract the solution generated by the solver
solution = extractSolution(result)
# validate the solution
isValidSolution = validateSolution(problemData, solution)
if isValidSolution:
print ("Solution is valid.")
else:
print ("Solution is not valid.")
# print the solution
printSolution(solution)