Exemple #1
0
def TEBDi(MPS, D, Tp, i):
    M = MPS[:]
    if(Tp.ndim == 2):       #--> Single Site operator
        A = np.einsum('ab,ibc,cd->iad', M[2*i], M[2*i+1], M[2*i+2], optimize = 'optimal')
        At = np.einsum('ji,ipq->jpq', Tp, A)
        At = np.einsum('ab,ibc,cd->iad', np.diag(1/np.diag(M[2*i])), At, np.diag(1/np.diag(M[2*i+2])), optimize = 'optimal')
        M[2*i+1] = At
    
    elif(Tp.ndim == 4):     #--> Two site operator
        assert (2*i+3)<len(MPS)
        A1 = np.einsum('ab,ibc,cd->iad', M[2*i], M[2*i+1], M[2*i+2])
        A = np.einsum('iad,jde,ef->ijaf', A1, M[2*i+3], M[2*i+4])
        At = np.einsum('kilj,ijac->klac', Tp, A)
        M1, M2, s, err = ct.svd_and_truncate('one_MPS', D, At)
        M[2*i+1] = np.einsum('ab,ibc->iac', np.diag(1/np.diag(M[2*i])), M1, optimize = 'optimal')
        M[2*i+2] = s
        M[2*i+3] = np.einsum('iab,bc->iac', M2, np.diag(1/np.diag(M[2*i+4])), optimize = 'optimal')
    
    elif(Tp.ndim == 6):       #--> 3-Site operator
        assert (2*i+5)<len(MPS)
        A = np.einsum('ab,ibc,cd->iad', M[2*i], M[2*i+1], M[2*i+2], optimize = 'optimal')
        A = np.einsum('iad,jde,ef-> ijaf', A, M[2*i+3], M[2*i+4], optimize = 'optimal')
        A = np.einsum('ijaf,kfg,gh->ijkah', A, M[2*i+5], M[2*i+6], optimize = 'optimal')
        At = np.einsum('ijkah, limjnk -> lmnah', A, Tp)
        V = ct.svd_truncate_all(At, D, 4)
        M[2*i+1] = np.einsum('ab,ibc->iac', np.diag(1/np.diag(M[2*i])), V[0], optimize = 'optimal')
        M[2*i+2] = V[1]
        M[2*i+3] = V[2]
        M[2*i+4] = V[3]
        M[2*i+5] = np.einsum('iab,bc->iac', V[4], np.diag(1/np.diag(M[2*i+6])), optimize = 'optimal')
    
    else:
        raise Exception('Enter a Valid Operator')
    
    return M
Exemple #2
0
def time_evolution(MPS, Tp2, D, Tp1=None):

    M = MPS[:]
    L = len(M)
    
    #--------------------------------------------------------------------------
    # Evolution of odd-bonds
    #--------------------------------------------------------------------------
    i = 0
    while(i<(L/2)):
        phi = np.einsum('iab,jbc->ijac', M[2*i],M[2*i+1])
        psi = np.einsum('ijac, risj->rsac', phi, Tp2)
        M1, M2, s1, err1 = ct.svd_and_truncate('one_MPS', D, psi)
        M[2*i] = M1
        if(i<(L/2-1)):
            psi = np.einsum('ab,ibc,jcd->ijad', s1, M2, M[2*i+2])
            M2, M3, s2, err2 = ct.svd_and_truncate('one_MPS', D, psi)
            M[2*i+2] = np.einsum('ab,ibc->iac', s2, M3)
            M[2*i+1] = M2
        else:
            M[2*i+1] = np.einsum('ab,ibc->iac', s1, M2)
        i=i+1
    
    #--------------------------------------------------------------------------
    # Evolution of even bonds
    #--------------------------------------------------------------------------
    i = 0
    while(i<(L+1)/2-1):
        phi = np.einsum('iab,jbc->ijac', M[2*i+1],M[2*i+2])
        psi = np.einsum('ijac, risj->rsac', phi, Tp2)
        M1, M2, s1, err1 = ct.svd_and_truncate('one_MPS', D, psi)
        M[2*i+1] = M1
        if(i<(L+1)/2-2):
            psi = np.einsum('ab,ibc,jcd->ijad', s1, M2, M[2*i+3])
            M2, M3, s2, err2 = ct.svd_and_truncate('one_MPS', D, psi)
            M[2*i+3] = np.einsum('ab,ibc->iac', s2, M3)
            M[2*i+2] = M2
        else:
            M[2*i+2] = np.einsum('ab,ibc->iac', s1, M2)
        i=i+1
    
    if(isinstance(Tp1, np.ndarray)==True):
        #--------------------------------------------------------------------------
        # Evolution of single sites
        #--------------------------------------------------------------------------
        TE_ss = [Tp1]*L
        M = ct.MPO_on_MPS(M, TE_ss)
    
    return M
