Example #1
0
def test_model2():
    J, Jz = -3., 2
    N1, N2 = 2, 3
    scfg = SpinSpaceConfig([N1 * N2, 2])
    config = array([1, 1, 0, 1, 1, 0])
    #config=array([1,1,0,0,1,0,1,1,0])
    #construct true H
    for periodic in [True, False]:
        h = HeisenbergH2D(N1, N2, J=J, Jz=Jz, periodic=periodic)
        H = FakeVMC(h).get_H()
        print 'Testing rmatmul of Hamiltonian(%s)' % ('PBC'
                                                      if periodic else 'OBC')
        wl, flips = h._rmatmul(1 - 2 * config)
        configs = []
        for flip in flips:
            nc = copy(1 - 2 * config)
            nc[asarray(flip)] *= -1
            configs.append(nc)
        ss = SparseState(wl, configs)

        vec = ss.tovec(scfg)
        v0 = zeros(scfg.hndim)
        v0[scfg.config2ind(config)] = 1
        v_true = v0.dot(H)
        assert_allclose(vec, v_true)
Example #2
0
 def get_Ising(self, h, J=1.):
     '''Get the hamiltonian.'''
     #the hamiltonian for Ising model
     Sz = opunit_Sz(spaceconfig=SpinSpaceConfig([1, 2]))
     Sx = opunit_Sx(spaceconfig=SpinSpaceConfig([1, 2]))
     H = -J * Sz.as_site(0) * Sz.as_site(1) + h * Sx.as_site(0)
     return H
Example #3
0
    def __init__(self, J, Jz, h, nsite, J2=0, J2z=None):
        self.spaceconfig = SpinSpaceConfig([1, 2])
        I = OpUnitI(hndim=2)
        scfg = SpinSpaceConfig([1, 2])
        Sx = opunit_Sx(spaceconfig=scfg)
        Sy = opunit_Sy(spaceconfig=scfg)
        Sz = opunit_Sz(spaceconfig=scfg)

        #with second nearest hopping terms.
        if J2 == 0:
            self.H_serial2 = None
            return
        elif J2z == None:
            J2z = J2
        SL = []
        for i in xrange(nsite):
            Sxi, Syi, Szi = copy.copy(Sx), copy.copy(Sy), copy.copy(Sz)
            Sxi.siteindex = i
            Syi.siteindex = i
            Szi.siteindex = i
            SL.append(array([Sxi, Syi, sqrt(J2z / J2) * Szi]))
        ops = []
        for i in xrange(nsite - 1):
            ops.append(J * SL[i].dot(SL[i + 1]))
            if i < nsite - 2 and J2z != 0:
                ops.append(J2 * SL[i].dot(SL[i + 2]))

        mpc = sum(ops)
        self.H_serial = mpc
Example #4
0
    def test_tovec(self):
        print 'Test @RBM.tovec'
        scfg = SpinSpaceConfig([2, 2])
        configs = scfg.ind2config(arange(scfg.hndim))
        vec = self.rbm.get_weight(1 - 2 * configs)
        assert_allclose(vec, self.vec, atol=1e-8)

        print 'Test Translational invariant @RBM.tovec.'
        vect = self.rbm_t.tovec(scfg)
        assert_allclose(vect, self.vec2, atol=1e-8)
Example #5
0
 def get_Haldane(self, D, J=1.):
     '''Get the hamiltonian.'''
     #the hamiltonian for Ising model
     Sx = opunit_Sx(spaceconfig=SpinSpaceConfig([1, 3]))
     Sy = opunit_Sy(spaceconfig=SpinSpaceConfig([1, 3]))
     Sz = opunit_Sz(spaceconfig=SpinSpaceConfig([1, 3]))
     H = J * (
         Sz.as_site(0) * Sz.as_site(1) + Sx.as_site(0) * Sx.as_site(1) +
         Sy.as_site(0) * Sy.as_site(1)) + D * Sz.as_site(0) * Sz.as_site(0)
     return H
