-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_cut_solvers.py
406 lines (298 loc) · 13.1 KB
/
min_cut_solvers.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import numpy as np
import networkx as nx
from qiskit_optimization.applications import Maxcut
from qiskit.algorithms import NumPyMinimumEigensolver
from qiskit_optimization.algorithms import MinimumEigenOptimizer
#!/usr/bin/env python
# coding: utf-8
import warnings
warnings.filterwarnings('ignore')
# Qiskit
from qiskit.algorithms import NumPyMinimumEigensolver
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit_optimization import QuadraticProgram
# Dwaves
import dimod
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite
import numpy as np
import pandas as pd
from sympy import *
import re
import os
from qiskit.algorithms.optimizers import COBYLA
################### ########
#Different distributions data generator functions
def create_dir(path, log=False):
if not os.path.exists(path):
if log:
print('The directory', path, 'does not exist and will be created')
os.makedirs(path)
else:
if log:
print('The directory', path, ' already exists')
#################################### SOLVER
def numpy_for_qubo(qubo, p=None): # Classical solver for QUBO
"""
numpy_for_qubo solves the given QUBO using Numpy library functions
:param
qubo: CSG problem instance reduced to the form of qubo.
return:
result: An array of binary digits which denotes the solutionn of the input qubo problem
"""
exact_mes = NumPyMinimumEigensolver()
exact = MinimumEigenOptimizer(exact_mes) # using the exact classical numpy minimum eigen solver
result = exact.solve(qubo)
return result
def solve_QUBO(linear, quadratic, algo, p=1):
"""
solve_QUBO is a higher order function to solve QUBO using the given algo parameter function
:param
linear: dictionary of linear coefficient terms in the QUBO formulation of the CSG problem.
quadratic: dictionary of quadratic coefficient terms in the QUBO formulation of the CSG problem.
algo: a callback function for qaoa_for_qubo or numpy_for_qubo
return:
result: An array of binary digits which denotes the solutionn of the input qubo problem
"""
keys = list(linear.keys())
keys.sort(key=natural_keys)
qubo = QuadraticProgram()
for key in keys:
qubo.binary_var(key) # initialize the binary variables
qubo.minimize(linear=linear, quadratic=quadratic) # initialize the QUBO maximization problem instance
op, offset = qubo.to_ising()
qp=QuadraticProgram()
qp.from_ising(op, offset, linear=True)
result = algo(qubo, p)
return result
###############################################
def natural_keys(text):
"""
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.ht
For example: Built-in function ['x_8','x_10','x_1'].sort() will sort as ['x_1', 'x_10', 'x_8']
But using natural_keys as callback function for sort() will sort as ['x_1','x_8','x_10']
param:
text: a list of strings ending with numerical characters
return:
sorted list in a human way
"""
return [ atoi(c) for c in re.split(r'(\d+)', text) ]
def atoi(text):
"""
Function returns the corresponding value of a numerical string as integer datatype
param:
text: string conntaining only numerical charcaters
return:
integer value corresponding to the input text
"""
return int(text) if text.isdigit() else text
def exact_solver(linear, quadratic, offset = 0.0):
"""
Solve Ising hamiltonian or qubo problem instance using dimod API.
dimod is a shared API for samplers.It provides:
- classes for quadratic models—such as the binary quadratic model (BQM) class that contains Ising and QUBO models used by samplers such as the D-Wave system—and higher-order (non-quadratic) models.
- reference examples of samplers and composed samplers.
- abstract base classes for constructing new samplers and composed samplers.
:params
linear: dictionary of linear coefficient terms in the QUBO formulation of the CSG problem.
quadratic: dictionary of quadratic coefficient terms in the QUBO formulation of the CSG problem.
offset: Constant energy offset associated with the Binary Quadratic Model.
:return
sample_set: Samples and any other data returned by dimod samplers.
"""
vartype = dimod.BINARY
bqm = dimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)
sampler = dimod.ExactSolver()
sample_set = sampler.sample(bqm)
return sample_set
def dwave_solver(linear, quadratic, offset = 0.0, runs=10000):
"""
Solve Ising hamiltonian or qubo problem instance using dimod API for using dwave system.
:params
linear: dictionary of linear coefficient terms in the QUBO formulation of the CSG problem.
quadratic: dictionary of quadratic coefficient terms in the QUBO formulation of the CSG problem.
runs: Number of repeated executions
:return
sample_set: Samples and any other data returned by dimod samplers.
"""
vartype = dimod.BINARY
bqm = dimod.BinaryQuadraticModel(linear, quadratic, 0.0, vartype)
sampler = EmbeddingComposite(DWaveSampler())
sample_set = sampler.sample(bqm, num_reads=runs)
return sample_set
def extract_best_result(df):
"""
A function to fetch the binary string with least energy of the input hamiltonian operator
:params
df:
:return
x: an array of binary digits specifies the solution of the input problem instance
fval: value of the operator corresponding to the binary digits in x
"""
row_min = df[df.energy == df.energy.min()]
cols = []
for col in df.columns:
if 'x_' in col:
cols.append(col)
x = []
for col in cols:
x.append(row_min.iloc[0][col])
fval = row_min.energy.iloc[0]
return x, fval
def from_bin_to_var(x, dictionary):
"""
function to convert binary string to coalition structure
:params
x: an array of binary digits (specifies the solution of the input problem instance)
dictionary: dictionary with coalitions as keys and coalition values as values (CSG problem instance)
:return
solution: list of lists. coalition structure.
"""
solution = []
for i in range(len(x)):
if x[i] == 1:
print(list(dictionary.keys())[i])
solution.append(list(dictionary.keys())[i])
return solution
def create_QUBO(linear_dict, quadratic_dict):
"""
create a QUBO problem instance using the linear and quadratic coefficients.
:params
linear: dictionary of linear coefficient terms in the QUBO formulation of the CSG problem.
quadratic: dictionary of quadratic coefficient terms in the QUBO formulation of the CSG problem.
:return
Object of QuadraticProgram class corresponding to the input linear and quadratic coefficients.
"""
qubo = QuadraticProgram()
keys = list(linear_dict.keys())
keys.sort(key=natural_keys)
for key in keys:
qubo.binary_var(key)
qubo.minimize(linear=linear_dict, quadratic=quadratic_dict)
return qubo
def from_columns_to_string(df):
cols = []
for col in df.columns:
if 'x_' in col:
cols.append(col)
df['x'] = 'x'
for index, row in df.iterrows():
x = ''
for col in cols:
x = x + str(row[col])
df.loc[index, 'x'] = x
return df[['x', 'num_occurrences', 'energy']]
def get_ordered_solution(dictionary):
"""
Reordering the (key,value) pairs in the dictionary to fetch only the values in order.
param:
dictionary: input dictionary.
return:
solution: list of values after reordering the dictionary elements.
"""
sortedDict = dict(sorted(dictionary.items(), key=lambda x: x[0].lower()))
solution = []
for k, v in sortedDict.items():
solution.append(v)
return solution
def results_from_dwave(sample_set, exact=False):
"""
Fetch the details of the output_ from D-Wave system (Quantum Annealing).
:params
sample_set: Samples and any other data returned by dimod samplers.
:return
solution: a list of binary values corresponding to the solution provided by the output_ of D-Wave device.
fval: The function value (operator value) of the input hamiltonian corresponding to the solution.
prob: Probability of the solution.
rank: rank of the solution out of all possible binary arrays.
time: time taken by the D-Wave device to compute the solution.
"""
df = sample_set.to_pandas_dataframe()
row_min = df[df.energy == df.energy.min()]
cols = []
for col in df.columns:
if 'x_' in col:
cols.append(col)
cols.sort(key=natural_keys)
solution = []
for col in cols:
solution.append(row_min.iloc[0][col])
fval = row_min.energy.iloc[0]
if not exact:
occ_min_fval = row_min.num_occurrences.to_list()[0]
occurences = df.num_occurrences.to_list()
occurences = sorted(occurences, reverse=True)
time = sample_set.info['timing']['qpu_sampling_time']/1000
rank = occurences.index(occ_min_fval)+1
prob = occ_min_fval / sum(df.num_occurrences)
else:
rank = 1
prob = 1
time = 1
return solution, fval, prob, rank, time
def min_cut_brute_force(n_agents, induced_subgraph_game, **kwargs):
#print("Received n_agents, induced_subgraph_game",n_agents, induced_subgraph_game)
G=nx.Graph()
G.add_nodes_from(np.arange(0,n_agents,1))
elist = [tuple((int(x)-1 for x in key.split(',')))+tuple([induced_subgraph_game[key]*-1]) for key in induced_subgraph_game]
G.add_weighted_edges_from(elist)
w = [[G.get_edge_data(i,j,default = {'weight': 0})['weight'] for j in range(n_agents)] for i in range(n_agents)]
x = [0] * n_agents
cost = [[w[i][j]*x[i]*(1-x[j]) for j in range(n_agents)] for i in range(n_agents)]
best_cost_brute = sum(induced_subgraph_game.values())
xbest_cut_brute = x
for b in range(1, 2**(n_agents-1)):
x = [int(t) for t in reversed(list(bin(b)[2:].zfill(n_agents)))]
cost = [[w[i][j]*x[i]*(1-x[j]) for j in range(n_agents)] for i in range(n_agents)]
cost = sum([sum(i) for i in cost])+sum(induced_subgraph_game.values())
if cost > best_cost_brute:
best_cost_brute = cost
xbest_cut_brute = x
return np.array(xbest_cut_brute), (abs(best_cost_brute) + best_cost_brute) / 2
def min_cut_qiskit_classical_eigensolver(n_agents, induced_subgraph_game, **kwargs):
G=nx.Graph()
G.add_nodes_from(np.arange(0,n_agents,1))
elist = [tuple((int(x)-1 for x in key.split(',')))+tuple([induced_subgraph_game[key]*-1]) for key in induced_subgraph_game]
G.add_weighted_edges_from(elist)
w = [[G.get_edge_data(i,j,default = {'weight': 0})['weight'] for j in range(n_agents)] for i in range(n_agents)]
w = np.array([np.array(row) for row in w])
max_cut = Maxcut(w)
qp = max_cut.to_quadratic_program()
#qubitOp, offset = qp.to_ising()
exact = MinimumEigenOptimizer(NumPyMinimumEigensolver())
result = exact.solve(qp)
return result.x, (abs(result.fval)+result.fval)/2
def min_cut_dwave_annealer(n_agents, induced_subgraph_game, save_log=False, name_folder='distribution', n_samples= 2000, n_run=1):
linear, quadratic = get_linear_quadratic_coeffs(n_agents, induced_subgraph_game)
sample = dwave_solver(linear, quadratic, offset = 0.0, runs=n_samples)
# if save_log:
# path = os.path.join('QA_results', name_folder, str(n), 'run_'+str(run))
# create_dir(path)
# try:
# sample.to_pandas_dataframe().to_csv(os.path.join(path, 'solutions.csv'))
# save_json(os.path.join(path, 'log'), sample.info)
# except:
# print("\n *** Warning: results for", name_folder, "with", n_agents, "agents not saved** \n")
dwave_annealer_solution=[]
for key, value in sample.first[0].items():
dwave_annealer_solution.append(value)
dwave_annealer_solution = np.array(dwave_annealer_solution)
dwave_annealer_value = from_columns_to_string(sample.to_pandas_dataframe()).loc[0,'energy']
# print("s: ", n_agents, " - time: ", sample.info['timing']['qpu_sampling_time']/10**6)
#dwave_annealer_tte = sample.info['timing']['qpu_sampling_time']/10**6
return dwave_annealer_solution, dwave_annealer_value
def get_linear_quadratic_coeffs(n_agents, induced_subgraph_game):
G=nx.Graph()
G.add_nodes_from(np.arange(0,n_agents,1))
elist = [tuple((int(x)-1 for x in key.split(',')))+tuple([induced_subgraph_game[key]*-1]) for key in induced_subgraph_game]
G.add_weighted_edges_from(elist)
w = [[G.get_edge_data(i,j,default = {'weight': 0})['weight'] for j in range(n_agents)] for i in range(n_agents)]
w = np.array([np.array(row) for row in w])
max_cut = Maxcut(w)
qp = max_cut.to_quadratic_program()
linear = qp.objective.linear.coefficients.toarray(order=None, out=None)
quadratic = qp.objective.quadratic.coefficients.toarray(order=None, out=None)
linear = {int(idx):-round(value,2) for idx,value in enumerate(linear[0])}
quadratic = {(int(iy),int(ix)):-quadratic[iy, ix] for iy, ix in np.ndindex(quadratic.shape) if iy<ix and abs(quadratic[iy, ix])}
return linear, quadratic