def GS_search(psi, Op, max_iter, tolerance):
    # Writing a code only for 4 sites
    
    L = len(psi)
    it1 = np.arange(0,L,1)
    it2 = np.arange(L-2,-1,-1)
    it = np.concatenate((it1,it2))      #---> For assigning sites
    d = np.shape(psi[0])[0]         #---> Dimensions of the physical index
    #Op---> Operator(e.g. Hamiltonian) 
    i=0
    psi_d = [np.matrix.conjugate(v) for v in psi]   #---> Forming psi dagger
    I = np.eye(d, dtype = float)
    energy = 0
    err=1
    i = 0
    while(err>tolerance):
        j = it[i%7]
        
        LtA = Ct.LR_contract_MPS_MPO_MPS(psi_d, psi, Op, j, 'left')
        RtA = Ct.LR_contract_MPS_MPO_MPS(psi_d, psi, Op, j, 'right')
        LtB = Ct.LR_contract_MPS_MPS(psi_d, psi, j, 'left')
        RtB = Ct.LR_contract_MPS_MPS(psi_d, psi, j, 'right')
        
        M = psi[j]
        W = Op[j]
        
        H = np.einsum('pqr, qjab, ijk -> apibrk', LtA, W, RtA)
        shH = np.shape(H)
        H = np.reshape(H, (shH[0]*shH[1]*shH[2], shH[3]*shH[4]*shH[5]))
        
        N = np.einsum('mn,yx,vz -> ymvxnz', LtB, I, RtB)
        shN = np.shape(N)
        N = np.reshape(N, (shN[0]*shN[1]*shN[2], shN[3]*shN[4]*shN[5]))
        
        shv = np.shape(M)
        v = np.reshape(M, (shv[0]*shv[1]*shv[2]))
        
        energy_new, eigvec = ssl.eigsh(H, k=1, M=N, which = 'SA', tol=1e-3)
        
        psi[j] = np.reshape(eigvec, (shv[0], shv[1], shv[2]))
        psi_d[j] = np.matrix.conjugate(psi[j])
        
        err = (energy_new-energy)/energy_new
        energy = energy_new
        
        i=i+1
    return energy, psi
#------------------------------------------------------------------------------

overlp = []
ti = []

for space1 in ['1Ag', '3Ag', '1Bu', '3Bu']:
    for space2 in ['1Ag', '3Ag', '1Bu', '3Bu']:
        for c1 in range(0, 2, 1):
            for c2 in range(0, 2, 1):
                t1 = time.time()
                #--------------------------------------------------------------
                # Form the MPS M, against which we want to calculate the overlaps
                #--------------------------------------------------------------
                St1 = syms.sym_adap_state(L, space1, t, U, V, c1)[0]
                St2 = syms.sym_adap_state(L, space2, t, U, V, c2)[0]
                M1 = ct.form_vidal_MPS(St1, d, D, L)
                M2 = ct.form_vidal_MPS(St2, d, D, L)
                M2 = M2[1:]
                M = M1 + M2
                for i in range(0, L, 1):
                    M = ct.permute_indices_vidal_right(M, 2 * i + 1, L + i, D)
                M = ct.vidal_to_LCM(M)
                f = open(
                    "overlaps/{}{}{}{}{}".format(L, c1 + 1, space1, c2 + 1,
                                                 space2), "a")
                for i in range(0, 250, 1):
                    x = 40 * i + 1
                    tme = x * tstep * 0.6
                    f_mat = np.load("{}/{}.npz".format(loc, x))
                    M_r = [0] * (4 * L + 1)
                    for j in range(0, 4 * L + 1, 1):
