-
Notifications
You must be signed in to change notification settings - Fork 4
/
spectral_optimization.py
474 lines (282 loc) · 19.5 KB
/
spectral_optimization.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import pickle
from numpy import *
from scipy.optimize import basinhopping
import os,sys,inspect
import time
from scipy import linalg as LA
from scipy import optimize
import numpy as np
from simulation_parameters import SimulationParameters, OptimizationParameters
from numpy.random import random
from ions import Chain
from ion_trap import *
import matplotlib.pylab as plt
class SimulatedAnnealing:
def __init__(self, initial_electrode_voltages, number_of_ions, spectral_seperation_ratio=0.01, voltage_scale=40.):
self.number_of_ions = number_of_ions
self.initial_electrode_voltages = initial_electrode_voltages
self.optimization_parameters = OptimizationParameters(number_of_ions, y_radial_freq,
x_radial_freq)
self.spectral_seperation_ratio = spectral_seperation_ratio
self.voltage_scale = voltage_scale
sp = Spectrum(self.optimization_parameters, self.initial_electrode_voltages, initial_positions_guess)
self.initial_positions_guess = []
#self.sim_temperature = sim_temperature
self.optimal_voltages_and_spectra = []
def displacement_probability(self, voltages):
pass
def run(self, num_of_runs):
init_voltages_guess = self.initial_electrode_voltages
for i in range(num_of_runs):
displaced_voltages = next_point( init_voltages, 0.05 )
sp = Spectrum(self.optimization_parameters, displaced_voltages, initial_positions_guess)
if cost_is_lower( init_voltages + displacement, ):
init_voltages += displacement
position_list.append(init)
def next_point(init_vec, mesh):
init_vec += (2*random(len(init_vec))-1.)*mesh
return init_vec
def cost_is_lower(self, spectrum, critical_ratio=.1):
try:
spectrum.set_ions_positions()
except ValueError, e:
return False
normal_modes_with_radial_correction = spectrum.get_spectrum(electrode_voltages)[1]
for i in range(3):
if (normal_modes_with_radial_correction[i]-normal_modes_with_radial_correction[i+1])/(normal_modes_with_radial_correction[i+1] \
- normal_modes_with_radial_correction[i+2]) <= critical_ratio:
save(electrode_voltages, spectrum)
return True
return False
class Optimization:
def __init__(self, trap, spectral_seperation_ratio=.1, laser_orientation='Y',
Y_field_max=0.02 * 1.e3 , X_field_max=0.02 * 1.e3, Z_field_max=0.2 * 1.e3,
voltage_scale=100., diff_voltage_scale=40., max_voltage_scale = 50.,
plot_cost = True, electrodes_connected=True):
self.trap = trap
self.Y_field_max = Y_field_max
self.X_field_max = X_field_max
self.Z_field_max = Z_field_max
self.voltage_scale = voltage_scale
self.max_voltage_scale = max_voltage_scale
self.diff_voltage_scale = diff_voltage_scale
#self.initial_potential = initial_potential
self.chain = trap.chain
self.laser_orientation = laser_orientation
self.spectral_seperation_ratio = spectral_seperation_ratio
self.optimal_voltages_and_spectra = []
self.cost = 1
self.run = 1
self.file = open('Optimization/Results/global_optimization_results_2modes_isolation.txt', 'w')
self.electrodes_connected = electrodes_connected
self.plot_cost = plot_cost
self.plot_counter = 1
def cost_function1(self, electrode_voltages):
try:
self.trap.set_electrode_voltages( electrode_voltages )
except Exception:
#self.randomize_voltages(electrode_voltages)
self.cost += .1*abs(self.cost)
return self.cost
#Following condition must turn into a zigzag avoiding condition:
if max(self.trap.chain.get_positions())-min(self.trap.chain.get_positions())< self.trap.chain.num_of_ions * 2.e-6:
#self.randomize_voltages(electrode_voltages)
self.cost += .4*abs(self.cost)
else:
normal_modes_with_radial_correction = self.trap.get_spectrum(self.laser_orientation)[1]
mode_splitting_ratios = []
for i in range(3):
mode_splitting_ratios.append( (normal_modes_with_radial_correction[i]-normal_modes_with_radial_correction[i+1])/(normal_modes_with_radial_correction[i+1] \
- normal_modes_with_radial_correction[i+2]) )
smallest_mode_splitting_ratio = min(mode_splitting_ratios)
#Set cost criteria for spectrum:
spectrum_cost = smallest_mode_splitting_ratio/self.spectral_seperation_ratio
Efield_cost = (1./3) *( sum( ( self.trap.potential.Y_1st_deriv( self.chain.get_positions() )/self.Y_field_max )**2 )/self.chain.num_of_ions + \
sum( ( self.trap.potential.X_1st_deriv( self.chain.get_positions() )/self.X_field_max )**2 )/self.chain.num_of_ions +\
sum( ( self.trap.potential.Z_1st_deriv( self.chain.get_positions() )/self.Z_field_max )**2 )/self.chain.num_of_ions )
voltage_cost = sum( ( self.trap.potential.electrode_voltages/self.voltage_scale )**2 )/len( self.trap.potential.used_electrodes )
self.cost = spectrum_cost + Efield_cost + voltage_cost
print("\nCost is %.15f" % self.cost)
print("Voltages are ", electrode_voltages)
return self.cost
def cost_function_new(self, electrode_voltages):
try:
self.trap.set_electrode_voltages( electrode_voltages )
except Exception:
#self.randomize_voltages(electrode_voltages)
self.cost += 5.#.1*abs(self.cost)
print "Dark region, cost: "+str(self.cost)
return self.cost
#Following condition must turn into a zigzag avoiding condition:
if max(self.trap.chain.get_positions())-min(self.trap.chain.get_positions())< self.trap.chain.num_of_ions * 2.e-6:
#self.randomize_voltages(electrode_voltages)
self.cost += 5.#.1*abs(self.cost)
print "Dark region, cost: "+str(self.cost)
return self.cost
else:
normal_modes_with_radial_correction = self.trap.get_spectrum(self.laser_orientation)[1]
'''
mode_splitting_ratios = []
for i in range(3):
mode_splitting_ratios.append( (normal_modes_with_radial_correction[i]-normal_modes_with_radial_correction[i+1])/(normal_modes_with_radial_correction[i+1] \
- normal_modes_with_radial_correction[i+2]) )
smallest_mode_splitting_ratio = min(mode_splitting_ratios)
'''
first_mode_splitting_ratio = (normal_modes_with_radial_correction[0]-normal_modes_with_radial_correction[1])/(normal_modes_with_radial_correction[1] \
- normal_modes_with_radial_correction[2])
#Set cost criteria for spectrum:
spectrum_cost = exp(first_mode_splitting_ratio/self.spectral_seperation_ratio)
Efield_cost = (1./3) *( sum( ( self.trap.potential.Y_1st_deriv( self.chain.get_positions() )/self.Y_field_max )**2 )/self.chain.num_of_ions + \
sum( ( self.trap.potential.X_1st_deriv( self.chain.get_positions() )/self.X_field_max )**2 )/self.chain.num_of_ions +\
5. *sum( ( self.trap.potential.Z_1st_deriv( self.chain.get_positions() )/self.Z_field_max )**2 )/self.chain.num_of_ions )
voltage_cost = sum( ( self.trap.potential.electrode_voltages/self.voltage_scale )**2 )/len( self.trap.potential.used_electrodes )
self.cost = spectrum_cost + Efield_cost + voltage_cost
print("\nCost is %.15f" % self.cost)
print"first_mode_splitting_ratio " + str(first_mode_splitting_ratio)
print("Voltages are ", electrode_voltages)
if self.plot_counter == 1 and self.plot_cost:
plt.ylim(0, 1.3*self.cost)
plt.ion()
plt.show()
self.plot_realtime( self.cost )
return self.cost
def cost_function_exp_voltages_punish(self, electrode_voltages):
try:
self.trap.set_electrode_voltages( electrode_voltages )
except Exception:
#self.randomize_voltages(electrode_voltages)
self.cost += .01*abs(self.cost)
return self.cost
#Following condition must turn into a zigzag avoiding condition:
if max(self.trap.chain.get_positions())-min(self.trap.chain.get_positions())< self.trap.chain.num_of_ions * 2.e-6:
#self.randomize_voltages(electrode_voltages)
self.cost += .01*abs(self.cost)
return self.cost
else:
normal_modes_with_radial_correction = self.trap.get_spectrum(self.laser_orientation)[1]
'''
mode_splitting_ratios = []
for i in range(3):
mode_splitting_ratios.append( (normal_modes_with_radial_correction[i]-normal_modes_with_radial_correction[i+1])/(normal_modes_with_radial_correction[i+1] \
- normal_modes_with_radial_correction[i+2]) )
smallest_mode_splitting_ratio = min(mode_splitting_ratios)
'''
first_mode_splitting_ratio = (normal_modes_with_radial_correction[0]-normal_modes_with_radial_correction[1])/(normal_modes_with_radial_correction[1] \
- normal_modes_with_radial_correction[2])
#Set cost criteria for spectrum:
spectrum_cost = first_mode_splitting_ratio/self.spectral_seperation_ratio
Efield_cost = (1./3) *( sum( ( self.trap.potential.Y_1st_deriv( self.chain.get_positions() )/self.Y_field_max )**2 )/self.chain.num_of_ions + \
sum( ( self.trap.potential.X_1st_deriv( self.chain.get_positions() )/self.X_field_max )**2 )/self.chain.num_of_ions +\
sum( ( self.trap.potential.Z_1st_deriv( self.chain.get_positions() )/self.Z_field_max )**2 )/self.chain.num_of_ions )
voltage_cost = sum( exp( (self.trap.potential.electrode_voltages/self.voltage_scale )**2 ) )/len( self.trap.potential.used_electrodes )
self.cost = spectrum_cost + Efield_cost + voltage_cost
print "\nCost is %.15f" % self.cost
print "Voltages: " + str( list(electrode_voltages) )
#self.save( self.cost, electrode_voltages )
self.run += 1
return self.cost
def cost_function_voltages_and_max_voltages_exp_punish(self, electrode_voltages):
try:
self.trap.set_electrode_voltages( electrode_voltages )
except Exception:
#self.randomize_voltages(electrode_voltages)
self.cost += .001*abs(self.cost)
print "\nBad Cost is %.15f" % self.cost
return self.cost
#raise Exception("Whatever")
#Following condition must turn into a zigzag avoiding condition:
if max(self.trap.chain.get_positions())-min(self.trap.chain.get_positions())< self.trap.chain.num_of_ions * 1.e-6:
#self.randomize_voltages(electrode_voltages)
self.cost += .001*abs(self.cost)
print "\nChain crushed. Cost is %.15f" % self.cost
return self.cost
#raise Exception("\nChain Crushed")
else:
normal_modes_with_radial_correction = self.trap.get_spectrum(self.laser_orientation)[1]
'''
mode_splitting_ratios = []
for i in range(3):
mode_splitting_ratios.append( (normal_modes_with_radial_correction[i]-normal_modes_with_radial_correction[i+1])/(normal_modes_with_radial_correction[i+1] \
- normal_modes_with_radial_correction[i+2]) )
smallest_mode_splitting_ratio = min(mode_splitting_ratios)
'''
first_mode_splitting_ratio = (normal_modes_with_radial_correction[0]-normal_modes_with_radial_correction[1])/(normal_modes_with_radial_correction[1] \
- normal_modes_with_radial_correction[2])
#Set cost criteria for spectrum:
spectrum_cost = 0*first_mode_splitting_ratio/self.spectral_seperation_ratio
spectrum_exp_cost = 10*exp( (first_mode_splitting_ratio/0.5)**2 ) -1
Efield_cost = (1./3) *( sum( ( self.trap.potential.Y_1st_deriv( self.chain.get_positions() )/self.Y_field_max )**2 )/self.chain.num_of_ions + \
sum( ( self.trap.potential.X_1st_deriv( self.chain.get_positions() )/self.X_field_max )**2 )/self.chain.num_of_ions +\
0*sum( exp( ( self.trap.potential.Z_1st_deriv( self.chain.get_positions() )/self.Z_field_max )**2) )/self.chain.num_of_ions )
voltage_cost = 0* sum( (self.trap.potential.electrode_voltages/self.voltage_scale )**2 )/len( self.trap.potential.used_electrodes )
#diff_voltages_cost = sum( exp( (diff(electrode_voltages)/self.diff_voltage_scale )**2 ) )/len( self.trap.potential.used_electrodes - 1 )
#Punish max voltage significantly if it was above 80
max_voltage_cost = 0 #exp( (max(abs(self.trap.potential.electrode_voltages))/self.max_voltage_scale)**2 )
#max_voltages_cost = (max(abs(self.trap.potential.electrode_voltages)) - min(abs(self.trap.potential.electrode_voltages)))/self.diff_voltage_scale
self.cost = spectrum_cost + spectrum_exp_cost + 0*Efield_cost + voltage_cost + max_voltage_cost
print "\nOK Cost is %.15f" % self.cost
print "Voltages: " + str( list(electrode_voltages) )
#self.save( self.cost, electrode_voltages )
print "\nfirst_mode_splitting_ratio: %.5f" % first_mode_splitting_ratio
last_ok_cost = self.cost
if self.plot_counter == 1 and self.plot_cost:
plt.ylim(0, 1.3*self.cost)
plt.ion()
plt.show()
self.plot_realtime( self.cost )
self.run += 1
return self.cost
def cost_function_play(self, electrode_voltages):
self.trap.set_electrode_voltages( electrode_voltages )
#Following condition must turn into a zigzag avoiding condition:
if max(self.trap.chain.get_positions())-min(self.trap.chain.get_positions())< self.trap.chain.num_of_ions * 2.e-6:
#self.randomize_voltages(electrode_voltages)
self.cost = 1000.#*abs(self.cost)
print "\nChain crushed. Cost is %.15f" % self.cost
#raise Exception("\nChain Crushed")
else:
normal_modes_with_radial_correction = self.trap.get_spectrum(self.laser_orientation)[1]
'''
mode_splitting_ratios = []
for i in range(3):
mode_splitting_ratios.append( (normal_modes_with_radial_correction[i]-normal_modes_with_radial_correction[i+1])/(normal_modes_with_radial_correction[i+1] \
- normal_modes_with_radial_correction[i+2]) )
smallest_mode_splitting_ratio = min(mode_splitting_ratios)
'''
first_mode_splitting_ratio = (normal_modes_with_radial_correction[0]-normal_modes_with_radial_correction[1])/(normal_modes_with_radial_correction[1] \
- normal_modes_with_radial_correction[2])
#Set cost criteria for spectrum:
spectrum_cost = first_mode_splitting_ratio/self.spectral_seperation_ratio
spectrum_exp_cost = exp( (first_mode_splitting_ratio/0.1)**2 ) -1
Efield_cost = (1./3) *( sum( ( self.trap.potential.Y_1st_deriv( self.chain.get_positions() )/self.Y_field_max )**2 )/self.chain.num_of_ions + \
sum( ( self.trap.potential.X_1st_deriv( self.chain.get_positions() )/self.X_field_max )**2 )/self.chain.num_of_ions +\
sum( exp( ( self.trap.potential.Z_1st_deriv( self.chain.get_positions() )/self.Z_field_max )**2) )/self.chain.num_of_ions )
voltage_cost = 0* sum( (self.trap.potential.electrode_voltages/self.voltage_scale )**2 )/len( self.trap.potential.used_electrodes )
#diff_voltages_cost = sum( exp( (diff(electrode_voltages)/self.diff_voltage_scale )**2 ) )/len( self.trap.potential.used_electrodes - 1 )
#Punish max voltage significantly if it was above 80
max_voltage_cost = 0 #exp( (max(abs(self.trap.potential.electrode_voltages))/self.max_voltage_scale)**2 )
#max_voltages_cost = (max(abs(self.trap.potential.electrode_voltages)) - min(abs(self.trap.potential.electrode_voltages)))/self.diff_voltage_scale
self.cost = spectrum_cost + spectrum_exp_cost + Efield_cost + voltage_cost + max_voltage_cost
print "\nCost is %.15f" % self.cost
print "Voltages: " + str( list(electrode_voltages) )
#self.save( self.cost, electrode_voltages )
print "\nfirst_mode_splitting_ratio: %.5f" % first_mode_splitting_ratio
last_ok_cost = self.cost
if self.plot_counter == 1 and self.plot_cost:
plt.ylim(0, 1.3*self.cost)
plt.ion()
plt.show()
self.plot_realtime( self.cost )
self.run += 1
return self.cost
def save(self, cost, electrode_voltages):
self.file.write( "\n############################################################" )
self.file.write( "\nRun %i " % self.run )
self.file.write( "\nCost %.10f" % cost )
self.file.write( "\nVoltages: \n" + str( list(electrode_voltages) ) )
def plot_realtime(self, point):
plt.scatter(self.plot_counter, point)
plt.draw()
self.plot_counter += 1
def randomize( self, electrode_voltages):
pass