Example #6
0
 def __init__(self, J, Jz, h, nsite):
     self.spaceconfig = SpinSpaceConfig([1, 2])
     I = OpUnitI(hndim=2)
     scfg = SpinSpaceConfig([1, 2])
     Sz = opunit_Sz(spaceconfig=scfg)
     Sp = opunit_Sp(spaceconfig=scfg)
     Sm = opunit_Sm(spaceconfig=scfg)
     wi = zeros((5, 5), dtype='O')
     wi[:, 0], wi[4, 1:] = (I, Sp, Sm, Sz,
                            -h * Sz), (J / 2. * Sm, J / 2. * Sp, Jz * Sz, I)
     WL = [copy.deepcopy(wi) for i in xrange(nsite)]
     WL[0] = WL[0][4:]
     WL[-1] = WL[-1][:, :1]
     self.H_serial = WL2OPC(WL)
Example #7
0
    def __init__(self,J,Jz,h,nsite):
        self.spaceconfig=SpinSpaceConfig([2,1])
        I=OpUnitI(hndim=2)
        Sx=opunit_Sx(spaceconfig=self.spaceconfig)
        Sy=opunit_Sy(spaceconfig=self.spaceconfig)
        Sz=opunit_Sz(spaceconfig=self.spaceconfig)

        SL,SZ=[],[]
        for i in xrange(nsite):
            Sxi,Syi,Szi=copy.copy(Sx),copy.copy(Sy),copy.copy(Sz)
            Sxi.siteindex=i
            Syi.siteindex=i
            Szi.siteindex=i
            SL.append(array([Sxi,Syi,sqrt(Jz/J)*Szi]))
            SZ.append(Szi)
        ops=[]
        for i in xrange(nsite):
            if i!=nsite-1:
                ops.append(J*SL[i].dot(SL[i+1]))
            ops.append(h[i]*SZ[i])

        mpc=sum(ops)
        self.H_serial=mpc
        mpo=mpc.toMPO(method='direct')
        mpo.compress()
        self.H=mpo
Example #8
0
    def __init__(self,J1,J2,K,Jp,nsite,impurity_site=None):
        self.spaceconfig=SpinSpaceConfig([2,1])
        if impurity_site is None:
            impurity_site=nsite/2  #nsite/2 and nsite/2-1
        self.impurity_site=impurity_site
        self.J1,self.J2,self.K,self.Jp=J1,J2,K,Jp
        I=OpUnitI(hndim=self.spaceconfig.hndim)
        Sx=opunit_Sx(spaceconfig=self.spaceconfig)
        Sy=opunit_Sy(spaceconfig=self.spaceconfig)
        Sz=opunit_Sz(spaceconfig=self.spaceconfig)
        SL=[]
        for i in xrange(nsite):
            Sxi,Syi,Szi=copy.deepcopy(Sx),copy.deepcopy(Sy),copy.deepcopy(Sz)
            Sxi.siteindex=i
            Syi.siteindex=i
            Szi.siteindex=i
            SL.append(array([Sxi,Syi,Szi]))
        ops=[]
        for i in xrange(nsite-1):
            factor1=J1
            if i==impurity_site or i==impurity_site-2:
                factor1*=Jp
            elif i==impurity_site-1:
                factor1*=K
            ops.append(factor1*SL[i].dot(SL[i+1]))
            if i<nsite-2 and i!=impurity_site-1 and i!=impurity_site-2:
                factor2=J2
                if i==impurity_site-3 or i==impurity_site:
                    factor2*=Jp
                ops.append(factor2*SL[i].dot(SL[i+2]))

        mpc=sum(ops)
        self.H_serial=mpc
Example #9
0
def solve1(hl,nsite,config,Jz=1.,J=1.,save=True):
    '''
    Run vMPS for Heisenberg model.
    '''
    #generate the model
    print 'SOLVING %s'%config
    model=HeisenbergModel(J=J,Jz=Jz,h=hl,nsite=nsite)

    #run vmps
    #generate a random mps as initial vector
    spaceconfig=SpinSpaceConfig([2,1])
    bmg=SimpleBMG(spaceconfig=spaceconfig,qstring='M')
    H=model.H.use_bm(bmg)
    k0=product_state(config=config,hndim=2,bmg=bmg)

    #setting up the engine
    vegn=VMPSEngine(H=H,k0=k0.toket(),eigen_solver='JD')
    eng,ket=vegn.run(endpoint=(6,'->',0),maxN=20,which='SL',nsite_update=2)
    #save to file
    if save:
        ind=sum(2**arange(nsite-1,-1,-1)*asarray(config))
        filename='data/ket%s_h%.2fN%s.dat'%(ind,mean(hl),nsite)
        quicksave(filename,(eng,ket))
    overlap=abs((k0.tobra()*ket).item())
    print 'OVERLAP %s'%overlap
    return eng,ket,overlap
