def eval_vbond(pepsa0, pepsa, i, j, auxbond): debug = False # Sz*Sz pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("pq,qludr->pludr", Sz, pepsa[i, j]) pepsb[i + 1, j] = einsum("pq,qludr->pludr", Sz, pepsa[i + 1, j]) valzz = peps.dot(pepsa0, pepsb, auxbond) if debug: print(i, j), 'valzz=', valzz # Sp*Sm pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("xpq,qludr->plxudr", Sp, pepsa[i, j]).merge_adjpair(2, 3) pepsb[i + 1, j] = einsum("xpq,qludr->pluxdr", Sm, pepsa[i + 1, j]).merge_adjpair(3, 4) valpm = peps.dot(pepsa0, pepsb, auxbond) if debug: print(i, j), 'valpm=', valpm # Sm*Sp pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("xpq,qludr->plxudr", Sm, pepsa[i, j]).merge_adjpair(2, 3) pepsb[i + 1, j] = einsum("xpq,qludr->pluxdr", Sp, pepsa[i + 1, j]).merge_adjpair(3, 4) valmp = peps.dot(pepsa0, pepsb, auxbond) if debug: print(i, j), 'valmp=', valmp return valzz + 0.5 * (valpm + valmp)
def test_conf(): np.random.seed(5) nr = 3 nc = 3 pdim = 2 bond = 2 auxbond = 4 # Initialization: # [[0 1 0 1] # [1 0 1 0] # [0 1 0 1] # [1 0 1 0] # [0 1 0 1] # [1 0 1 0]] configa = np.zeros([nr, nc], dtype=np.int) for i in range(nr): for j in range(nc): configa[i, j] = (i + j) % 2 pepsa = peps.create(pdim, configa) #for i in range(nr): # for j in range(nc): # print '(i,j)=',(i,j) # pepsa[i,j].prt() print "CEVAL =", peps.ceval(pepsa, configa, auxbond) PP = peps.dot(pepsa, pepsa, None) print "<P|P> =", PP Sp = peps_h.get_Sp() Sm = peps_h.get_Sm() Sz = peps_h.get_Sz() i = j = 0 # Sm*Sp pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("xpq,qludr->plxudr", Sm, pepsa[i, j]).merge_adjpair(2, 3) pepsb[i, j].prt() pepsb[i, j + 1] = einsum("xpq,qludr->pluxdr", Sp, pepsa[i, j + 1]).merge_adjpair(3, 4) valmp = peps.dot(pepsb, pepsa, auxbond) print 'valmp=', valmp # Sz*Sz pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("pq,qludr->pludr", Sz, pepsa[i, j]) pepsb[i, j + 1] = einsum("pq,qludr->pludr", Sz, pepsa[i, j + 1]) valzz = peps.dot(pepsb, pepsa, auxbond) print 'valzz=', valzz # Sp*Sm pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("xpq,qludr->plxudr", Sp, pepsa[i, j]).merge_adjpair(2, 3) pepsb[i, j + 1] = einsum("xpq,qludr->pluxdr", Sm, pepsa[i, j + 1]).merge_adjpair(3, 4) valpm = peps.dot(pepsb, pepsa, auxbond) print 'valpm=', valpm return 0
def eval_vbond(pepsa0, pepsa, i, j, auxbond): pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("pq,qludr->pludr", Sz, pepsa[i, j]) pepsb[i + 1, j] = einsum("pq,qludr->pludr", Sz, pepsa[i + 1, j]) valzz = peps.dot(pepsa0, pepsb, auxbond) pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("pq,qludr->pludr", Sp, pepsa[i, j]) pepsb[i + 1, j] = einsum("pq,qludr->pludr", Sm, pepsa[i + 1, j]) valpm = peps.dot(pepsa0, pepsb, auxbond) pepsb = peps.copy(pepsa) pepsb[i, j] = einsum("pq,qludr->pludr", Sm, pepsa[i, j]) pepsb[i + 1, j] = einsum("pq,qludr->pludr", Sp, pepsa[i + 1, j]) valmp = peps.dot(pepsa0, pepsb, auxbond) return valzz + .5 * (valpm + valmp)
def energy_fn(vec, pdim, bond): P = peps.aspeps(vec, (nr, nc), pdim, bond) PHP = peps_h.eval_heish(P, P, auxbond) PP = peps.dot(P, P, auxbond) e = PHP / PP print ' PHP,PP,PHP/PP,eav=', PHP, PP, e, e / (nr * nc) return PHP / PP
def test(): pdim = 2 bond = 2 numpy.random.seed(5) nr = 4 nc = 4 configs = spins.configs(nr * nc, pdim) zpeps = peps.random([nr, nc], pdim, bond) print zpeps for i in range(nr): for j in range(nc): print np.linalg.norm(zpeps[i, j]) #auxbond = 2 # for config in configs: # print config # cpeps = peps.cpeps(zpeps,config) # print "norms", np.linalg.norm(cpeps[0,0]), np.linalg.norm(cpeps[0,1]), np.linalg.norm(cpeps[1,0]), np.linalg.norm(cpeps[1,1]) # print peps.ceval(zpeps,config,auxbond) php = peps_h.eval_heish(zpeps, None) pp = peps.dot(zpeps, zpeps, None) print pp print numpy.random.random([1]) print "Expectation value", php, pp, php / pp
def test_basic(vec, nr, nc, pdim, bond): P = peps.aspeps(vec, (nr, nc), pdim, bond) # Initialization auxbond = 16 configa = np.zeros([nr, nc], dtype=np.int) configb = np.zeros([nr, nc], dtype=np.int) for i in range(nr): for j in range(nc): configa[i, j] = (i + j) % 2 configb[i, j] = (i + j + 1) % 2 assert np.sum(configa) % 2 == 0 assert np.sum(configb) % 2 == 0 x = mc_helper.fromConf2Bits(configa) print configa print x, bin(x) conf = mc_helper.fromBits2Conf(x, nr, nc) print conf print configb = configa.copy() configb[1, 1] = 1 configb[1, 2] = 0 y = mc_helper.fromConf2Bits(configb) print configb print y, bin(y) Pa = peps.create(pdim, configa) Pb = peps.create(pdim, configb) print '<Pa|P>=', peps.dot(Pa, P, auxbond) print '<Pb|P>=', peps.dot(Pb, P, auxbond) print print ' configa=\n', configa print ' configb=\n', configb #peps_h.eval_hbond(Pa, Pb, 1, 1, auxbond) Hab = peps_h.eval_heish(Pa, Pb, auxbond) print ' Hab=', Hab assert abs(Hab - 0.5) < 1.e-10 Haa = peps_h.eval_heish(Pa, Pa, auxbond) print ' Haa=', Haa assert abs(Haa + 6.0) < 1.e-10 Hbb = peps_h.eval_heish(Pb, Pb, auxbond) print ' Hbb=', Hbb assert abs(Hbb + 3.0) < 1.e-10 return 0
def genWs(by): cy = mc_helper.fromBits2Conf(by, nr, nc) py = peps.create(pdim, cy) wy = peps.dot(py, P, auxbond) return wy
def energy_fn(vec, pdim, bond): P = peps.aspeps(vec, (nr, nc), pdim, bond) PHP = peps_h.eval_heish(P, auxbond) PP = peps.dot(P, P, auxbond) print PHP, PP, PHP / PP return PHP / PP
def test_min(): numpy.random.seed(5) nr = 6 nc = 4 auxbond = 8 def energy_fn(vec, pdim, bond): P = peps.aspeps(vec, (nr, nc), pdim, bond) PHP = peps_h.eval_heish(P, auxbond) PP = peps.dot(P, P, auxbond) print PHP, PP, PHP / PP return PHP / PP afma = np.zeros([nr * nc]) configa = np.zeros([nr, nc], dtype=np.int) configb = np.zeros([nr, nc], dtype=np.int) for i in range(nr): for j in range(nc): configa[i, j] = (i + j) % 2 configb[i, j] = (i + j + 1) % 2 #print afma #print afmb pdim = 2 pepsa = peps.create((nr, nc), pdim, configa) pepsb = peps.create((nr, nc), pdim, configb) print peps.dot(pepsa, pepsa, None) print peps.dot(pepsb, pepsb, None) print peps.dot(pepsa, pepsb, None) PHP = peps_h.eval_heish(pepsa, None) #print "config", configa[0,0], configa[0,1] print "PHP energy", PHP print "PP", peps.dot(pepsa, pepsa, None) # flata = peps.flatten(pepsa) # pepsA = peps.aspeps(flata, (nr,nc), pdim, bond=1) # print "reconstituted dot", peps.dot(pepsA,pepsa,None) # print pepsa # print pepsA print "A energy", energy_fn(peps.flatten(pepsa), pdim, bond=1) print "B energy", energy_fn(peps.flatten(pepsb), pdim, bond=1) print "---start opt-----" pdim = 2 bond = 2 peps0 = peps.add(pepsa, pepsb) # this has bond=2 pepsc = peps.zeros(pepsa.shape, pdim, bond - 2) # add some zeros to make up full bond peps0 = peps.add(peps0, pepsc) peps0 = add_noise(peps0, 0.1) print energy_fn(peps.flatten(peps0), pdim, bond) #vec = 0.3*np.random.random(nparam) vec = peps.flatten(peps0) #0.3*np.random.random(nparam) def bound_energy_fn(vec): return energy_fn(vec, pdim, bond) deriv = autograd.grad(bound_energy_fn) d = deriv(vec) print bound_energy_fn(vec) result = scipy.optimize.minimize(bound_energy_fn, jac=deriv, x0=vec) print "max value", np.max(result.x) P0 = peps.aspeps(result.x, (nr, nc), pdim, bond)