def MPS_compression(M, d, D, max_iter=None, tol=None):
    #M              ---> Given input
    #D              ---> Maximum Bond dimensions
    #d              ---> Physical index dimensions
    #tol            ---> Error Tolerance
    #max_iter       ---> Maximum number of iterations
    L = len(M)
    it1 = np.arange(0,L,1)
    it2 = np.arange(L-2,-1,-1)
    it = np.concatenate((it1,it2))      #---> For assigning sites
    if(L%2!=0):
       L1 = (L+1)/2
       BD1 = np.arange(0,L1,1)
       BD2 = np.arange(L1-1,-1,-1)
    else:
       L1 = L/2
       BD1 = np.arange(0,L1+1,1)
       BD2 = np.arange(L1-1,-1,-1)
       
    BD = np.concatenate((BD1,BD2))
    BD = np.power(d,BD)
    for i in range(0,len(BD), 1):
        BD[i] = BD[i]*(BD[i]<=D) + D*(BD[i]>D)   #---> Bond Dimensions
    
    
    M_t = [0]*L
    M_t_d = [0]*L
    for i in range(0,L,1):
        M_t[i] = np.random.random((d, BD[i], BD[i+1]))
        M_t_d[i] = np.matrix.conjugate(M_t[i])
    
    N1 = np.sqrt(Ct.overlap(M,M))
    N2 = np.sqrt(Ct.overlap(M_t,M_t))
    M[0] = M[0]/N1
    M_t[0] = M_t[0]/N2
    M_t_d[0] = M_t_d[0]/N2
    
    err = abs(1 - Ct.overlap(M,M_t) - Ct.overlap(M_t,M) + Ct.overlap(M_t,M_t))
    i = 0
    
    while(err>tol):
        j = it[i%(2*L-1)]     #---> j denotes the site which has to be optimized
        
        
        # Compute LtA, RtA, LtA, RtB here
        LtA = Ct.LR_contract_MPS_MPS(M_t_d, M_t, j, 'left')
        RtA = Ct.LR_contract_MPS_MPS(M_t_d, M_t, j, 'right')
        LtB = Ct.LR_contract_MPS_MPS(M_t_d, M, j, 'left')
        RtB = Ct.LR_contract_MPS_MPS(M_t_d, M, j, 'right')
        
        Op = np.einsum('ab,ec -> aebc', LtA, RtA)    #--> Operator Op
        shO = np.shape(Op)                           #--> Shape of operator Op
        Op = np.reshape(Op, (shO[0]*shO[1],shO[2]*shO[3]))
        P = np.einsum('ij,ljk,mk -> lim', LtB, M[j], RtB)
        shP = np.shape(P)
        P = np.reshape(P, (shP[0],shP[1]*shP[2]))
        for k in range(0,shP[0],1):
            V = sp.solve(Op,P[k,:])
            M_t[j][k,:,:] = V.reshape(shP[1], shP[2])
        
        M_t_d[j] = np.matrix.conjugate(M_t[j])
        err = abs(1 - Ct.overlap(M,M_t) - Ct.overlap(M_t,M) + Ct.overlap(M_t,M_t))
        
        i=i+1
    return M_t
def Opt_GS_search(M, Op, tol1, tol2):
#    tol1         #---> Tolerance for mat_opt
#    tol2         #---> Tolerance for energy difference
    L = len(M)
    assert len(M) == len(Op)
    
    psi = ct.MC_around_bond_or_site(M, 0, 'site')
    psi_t = [np.matrix.conjugate(i) for i in psi]
    
    F = [0]*L
    G = [0]*L
    
    F[0] = np.array([[[1]]])
    for i in range(1,L,1):
        F[i] = np.einsum('pqr, brk, qjab, api -> ijk', F[i-1], psi[i-1], Op[i-1], psi_t[i-1])
    
    G[L-1] = np.array([[[1]]])
    for i in range(L-2, -1, -1):
        G[i] = np.einsum('ijk, brk, qjab, api -> pqr', G[i+1], psi[i+1], Op[i+1], psi_t[i+1])
    
    
    energy = 0
    err = 1
    sweep = 0       #---> For counting sweeps
    while(err>tol2):
        i = 0
        # Sweeping left to right
        while(i<L-1):
            psi[i], energy_new = mat_opt(psi[i], F[i], G[i], Op[i], tol1)
            psi[i], X = ct.make_canonical(psi[i], 'left')
            psi[i+1] = np.einsum('ab,ibc -> iac', X, psi[i+1])
            psi_t[i] = np.matrix.conjugate(psi[i])
            psi_t[i+1] = np.matrix.conjugate(psi[i+1])
            F[i+1] = np.einsum('pqr, brk, qjab, api -> ijk', F[i], psi[i], Op[i], psi_t[i])
            i = i+1
        
        # Sweeping right to left
        while(i>0):
            psi[i], energy_new = mat_opt(psi[i], F[i], G[i], Op[i], tol1)
            psi[i], X = ct.make_canonical(psi[i], 'right')
            psi[i-1] = np.einsum('iab, bc -> iac', psi[i-1], X)
            psi_t[i] = np.matrix.conjugate(psi[i])
            psi_t[i-1] = np.matrix.conjugate(psi[i-1])
            G[i-1] = np.einsum('ijk, brk, qjab, api -> pqr', G[i], psi[i], Op[i], psi_t[i])
            i = i-1
        
        Op_sqr = ct.MPO_on_MPO(Op, Op)
        energy_sqr = ct.expectation(psi, Op_sqr, psi)
        err = abs((np.square(energy_new)-energy_sqr)/np.square(energy_new))
