Пример #1
0
 def test_one_site(self):
     mpiprint(
         0,
         '\n' + '=' * 20 + '\nTesting One Site Optimization\n' + '=' * 20)
     # Create an MPS
     from cyclomps.tools.mps_tools import create_mps_list, make_mps_list_right
     from cyclomps.tools.env_tools import calc_env, env_load_ten
     from cyclomps.tools.diag_tools import eig1
     d = 3
     N = 10
     mbd = 100
     nState = 4
     mpsList = create_mps_list([d] * N, mbd, nState)
     mpsList = make_mps_list_right(mpsList)
     # Load an mpo
     mpoList = []
     mpo = [None] * N
     mpoList.append(mpo)
     # Calculate the environment
     envList = calc_env(mpsList, mpoList)
     # Retrieve Relevant tensors for the local optimization
     (mps, ) = retrieve_tensors(0, mpsList=mpsList)
     (envl, envr) = retrieve_tensors(0, envList=envList)
     (mpo, ) = retrieve_tensors(0, mpoList=mpoList)
     # Send to diagonalization routine
     E, mps0, ovlp = eig1(mps, mpo, envl, envr, alg='exact')
     mpiprint(0, 'Exact Worked')
     #E,mps0,ovlp = eig1(mps,mpo,envl,envr,alg='arnoldi')
     #mpiprint(0,'Arnoldi Worked')
     E, mps0, ovlp = eig1(mps, mpo, envl, envr, alg='davidson')
     mpiprint(0, 'Davidson Worked')
     mpiprint(0, 'Passed\n' + '=' * 20)
Пример #2
0
 def test_empty_mpo(self):
     mpiprint(
         0, '\n' + '=' * 50 +
         '\nTesting Initial Environment Using Identity MPO\n' + '-' * 50)
     # Create an MPS
     from cyclomps.tools.mps_tools import create_mps_list, make_mps_list_right
     from cyclomps.tools.env_tools import calc_env, env_load_ten
     d = 3
     N = 10
     mbd = 100
     nState = 4
     mpsList = create_mps_list([d] * N, mbd, nState)
     mpsList = make_mps_list_right(mpsList)
     # Load an mpo
     mpoList = []
     mpo = [None] * N
     mpoList.append(mpo)
     # Calculate the environment
     envList = calc_env(mpsList, mpoList)
     # We can assert that the environment is now an identity at each site
     for site in range(N - 1, 0, -1):
         ident_check = env_load_ten(envList, 0, site)
         ident_check = einsum('ijk->ik', ident_check)
         (nx, ny) = ident_check.shape
         I = eye(nx)
         diff = summ(abss(I - ident_check))
         self.assertTrue(diff < 1e-8)
     # We can check the norm to see if it agrees
     mpiprint(0, 'Passed\n' + '=' * 50)
Пример #3
0
    def test_renorm_left_energy(self):
        mpiprint(
            0, '\n' + '=' * 50 +
            '\nTesting left renormalization effect on Energy\n' + '-' * 50)
        from cyclomps.tools.mps_tools import create_mps_list, make_mps_list_left, load_mps_list, calc_mps_norm, move_gauge
        from cyclomps.mpo.tasep import return_mpo
        from cyclomps.algs.dmrg1 import renormalize_left
        # Create an MPS
        d = 2
        N = 4
        mbd = 10
        nState = 4
        mps = create_mps_list([d] * N, mbd, nState)
        mps = make_mps_list_left(mps)
        # Load the MPO
        mpo = return_mpo(N, (0.5, 0.5, 1.))

        # Create a function to compute energy
        def calc_energy(mpsL, mpo):
            mps = load_mps_list(mpsL)
            E = zeros(nState, dtype=mps[0][0].dtype)
            norm = zeros(nState, dtype=mps[0][0].dtype)
            for state in range(nState):
                psi = einsum('apb,bqc,crd,dse->pqrs', mps[state][0],
                             mps[state][1], mps[state][2], mps[state][3])
                op = einsum('iPpj,jQqk,kRrl,lSsm->PpQqRrSs', mpo[0][0],
                            mpo[0][1], mpo[0][2], mpo[0][3])
                psi_conj = einsum('apb,bqc,crd,dse->pqrs', conj(mps[state][0]),
                                  conj(mps[state][1]), conj(mps[state][2]),
                                  conj(mps[state][3]))
                E[state] = einsum('pqrs,PpQqRrSs,PQRS', psi, op, psi_conj)
                norm[state] = calc_mps_norm(mpsL, state=state)
            return E, norm

        # Calc initial Energy
        E1, norm1 = calc_energy(mps, mpo)
        # Move gauge using renormalization
        site = N - 1
        (mps0, ) = retrieve_tensors(site - 1, mpsList=mps)
        (mps1, ) = retrieve_tensors(site, mpsList=mps)
        mps0, mps1, EE, EEs, wgt = renormalize_left(mps0,
                                                    mps1,
                                                    state_avg=False)
        save_tensors(site - 1, mpsList=mps, mps=mps0)
        save_tensors(site, mpsList=mps, mps=mps1)
        # Calc Energy Again
        E2, norm2 = calc_energy(mps, mpo)
        # Check Energies
        self.assertTrue(summ(abss(E1[0] - E2[0])) < 1e-10)
        self.assertTrue(summ(abss(E1[1] - E2[1])) < 1e-10)
        self.assertTrue(summ(abss(E1[2] - E2[2])) < 1e-10)
        self.assertTrue(summ(abss(E1[3] - E2[3])) < 1e-10)
        # Check for correct canonical form
        for state in range(nState):
            eye_check = einsum('apb,cpb->ac', mps1[state], conj(mps1[state]))
            eye_actual = eye(eye_check.shape[0])
            self.assertTrue(summ(abss(eye_check - eye_actual)) < 1e-10)
        mpiprint(0, 'Passed\n' + '=' * 50)