Example #10
0
def measure_op(which,ket,usempi=False,ofile=None):
    '''
    Get order parameter from ground states.

    Parameters:
        :which: str, 

            * 'Sz', <Sz>
        :siteindices: list, the measure points.

    Return:
        array, the value of order parameter.
    '''
    nsite=ket.nsite
    spaceconfig=SpinSpaceConfig([2,1])
    ml=[]
    def measure1(siteindex):
        if which=='Sz':
            op=opunit_S(which='z',spaceconfig=spaceconfig,siteindex=siteindex)
        else:
            raise NotImplementedError()
        m=get_expect(op,ket=ket).item() #,bra=ket.tobra(labels=ket.labels))
        print 'Get %s for site %s = %s @RANK: %s'%(which,siteindex,m,RANK)
        return m

    if usempi:
        ml=mpido(measure1,inputlist=siteindices)
    else:
        ml=[]
        for siteindex in arange(nsite):
            ml.append(measure1(siteindex))
    ml=array(ml)
    if RANK==0 and ofile is not None:
        savetxt(ofile,ml.real)
    return ml
Example #11
0
    def __init__(self):
        nsite = 6  #number of sites
        hndim = 2
        l = 0
        vec = random.random(hndim**nsite)  #a random state in form of 1D array.
        vec2 = random.random(
            hndim**nsite)  #a random state in form of 1D array.

        mps = state2MPS(vec, sitedim=hndim, l=l,
                        method='svd')  #parse the state into a <MPS> instance.
        mps2 = state2MPS(vec2, sitedim=hndim, l=l,
                         method='svd')  #parse the state into a <MPS> instance.

        j1, j2 = 0.5, 0.2
        scfg = SpinSpaceConfig([1, 2])
        I = OpUnitI(hndim=hndim)
        Sz = opunit_Sz(spaceconfig=scfg)
        Sp = opunit_Sp(spaceconfig=scfg)
        Sm = opunit_Sm(spaceconfig=scfg)
        wi = zeros((4, 4), dtype='O')
        wi[0, 0], wi[1, 0], wi[2, 1], wi[3,
                                         1:] = I, Sz, I, (j1 * Sz, j2 * Sz, I)
        WL = [deepcopy(wi) for i in xrange(nsite)]
        WL[0] = WL[0][3:4]
        WL[-1] = WL[-1][:, :1]
        mpo = WL2MPO(WL)

        self.mps, self.mps2 = mps, mps2
        self.mpo = mpo
        self.vec, self.vec2 = vec, vec2
        self.spaceconfig = scfg
Example #12
0
    def test_Haldane(self):
        '''Solve model hamiltonians'''
        egn = ITEBDEngine(tol=1e-10)
        npart = 2
        Dlist = arange(0, -0.6, -0.05)
        spaceconfig = SpinSpaceConfig([1, 3])
        #the initial state
        GL = []
        for i in xrange(npart):
            Gi = (0.5 - random.rand(1, 3, 1)) / 100.
            Gi[0, 2 * (i % 2), 0] = 1
            GL.append(Gi)
        LL = []
        for i in xrange(npart):
            LL.append(ones([1]))

        ivmps0 = IVMPS(GL=GL, LL=LL)
        mpsl, HL, EEL = [], [], []
        for D in Dlist:
            H = self.get_Haldane(J=1., D=D)
            mps = egn.run(hs=H,
                          ivmps=copy.copy(ivmps0),
                          spaceconfig=spaceconfig,
                          maxN=20)
            mpsl.append(mps)
            HL.append(H)
            print D, mps.LL[0][:4]

        SL, EL, ML1, ML2 = [], [], [], []
        for mps, H in zip(mpsl, HL):
            mps2 = mps.roll(1)
            SL.append(mean(entanglement_entropy(mps)))
            print get_expect_ivmps(op=H, ket=mps), get_expect_ivmps(op=H,
                                                                    ket=mps2)
            EL.append(
                mean([
                    get_expect_ivmps(op=H, ket=mps),
                    get_expect_ivmps(op=H, ket=mps2)
                ]))
            ML1.append(
                get_expect_ivmps(
                    op=2 *
                    opunit_S(which='z', siteindex=0, spaceconfig=spaceconfig),
                    ket=mps))
            ML2.append(
                get_expect_ivmps(
                    op=2 *
                    opunit_S(which='z', siteindex=0, spaceconfig=spaceconfig),
                    ket=mps2))

        ion()
        subplot(311)
        plot(Dlist, SL)
        subplot(312)
        plot(Dlist, array(EL))
        subplot(313)
        plot(Dlist, array(ML1))
        plot(Dlist, array(ML2))
        pdb.set_trace()
