def genHfacSpatialQt(p, nsite, isite, hq, vqrs): iop = 1 isym = 2 status = 'L' isz = p % 2 site = mpo_dmrg_opers.genHfacSpatial(p, nsite, isite, hq, vqrs) # Status qt = qtensor.qtensor([False, True, False, True]) # Site physical indices qu, qd = genQphys(isym, isite) # Orbital dependent part ql1e, qr1e = genElemQnums(p, 2 * isite, iop, status) ql2e, qr2e = genElemQnums(p, 2 * isite + 1, iop, status) ql1 = ql1e qr1 = qr2e ql1w, qr1w = genWfacQnums(nsite, 2 * isite, isz, status) ql2w, qr2w = genWfacQnums(nsite, 2 * isite + 1, isz, status) ql2 = ql1w qr2 = qr2w ql = [q1 + q2 for q1, q2 in itertools.product(ql1, ql2)] qr = [q1 + q2 for q1, q2 in itertools.product(qr1, qr2)] # Combine qt.fromDenseTensor(site, [ql, qr, qu, qd]) return qt
def testHfacSpatial(): # # ta = 29.8947856426 # tb = 34.9535117149 # tc = 27.4478466511 [again for large nsite=100, contraction based is better.] # # p,isz,isite= 1 1 50 # hop= (402, 402, 4, 4) # site0.shape= (402, 402, 4, 4) sum= 763.35358861 # Basic information: # rank = 4 shape= [402 402 4 4] nsyms= [8 8 4 4] # nblks_allowed = 84 nblks = 1024 # size_allowed = 212812 size = 2585664 savings= 0.0823045840449 # site1a.shape= (402, 402, 4, 4) sum= 763.35358861 # isz,isite,diffa= 1 50 0.0 # Basic information: # rank = 4 shape= [402 402 4 4] nsyms= [8 8 4 4] # nblks_allowed = 84 nblks = 1024 # size_allowed = 212812 size = 2585664 savings= 0.0823045840449 # site1b.shape= (402, 402, 4, 4) sum= 763.35358861 # isz,isite,diffb= 1 50 0.0 # isz,isite,diffab= 1 50 0.0 # t1[mat]= 0.183460950851 # t2[Qt ]= 0.20934510231 # t3[Qt0]= 0.174911975861 # equalSym= True # numpy.random.seed(10) nsite = 10 nbas = nsite * 2 hmo = numpy.random.uniform(-1, 1, (nbas, nbas)) hmo[::2, 1::2] = hmo[1::2, ::2] = 0. # [ij|kl] eri = numpy.random.uniform(-1, 1, (nbas, nbas, nbas, nbas)) eri[::2, 1::2] = eri[1::2, ::2] = eri[:, :, ::2, 1::2] = eri[:, :, 1::2, ::2] = 0. # The spin symmetry is essential. # <ij|kl>=[ik|jl] eri = eri.transpose(0, 2, 1, 3) # Antisymmetry is not since only r<s is taken. # # Antisymmetrize V[pqrs]=-1/2*<pq||rs> # eri = -0.5*(eri-eri.transpose(0,1,3,2)) ta = 0. tb = 0. tc = 0. for p in range(nbas): isz = p % 2 hq = hmo[isz] vqrs = eri[isz] for isite in range(nsite): print '\np,isz,isite=', p, isz, isite t0 = time.time() site0 = mpo_dmrg_opers.genHfacSpatial(p, nbas, isite, hq, vqrs) t1 = time.time() print 'hop=', site0.shape print 'site0.shape=', site0.shape, ' sum=', numpy.sum(site0) # Wsite qt12a = genHfacSpatialQt(p, nbas, isite, hq, vqrs) qt12a.prt() site1a = qt12a.toDenseTensor() t2 = time.time() print 'site1a.shape=', site1a.shape, ' sum=', numpy.sum(site1a) assert site0.shape == site1a.shape diffa = numpy.linalg.norm(site0 - site1a) print 'isz,isite,diffa=', isz, isite, diffa t3 = time.time() qt12e = genHfacSpinQt(p, nbas, 2 * isite, hq, vqrs) qt12o = genHfacSpinQt(p, nbas, 2 * isite + 1, hq, vqrs) qt12b = linkTwoOpers(qt12e, qt12o) qt12b.prt() site1b = qt12b.toDenseTensor() t4 = time.time() print 'site1b.shape=', site1b.shape, ' sum=', numpy.sum(site1b) assert site0.shape == site1b.shape diffb = numpy.linalg.norm(site0 - site1b) print 'isz,isite,diffb=', isz, isite, diffb ta += t1 - t0 tb += t2 - t1 tc += t4 - t3 diffab = numpy.linalg.norm(site1a - site1b) print 'isz,isite,diffab=', isz, isite, diffab print ' t1[mat]=', t1 - t0 print ' t2[Qt ]=', t2 - t1 print ' t3[Qt0]=', t4 - t3 assert diffa < 1.e-10 assert diffb < 1.e-10 assert diffab < 1.e-10 print ' equalSym=', qt12a.equalSym(qt12b) assert qt12a.equalSym(qt12b) print print 'ta =', ta print 'tb =', tb print 'tc =', tc return 0
def test_Hfac(): # #------------------------------------------------------------------------ # The large memory cost of Wop*|Psi> [O(K2D2)] requires, for large Dmps # and Dwop, a sequential compression should be implemented as a sweep, # such that the [Wsite*MPSsite] can be avoided partially. #------------------------------------------------------------------------ # # isite,diff= 0 (1, 4, 232) 0.0 t0= 0.0527150630951 t1= 0.0431280136108 # isite,diff= 1 (232, 4, 928) 0.0 t0= 0.0108029842377 t1= 0.050961971283 # isite,diff= 2 (928, 4, 3712) 0.0 t0= 0.146265029907 t1= 0.182390928268 # isite,diff= 3 (3712, 4, 14848) 0.0 t0= 2.27565908432 t1= 1.25626206398 # isite= 4 [14848 4 58870] t1= 3.11030101776 # isite= 5 [ 58870 4 105676] t1= 30.1744351387 # isite= 6 [105676 4 162806] t1= 101.686741114 # isite= 7 [162806 4 76966] t1= 75.6775298119 # isite= 8 [76966 4 29638] t1= 8.44200801849 # isite= 9 [29638 4 9976] t1= 1.69688081741 # isite= 10 [9976 4 3074] t1= 0.343852996826 # isite= 11 [3074 4 870] t1= 0.101953983307 # isite= 12 [870 4 232] t1= 0.044182062149 # isite= 13 [232 4 1] t1= 0.00591492652893 # # LOAD MPS fname = './mps.h5' mps, qnum = mps_io.loadMPS(fname) lmps = [mps, qnum] bdim = [len(x) for x in qnum] print(' bdim = ', bdim) nsite = len(mps) qphys = mpo_dmrg_qphys.initSpatialOrb(nsite, 2) ta = 0. tb = 0. nbas = 2 * nsite # random hmo = numpy.random.uniform(-1, 1, (nbas, nbas)) hmo[::2, 1::2] = hmo[1::2, ::2] = 0. # [ij|kl] eri = numpy.random.uniform(-1, 1, (nbas, nbas, nbas, nbas)) eri[::2, 1::2] = eri[1::2, ::2] = eri[:, :, ::2, 1::2] = eri[:, :, 1::2, ::2] = 0. # The spin symmetry is essential. # <ij|kl>=[ik|jl] eri = eri.transpose(0, 2, 1, 3) maxn = 3 for p in [6, 5]: isz = p % 2 hq = hmo[isz] vqrs = eri[isz] for isite in range(min(nsite, 5)): ql = qnum[isite] qn = qphys[isite] qr = qnum[isite + 1] cl = qtensor_util.classification(ql) cn = qtensor_util.classification(qn) cr = qtensor_util.classification(qr) print() print('isite/nsite=', isite, nsite) site = mps[isite] tmps = qtensor.qtensor([False, False, True]) tmps.fromDenseTensor(site, [ql, qn, qr]) print('mps site info:') tmps.prt() if isite < maxn: t0 = time.time() #op = mpo_dmrg_opers.genWfacSpatial(nbas,isite,hq,vqrs) op = mpo_dmrg_opers.genHfacSpatial(p, nbas, isite, hq, vqrs) print(' wop=', op.shape) #csite = numpy.einsum('lrij,ajb->lairb',op,site) csite = numpy.tensordot(op, site, axes=([3], [1])) # lriab csite = csite.transpose(0, 3, 2, 1, 4) # lriab->lairb s = csite.shape csite = csite.reshape((s[0] * s[1], s[2], s[3] * s[4])) t1 = time.time() print(' t1=', t1 - t0) #qop = qtensor_opers.genWfacSpatialQt(nbas,isite,hq,vqrs,isz) qop = qtensor_opers.genHfacSpatialQt(p, nbas, isite, hq, vqrs) tmps2 = qtensor.tensordot(qop, tmps, axes=([3], [1])) tmps2 = tmps2.transpose(0, 3, 2, 1, 4) tmps2 = tmps2.merge([[0, 1], [2], [3, 4]]) tsite = tmps2.toDenseTensor() t2 = time.time() print(' t2=', t2 - t1) assert csite.shape == tsite.shape diff = numpy.linalg.norm(tsite - csite) print('isite,diff=', isite, csite.shape, diff, ' t0=', t1 - t0, ' t1=', t2 - t1) assert diff < 1.e-10 ta += t1 - t0 tb += t2 - t1 else: if isite == maxn: print('>>> Check internal consistency <<<') t0 = time.time() #qop = qtensor_opers.genWfacSpatialQt(nbas,isite,hq,vqrs,isz) qop = qtensor_opers.genHfacSpatialQt(p, nbas, isite, hq, vqrs) tmps2 = qtensor.tensordot(qop, tmps, axes=([3], [1])) tmps2 = tmps2.transpose(0, 3, 2, 1, 4) tmps2 = tmps2.merge([[0, 1], [2], [3, 4]]) tmps2.prt() t1 = time.time() sum1 = numpy.sum(tmps2.value) print('isite=', isite, tmps.shape, ' t0=', t1 - t0, ' sum=', sum1) tmps2 = None qop = qtensor_opers.genHfacSpatialQt0(p, nbas, isite, hq, vqrs) tmps2 = qtensor.tensordot(qop, tmps, axes=([3], [1])) tmps2 = tmps2.transpose(0, 3, 2, 1, 4) tmps2 = tmps2.merge([[0, 1], [2], [3, 4]]) tmps2.prt() t2 = time.time() sum2 = numpy.sum(tmps2.value) print('isite=', isite, tmps2.shape, ' t1=', t2 - t1, ' sum=', sum2) tmps2 = None diff = abs(sum1 - sum2) print('diff =', diff) assert diff < 1.e-10 print() print('ta=', ta) print('tb=', tb) print() return 0