def test_quasiboson_solver(value): np.random.seed(0) # normal boson mol_list1 = parameter.custom_mol_list(None, *value[0]) mps1, mpo1 = solver.construct_mps_mpo_2( mol_list1, procedure[0][0], nexciton ) mps1.optimize_config.procedure = procedure # quasiboson mol_list2 = parameter.custom_mol_list(None, *value[1]) mps2, mpo2 = solver.construct_mps_mpo_2( mol_list2, procedure[0][0], nexciton ) mps2.optimize_config.procedure = procedure # quasiboson + normal boson mol_list3 = parameter.custom_mol_list(None, *value[2]) mps3, mpo3 = solver.construct_mps_mpo_2( mol_list3, procedure[0][0], nexciton ) mps3.optimize_config.procedure = procedure for method in ["1site", "2site"]: mps1.optimize_config.method = method mps2.optimize_config.method = method mps3.optimize_config.method = method energy1 = solver.optimize_mps(mps1, mpo1) energy2 = solver.optimize_mps(mps2, mpo2) energy3 = solver.optimize_mps(mps3, mpo3) assert np.min(energy1) == pytest.approx(np.min(energy2), rel=1e-4) assert np.min(energy2) == pytest.approx(np.min(energy3), rel=1e-4)
def test_construct_MPO_scheme3(): Mmax = 10 J = (np.array([[0.0, -0.1, 0.0], [-0.1, 0.0, -0.3], [0.0, -0.3, 0.0]]) / constant.au2ev) mol_list = custom_mol_list(J) mps2, mpo2 = construct_mps_mpo_2(mol_list, Mmax, nexciton) mps3, mpo3 = construct_mps_mpo_2(mol_list.switch_scheme(3), Mmax, nexciton) assert mpo2.ephtable == mpo3.ephtable assert mpo2.pbond_list == mpo3.pbond_list assert mpo3.distance(mpo2) == pytest.approx(0)
def test_construct_MPO(): Mmax = 10 mps1, mpo1 = construct_mps_mpo_2(mol_list.switch_scheme(1), Mmax, nexciton) mps2, mpo2 = construct_mps_mpo_2(mol_list, Mmax, nexciton) assert mpo1.ephtable == mpo2.ephtable assert mpo1.pbond_list == mpo2.pbond_list # for double precision the abs could be near 0. In single precision # the norm of mpo2 is not correct. (mpo2.dot(mpo2) != mpo1.dot(mpo1) # but mpo1.dot(mpo1) == mpo1.dot(mpo2)). Reason unknown assert mpo1.distance(mpo2) == pytest.approx(0, abs=1e-3)
def test_hybrid_DMRG_H_SCF(mol_list, target): nexciton = 1 mps, mpo = solver.construct_mps_mpo_2(mol_list, 10, nexciton) Etot = solver.optimize_mps(mps, mpo) # print("Etot", Etot) assert Etot == pytest.approx(target, abs=1e-5) nexciton = 0 mps, mpo = solver.construct_mps_mpo_2(mol_list, 10, nexciton) Etot = solver.optimize_mps(mps, mpo) # print("Etot", Etot) assert Etot == pytest.approx(0.0, abs=1e-5)
def test_optimization(scheme): mps, mpo = construct_mps_mpo_2(mol_list.switch_scheme(scheme), procedure[0][0], nexciton) mps.optimize_config.procedure = procedure mps.optimize_config.method = "2site" energy = optimize_mps(mps.copy(), mpo) assert energy * constant.au2ev == pytest.approx(2.28614053133, rel=1e-5) mps.optimize_config.method = "1site" energy = optimize_mps(mps.copy(), mpo) assert energy * constant.au2ev == pytest.approx(2.28614053133, rel=1e-5)
def test_csvd(): np.random.seed(0) mps1, mpo = construct_mps_mpo_2(mol_list, procedure[0][0], nexciton) mps1.threshold = 1e-6 mps1.optimize_config.procedure = procedure optimize_mps(mps1, mpo) mps1.compress() mps2 = Mps.load(mol_list, os.path.join(cur_dir, "test_svd_qn.npz")) d = pytest.approx(mps1.distance(mps2), abs=1e-4) # the same direction or opposite direction assert d == 0 or d == 2
def test_multistate(): mps, mpo = construct_mps_mpo_2(mol_list, procedure[0][0], nexciton) mps.optimize_config.procedure = procedure mps.optimize_config.nroots = 5 mps.optimize_config.method = "1site" energy1 = optimize_mps(mps.copy(), mpo) mps.optimize_config.method = "2site" energy2 = optimize_mps(mps.copy(), mpo) # print energy1[-1], energy2[-1] energy_std = [0.08401412, 0.08449771, 0.08449801, 0.08449945] assert np.allclose(energy1[:4], energy_std) assert np.allclose(energy2[:4], energy_std)
def test_quasiboson_constructMPO(): mol_list1 = parameter.custom_mol_list(None, [4, 4]) mps1, mpo1 = solver.construct_mps_mpo_2(mol_list1, procedure[0][0], nexciton) mol_list2 = mol_list = parameter.custom_mol_list(None, [4, 4], [2, 2], [1e-7, 1e-7]) mps2, mpo2 = solver.construct_mps_mpo_2(mol_list2, procedure[0][0], nexciton) # merge the decomposed MPO mpo_merge = Mpo() mpo_merge.mol_list = mol_list impo = 0 for mol in mol_list: mpo_merge.append(mpo2[impo]) impo += 1 for _ in mol.dmrg_phs: mo = einsum("abcd, defg -> abecfg", mpo2[impo], mpo2[impo + 1]).reshape((mpo2[impo].shape[0], 4, 4, mpo2[impo + 1].shape[-1])) mpo_merge.append(mo) impo += 2 assert mpo1.distance(mpo_merge) == pytest.approx(0) mps2.optimize_config.procedure = procedure mps2.optimize_config.method = "2site" energy = solver.optimize_mps(mps2, mpo2) assert np.min(energy) * constant.au2ev == pytest.approx(2.28614053133, rel=1e-4) mps2.optimize_config.method = "1site" energy = solver.optimize_mps(mps2, mpo2) assert np.min(energy) * constant.au2ev == pytest.approx(2.28614053133, rel=1e-4)
def test_SCF_exact(): nexciton = 1 dmrg_mol_list = custom_mol_list(None, ph_phys_dim, dis=[Quantity(0), Quantity(0)]) # DMRG calculation procedure = [[40, 0.4], [40, 0.2], [40, 0.1], [40, 0], [40, 0]] mps, mpo = construct_mps_mpo_2(dmrg_mol_list, 40, nexciton) mps.optimize_config.procedure = procedure energy = optimize_mps(mps, mpo) dmrg_e = mps.expectation(mpo) # print occupation dmrg_occ = [] for i in [0, 1, 2]: mpo = Mpo.onsite(dmrg_mol_list, r"a^\dagger a", dipole=False, mol_idx_set={i}) dmrg_occ.append(mps.expectation(mpo)) print("dmrg_occ", dmrg_occ) hartree_mol_list = custom_mol_list(None, ph_phys_dim, dis=[Quantity(0), Quantity(0)], hartrees=[True, True]) WFN, Etot = tdh.SCF(hartree_mol_list, nexciton) assert Etot == pytest.approx(dmrg_e) fe, fv = 1, 6 HAM, Etot, A_el = tdh.construct_H_Ham(hartree_mol_list, nexciton, WFN, fe, fv, debug=True) assert Etot == pytest.approx(dmrg_e) assert np.allclose(A_el.flatten(), dmrg_occ, rtol=1e-4)
def init_oper(self): if self.spectratype == "abs": self.nexciton = 0 dipoletype = r"a^\dagger" else: self.nexciton = 1 dipoletype = "a" dipole_mpo = \ Mpo.onsite( self.mol_list, dipoletype, dipole=True ) mps, self.mpo = \ construct_mps_mpo_2( self.mol_list, self.procedure_gs[0][0], self.nexciton ) # ground state calculation mps.optimize_config = OptimizeConfig(procedure=self.procedure_gs) mps.optimize_config.method = "2site" self.lowest_e = optimize_mps(mps, self.mpo) ket_mps = dipole_mpo.apply(mps, canonicalise=True) self.b_oper = ket_mps.scale(-self.eta)