Example #13
0
 def test_lanczos(self):
     '''test for directly construct and solve the ground state energy.'''
     model = self.get_model(10, 1)
     hgen1 = ExpandGenerator(spaceconfig=SpinSpaceConfig([1, 2]),
                             H=model.H_serial,
                             evolutor_type='null')
     hgen2 = ExpandGenerator(spaceconfig=SpinSpaceConfig([1, 2]),
                             H=model.H_serial,
                             evolutor_type='normal')
     dmrgegn = DMRGEngine(hgen=hgen1, tol=0, iprint=10)
     H = get_H(hgen=hgen1)
     H2, bm2 = get_H_bm(hgen=hgen2, bstr='M')
     Emin = eigsh(H, k=1)[0]
     Emin2 = eigsh(bm2.extract_block(H2, (zeros(1), zeros(1)),
                                     uselabel=True),
                   k=1)[0]
     print 'The Ground State Energy is %s, tolerence %s.' % (Emin,
                                                             Emin - Emin2)
     assert_almost_equal(Emin, Emin2)
Example #14
0
 def test_dmrg_finite(self):
     '''
     Run iDMFT for heisenberg model.
     '''
     nsite = 10
     model = self.get_model(nsite, 1)
     hgen1 = ExpandGenerator(spaceconfig=SpinSpaceConfig([1, 2]),
                             H=model.H_serial,
                             evolutor_type='null')
     hgen2 = ExpandGenerator(spaceconfig=SpinSpaceConfig([1, 2]),
                             H=model.H_serial,
                             evolutor_type='masked')
     H = get_H(hgen1)
     EG1 = eigsh(H, k=1, which='SA')[0]
     dmrgegn = DMRGEngine(hgen=hgen2, tol=0, reflect=True)
     dmrgegn.use_U1_symmetry('M', target_block=zeros(1))
     EG2 = dmrgegn.run_finite(endpoint=(5, '<-', 0),
                              maxN=[10, 20, 40, 40, 40],
                              tol=0)[0]
     assert_almost_equal(EG1, EG2, decimal=4)
Example #15
0
 def test_dmrg_infinite(self):
     '''test for infinite dmrg.'''
     maxiter = 100
     model = self.get_model(maxiter + 2, 1)
     hgen = ExpandGenerator(spaceconfig=SpinSpaceConfig([1, 2]),
                            H=model.H_serial,
                            evolutor_type='masked')
     dmrgegn = DMRGEngine(hgen=hgen, tol=0, reflect=True, iprint=10)
     dmrgegn.use_U1_symmetry('M', target_block=zeros(1))
     EG = dmrgegn.run_infinite(maxiter=maxiter, maxN=20, tol=0)[0]
     assert_almost_equal(EG, 0.25 - log(2), decimal=2)
Example #16
0
    def __init__(self):
        J = 1.
        nsite = 4
        self.scfg = SpinSpaceConfig([nsite, 2])

        #construct true H
        I2, I4 = eye(2), eye(4)
        h2 = J / 4. * (kron(sx, sx) + kron(sz, sz) + kron(sy, sy))

        #construct operator H act on config
        self.h1 = TFI(nsite=4)
        self.h2 = HeisenbergH(nsite=4)
        self.toy1, self.toy2 = FakeVMC(self.h1), FakeVMC(self.h2)
        self.H1, self.H2 = self.toy1.get_H(), self.toy2.get_H()
        self.pW = PartialW()

        #generate a random rbm and the corresponding vector v
        self.rbm = random_rbm(nin=nsite, nhid=nsite)
        self.v = self.rbm.tovec(self.scfg)

        self.rbm_g = random_rbm(nin=nsite, nhid=nsite, group=TIGroup(4))
        self.v_g = self.rbm_g.tovec(self.scfg)