Пример #4
0
    def test_tasep_mpo(self):
        mpiprint(
            0, '\n' + '=' * 50 +
            '\nTesting Initial Environment Using TASEP MPO\n' + '-' * 50)
        # Create an MPS
        from cyclomps.tools.mps_tools import create_mps_list, make_mps_list_right, load_mps_list, calc_mps_norm
        d = 2
        N = 4
        mbd = 10
        nState = 4
        mpsList = create_mps_list([d] * N, mbd, nState)
        # Load the MPO
        from cyclomps.mpo.tasep import return_mpo
        mpo = return_mpo(N, (0.5, 0.5, 1.))

        # Create a function to compute energy
        def calc_energy(mps, mpo, load_mps=True):
            if load_mps:
                mps = load_mps_list(mps)
            E = zeros(nState, dtype=mps[0][0].dtype)
            for state in range(nState):
                psi = einsum('apb,bqc,crd,dse->pqrs', mps[state][0],
                             mps[state][1], mps[state][2], mps[state][3])
                op = einsum('iPpj,jQqk,kRrl,lSsm->PpQqRrSs', mpo[0][0],
                            mpo[0][1], mpo[0][2], mpo[0][3])
                psi_conj = einsum('apb,bqc,crd,dse->pqrs', conj(mps[state][0]),
                                  conj(mps[state][1]), conj(mps[state][2]),
                                  conj(mps[state][3]))
                E[state] = einsum('pqrs,PpQqRrSs,PQRS', psi, op, psi_conj)
                norm = einsum('pqrs,pqrs->', psi, psi_conj)
                E[state] /= norm
            return E

        # Compute energy
        E1 = calc_energy(mpsList, mpo)
        mpiprint(0, 'Initial Energy =\n{}'.format(E1))
        # Right Canonicalize
        mpsList = make_mps_list_right(mpsList)
        # Compute energy again
        E2 = calc_energy(mpsList, mpo)
        mpiprint(0, 'Energy after canonicalization =\n{}'.format(E2))
        # Calculate the environment
        from cyclomps.tools.env_tools import calc_env, env_load_ten, load_env_list
        envList = calc_env(mpsList, mpo)
        # Calculate the energy using the environment
        env = load_env_list(envList)
        mps = load_mps_list(mpsList)
        E3 = zeros(nState, dtype=mps[0][0].dtype)
        for state in range(nState):
            E3[state] = einsum('Ala,apb,lPpr,APB,Brb->', env[0][0], mps[0][0],
                               mpo[0][0], conj(mps[0][0]), env[0][1])
            E3[state] /= calc_mps_norm(mpsList, state=state)
        mpiprint(0, 'Energy from environment =\n{}'.format(E3))
        # Check Results
        self.assertTrue(summ(abss(E1[0] - E2[0])) < 1e-10)
        self.assertTrue(summ(abss(E1[0] - E3[0])) < 1e-10)
        mpiprint(0, 'Passed\n' + '=' * 50)
Пример #5
0
 def test_retrieve(self):
     mpiprint(
         0, '\n' + '=' * 20 + '\nTesting Tensor Retrieve Function\n' +
         '=' * 20)
     # Create an MPS
     from cyclomps.tools.mps_tools import create_mps_list, make_mps_list_right
     from cyclomps.tools.env_tools import calc_env, env_load_ten
     d = 3
     N = 10
     mbd = 100
     nState = 4
     mpsList = create_mps_list([d] * N, mbd, nState)
     mpsList = make_mps_list_right(mpsList)
     # Load an mpo
     mpoList = []
     mpo = [None] * N
     mpoList.append(mpo)
     # Calculate the environment
     envList = calc_env(mpsList, mpoList)
     # Retrieve Relevant tensors for the local optimization
     (mps0, ) = retrieve_tensors(0, mpsList=mpsList)
     (mps1, ) = retrieve_tensors(1, mpsList=mpsList)
     (envL, envR) = retrieve_tensors(0, envList=envList)
     (env2L, env2R) = retrieve_tensors(0, envList=envList, twoSite=True)
     (mpo0, ) = retrieve_tensors(0, mpoList=mpoList)
     (mpo1, ) = retrieve_tensors(1, mpoList=mpoList)
     # Check to make sure all dimensions align
     (n1, n2, n3) = mps0[0].shape
     (n4, n5, n6) = mps1[0].shape
     (n7, n8, n9) = envL[0].shape
     (n10, n11, n12) = envR[0].shape
     (n13, n14, n15) = env2L[0].shape
     (n16, n17, n18) = env2R[0].shape
     self.assertEqual(n1, n7)
     self.assertEqual(n1, n13)
     self.assertEqual(n3, n4)
     self.assertEqual(n3, n12)
     self.assertEqual(n3, n10)
     self.assertEqual(n6, n16)
     self.assertEqual(n6, n18)
     # We can check the norm to see if it agrees
     mpiprint(0, 'Passed\n' + '=' * 20)
