-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_q_annealing.py
259 lines (172 loc) · 7.37 KB
/
process_q_annealing.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
""" prepare inputs and analyze outputs from quantum annelaing """
import pickle
import os.path
import argparse
from dimod import utilities
from QTrains import file_LP_output, file_QUBO, file_QUBO_comp, file_hist
from QTrains import solve_on_LP, prepare_qubo, solve_qubo, analyze_qubo_Dwave
from QTrains import display_prec_feasibility, plot_hist_pass_obj, approx_no_physical_qbits, Analyze_qubo
from trains_timetable import Input_timetable, Comp_parameters
def prepare_Ising(trains_input, q_pars):
qubo_file = file_QUBO(trains_input, q_pars)
with open(qubo_file, 'rb') as fp:
dict_read = pickle.load(fp)
qubo_to_analyze = Analyze_qubo(dict_read)
Q = qubo_to_analyze.qubo
Ising = utilities.qubo_to_ising(Q, offset=0.0)
print("compute")
ising_file = qubo_file.replace("qubo_", "ising_").replace("QUBOs", "Ising").replace(".json", ".pkl")
if not os.path.isfile(ising_file):
print("save")
with open(ising_file, 'wb') as fp:
pickle.dump(Ising, fp)
def process(trains_input, q_pars):
""" the sequence of calculation makes computation if results has not been saved already"""
qubo_file = file_QUBO(trains_input, q_pars)
lp_file = file_LP_output(trains_input, q_pars)
qubo_output_file = file_QUBO_comp(trains_input, q_pars)
hist_file = file_hist(trains_input, q_pars)
if not os.path.isfile(qubo_file):
prepare_qubo(trains_input, q_pars, qubo_file)
if q_pars.compute:
if not os.path.isfile(lp_file):
solve_on_LP(trains_input, q_pars, lp_file)
if not os.path.isfile(qubo_output_file):
solve_qubo(q_pars, qubo_file, qubo_output_file)
if q_pars.analyze:
try:
if not os.path.isfile(hist_file):
analyze_qubo_Dwave(trains_input, q_pars, qubo_file, lp_file, qubo_output_file, hist_file)
file_pass = hist_file.replace(".json", f"{trains_input.objective_stations[0]}_{trains_input.objective_stations[1]}.pdf")
file_obj = hist_file.replace(".json", "obj.pdf")
plot_hist_pass_obj(trains_input, q_pars, hist_file, file_pass, file_obj)
display_prec_feasibility(trains_input, q_pars, hist_file)
except Exception as e:
print(" XXXXXXXXXXXXXXXXXXXXXX ")
print( f"not working for {qubo_output_file}" )
print(f"{e}")
def get_no_physical_qbits(ret_dict, trains_input, q_pars, trains):
""" counts no physical q-bits update dict """
no_logical, no_physical = approx_no_physical_qbits(trains_input, q_pars)
if trains_input.delays != {}:
ret_dict[f"{trains}_{q_pars.dmax}_disturbed"] = {"no_logical": no_logical, "no_physical": no_physical}
else:
ret_dict[f"{trains}_{q_pars.dmax}_notdisturbed"] = {"no_logical": no_logical, "no_physical": no_physical}
def count_no_qbits(qubo, parameters):
""" counts no physical q-bits after embedding for 1 - 12 trains """
delays_list = [{}, {1:5, 2:2, 4:5}]
ret_dict = {}
for d in [2,6]:
parameters.dmax = d
for delays in delays_list:
qubo.qubo_real_1t(delays)
get_no_physical_qbits(ret_dict, qubo, parameters, 1)
qubo.qubo_real_2t(delays)
get_no_physical_qbits(ret_dict, qubo, parameters, 2)
qubo.qubo_real_4t(delays)
get_no_physical_qbits(ret_dict, qubo, parameters, 4)
qubo.qubo_real_6t(delays)
get_no_physical_qbits(ret_dict, qubo, parameters, 6)
qubo.qubo_real_8t(delays)
get_no_physical_qbits(ret_dict, qubo, parameters, 8)
qubo.qubo_real_10t(delays)
get_no_physical_qbits(ret_dict, qubo, parameters, 10)
qubo.qubo_real_11t(delays)
get_no_physical_qbits(ret_dict, qubo, parameters, 11)
qubo.qubo_real_12t(delays)
get_no_physical_qbits(ret_dict, qubo, parameters, 12)
return ret_dict
def series_of_computation(qubo, parameters):
""" performs series of computation for 1 - 12 trains """
delays_list = [{}, {1:5, 2:2, 4:5}]
for delays in delays_list:
qubo.qubo_real_1t(delays)
process(qubo, parameters)
qubo.qubo_real_2t(delays)
process(qubo, parameters)
qubo.qubo_real_4t(delays)
process(qubo, parameters)
qubo.qubo_real_6t(delays)
process(qubo, parameters)
qubo.qubo_real_8t(delays)
process(qubo, parameters)
qubo.qubo_real_10t(delays)
process(qubo, parameters)
qubo.qubo_real_11t(delays)
process(qubo, parameters)
qubo.qubo_real_12t(delays)
process(qubo, parameters)
if __name__ == "__main__":
parser = argparse.ArgumentParser("mode of problem solving: computation / output analysis")
parser.add_argument(
"--mode",
type=int,
help="process mode: 0: prepare only QUBO, 1: make computation (ILP and annealing), 2: analyze outputs, 3: count q-bits, 4: save Ising ",
default=2,
)
parser.add_argument(
"--simulation",
type=bool,
help="if True solve / analyze output of simulated annealing (via DWave software), if False real annealing",
default=False,
)
parser.add_argument(
"--softern_pass",
type=bool,
help="if true analyze output without feasibility check on minimal passing time constrain",
default=False,
)
args = parser.parse_args()
q_par = Comp_parameters()
q_par.softern_pass = args.softern_pass
q_par.compute = False # make computations / optimisation
q_par.analyze = False # Analyze results
assert args.mode in [0,1,2,3,4]
if args.mode in [1, 3]:
q_par.compute = True # make computations / optimisation
elif args.mode == 2:
q_par.analyze = True
our_qubo = Input_timetable()
if args.simulation:
q_par.method = "sim"
for d_max in [2]:
q_par.dmax = d_max
q_par.ppair = 2.0
q_par.psum = 4.0
series_of_computation(our_qubo, q_par)
q_par.ppair = 20.0
q_par.psum = 40.0
series_of_computation(our_qubo, q_par)
elif args.mode == 3:
q_par.solver = "Advantage_system4.1"
no_qbits = count_no_qbits(our_qubo, q_par)
with open("solutions/embedding.json", 'wb') as fp:
pickle.dump(no_qbits, fp)
elif args.mode == 4:
q_pars = Comp_parameters()
our_qubo = Input_timetable()
q_pars.compute = False # make computations / optimisation
q_pars.analyze = False
q_pars.dmax = 6
q_pars.ppair = 2.0
q_pars.psum = 4.0
delays_list = [{}, {1:5, 2:2, 4:5}]
for delays in delays_list:
our_qubo.qubo_real_2t(delays)
prepare_Ising(our_qubo, q_pars)
our_qubo.qubo_real_6t(delays)
prepare_Ising(our_qubo, q_pars)
our_qubo.qubo_real_11t(delays)
prepare_Ising(our_qubo, q_pars)
else:
q_par.method = "real"
q_par.solver = "Advantage_system6.3"
for d_max in [2,6]:
q_par.dmax = d_max
for q_par.annealing_time in [10, 1000]:
q_par.ppair = 2.0
q_par.psum = 4.0
series_of_computation(our_qubo, q_par)
q_par.ppair = 20.0
q_par.psum = 40.0
series_of_computation(our_qubo, q_par)