Example #17
0
def solve_ed(hl,nsite,Jz=1.,J=1.):
    '''
    Run ED for Heisenberg model.
    '''
    #generate the model
    model=HeisenbergModel(J=J,Jz=Jz,h=hl,nsite=nsite)

    #run vmps
    #generate a random mps as initial vector
    spaceconfig=SpinSpaceConfig([2,1])
    bmg=SimpleBMG(spaceconfig=spaceconfig,qstring='M')
    H=model.H_serial.H().real
    E,U=eigh(H)
    return E,U
Example #18
0
 def __init__(self, g, h, nsite, J=1):
     self.spaceconfig = SpinSpaceConfig([2, 1])
     self.J, self.g, self.h, self.nsite = J, g, h, nsite
     scfg = self.spaceconfig
     I = OpUnitI(hndim=scfg.hndim)
     Sx = opunit_Sx(spaceconfig=scfg) * 2
     Sz = opunit_Sz(spaceconfig=scfg) * 2
     opc = 0
     for i in range(nsite):
         hi = h - J if i == 0 or i == nsite - 1 else h
         opc = opc + (hi * Sz.as_site(i) + g * Sx.as_site(i))
         if i != nsite - 1:
             opc = opc + J * Sz.as_site(i) * Sz.as_site(i + 1)
     self.opc = opc
Example #19
0
    def get_H(self):
        '''Get the target Hamiltonian Matrix.'''
        nsite,periodic=self.h.nsite,self.h.periodic
        scfg=SpinSpaceConfig([nsite,2])
        if isinstance(self.h,TFI):
            Jz,h=self.h.Jz,self.h.h
            h2=Jz/4.*kron(sz,sz)
            H=0
            for i in xrange(nsite):
                if i!=nsite-1:
                    H=H+kron(kron(eye(2**i),h2),eye(2**(nsite-2-i)))
                elif periodic: #periodic boundary
                    H=H+Jz/4.*kron(kron(sz,eye(2**(nsite-2))),sz)
                H=H+h/2.*kron(kron(eye(2**i),sx),eye(2**(nsite-i-1)))
            return H
        elif isinstance(self.h,HeisenbergH):
            J,Jz=self.h.J,self.h.Jz
            h2=J/4.*(kron(sx,sx)+kron(sy,sy))+Jz/4.*kron(sz,sz)
            H=0
            for i in xrange(nsite-1):
                H=H+kron(kron(eye(2**i),h2),eye(2**(nsite-2-i)))

            #impose periodic boundary
            if periodic:
                H=H+J/4.*(kron(kron(sx,eye(2**(nsite-2))),sx)+kron(kron(sy,eye(2**(nsite-2))),sy))+Jz/4.*(kron(kron(sz,eye(2**(nsite-2))),sz))
            return H
        elif isinstance(self.h,HeisenbergH2D):
            from tba.lattice import Square_Lattice
            J,Jz=self.h.J,self.h.Jz
            lattice=Square_Lattice(N=(self.h.N1,self.h.N2))
            if self.h.periodic: lattice.set_periodic([True,True])
            lattice.initbonds(5)
            b1s=lattice.getbonds(1)
            b1s=[b for b in b1s if b.atom1<b.atom2]
            H=0
            if False:
                sx0=csr_matrix(sx)
                sy0=csr_matrix(sy)
                sz0=csr_matrix(sz)
            sx0,sy0,sz0=sx,sy,sz
            for b in b1s:
                for ss,Ji in zip([sx0,sy0,sz0],[J,J,Jz]):
                    H=H+Ji/4.*kron(kron(kron(kron(eye(2**b.atom1),ss),eye(2**(b.atom2-b.atom1-1))),ss),eye(2**(lattice.nsite-b.atom2-1)))
                    #H=H+Ji/4.*kron(kron(kron(kron(eye(2**(lattice.nsite-b.atom2-1)),ss),eye(2**(b.atom2-b.atom1-1))),ss),eye(2**(b.atom1)))
            #e,v=eigsh(H,k=1,which='SA')
            #print e/lattice.nsite
            return H