#        energy = energy_new
        sweep = sweep + 1
    
    return psi, energy_new

##%%
#M = [0]*6
#D = 10
#BD = [1,4,20,9,20,4,1]
#for i in range(0,6,1):
#    M[i] = np.random.random((4, BD[i], BD[i+1]))
#M_opt = Opt_MPS_compression(M, 4, D, 200, 1e-4)
##%%
#St1 = np.einsum('iab,jbc,kcd,lde,mef,nfg->ijklmn', M[0], M[1], M[2], M[3], M[4], M[5])
#St2 = np.einsum('iab,jbc,kcd,lde,mef,nfg->ijklmn', M_opt[0], M_opt[1], M_opt[2], M_opt[3], M_opt[4], M_opt[5])
def Opt_MPS_compression(M, d, D, max_iter = None, tol = None):
    #M              ---> Given input
    #D              ---> Maximum Bond dimensions
    #d              ---> Physical index dimensions
    #tol            ---> Error Tolerance
    #max_iter       ---> Maximum number of iterations
     
#    L = len(M)
#    BD = [0]*(L+1)
#    for i in range(0,L,1):
#        BD[i] = np.shape(M[i])[1]
#    BD[L] = np.shape(M[L-1])[2]
#    for i in range(len(BD)):
#        BD[i] = BD[i]*(BD[i]<=D) + D*(BD[i]>D)   #---> Bond Dimensions
    L = len(M)
    it1 = np.arange(0,L,1)
    it2 = np.arange(L-2,-1,-1)
    it = np.concatenate((it1,it2))      #---> For assigning sites
    if(L%2!=0):
       L1 = (L+1)/2
       BD1 = np.arange(0,L1,1)
       BD2 = np.arange(L1-1,-1,-1)
    else:
       L1 = L/2
       BD1 = np.arange(0,L1+1,1)
       BD2 = np.arange(L1-1,-1,-1)
       
    BD = np.concatenate((BD1,BD2))
    BD = np.power(d,BD)
    for i in range(0,len(BD), 1):
        BD[i] = BD[i]*(BD[i]<=D) + D*(BD[i]>D)   #---> Bond Dimensions
    
    M_t = [0]*L
    M_t_d = [0]*L
    for i in range(0,L,1):
        M_t[i] = np.random.random((d, BD[i], BD[i+1]))
    
    N1 = np.sqrt(Ct.overlap(M,M))       #
    N2 = np.sqrt(Ct.overlap(M_t,M_t))   #
    M[0] = M[0]/N1                      #---> Normalization
    M_t[0] = M_t[0]/N2                  #
    
    M_t = Ct.MC_around_bond_or_site(M_t, 0, 'site')
    M_t_d = [np.matrix.conjugate(i) for i in M_t]
    err = abs(1 - Ct.overlap(M,M_t) - Ct.overlap(M_t,M) + Ct.overlap(M_t,M_t))
    
    F = [0]*L
    G = [0]*L
    F[0] = np.array([[1]])
    for i in range(1,L,1):
        F[i] = np.einsum('pr, ark, api -> ik', F[i-1], M[i-1], M_t_d[i-1])
    G[L-1] = np.array([[1]])
    for i in range(L-2, -1, -1):
        G[i] = np.einsum('ik, ark, api -> pr', G[i+1], M[i+1], M_t_d[i+1])
    
    
    while(err>tol):
        #Swiping from left to right
        i = 0
        while(i<L-1):
            M_t[i] = np.einsum('ij,ljk,mk -> lim', F[i], M[i], G[i])
            M_t[i], X = Ct.make_canonical(M_t[i], 'left')
            M_t[i+1] = np.einsum('ab,ibc -> iac', X, M_t[i+1])
            M_t_d[i] = np.matrix.conjugate(M_t[i])
            M_t_d[i+1] = np.matrix.conjugate(M_t[i+1])
            F[i+1] = np.einsum('pr, ark, api -> ik', F[i], M[i], M_t_d[i])
            i = i+1
        
        #Sweeping from right to left
        while(i<0):
            M_t[i] = np.einsum('ij,ljk,mk -> lim', F[i], M[i], G[i])
            M_t[i], X = Ct.make_canonical(M_t[i], 'right')
            M_t[i-1] = np.einsum('iab,bc -> iac', M[i-1], X)
            M_t_d[i] = np.matrix.conjugate(M_t[i])
            M_t_d[i-1] = np.matrix.conjugate(M_t[i-1])
            G[i-1] = np.einsum('ik, ark, api -> pr', G[i], M[i], M_t_d[i])
            i = i-1
        
        err = abs(1 - Ct.overlap(M,M_t) - Ct.overlap(M_t,M) + Ct.overlap(M_t,M_t))
    return M_t
