def analyse_psiscaling(J2, size): h = load_hamiltonian('J1J2', size=size, J2=J2) configs, indexer = h.get_config_table() H, e0, v0, configs = analyse_exact(h, do_printsign=False, num_eng=1) rand = np.random.randn(len(v0)) func_list = [ lambda x: x**3, lambda x: x**5, lambda x: np.sinh(x), lambda x: np.cosh(x) ] labels = ['x^3', 'x^5', 'sinh(x)', 'cosh(x)'] x = np.arange(1, len(v0) + 1) amp = abs(v0) amp = np.sort(amp)[::-1] plt.ion() #plt.plot(np.log(amp)) plt.plot(x, amp) for func in func_list: amp2 = abs(func(rand)) amp2 = np.sort(amp2)[::-1] / np.linalg.norm(amp2) plt.plot(x, amp2) #plt.xscale('log') plt.legend(['$v$'] + labels) pdb.set_trace()
def show_prod(J2, size): h = load_hamiltonian('J1J2', size=size, J2=J2) H = h.get_mat() c0, indexer = h.get_config_table() e0, v0 = sps.linalg.eigsh(H, which='SA', k=1) v0 = v0.ravel() order = np.argsort(abs(v0))[::-1] v0 = v0[order] c0 = c0[order] amp = np.abs(v0) * 100 nn = np.sum(c0 * np.roll(c0, 1, axis=1), axis=1) nnn = np.sum(c0 * np.roll(c0, 2, axis=1), axis=1) hndim = len(c0) #wlist = np.linspace(0,hndim,2000) #nn = dos(np.arange(hndim), wlist=wlist, weights=nn, eta=2.)*hndim plt.ion() plt.plot(np.arange(hndim), amp) plt.fill_between(np.arange(hndim), nn, 0, alpha=0.5, color='r') plt.fill_between(np.arange(hndim), nnn, 0, alpha=0.5, color='g') plt.legend([ '$|\psi|$', r'$\sum_iS^z_i\times S^z_{i+1}$', r'$\sum_iS^z_i\times S^z_{i+2}$' ]) plt.axhline(0, color='#999999') pdb.set_trace() plt.savefig('notes/img/prod_J2%sN%s.png' % (J2, size), dpi=300)
def bverr(self, subfolder, bench_id, extension='png'): '''show energy error function.''' # get configuration and foler configfile = 'benchmarks/%s/config-sample.ini' % subfolder folder = os.path.dirname(configfile) config = load_config(configfile) # modification to parameters sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(config, bench_id) EG = config['hamiltonian']['EG'] if EG is None: h = load_hamiltonian(model='J1J2', J1=1, J2=config['hamiltonian']['J2'], size=config['hamiltonian']['size']) H, EG, v, configs = analyse_exact(h, do_printsign=False, num_eng=1) context = DShow( (5, 3.5), filename="notes/img/ERRL-%s-%s.%s" % (subfolder, bench_id, extension)) if extension is not '-' else contextlib.ExitStack() with context: show_el(datafiles=['%s/el-%i.dat' % (folder, bench_id)], nsite=np.prod(config['hamiltonian']['size']), EG=EG, legends=['id = %s' % bench_id], show_err=True, xlogscale=False)
def ed(self, J2, size, *tasks): h = load_hamiltonian(model='J1J2', J1=1, J2=J2, size=size) H, e, v, configs = analyse_exact(h, do_printsign=False, num_eng=1) if 'v' in tasks: print('v = %s' % v) if 'H' in tasks: print('H = %s' % H) if 'sign' in tasks: np.set_printoptions(threshold='nan') print('sign = %s' % np.sign(v)) np.set_printoptions(threshold=1000)
def run_ed_msr(J2, nsite): from qstate.classifier.rules import marshall_sign_rule h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) H = h.get_mat() e0, v0 = sps.linalg.eigsh(H, which='SA', k=1) v0 = v0.ravel() marshall_signs = marshall_sign_rule(h.configs) plt.ion() scatter_vec_phase(v0[marshall_signs == 1], color='r') scatter_vec_phase(v0[marshall_signs == -1], color='b') plt.legend([r'$+$', r'$-$']) pdb.set_trace()
def scale_ed_msr(size, J2MIN=0, J2MAX=1, NJ2=51, yscale='log'): from qstate.classifier.rules import marshall_sign_rule J2L = np.linspace(J2MIN, J2MAX, NJ2) e0l, el = [], [] for i, J2 in enumerate(J2L): h = load_hamiltonian('J1J2', size=size, J2=J2) H = h.get_mat() e0, v0 = sps.linalg.eigsh(H, which='SA', k=1) v0 = v0.ravel() configs, config_indexer = h.get_config_table() marshall_signs = marshall_sign_rule(configs, size=size) v = abs(v0) * marshall_signs el.append(v.T.conj().dot(H.dot(v))) e0l.append(e0.item()) print('%s' % i) np.savetxt('notes/data/scale_msr_%s.dat' % (size, ), list(zip(el, e0l))) plt.ion() plt.plot(J2L, np.array(el) - e0l) plt.xlabel(r'$J_2$') plt.ylabel(r'$E-E_0$') plt.yscale(yscale) pdb.set_trace()
def analyse_symmetry(J2, size): h = load_hamiltonian('J1J2', size=size, J2=J2) configs, indexer = h.get_config_table() H, e0, v0, configs = analyse_exact(h, do_printsign=False, num_eng=1) rl = [] while True: pos = np.random.choice(h.nsite, h.nsite // 2, replace=False) config = np.ones(h.nsite, dtype='int32') config[pos] *= -1 ind = indexer[packnbits_pm(config)] if abs(v0[ind]) > 1e-5: break print('analysing space inversion symmetry v0[ind] = %s' % v0[ind]) config_ = space_inversion(config, h.size, directions=None) ind_ = indexer[packnbits_pm(config_)] ratio = v0[ind_] / v0[ind] print('ratio = %s' % ratio) rl.append(ratio) print('analysing spin flip symmetry') config_ = -config ind_ = indexer[packnbits_pm(config_)] ratio = v0[ind_] / v0[ind] print('ratio = %s' % ratio) rl.append(ratio) print('analysing translate 1 symmetry') config_ = translate(config, h.size, vec=(1, )) ind_ = indexer[packnbits_pm(config_)] ratio = v0[ind_] / v0[ind] print('ratio = %s' % ratio) rl.append(ratio) pdb.set_trace() return rl
def run_wanglei4(J2, size, optimize_method='adam', momentum=0., do_plot_wf=True, compare_to_exact=True, learning_rate=1e-2): from models.wanglei4 import WangLei4 # definition of a problem h = load_hamiltonian('J1J2', size=size, J2=J2) rbm = WangLei4(input_shape=size, NF=8, K=3, num_features=[8], version='conv', itype='complex128') # visualize network from poornn import viznn viznn(rbm, filename='data/%s.png' % rbm.__class__.__name__) problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='delta', sr_layerwise=False) sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc vmc.inverse_rate = 0.05 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method=optimize_method, step_rate=learning_rate, momentum=momentum) # setup canvas if do_plot_wf: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf: vv = rbm.tovec(mag=h.mag) vv = vv / np.linalg.norm(vv) fig.clear() plt.subplot(121) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) D = 0.8 plt.xlim(-D, D) plt.ylim(-D, D) plt.pause(0.01) vv_pre = vv if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) num_iter = info['n_iter'] #optimizer.step_rate *= 0.995 if num_iter >= 1000: break print('\nRunning %s-th Iteration.' % (num_iter + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def run_rtheta_mlp_exp(J2, nsite, mlp_shape): from models.rtheta_mlp_exp import RTheta_MLP_EXP # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) rbm = RTheta_MLP_EXP(input_shape=(h.nsite, ), num_feature_hidden=4, mlp_shape=mlp_shape, use_msr=False, theta_period=2) #rbm = get_ground_toynn(h, mode='r-theta', train_amp=False, theta_period=nsite) #pdb.set_trace() problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='sd') sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc sr.rtheta_training_ratio = 30 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='adam', step_rate=3e-3, momentum=momentum) # do_plot_wf = True compare_to_exact = True # setup canvas # if do_plot_wf: # plt.ion() # fig=plt.figure(figsize=(10,5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] # if do_plot_wf: # # vv = rbm.tovec(mag=h.mag) # amp = [] # signs = [] # for config in h.configs: # amp.append(rbm.forward(config)) # signs.append(rbm.get_sign(config)) # amp = np.asarray(amp) # amp = amp/np.linalg.norm(amp) # vv = amp*signs # plt.clf() # plt.subplot(121) # compare_wf(amp, v0) # plt.subplot(122) # scatter_vec_phase(vv, vv_pre) # plt.pause(0.1) # vv_pre = vv if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) if info['n_iter'] >= 1000: break print('\nRunning %s-th Iteration.' % (info['n_iter'] + 1)) number = '' for i in mlp_shape: number += str(i) number += '-' np.savetxt('data/rtheta-mlp-exp-%sel-%s%s.dat' % (number, h.nsite, 'p' if h.periodic else 'o'), el, fmt='%.10f%+.10fj')
def run_target_sign(J2, nsite): '''Given Sign train amplitude, the arbituary state version.''' from models.wanglei import WangLei # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) rbm = WangLei(input_shape=(h.nsite, ), version='linear', use_conv=True, itype='complex128') problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='delta', sr_layerwise=True) sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc vmc.inverse_rate = 0.05 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=1e-1, momentum=momentum) do_plot_wf = True compare_to_exact = True # setup canvas if do_plot_wf: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf: vv = rbm.tovec(mag=h.mag) vv = vv / np.linalg.norm(vv) fig.clear() plt.subplot(121) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) plt.xlim(-0.3, 0.3) plt.ylim(-0.3, 0.3) plt.pause(0.01) vv_pre = vv num_iter = info['n_iter'] #optimizer.step_rate *= 0.995 if num_iter >= 200: break print('\nRunning %s-th Iteration.' % (num_iter + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def run_rtheta_switch(J2, nsite, rtheta_training_ratio, switch_step, momentum=0., \ do_plot_wf=True, compare_to_exact=True, do_check_sample=False): from models.wanglei2 import WangLei2 # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J1=1., J2=J2) rbm = WangLei2(input_shape=(h.nsite, ), num_feature_hidden=4, use_msr=False, theta_period=2, with_linear=False, itype='float64') #rbm.thnn = get_exact_thnn4(fixed_var=True) problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='sd') sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc vmc.inverse_rate = 0.05 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=3e-3, momentum=momentum) # setup canvas if do_plot_wf or do_check_sample: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False, num_eng=5) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') sr.rtheta_training_ratio = [rtheta_training_ratio[0], 0] for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf or do_check_sample: amp = [] signs = [] for config in h.configs: amp.append(rbm.forward(config)) signs.append(rbm.get_sign(config)) amp = np.asarray(amp) amp = amp / np.linalg.norm(amp) vv = amp * signs #vv = rbm.tovec(mag=h.mag) if do_plot_wf: fig.clear() plt.subplot(121) #compare_wf(amp, v0) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) plt.xlim(-0.8, 0.8) plt.ylim(-0.8, 0.8) plt.pause(0.01) vv_pre = vv if do_check_sample: fig.clear() check_sample(rbm, h, problem.cache['samples']) plt.pause(0.01) if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) k = info['n_iter'] if k >= 800: break if k % (2 * switch_step) < switch_step: print('\nRunning %s-th Iteration (optimize amplitudes).' % (k + 1)) sr.rtheta_training_ratio = [rtheta_training_ratio[0], 0] else: print('\nRunning %s-th Iteration (optimize signs).' % (k + 1)) sr.rtheta_training_ratio = [0, rtheta_training_ratio[1]] np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def rbm_given_sign(J2, nsite): from models.poorrbm import RBM do_plot_wf = True compare_to_exact = True # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False) rbm = RBM(input_shape=(h.nsite, ), num_feature_hidden=4, itype='float64', sign_func=sign_func_from_vec(h.configs, v0)) problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='delta', sr_layerwise=False) sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc vmc.inverse_rate = 0.05 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=3e-2, momentum=momentum) # setup canvas if do_plot_wf: plt.ion() fig = plt.figure(figsize=(10, 5)) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf: vv = rbm.tovec(mag=h.mag) vv = vv / np.linalg.norm(vv) fig.clear() plt.subplot(121) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) plt.xlim(-0.3, 0.3) plt.ylim(-0.3, 0.3) plt.pause(0.01) vv_pre = vv if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) num_iter = info['n_iter'] #optimizer.step_rate *= 0.995 if num_iter >= 2000: break print('\nRunning %s-th Iteration.' % (num_iter + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def run_rtheta_toy(J2, nsite, version, rtheta_training_ratio, momentum=0.): from models.wanglei2 import WangLei2 from models.toythnn import ToyTHNN h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) if version == '2l': from models.psnn_leo import PSNN thnn = PSNN((nsite, ), nf=16, batch_wise=False, period=2, output_mode='theta') elif version == '1l': from qstate.classifier import PSNN thnn = PSNN((nsite, ), batch_wise=False, period=2, output_mode='theta', use_msr=False) elif version == 'toy': thnn = ToyTHNN(h) # definition of a problem H = h.get_mat() rbm = get_ground_toynn(h, thnn=thnn, train_amp=True, theta_period=2) problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='sd', sr_layerwise=False if version == 'toy' else True) sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc sr.rtheta_training_ratio = rtheta_training_ratio optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=3e-3, momentum=momentum) do_plot_wf = True compare_to_exact = True # setup canvas if do_plot_wf: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf: vv = rbm.tovec(mag=h.mag) vv = vv / np.linalg.norm(vv) plt.clf() plt.subplot(121) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) plt.pause(0.01) vv_pre = vv if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) if info['n_iter'] >= 300: plt.savefig('data/SIGN-N%s-J2%s-%s.png' % (nsite, J2, version)) break print('\nRunning %s-th Iteration.' % (info['n_iter'] + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def run_rtheta(J2, nsite, rtheta_training_ratio, momentum=0.): from models.wanglei2 import WangLei2 # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) rbm = WangLei2(input_shape=(h.nsite, ), num_feature_hidden=4, use_msr=False, theta_period=2, with_linear=False, itype='float64') problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='sd') sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc sr.rtheta_training_ratio = rtheta_training_ratio optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=1e-2, momentum=momentum) do_plot_wf = True compare_to_exact = True do_check_sample = False # setup canvas if do_plot_wf or do_check_sample: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False, num_eng=10) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf or do_check_sample: amps = [] thetas = [] signs = [] for config in h.configs: amps.append(rbm.forward(config)) thetas.append(rbm.thnn.forward(config)) signs.append(np.exp(1j * thetas[-1])) amps = np.asarray(amps) amps = amps / np.linalg.norm(amps) vv = amps * signs #vv = rbm.tovec(mag=h.mag) if do_plot_wf: fig.clear() plt.subplot(121) #compare_wf(amps, v0) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre, winding=np.int32( np.floor(np.array(thetas) / 2 / np.pi))) plt.xlim(-0.8, 0.8) plt.ylim(-0.8, 0.8) plt.pause(0.01) vv_pre = vv if do_check_sample: fig.clear() check_sample(rbm, h, problem.cache['samples']) plt.pause(0.01) if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) if info['n_iter'] >= 800: break print('\nRunning %s-th Iteration.' % (info['n_iter'] + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def get_sign_func(J2, size): # definition of a problem h = load_hamiltonian('J1J2', size=size, J2=J2) # Exact Results H, e0, v0, configs = analyse_exact(h, do_printsign=False) return sign_func_from_vec(configs, v0)