Example #20
0
    def test_ising(self):
        '''Solve model hamiltonians'''
        npart = 2
        hlist = arange(0., 2, 0.1)
        spaceconfig = SpinSpaceConfig([1, 2])
        #the initial state
        GL = []
        for i in xrange(npart):
            Gi = Tensor(0.5 - 0.01 * ones([spaceconfig.hndim, 1, 1]),
                        labels=['s%s' % i, 'a%s' % i,
                                'b%s' % i])
            Gi[0, 0, 0] = 1
            GL.append(Gi)
        GL[1].labels[1:] = GL[1].labels[1:][::-1]
        #print GL[0].labels
        #print GL[1].labels
        LL = [Link(['b0', 'b1'], ones([1])), Link(['a1', 'a0'], ones([1]))]

        mpsl, EEL, HL = [], [], []
        for h in hlist:
            hb = self.get_Ising(h=2 * h, J=4.)
            egn = ITEBDEngine(hs=[hb] * 2, tol=1e-10)
            ivmps0 = IVMPS(tensors=GL, LL=LL)
            mps = egn.run(ivmps=ivmps0, maxN=5)
            mpsl.append(mps)
            HL.append(hb)

            ################# Get the exact energies ######################
            f = lambda k, h: -2 * sqrt(1 + h**2 - 2 * h * cos(k)) / pi / 2.
            E0_exact = integrate.quad(f, 0, pi, args=(h, ))[0]
            EEL.append(E0_exact)

        SL, EL, ML = [], [], []
        for mps, H in zip(mpsl, HL):
            SL.append(entanglement_entropy(mps))
            #print get_expect_ivmps(op=H,ket=mps),get_expect_ivmps(op=H,ket=mps2)
            #EL.append(mean([get_expect_ivmps(op=H,ket=mps),get_expect_ivmps(op=H,ket=mps2)]))
            #ML.append(get_expect_ivmps(op=2*opunit_S(which='z',siteindex=0,spaceconfig=spaceconfig),ket=mps))

        ion()
        subplot(311)
        #plot(hlist,abs(array(ML)))
        subplot(312)
        plot(hlist, SL)
        subplot(313)
        #plot(hlist,abs(array(EL)-EEL))
        pdb.set_trace()
Example #21
0
 def __init__(self,J,Jz,h,nsite,nspin=3):
     self.spaceconfig=SpinSpaceConfig([1,nspin])
     scfg=self.spaceconfig
     I=OpUnitI(hndim=scfg.hndim)
     Sx=opunit_Sx(spaceconfig=scfg)
     Sy=opunit_Sy(spaceconfig=scfg)
     Sz=opunit_Sz(spaceconfig=scfg)
     Sp=opunit_Sp(spaceconfig=scfg)
     Sm=opunit_Sm(spaceconfig=scfg)
     wi=zeros((5,5),dtype='O')
     #wi[:,0],wi[4,1:]=(I,Sp,Sm,Sz,-h*Sz),(J/2.*Sm,J/2.*Sp,Jz*Sz,I)
     wi[:,0],wi[4,1:]=(I,Sx,Sy,Sz,-h*Sz),(J*Sx,J*Sy,Jz*Sz,I)
     WL=[copy.deepcopy(wi) for i in xrange(nsite)]
     WL[0]=WL[0][4:]
     WL[-1]=WL[-1][:,:1]
     self.H=WL2MPO(WL)
     mpc=WL2OPC(WL)
     self.H_serial=mpc
Example #22
0
    def __init__(self,nsite,periodic,model='AFH'):
        self.nsite,self.periodic=nsite,periodic
        self.model=model
        if model=='AFH':
            self.h=HeisenbergH(nsite=nsite,J=-1.,Jz=1.,periodic=periodic)
        elif model=='AFH2D':
            N=int(sqrt(nsite))
            self.h=HeisenbergH2D(N,N,J=-1.,Jz=1.,periodic=periodic)
        elif model=='TFI':
            self.h=TFI(nsite=nsite,Jz=-4.,h=-1.,periodic=periodic)
        else:
            raise ValueError()
        self.scfg=SpinSpaceConfig([nsite,2])

        #vmc config
        c0=[-1,1]*(nsite/2); random.shuffle(c0)
        cgen=RBMConfigGenerator(initial_config=c0,nflip=2 if model[:3]=='AFH' else 1)#repeat([-1,1],(nsite/2)))
        self.vmc=VMC(cgen,nbath=500*nsite,nsample=5000*nsite,nmeasure=nsite,sampling_method='metropolis')