word_fd = FreqDist()
category_fd = ConditionalFreqDist()

punctuation = [
    '(', ')', '?', ':', ';', ',', '.', '!', '/', '"', "-", "--", "@", "..."
]

positives = []
with open("/Users/sameer/Desktop/PJAS/pos_tweets.txt",
          "r",
          encoding='utf-8-sig') as f:
    for l in f:
        #expand contradictions
        for k in punctuation:
            l = l.replace(k, " ")
        l = Contractions.expandContractions(l)
        sentenceWords = nltk.word_tokenize(l)
        for word in sentenceWords:
            word_fd[word.lower()] += 1
            category_fd['pos'][word.lower()] += 1
        positives.append(l)

negatives = []
with open("/Users/sameer/Desktop/PJAS/neg_tweets.txt",
          "r",
          encoding='utf-8-sig') as f:
    for l in f:
        #expand contradictions
        for k in punctuation:
            l = l.replace(k, " ")
        l = Contractions.expandContractions(l)
Exemple #9
0
if os.path.exists("{}_{}_{}_{}/overlap".format(L, L, e1, e2)):
    os.remove("{}_{}_{}_{}/overlap".format(L, L, e1, e2))
with open("{}_{}_{}_{}/overlap".format(L, L, e1, e2), "a") as fovlp:
    fovlp.write("Iteration \t\t Overlap \n")

if os.path.exists("{}_{}_{}_{}/iter_time".format(L, L, e1, e2)):
    os.remove("{}_{}_{}_{}/iter_time".format(L, L, e1, e2))
with open("{}_{}_{}_{}/iter_time".format(L, L, e1, e2), "a") as ftime:
    ftime.write("Iteration \t Total \n")

#%%
#------------------------------------------------------------------------------
# Forming the initial State MPS
#------------------------------------------------------------------------------
St1 = sh.estates_of_H(L, e1, ms1, t, U, V, 1)
M1 = ct.form_vidal_MPS(St1, d, D, L)
St2 = sh.estates_of_H(L, e2, ms2, t, U, V, 1)
M2 = ct.form_vidal_MPS(St2, d, D, L)
M2 = M2[1:]
vform = M1 + M2  #--> vform is now of the form 1-2-3-4-1'-2'-3'-4'
# We need vform to be of the form 1-1'-2-2'-3-3'-4-4'.
for i in range(0, L, 1):
    vform = ct.permute_indices_vidal_right(vform, 2 * i + 1, L + i, D)
#%%
#------------------------------------------------------------------------------
# Forming the time evolution operators
#------------------------------------------------------------------------------
Tp3 = hb.form_TEOp_three_site(d, tstep, t, V)
Tp1 = hb.form_TEOp_one_site(d, tstep, U)
TETp = hb.form_TEOp_two_site(d, tstep, T, 0)
Exemple #10
0
cdir = os.getcwd()
newpath = os.path.join(cdir, '{}_{}_{}_{}_{}'.format(file_idx, L, L, e1, e2))
if not os.path.exists(newpath):
    os.makedirs(newpath)
#%%
#------------------------------------------------------------------------------
# Forming the time evolution operators
#------------------------------------------------------------------------------
Tp3 = hb.form_TEOp_three_site(d, tstep, t, V)
Tp1 = hb.form_TEOp_one_site(d, tstep, U)
TETp = hb.form_TEOp_two_site(d, tstep, T, 0)

#%%
M = M_r[:]
M_L = ct.vidal_to_LCM(M_r)
ovlp = [ct.overlap(M_L, M_L)]
tme1 = []

t0 = time.clock()
for j in range(1, N - file_idx, 1):
    print(file_idx + j)
    t1 = time.clock()
    #--------------------------------------------------------------------------
    # Odd bonds evolution
    #--------------------------------------------------------------------------
    for i in range(0, int(L / 2), 1):
        M = hb.TEBDi(M, D, Tp3, 4 * i)  #--> Chain 1
        M = hb.TEBDi(M, D, Tp3, 4 * i + 1)  #--> Chain 2
    #--------------------------------------------------------------------------
    # Even bonds evolution