Пример #6
0
    def test_renorm_avg_left_energy2(self):
        mpiprint(
            0, '\n' + '=' * 50 +
            '\nTesting state avg left renormalization effect on Energy\n' +
            '-' * 50)
        from cyclomps.tools.mps_tools import create_mps_list, make_mps_list_left, load_mps_list, calc_mps_norm, move_gauge
        from cyclomps.mpo.tasep import return_mpo
        from cyclomps.algs.dmrg1 import renormalize_left
        # Create an MPS
        d = 2
        N = 4
        mbd = 10
        nState = 4
        mpsL = create_mps_list([d] * N, mbd, nState)
        mpsL = make_mps_list_left(mpsL)
        # Load the MPO
        mpo = return_mpo(N, (0.5, 0.5, 1.))

        # Create a function to compute energy
        def calc_energy(mpsL, mpo, load_mps=True):
            if load_mps:
                mps = load_mps_list(mpsL)
            else:
                mps = mpsL
            E = zeros(nState, dtype=mps[0][0].dtype)
            for state in range(nState):
                psi = einsum('apb,bqc,crd,dse->pqrs', mps[state][0],
                             mps[state][1], mps[state][2], mps[state][3])
                op = einsum('iPpj,jQqk,kRrl,lSsm->PpQqRrSs', mpo[0][0],
                            mpo[0][1], mpo[0][2], mpo[0][3])
                psi_conj = einsum('apb,bqc,crd,dse->pqrs', conj(mps[state][0]),
                                  conj(mps[state][1]), conj(mps[state][2]),
                                  conj(mps[state][3]))
                E[state] = einsum('pqrs,PpQqRrSs,PQRS', psi, op, psi_conj)
            return E

        # Calc initial Energy
        E1 = calc_energy(mpsL, mpo)
        mps = load_mps_list(mpsL)
        E1_ = calc_energy(mps, mpo, load_mps=False)
        E1__ = calc_energy(mps, mpo, load_mps=False)
        # Move gauge using renormalization
        site = N - 1
        (mps0, ) = retrieve_tensors(site - 1, mpsList=mpsL)
        (mps1, ) = retrieve_tensors(site, mpsList=mpsL)
        mps0_ = copy.deepcopy(mps0)
        mps1_ = copy.deepcopy(mps1)
        mps0, mps1, EE, EEs, wgt = renormalize_left(mps0, mps1, state_avg=True)
        save_tensors(site - 1, mpsList=mpsL, mps=mps0)
        save_tensors(site, mpsList=mpsL, mps=mps1)
        # Check to make sure contracted states and local energies are the same
        for state in range(nState):
            finalState = einsum('apb,bqc->apqc', conj(mps0[state]),
                                conj(mps1[state]))
            initState = einsum('apb,bqc->apqc', conj(mps0_[state]),
                               conj(mps1_[state]))
            mpoComb = einsum('lPpm,mQqn->lPpQqn', mpo[0][site - 1],
                             mpo[0][site])
            initLocalE = einsum('apqc,lPpQqn,APQC->AlaCnc', initState, mpoComb,
                                conj(initState))
            finalLocalE = einsum('apqc,lPpQqn,APQC->AlaCnc', finalState,
                                 mpoComb, conj(finalState))
            self.assertTrue(summ(abss(finalState - initState)) < 1e-10)
            self.assertTrue(summ(abss(initLocalE - initLocalE)) < 1e-10)
        # Check to make sure we get the same energies from resulting states
        for state in range(nState):
            mps[state][site - 1] = mps0[state]
            mps[state][site] = mps1[state]
        # Calc Energy Again
        E2 = calc_energy(mps, mpo, load_mps=False)
        # Check Energies
        self.assertTrue(summ(abss(E1[0] - E2[0])) < 1e-10)
        self.assertTrue(summ(abss(E1[1] - E2[1])) < 1e-10)
        self.assertTrue(summ(abss(E1[2] - E2[2])) < 1e-10)
        self.assertTrue(summ(abss(E1[3] - E2[3])) < 1e-10)
        # Check for correct canonical form
        for state in range(nState):
            eye_check = einsum('apb,cpb->ac', mps1[state], conj(mps1[state]))
            eye_actual = eye(eye_check.shape[0])
            self.assertTrue(summ(abss(eye_check - eye_actual)) < 1e-10)
        mpiprint(0, 'Passed\n' + '=' * 50)