Example #23
0
    def __init__(self,nsite,periodic,model='AFH'):
        self.nsite,self.periodic=nsite,periodic
        self.model=model
        if model=='AFH':
            self.h=HeisenbergH(nsite=nsite,J=1.,Jz=1.,periodic=periodic)
        elif model=='TFI':
            self.h=TFI(nsite=nsite,Jz=-4.,h=-1.,periodic=periodic)
        elif model=='AFH2D':
            N1=N2=int(sqrt(nsite))
            self.h=HeisenbergH2D(N1,N2,J=1.,Jz=1.,periodic=periodic)
        else:
            raise ValueError()
        self.scfg=SpinSpaceConfig([nsite,2])
        self.fv=FakeVMC(self.h)

        #vmc config
        cgen=RBMConfigGenerator(initial_config=[-1,1]*(nsite/2)+[1]*(nsite%2),nflip=2 if model=='AFH' else 1)
        self.vmc=VMC(cgen,nbath=500*nsite,nsample=5000*nsite,nmeasure=nsite,sampling_method='metropolis')
Example #24
0
 def __init__(self,J,Jz,h,N1,N2,nspin=3):
     self.spaceconfig=SpinSpaceConfig([1,nspin])
     scfg=self.spaceconfig
     I=OpUnitI(hndim=scfg.hndim)
     Sx=opunit_Sx(spaceconfig=scfg)
     Sy=opunit_Sy(spaceconfig=scfg)
     Sz=opunit_Sz(spaceconfig=scfg)
     Sp=opunit_Sp(spaceconfig=scfg)
     Sm=opunit_Sm(spaceconfig=scfg)
     opc=OpCollection()
     lt=Square_Lattice((N1,N2))
     lt.set_periodic([True,True])
     lt.initbonds(5)
     bonds=[b for b in lt.getbonds(1) if b.atom2>b.atom1]
     for b in bonds:
         atom1,atom2=b.atom1,b.atom2
         opc+=J*(Sx.as_site(atom1)*Sx.as_site(atom2)+Sy.as_site(atom1)*Sy.as_site(atom2))
         opc+=Jz*(Sz.as_site(atom1)*Sz.as_site(atom2))
     for i in xrange(lt.nsite):
         opc=opc+h*Sz.as_site(i)
     mpo=opc.toMPO(nsite=lt.nsite,method='direct')
     self.H=mpo
Example #25
0
    4. Get <H> under kA.
