def test_clear(): gs_mps = Mps.gs(mol_list, max_entangled=False) mps = creation_operator.apply(gs_mps) new_mps = mps.copy() new_mps.clear_memory() assert new_mps.total_bytes < mps.total_bytes check_property(new_mps)
def init_mps(self): tentative_mpo = Mpo(self.mol_list) if self.temperature == 0: gs_mp = Mps.gs(self.mol_list, max_entangled=False) if self.dissipation != 0: gs_mp = MpDm.from_mps(gs_mp) else: gs_mp = MpDm.max_entangled_gs(self.mol_list) # subtract the energy otherwise might cause numeric error because of large offset * dbeta energy = Quantity(gs_mp.expectation(tentative_mpo)) mpo = Mpo(self.mol_list, offset=energy) tp = ThermalProp(gs_mp, mpo, exact=True, space="GS") tp.evolve(None, len(gs_mp), self.temperature.to_beta() / 2j) gs_mp = tp.latest_mps init_mp = self.create_electron(gs_mp) if self.dissipation != 0: init_mp = MpDmFull.from_mpdm(init_mp) energy = Quantity(init_mp.expectation(tentative_mpo)) self.mpo = Mpo(self.mol_list, offset=energy) logger.info(f"mpo bond dims: {self.mpo.bond_dims}") logger.info(f"mpo physical dims: {self.mpo.pbond_list}") if self.dissipation != 0: self.mpo = SuperLiouville(self.mpo, self.dissipation) init_mp.canonicalise() init_mp.evolve_config = self.evolve_config # init the compress config if not using threshold and not set if self.compress_config.criteria is not CompressCriteria.threshold\ and self.compress_config.max_dims is None: self.compress_config.set_bonddim(length=len(init_mp) + 1) init_mp.compress_config = self.compress_config # init_mp.invalidate_cache() return init_mp
def f(mol_list, run_qutip=True): tentative_mpo = Mpo(mol_list) init_mps = (Mpo.onsite(mol_list, r"a^\dagger", mol_idx_set={0}) @ Mps.gs( mol_list, False)).expand_bond_dimension(hint_mpo=tentative_mpo) init_mpdm = MpDm.from_mps(init_mps).expand_bond_dimension( hint_mpo=tentative_mpo) e = init_mps.expectation(tentative_mpo) mpo = Mpo(mol_list, offset=Quantity(e)) if run_qutip: # calculate result in ZT. FT result is exactly the same TIME_LIMIT = 10 QUTIP_STEP = 0.01 N_POINTS = TIME_LIMIT / QUTIP_STEP + 1 qutip_time_series = np.linspace(0, TIME_LIMIT, N_POINTS) init = qutip.Qobj(init_mps.full_wfn(), [qutip_h.dims[0], [1] * len(qutip_h.dims[0])]) # the result is not exact and the error scale is approximately 1e-5 res = qutip.sesolve(qutip_h - e, init, qutip_time_series, e_ops=[c.dag() * c for c in qutip_clist]) qutip_expectations = np.array(res.expect).T return qutip_expectations, QUTIP_STEP, init_mps, init_mpdm, mpo else: return init_mps, init_mpdm, mpo
def max_entangled_ex(cls, mol_list, normalize=True): """ T = \\infty maximum entangled EX state """ mps = Mps.gs(mol_list, max_entangled=True) # the creation operator \\sum_i a^\\dagger_i ex_mps = Mpo.onsite(mol_list, r"a^\dagger").apply(mps) if normalize: ex_mps.normalize(1.0) return cls.from_mps(ex_mps)
def test_phonon_onsite(): gs = Mps.gs(mol_list, max_entangled=False) assert not gs.ph_occupations.any() b2 = Mpo.ph_onsite(mol_list, r"b^\dagger", 0, 0) p1 = b2.apply(gs).normalize() assert np.allclose(p1.ph_occupations, [1, 0, 0, 0, 0, 0]) p2 = b2.apply(p1).normalize() assert np.allclose(p2.ph_occupations, [2, 0, 0, 0, 0, 0]) b = b2.conj_trans() assert b.distance(Mpo.ph_onsite(mol_list, r"b", 0, 0)) == 0 assert b.apply(p2).normalize().distance(p1) == pytest.approx(0, abs=1e-5)
def test_displacement(): def get_e_occu(idx): res = np.zeros(len(mol_list)) res[idx] = 1 return res gs = Mps.gs(mol_list, max_entangled=False) gs = Mpo.onsite(mol_list, r"a^\dagger", mol_idx_set={0}).apply(gs).compress() assert np.allclose(gs.e_occupations, get_e_occu(0)) gs = Mpo.displacement(mol_list, 0, 2).apply(gs) assert np.allclose(gs.e_occupations, get_e_occu(2)) gs = Mpo.displacement(mol_list, 2, 0).apply(gs) assert np.allclose(gs.e_occupations ,get_e_occu(0))
def test_save_load(): mps = Mpo.onsite(parameter.mol_list, "a^\dagger", mol_idx_set={0}).apply(Mps.gs(parameter.mol_list, False)) mpo = Mpo(parameter.mol_list) mps1 = mps.copy() for i in range(2): mps1 = mps1.evolve(mpo, 10) mps2 = mps.evolve(mpo, 10) fname = "test.npz" mps2.dump(fname) mps2 = Mps.load(parameter.mol_list, fname) mps2 = mps2.evolve(mpo, 10) assert np.allclose(mps1.e_occupations, mps2.e_occupations) os.remove(fname)
def test_save_load(): mol_list = custom_mol_list(hartrees=[True, False]) mps = Mpo.onsite(mol_list, "a^\dagger", mol_idx_set={0}) @ Mps.gs( mol_list, False) mpo = Mpo(mol_list) mps1 = mps.copy() for i in range(2): mps1 = mps1.evolve(mpo, 10) mps2 = mps.evolve(mpo, 10) fname = "test.npz" mps2.dump(fname) mps2 = Mps.load(mol_list, fname) mps2 = mps2.evolve(mpo, 10) assert np.allclose(mps1.e_occupations, mps2.e_occupations) os.remove(fname)
def init_mps(self): logger.debug( f"mpo bond and physical dimension: {self.h_mpo.bond_dims}, {self.h_mpo.pbond_list}" ) if self.temperature == 0: init_mps = Mps.gs(self.mol_list, False) else: raise NotImplementedError init_mps.compress_config = self.compress_config init_mps.evolve_config = self.evolve_config init_mps.use_dummy_qn = True self.h_mpo = Mpo(self.mol_list, offset=Quantity(init_mps.expectation(self.h_mpo))) init_mps = init_mps.expand_bond_dimension(self.h_mpo) return init_mps
def test_zt(): mol = get_mol() mol_list = MolList([mol], Quantity(0)) mps = Mps.gs(mol_list, False) mps.compress_config = CompressConfig(threshold=1e-6) mps.evolve_config = EvolveConfig(adaptive=True, guess_dt=0.1) mps.use_dummy_qn = True mpo = Mpo(mol_list) time_series = [0] spin = [1] for i in range(30): dt = mps.evolve_config.guess_dt mps = mps.evolve(mpo, evolve_dt=dt) time_series.append(time_series[-1] + dt) spin.append(1 - 2 * mps.e_occupations[0]) exact = get_exact_zt(mol, time_series) assert np.allclose(exact, spin, atol=1e-3)
def test_bogoliubov(): # REF: JCP, 2016, 145, 224101 evolve_config = EvolveConfig(EvolveMethod.tdvp_ps) # evolve_config = EvolveConfig() omega = 1 D = 1 nlevel = 10 T = Quantity(1) ph1 = Phonon.simple_phonon(Quantity(omega), Quantity(D), nlevel) mol1 = Mol(Quantity(0), [ph1]) mlist = MolList([mol1] * 2, Quantity(1), scheme=4) mpdm1 = MpDm.max_entangled_gs(mlist) mpdm1.evolve_config = evolve_config mpo1 = Mpo(mlist) tp = ThermalProp(mpdm1, mpo1, exact=True) tp.evolve(nsteps=20, evolve_time=T.to_beta() / 2j) mpdm2 = tp.latest_mps e1 = mpdm2.expectation(mpo1) mpdm3 = (Mpo.onsite(mlist, r"a^\dagger", False, {0}) @ mpdm2).expand_bond_dimension(mpo1) es1 = [mpdm3.e_occupations] for i in range(40): mpdm3 = mpdm3.evolve(mpo1, 0.1) es1.append(mpdm3.e_occupations) theta = np.arctanh(np.exp(-T.to_beta() * omega / 2)) ph2 = Phonon.simple_phonon(Quantity(omega), Quantity(D * np.cosh(theta)), nlevel) ph3 = Phonon.simple_phonon(Quantity(-omega), Quantity(-D * np.sinh(theta)), nlevel) mol2 = Mol(Quantity(0), [ph2, ph3]) mlist2 = MolList([mol2] * 2, Quantity(1), scheme=4) mps1 = Mps.gs(mlist2, False) mps1.evolve_config = evolve_config mpo2 = Mpo(mlist2) e2 = mps1.expectation(mpo2) mps2 = (Mpo.onsite(mlist2, r"a^\dagger", False, {0}) @ mps1).expand_bond_dimension(mpo2) es2 = [mps2.e_occupations] for i in range(20): mps2 = mps2.evolve(mpo2, 0.2) es2.append(mps2.e_occupations) assert np.allclose(es1[::2], es2, atol=5e-3)
def test_2site(): ph = Phonon.simple_phonon(Quantity(1), Quantity(1), 2) m = Mol(Quantity(0), [ph]) mol_list = MolList([m] * 2, Quantity(1), scheme=3) gs_mp = Mpo.onsite(mol_list, opera=r"a^\dagger", mol_idx_set={0}).apply(Mps.gs(mol_list, max_entangled=False)) mpdm = MpDm.from_mps(gs_mp) mpdm_full = MpDmFull.from_mpdm(mpdm) mpdm_full.compress_config = CompressConfig(threshold=1e-4) liouville = SuperLiouville(Mpo(mol_list), dissipation=1) ph_occupations_array = [] energies = [] for i in range(51): logger.info(mpdm_full) logger.info(mpdm_full.ph_occupations) ph_occupations_array.append(mpdm_full.ph_occupations) logger.info(mpdm_full.expectation(liouville)) energies.append(mpdm_full.expectation(liouville)) mpdm_full = mpdm_full.evolve(liouville, 0.4) ph_occupations_array = np.array(ph_occupations_array) assert energies[-1] == pytest.approx(-0.340162, rel=1e-2) assert np.allclose(ph_occupations_array[-1], [0.0930588, 0.099115], rtol=1e-2)
def max_entangled_ex(cls, mol_list, normalize=True): """ T = \\infty locally maximal entangled EX state """ mps = Mps.gs(mol_list, max_entangled=True) # the creation operator \\sum_i a^\\dagger_i if isinstance(mol_list, MolList): ex_mpo = Mpo.onsite(mol_list, r"a^\dagger") else: model = {} for dof in mol_list.e_dofs: model[(dof, )] = [(Op("a^\dagger", 1), 1.0)] ex_mpo = Mpo.general_mpo( mol_list, model=model, model_translator=ModelTranslator.general_model) ex_mps = ex_mpo @ mps if normalize: ex_mps.normalize(1.0) return cls.from_mps(ex_mps)
def test_zt(n_dmrg_phs, scheme): mol_list = parameter_PBI.construct_mol(4, n_dmrg_phs, 10 - n_dmrg_phs).switch_scheme(scheme) mps = Mps.gs(mol_list, False) # create electron mps = Mpo.onsite(mol_list, r"a^\dagger", mol_idx_set={0}).apply(mps).normalize(1.0) tentative_mpo = Mpo(mol_list) offset = mps.expectation(tentative_mpo) mpo = Mpo(mol_list, offset=Quantity(offset, "a.u.")) # do the evolution nsteps = 30 dt = 30.0 occ = [mps.e_occupations] for i in range(nsteps): mps = mps.evolve(mpo, dt) occ.append(mps.e_occupations) # make it compatible with std data occ = np.array(occ[:nsteps]).transpose() with open(os.path.join(cur_dir, "ZT_occ" + str(n_dmrg_phs) + ".npy"), "rb") as f: std = np.load(f) assert np.allclose(occ, std, rtol=1e-2, atol=1e-4)
def init_mps(self): tentative_mpo = Mpo(self.mol_list) if self.temperature == 0: gs_mp = Mps.gs(self.mol_list, max_entangled=False) if self.dissipation != 0: gs_mp = MpDm.from_mps(gs_mp) else: if self._defined_output_path: gs_mp = load_thermal_state(self.mol_list, self._thermal_dump_path) else: gs_mp = None if gs_mp is None: gs_mp = MpDm.max_entangled_gs(self.mol_list) # subtract the energy otherwise might cause numeric error because of large offset * dbeta energy = Quantity(gs_mp.expectation(tentative_mpo)) mpo = Mpo(self.mol_list, offset=energy) tp = ThermalProp(gs_mp, mpo, exact=True, space="GS") tp.evolve(None, max(20, len(gs_mp)), self.temperature.to_beta() / 2j) gs_mp = tp.latest_mps if self._defined_output_path: gs_mp.dump(self._thermal_dump_path) init_mp = self.create_electron(gs_mp) if self.dissipation != 0: init_mp = MpDmFull.from_mpdm(init_mp) energy = Quantity(init_mp.expectation(tentative_mpo)) self.mpo = Mpo(self.mol_list, offset=energy) logger.info(f"mpo bond dims: {self.mpo.bond_dims}") logger.info(f"mpo physical dims: {self.mpo.pbond_list}") init_mp.evolve_config = self.evolve_config init_mp.compress_config = self.compress_config if self.evolve_config.is_tdvp: init_mp = init_mp.expand_bond_dimension(self.mpo) if self.dissipation != 0: self.mpo = SuperLiouville(self.mpo, self.dissipation) init_mp.canonicalise() return init_mp
def test_mps(): gs_mps = Mps.gs(mol_list, max_entangled=False) mps = creation_operator.apply(gs_mps) check_property(mps)
def max_entangled_gs(cls, mol_list): return cls.from_mps(Mps.gs(mol_list, max_entangled=True))