-
Notifications
You must be signed in to change notification settings - Fork 3
/
Problem.py
218 lines (181 loc) · 7.15 KB
/
Problem.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from dimod.binary_quadratic_model import BinaryQuadraticModel
import dimod
import dwave_networkx as dnx
import matplotlib.pyplot as plt
class Problem:
"""Class representing a problem in Ising or QUBO format. This class provides function for setting solving
parameters and specific solvers and functions that belong to problems, e.g. find embeddings for this problem or
solve the problem.
Attributes
----------
solver_params: dict
uq_params: dict
Dictionary that contains the key "num_repeats", which specifies the number of samplings the solver will process
config
Contains the users configuration data
solver
Specifies which solver should be used
platform
Specifies which platform should be used
embedding
Chimera or Pegasus embedding for the problem
connection
Connection object containing the configuration data of the user
Methods
-------
with_solver(solver)
Set the solver attribute to solver
with_platform(platform)
Set the platform attribute to platform
with_params(**kw_params)
Update the dictionary solver_params with the elements from kw_params
with_uq_params(**kw_params)
Update the dictionary uq_params with the elements from kw_params
find_chimera_embedding()
Call the connections find_chimera_embedding method to make a server request for finding a chimera
embedding
draw_chimera_embedding()
Save the chimera embedding to a file.
find_pegasus_embedding()
Call the connections find_chimera_embedding method to make a server request for finding a pegasus
embedding
draw_pegasus_embedding()
Save the pegasus embedding to a file.
solve(times)
Solve a problem by either calling the connections solve_qubo or solve_ising function.
"""
def __init__(self, config):
self.solver_params = {}
self.uq_params = {}
self.config = config
self.solver = None
self.platform = None
self.embedding = None
self.connection = config.create_connection()
# ------------------ Set attributes ------------------ #
def with_solver(self, solver):
"""Set the solver attribute"""
self.solver = solver
return self
def with_platform(self, platform):
"""Set the platform attribute"""
self.platform = platform
return self
def with_params(self, **kw_params):
"""Update the solver_params dictionary with the elements from kw_params"""
self.solver_params.update(kw_params)
return self
def with_uq_params(self, **kw_params):
"""Update the uq_params dictionary with the elements from kw_params"""
self.uq_params.update(kw_params)
return self
# ------------------ find embeddings ------------------ #
def find_chimera_embedding(self):
"""Call the connections find_chimera_embedding method to make a server request for finding a chimera
embedding """
self.embedding = self.connection.find_chimera_embedding(self)
return self.embedding
def draw_chimera_embedding(self, output_path):
"""Save the chimera embedding to a file.
Parameters
----------
output_path
Specifies the path to the file where to save the embedding
"""
dnx.draw_chimera_embedding(dnx.chimera_graph(16, 16, 4), emb=self.embedding, node_size=3, width=.3)
plt.savefig(output_path)
def find_pegasus_embedding(self):
"""Call the connections find_chimera_embedding method to make a server request for finding a pegasus
embedding """
self.embedding = self.connection.find_pegasus_embedding(self)
return self.embedding
def draw_pegasus_embedding(self, output_path):
"""Save the pegasus embedding to a file.
Parameters
----------
output_path
Specifies the path to the file where to save the embedding
"""
dnx.draw_pegasus_embedding(dnx.pegasus_graph(11), emb=self.embedding, node_size=3, width=.3)
plt.savefig(output_path)
# ---------------- find initial state ---------------- #
def find_initial_state(self, times=1):
"""Call the connections find_initial_state method for reverse annealing process. """
self.uq_params.update({"num_repeats": times})
return self.connection.find_initial_state(self)
# ------------------ Solve problems ------------------ #
def solve(self, times=1):
"""Solve a problem by either calling the connections solve_qubo or solve_ising function.
Parameters
----------
times: int
Specifies the count of iterations. If no parameter is passed, the default value is 1.
"""
self.uq_params.update({"num_repeats": times})
if self.solver is not None:
self.connection.set_preferred_solver(self.solver)
if self.platform is not None:
self.connection.set_preferred_platform(self.platform)
if isinstance(self, Qubo):
return self.connection.solve_qubo(self)
if isinstance(self, Ising):
return self.connection.solve_ising(self)
class Qubo(Problem):
"""Represents a problem in QUBO format.
Attributes
----------
problem_dict: dict
QUBO represantation of a problem
Methods
-------
to_json()
Transform a QUBO dictionary into a dimod.BinaryQuadraticModel (BQM) and return the serialized BQM
"""
def __init__(self, config, qubo_dict):
Problem.__init__(self, config)
self.problem_dict = qubo_dict
def to_json(self):
"""Transform a QUBO dictionary into a dimod.BinaryQuadraticModel (BQM) and return the serialized BQM.
Returns
-------
bqm.to_serializable(): dict
The serialized BQM
"""
linear = {}
quadratic = {}
for (a, b) in self.problem_dict.keys():
if a == b:
linear[a] = self.problem_dict[(a, b)]
else:
quadratic[(a, b)] = self.problem_dict[(a, b)]
bqm = BinaryQuadraticModel(linear, quadratic, 0.0, dimod.BINARY)
return bqm.to_serializable()
class Ising(Problem):
"""Represents a problem in Ising format.
Attributes
----------
linear_dict: dict
external magnetic field values
quadratic_dict: dict
interaction values
Methods
-------
to_json()
Transform an Ising representation of a problem into a dimod.BinaryQuadraticModel (BQM) and return the serialized
BQM
"""
def __init__(self, config, linear_dict, quadratic_dict):
Problem.__init__(self, config)
self.linear_dict = linear_dict
self.quadratic_dict = quadratic_dict
def to_json(self):
"""
Transform an Ising representation of a problem into a dimod.BinaryQuadraticModel (BQM) and return the serialized
BQM.
Returns
-------
bqm.to_serializable(): dict
The serialized BQM
"""
bqm = BinaryQuadraticModel(self.linear_dict, self.quadratic_dict, 0.0, dimod.SPIN)
return bqm.to_serializable()