'''

from numpy import sqrt, Inf

from mpslib import random_bmps
from opstring import OpCollection
from mpo import OPC2MPO
from mpolib import opunit_S
from tba.hgen import SpinSpaceConfig
from contraction import Contractor
from pymps.blockmarker import SimpleBMG

########### Generate a random State ######
# the single site Hilbert space(Spin) config, 2 spin, 1 orbital.
spaceconfig = SpinSpaceConfig([1, 2])
# the generator of block marker,
# using magnetization `M = nup-ndn` as good quantum number.
bmg = SimpleBMG(spaceconfig=spaceconfig, qstring='M')
# generate a random mps of 10 site and chi = 20,
# which is under the supervise of above <BlockMarkerGenerator>.
kA = random_bmps(bmg=bmg, nsite=10, maxN=20)
# nomalize it
kA = kA / sqrt(kA.tobra() * kA).item()

########### Get the Good quantum Number ######
# number of steps, tolerence, maximum bond dimension.
kA << kA.nsite, 1e-8, Inf
# get the block marker,
# the (quantum number)flow direction is left->right,
# so the right most bond contains the final good quantum number.
Example #26
0
def test_compress():
    scfg=SpinSpaceConfig([4,2])
    ss=SparseState([0.1j,0.2,0.3],[[1,1,0,1],[1,0,1,0],[1,1,0,1]])
    assert_allclose(ss.ws,[0.2,0.3+0.1j])
    assert_allclose(ss.configs,[[1,0,1,0],[1,1,0,1]])
Example #27
0
    def __init__(self):
        a = [0.1j, 0.2]
        b = [0.1j, 0.2, 0.3]
        b2 = [-0.1j, 0.2, -0.3]
        W = [[0.1, -0.1, 0.1], [-0.1j, 0.1, 0.1j]]
        self.rbm = RBM(a, b, W)

        #translational invariant group
        tig = TIGroup(ngs=[2])
        self.rbm_t = RBM(a, b2, W, group=tig)

        #get vec in the brute force way.
        self.vec = []
        scfg1 = SpinSpaceConfig([2, 2])
        scfg2 = SpinSpaceConfig([3, 2])
        for i in xrange(scfg1.hndim):
            config1 = scfg1.ind2config(i)
            s = 1 - config1 * 2
            vi = 0j
            for j in xrange(scfg2.hndim):
                config2 = scfg2.ind2config(j)
                h = 1 - 2 * config2
                vi += exp((s * a).sum() + (h * b).sum() + s.dot(W).dot(h))
            self.vec.append(vi)
        self.vec = array(self.vec)

        #the second vec
        self.vec2 = []
        scfg1 = SpinSpaceConfig([2, 2])
        scfg2 = SpinSpaceConfig([6, 2])
        for i in xrange(scfg1.hndim):
            config1 = scfg1.ind2config(i)
            s = 1 - config1 * 2
            vi = 0j
            for j in xrange(scfg2.hndim):
                config2 = scfg2.ind2config(j)
                h = 1 - 2 * config2
                vi += exp((s * a).sum() + (s[::-1] * a).sum() +
                          (h * concatenate([b2, b2])).sum() +
                          s.dot(W).dot(h[:3]) + s[::-1].dot(W).dot(h[3:]))
            self.vec2.append(vi)
        self.vec2 = array(self.vec2)
Example #28
0
class TestLinop(object):
    def __init__(self):
        J = 1.
        nsite = 4
        self.scfg = SpinSpaceConfig([nsite, 2])

        #construct true H
        I2, I4 = eye(2), eye(4)
        h2 = J / 4. * (kron(sx, sx) + kron(sz, sz) + kron(sy, sy))

        #construct operator H act on config
        self.h1 = TFI(nsite=4)
        self.h2 = HeisenbergH(nsite=4)
        self.toy1, self.toy2 = FakeVMC(self.h1), FakeVMC(self.h2)
        self.H1, self.H2 = self.toy1.get_H(), self.toy2.get_H()
        self.pW = PartialW()

        #generate a random rbm and the corresponding vector v
        self.rbm = random_rbm(nin=nsite, nhid=nsite)
        self.v = self.rbm.tovec(self.scfg)

        self.rbm_g = random_rbm(nin=nsite, nhid=nsite, group=TIGroup(4))
        self.v_g = self.rbm_g.tovec(self.scfg)

    def test_sandwichh(self):
        for h, H in [(self.h1, self.H1), (self.h2, self.H2)]:
            print 'Testing sandwich %s.' % h.__class__
            config = random.randint(0, 2, 4)
            ket = zeros(self.scfg.hndim)
            ket[self.scfg.config2ind(config)] = 1

            H, v, v_g = self.H2, self.v, self.v_g
            print 'Testing the non-group version'
            O_true = ket.conj().dot(H).dot(v) / sum(ket.conj() * v)
            cg = RBMConfigGenerator(nflip=2, initial_config=1 - 2 * config)
            cg.set_state(self.rbm)

            O_s = c_sandwich(self.h2, cg)
            assert_almost_equal(O_s, O_true, decimal=8)

            print 'Testing the group version'
            cg.set_state(self.rbm_g)
            O_sg = c_sandwich(self.h2, cg)
            O_trueg = ket.conj().dot(H).dot(v_g) / sum(ket.conj() * v_g)

            assert_almost_equal(O_sg, O_trueg, decimal=8)

    def test_sandwichpw(self):
        print 'Testing sandwich PartialW.'
        config = random.randint(0, 2, 4)
        ket = zeros(self.scfg.hndim)
        ket[self.scfg.config2ind(config)] = 1
        H, v, v_g = self.H, self.v, self.v_g

        cg = RBMConfigGenerator(nflip=2, initial_config=1 - 2 * config)
        cg.set_state(self.rbm)
        O_true = ket.conj().dot(Wmat).dot(v) / sum(ket.conj() * v)
        O_s = c_sandwich(self.pW, cg)

        O_trueg = ket.conj().dot(Wmat).dot(v_g) / sum(ket.conj() * v_g)
        cg.set_state(self.rbm_g)
        O_sg = c_sandwich(self.pW, cg)

        assert_almost_equal(O_s, O_true, decimal=8)
        assert_almost_equal(O_sg, O_trueg, decimal=8)
Example #29
0
 def __init__(self,h):
     self.h=h
     self.scfg=scfg=SpinSpaceConfig([h.nsite,2])
Example #30
0
def test_randomrbm():
    nsite = 6
    scfg = SpinSpaceConfig([nsite, 2])
    rbm = random_rbm(nin=nsite, nhid=2)
    print rbm.tovec(scfg)