def main(): ut = UTILS() parameters=ut.read_parameter_file(file = "../para.dat") parameters['L']=2 nrange = np.arange(4,200,4,dtype=int) nrange = [nrange[18]] dt = 0.02 parameters['dt'] = dt mean_fid = [] std_fid = [] for n in nrange: #model = ut.quick_setup(argv=['T=%.3f'%T,'n_step=%i'%n_step],file='../para.dat') parameters['T'] = n*dt parameters['n_step']= n file_name = ut.make_file_name(parameters,root="../data/") res = parse_data(file_name,v=2) # results stored here ... mean_fid.append(np.mean(res['F'])) std_fid.append(np.std(res['F'])) prot = res['protocol'] n_step = len(res['protocol'][0]) plotting.protocol(np.arange(0,n_step)*dt,np.mean(res['protocol'],axis=0),title='$T=%.3f$'%(dt*n_step)) #plt.scatter(nrange*dt,std_fid) #plt.plot(nrange*dt,std_fid) plt.show()
def main(): ut = UTILS() parameters = ut.read_parameter_file(file="../para.dat") parameters['L'] = 1 n_step = 400 nrange = np.arange(10, 800, 10, dtype=int) Trange = np.arange(0.05, 4.01, 0.05) dt = 0.005 parameters['dt'] = dt mean_fid = [] std_fid = [] n_fid = [] ed1 = [] ed2 = [] for n in nrange: #for n in nrange: #model = ut.quick_setup(argv=['T=%.3f'%T,'n_step=%i'%n_step],file='../para.dat') parameters['T'] = n * dt #parameters['T'] = T parameters['n_step'] = n #parameters['n_step']= n_step #parameters['dt'] = parameters['T']/parameters['n_step'] file_name = ut.make_file_name(parameters, root="../data/") res = parse_data(file_name) # results stored here ... print(n, '\t', len(res['F'])) mean_fid.append(np.max(res['F'])) n_fid.append(np.mean(res['n_fid'])) std_fid.append(np.std(res['F'])) tmp = 8 * (res['protocol'] - 0.5) ed1.append(Ed_Ad_OP(tmp)) #ed2.append(Ed_Ad_OP_2(res['protocol'],min_h=0, max_h=1)) plt.plot(nrange * dt, n_fid, label='ed1') #plt.plot(nrange*dt, ed2,label='ed2') plt.legend(loc='best') plt.show() exit() n_step = len(res['protocol'][0]) plotting.protocol(Trange, np.mean(res['protocol'], axis=0), title='$T=%.3f$' % (dt * n_step)) #plotting.protocol(np.arange(0,n_step)*dt,np.mean(res['protocol'],axis=0),title='$T=%.3f$'%(dt*n_step)) #plt.scatter(nrange*dt,std_fid) #plt.plot(nrange*dt,std_fid) plt.show()
def main(): param = { 'N_time_step': 100, 'N_quench': 0, 'Ti': 0.04, 'action_set': 0, 'hx_initial_state': -2.0, 'hx_final_state': 2.0, 'delta_t': 0.001, 'hx_i': -4.0, 'RL_CONSTRAINT': True, 'L': 6, 'J': 1.00, 'hz': 1.0, 'symmetrize': False } file_name = ut.make_file_name(param) res = ut.gather_data(param, "../data/") print(compute_observable.Ed_Ad_OP(res['h_protocol'], 4)) plotting.protocol(range(100), res['h_protocol'][0]) #plotting.protocol(range(100),res['h_protocol'][1]) #print(res['fid']) #print(res.keys()) print(file_name) #with open(' exit() import os #=========================================================================== # pca=PCA(n_components=2) # param['N_time_step']=10 # dc=ut.gather_data(param,'../data/') # pca.fit(dc['h_protocol']/4.) # X=pca.transform(dc['h_protocol']/4.) # # plt.scatter(X[:,0],X[:,1]) # plt.title('PCA, $t=0.1$, continuous protocol') # plt.savefig("PCA_AS2_t-0p1.pdf") # plt.show() # exit() #=========================================================================== #=========================================================================== # dataBB8=[] # param['action_set']=0 # param['N_time_step']=60 # # param['delta_t']=0.5/60. # dc=ut.gather_data(param,'../data/') # pca=PCA(n_components=2) # pca.fit(dc['h_protocol']/4.) # print(pca.explained_variance_ratio_) # exit() # # param['delta_t']=3.0/60. # dc=ut.gather_data(param,'../data/') # X=pca.transform(dc['h_protocol']/4.) # # title='PCA$_{50}$, $t=3.0$, continuous protocol, nStep$=60$' # out_file="PCA_AS0_t-3p0_nStep-60.pdf" # plotting.visne_2D(X[:,0],X[:,1],dc['fid'],zlabel="Fidelity",out_file=out_file,title=title,show=True,xlabel='PCA-1',ylabel='PCA-2') # #=========================================================================== #exit() #plt.scatter(X[:,0],X[:,1]) #plt.title('PCA$_{50}$, $t=1.5$, continuous protocol, nStep$=60$') #plt.savefig("PCA_AS0_t-0p8_nStep-60.pdf") #plt.show() #exit() # exit() #=========================================================================== # param['N_time_step']=2 # param['action_set']=0 # dc=ut.gather_data(param,'../data/') # print(dc['h_protocol']) # exit() # dataBB8=[] #=========================================================================== #=============================================================================== # # param['action_set']=0 # param['N_time_step']=60 # param['delta_t']=0.5/60 # # dc=ut.gather_data(param,'../data/') # # protocols=dc['h_protocol'] # #print(np.shape(dc['h_protocol'])) # sort_f=np.argsort(dc['fid'])[::-1] # # print(sort_f[0]) # # #protocols[sort_f[0]] # # best_prot=protocols[sort_f[0:10]] # x=np.array(range(60))*1.0/60 # #print(best_prot.reshape) # #print(x.shape) # #print(np.array(range(60))*0.1/60) # #print(best_prot) # #print(np.shape(best_prot)) # #print(np.shape(np.arange(0.1,3.05,0.1)*0.05)) # # plotting.protocol(protocols[:2],x,labels=dc['fid'][:2],show=True) # # exit() # # #=============================================================================== param['N_time_step'] = 60 param['action_set'] = 0 dataBB8 = [] compTime = [] x = [] for t in np.arange(0.1, 3.05, 0.1): dt = t / param['N_time_step'] param['delta_t'] = dt # Changed it to be returning False if file is not found ... dc = ut.gather_data(param, '../data/') if dc is not False: eaop = compute_observable.Ed_Ad_OP(dc['h_protocol'], 4.0) print(t, eaop, dc['fid'].shape, '\t', np.mean(dc['n_fid'])) compTime.append(np.mean(dc['n_fid'])) dataBB8.append(eaop) x.append(t) else: print("Data not available for %.3f" % dt) y = compTime plotting.observable(y, x, title='Depth of search for bang-bang protocol', ylabel='\# of fidelity evaluations', xlabel='$T$', marker="-", labels=['Obtained time (SGD)']) exit() #=========================================================================== # param['action_set']=0 # param['delta_t']=0.01 #=========================================================================== #=========================================================================== # for i in range(2,300,4): # param['N_time_step']=i # is_there,dc=ut.gather_data(param,'../data/') # if is_there: # eaop=compute_observable.Ed_Ad_OP(dc['h_protocol'],4.0) # print(i,eaop,dc['fid'].shape,'\t',np.mean(dc['n_fid'])) # compTime.append(np.mean(dc['n_fid'])) # dataBB8.append(eaop) # x.append(i) # else: # print("Data not available for %i"%i) # #=========================================================================== #=========================================================================== # param['N_time_step']=150 # is_there,dc=ut.gather_data(param,'../data/') # x=np.arange(0,150*0.01,0.01) # plotting.protocol(dc['h_protocol'][:3],x,labels=dc['fid'][:3],show=True) # exit() # #x=np.array(range(2,300,4))*0.01 #=========================================================================== param['action_set'] = 0 param['delta_t'] = 0.01 mean_fid_BB = [] h_protocol_BB = {} fid_BB = {} n_fid_BB = [] x = [] sigma_fid = [] EA_OP = [] for i in range(2, 300, 4): param['N_time_step'] = i data_is_available, dc = ut.gather_data(param, '../data/') if data_is_available: mean_fid_BB.append(np.mean(dc['fid'])) sigma_fid.append(np.std(dc['fid'])) fid_BB[i] = dc['fid'] EA_OP.append(compute_observable.Ed_Ad_OP(dc['h_protocol'], 4.0)) h_protocol_BB[i] = dc['h_protocol'] n_fid_BB.append(np.mean(dc['n_fid'])) x.append(i * param['delta_t']) #print(fid_BB[130]) #mean=np.mean(fid_BB[130]) #sns.distplot(fid_BB[130],bins=np.linspace(mean-0.005,mean+0.005,100)) #plt.tick_params(labelleft='off') #plt.show() x = np.array(x) y = [ n / (x[i] / param['delta_t']) for n, i in zip(n_fid_BB, range(len(n_fid_BB))) ] plotting.observable(y, x, title='Depth of search for bang-bang protocol', ylabel='(\# of fidelity evaluations)/$N$', xlabel='$T$', marker="-", labels=['Minimum time', 'Obtained time (SGD)']) #plotting.protocol(h_protocol_BB[130][20:25],np.arange(0,130,1)*param['delta_t']) exit() #pca.fit() #=========================================================================== # dataCONT=[] # for t in range(2,300,4): # print(t) # param['N_time_step']=t # dc=ut.gather_data(param,'../data/') # #print(dc['h_protocol'].shape) # eaop=compute_observable.Ed_Ad_OP(dc['h_protocol'],4.0) # print(eaop) # dataCONT.append(eaop) # # file="../data/EAOP_"+ut.make_file_name(param) # with open(file,'wb') as f: # pickle.dump(dataCONT,f);f.close(); # # exit() # #=========================================================================== #=========================================================================== # param['action_set']=0 # dataBB8=[] # for t in range(2,300,4): # print(t) # param['N_time_step']=t # dc=ut.gather_data(param,'../data/') # eaop=compute_observable.Ed_Ad_OP(dc['h_protocol'],4.0) # print(eaop) # #print(dc['h_protocol'].shape) # dataBB8.append(eaop) # # file="../data/EAOP_"+ut.make_file_name(param) # with open(file,'wb') as f: # pickle.dump(dataBB8,f);f.close(); # # exit() #=========================================================================== #=========================================================================== # param['N_time_step']=298 # param['action_set']=0 # file="../data/EAOP_"+ut.make_file_name(param) # with open(file,'rb') as f: # dataBB8=pickle.load(f);f.close(); # # param['action_set']=2 # f="../data/EAOP_"+ut.make_file_name(param) # with open(f,'rb') as file: # dataCONT=pickle.load(file); # # time_axis=np.array(range(2,300,4))*0.01 # title="Edward-Anderson parameter ($n=400$) vs. evolution time for SGD\n with the different action protocols ($L=1$)" # plotting.observable([dataBB8,dataCONT],[time_axis,time_axis],title=title, # out_file="SGD_EAOPvsT_AS0-2.pdf",show=True, # ylabel="$q_{EA}$",xlabel="$t$",labels=['bang-bang8','continuous']) #=========================================================================== #=========================================================================== # param['N_time_step']=250 # dc=ut.gather_data(param,'../data/') # sns.distplot(dc['fid'],kde=False,label='$t=%.3f$'%(param['N_time_step']*0.01)) # plt.legend(loc='best') # plt.savefig('SGD_hist_fid_t2p5.pdf') # plt.show() # exit() #=========================================================================== #=========================================================================== # title="Fidelity ($n=400$) vs. evolution time for SGD\n with the different action protocols ($L=1$)" # plotting.observable(np.array(data),np.array(range(2,300,4))*0.01,title=title, # out_file="SGD_FvsT_AS2.pdf",show=True, # ylabel="$F$",xlabel="$t$",labels=['continuous']) # #=========================================================================== exit()
def main(): L=6 dt = 0.005 m = 0 n_step = 100 param = {'L' : L, 'dt': dt, 'n_step': n_step, 'm': m} file_name = make_file_name(param, root= "data/") custom_prot=LZ.custom_protocol( J=1.0, L=L, hz=1.0, hx_init_state=-2.0, hx_target_state=2.0, delta_t=dt, hx_i=-4., hx_max=4., action_set_=[-8.,0.,8.], option='fast' ) choice = 1 count = -1 palette = np.array(sns.color_palette('hls',10)) n_step = 330 dt=0.01 param = {'L' : L, 'dt': dt, 'n_step': n_step, 'm': m} file_name = make_file_name(param, root= "data/") with open(file_name,'rb') as f: data=pickle.load(f) #print(data[4][0]) #print(data[4][1]) #print(gamma(data[4][1])) #plotting.protocol(range(330),data[4][1]) #exit() #for d in data: # print(d[0],'\t',gamma(d[1])) #exit() for m in [0]: count+=1 best_fid_all = [] mean_time_all = [] gamma_all = [] n_step_all = [] for n_step in np.arange(10,401,10): L = 6 dt = 0.01 #m = 2 #n_step = 100 param = {'L' : L, 'dt': dt, 'n_step': n_step, 'm': m} file_name = make_file_name(param, root= "data/") if os.path.exists(file_name): n_step_all.append(n_step) with open(file_name,'rb') as f: result = pickle.load(f) eval_times = [] symm_all = [] fid_all = [] for sample in result : [fid_best, hx_tmp, fid_best_list, hx_tmp_list] = sample eval_times.append(fid_best_list[-1][0]) symm_all.append(gamma(hx_tmp)) fid_all.append(fid_best) print(" n = %i "%n_step) print(max(fid_all),'\t\t', np.mean(fid_all)) print(np.mean(eval_times),'\t\t', np.std(eval_times)) print(min(symm_all),'\t\t', np.mean(symm_all)) print("\n") best_fid_all.append(np.max(fid_all)) mean_time_all.append(np.max(eval_times)) gamma_all.append(np.min(symm_all)) if choice == 0: plt.plot(np.array(n_step_all)*dt,best_fid_all, c=palette[count],label='$m=%i$'%m) elif choice == 1: plt.plot(np.array(n_step_all)*dt,mean_time_all, marker='o',label='$m=%i$'%m) elif choice == 2: plt.plot(np.array(n_step_all)*dt,gamma_all, marker='o',label='$m=%i$'%m) title = "$m=%i$, $L=%i$, $dt=%.4f$"%(m, L, dt) plt.legend(loc='best') plt.title(title) plt.tight_layout() if choice == 0: plt.xlabel('$T$') plt.ylabel("$F$") if choice == 1: plt.ylabel("\# of fid. eval") plt.xlabel("$T$") if choice == 2: plt.ylabel("$\Gamma$") plt.xlabel("$T$") plt.show() exit() #with open("long-ass-SA.pkl",'rb') as f: # [fid_best, hx_tmp, fid_best_list, hx_tmp_list]=pickle.load(f) #symmetrize(hx_tmp,left_unchanged=True) #print(custom_prot.evaluate_protocol_fidelity(hx # _tmp)) #plotting.protocol(np.arange(0.,250)*dt, hx_tmp) #exit() #print(gamma(hx_tmp)) #exit() #hx_tmp=[-4,4,-4,4,4,4] #print(custom_prot.evaluate_protocol_fidelity(hx_tmp)) #exit() '''for dt_ in np.arange(0.2,4.0,0.2)/20.: print("--------> T = ",dt_*20.) dt = dt_ custom_prot=LZ.custom_protocol( J=-1.0, L=L, hz=1.0, hx_init_state=-2.0, hx_target_state=2.0, delta_t=dt, hx_i=-4., hx_max=4., action_set_=[-8.,0.,8.], option='fast' ) fid=np.ones(2**20,dtype=np.float) for s in range(2**20): if s % 10000 == 0: print(s) hx_tmp=b2(s,w=20) fid[s]=custom_prot.evaluate_protocol_fidelity(hx_tmp) with open('fid_exact_T-%.3f_L-6_nstep-20.pkl'%(dt_*20),'wb') as f: pickle.dump(fid,f) exit()''' #np.histogram(fid,bins=100) #plt.show() #exit() #print(np.array(list(np.binary_repr(3, width=20)),dtype=np.int)) #exit() #for a in range(tot): #x = (250 - 2*(t1+t2)) //2 #print(2*x+2*t1+2*t2) '''for t1 in range(5,50): for t2 in range(5,50): x = (250 - 2*(t1+t2)) //2 if (2*x+2*t1+2*t2 == 250) & (x > -1): rad = [[-4,4][np.random.randint(2)] for i in range(2*x)] hx_tmp=np.array([4]*t1+[-4]*t2 + rad +[4]*t2+[-4]*t1) #print(2*x+2*t1+2*t2,hx_tmp.shape,'\t',t1,t2) #print(hx_tmp.shape) fid = custom_prot.evaluate_protocol_fidelity(hx_tmp) if fid > fid_best: fid_best = fid hx_best = hx_tmp t=[t1,t2]''' #print(custom_prot.evaluate_protocol_fidelity(hx_tmp)) #exit() #np.random.seed(0) #fid_best, hx_tmp, fid_best_list = SD_symmetrized(n_step, custom_prot, n_refuse_max=10000, n_eval_max = 10000, init_random = True) #fid_best, # hx_tmp, fid_best_list = SD_symmetrized(n_step, custom_prot, n_refuse_max=10000, n_eval_max = 200000, init_random = True) n_step = 320 m = 0 Ti=1e-3 N_quench = 20000 fid_best, hx_tmp, fid_best_list, hx_tmp_list = SA(Ti, N_quench, n_step, m, custom_prot, n_refuse_max = 20000, n_eval_max = 20000, init_random = True) fid_best_list=np.array(fid_best_list) plt.scatter(fid_best_list[:,0],fid_best_list[:,1]) plt.show() Gamma=gamma(hx_tmp) plotting.protocol(np.arange(0.,n_step)*dt, hx_tmp, title = "$m=%i$, $\Gamma=%.4f$, $F=%.12f$,\n $L=%i$, $dt=%.4f$, $T=%.3f$"%(m,Gamma,fid_best,L,dt,dt*n_step)) exit() #with open("data/nstep-%i_m-0_L-6_dt-0p0100.pkl"%n_step,'wb') as f: # pickle.dump([fid_best, hx_tmp, fid_best_list, hx_tmp_list],f) # f.close() for n_step in range(10,400,10): fid_list = [] fid_list_2 = [] hx_tmp_list = [] hx_tmp_list_2 = [] m = 0 Ti=1e-3 N_quench = 20000 fid_best, hx_tmp, fid_best_list, hx_tmp_list = SA(Ti, N_quench, n_step, m, custom_prot, n_refuse_max = 20000, n_eval_max = 20000, init_random = True) with open("data/nstep-%i_m-0_L-6_dt-0p0100.pkl"%n_step,'wb') as f: pickle.dump([fid_best, hx_tmp, fid_best_list, hx_tmp_list],f) f.close() exit() #fid_best, hx_tmp, fid_best_list, hx_tmp_list = SD(n_step, m, custom_prot, n_refuse_max=50000, n_eval_max = 400000, init_random = True) #print("Obtained fid: \t %.14f"%custom_prot.evaluate_protocol_fidelity(hx_tmp)) #Gamma = gamma(hx_tmp) #fid_test = custom_prot.evaluate_protocol_fidelity(hx_tmp) #plotting.protocol(np.arange(0.,n_step)*dt, hx_tmp, #title = "$m=%i$, $\Gamma=%.4f$, $F=%.12f$,\n $L=%i$, $dt=%.4f$, $T=%.3f$"%(m,Gamma,fid_test,L,dt,dt*n_step), #show = True #) '''with open("long-ass-SA.pkl",'wb') as f: pickle.dump([fid_best, hx_tmp, fid_best_list, hx_tmp_list],f) plotting.protocol_ising_2D_map(hx_tmp_list) fid_best_list=np.array(fid_best_list) plt.scatter(fid_best_list[:,0],fid_best_list[:,1]) plt.show() exit() hx_tmp_cp=np.copy(hx_tmp) symmetrize(hx_tmp,left_unchanged=True) fid_symm_1=custom_prot.evaluate_protocol_fidelity(hx_tmp) print(fid_symm_1) symmetrize(hx_tmp_cp,left_unchanged=False) fid_symm_2=custom_prot.evaluate_protocol_fidelity(hx_tmp_cp) print(fid_symm_2) exit()''' '''m = 2 for i in range(10): fid_best, hx_tmp, fid_best_list = SD(n_step, m, custom_prot, n_refuse_max=10000, n_eval_max = 200000, init_random = True) fid_list_2.append(fid_best) hx_tmp_list_2.append(fid_best) ''' '''print("fid",fid_list) print("fid 2",fid_list_2) with open("out_fid.pkl","wb") as f: pickle.dump([fid_list,hx_tmp_list,fid_list_2,hx_tmp_list_2]) ''' #for i in range(10): # fid_best, hx_tmp, fid_best_list = SD_symmetrized(n_step, custom_prot, n_refuse_max=10000, n_eval_max = 100000, init_random = True) #plotting.protocol(np.arange(0.0,n_step)*dt, hx_tmp, show=True) '''exit()
file=make_file_name(par) with open('data/'+file, 'rb') as f: data = pickle.load(f) for d in data[1]: if d[1] > best_fid: best_fid = d[1] best_fid_list.append(best_fid) np.savetxt('out.txt',best_fid_list) #print(data) exit() h=data[0][1] dt=0.015 t=np.arange(0,3.0,dt) plotting.protocol(t,h) exit() #with open("data/ES_L-06_T-1.000_n_step-20_slice-10.pkl", "rb") as f: # data=pickle.load(f) #L=6 #dt=1.0/20 #n_step = 20 #int_size = 2**20//32 #custom_prot=LZ.custom_protocol( # J=1.0, # L=L, hz=1.0, hx_init_state=-2.0, hx_target_state=2.0, # delta_t=dt, hx_i=-4., hx_max=4., action_set_=[-8.,0.,8.],
import LZ_sim_anneal as LZ import time import numpy as np import sys from matplotlib import pyplot as plt import plotting import pickle import utils import os p = [ 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0 ] plotting.protocol(np.linspace(0, 3.5, 50), p, show=True) exit() def b2(n10, w=10): x = np.array(list(np.binary_repr(n10, width=w)), dtype=np.float) x[x > 0.5] = 4. x[x < 0.5] = -4. return x L = 6 dt = 0.01 custom_prot = LZ.custom_protocol(J=1.0, L=L, hz=1.0,