def test_add_mpo(self): h = MPO_ham_heis(12) h2 = h + h assert max(h2[6].shape) == 10 h.lower_ind_id = h.upper_ind_id t = h ^ ... h2.upper_ind_id = h2.lower_ind_id t2 = h2 ^ ... assert_allclose(2 * t, t2)
def test_adding_mpo(self): h = MPO_ham_heis(6) hd = h.to_dense() assert_allclose(h @ h.H, (hd @ hd.H).tr()) h2 = h + h assert_allclose(h2 @ h2.H, (hd @ hd.H).tr() * 4) h2.right_compress() assert_allclose(h2 @ h2.H, (hd @ hd.H).tr() * 4) assert max(h2['I3'].shape) == 5
def test_mpo_site_ham_heis(self, cyclic, j, bz, n): hh_mpo = MPO_ham_heis(n, tags=['foo'], cyclic=cyclic, j=j, bz=bz) assert hh_mpo[0].tags == {'I0', 'foo'} assert hh_mpo[1].tags == {'I1', 'foo'} assert hh_mpo[-1].tags == {'I{}'.format(n - 1), 'foo'} assert hh_mpo.shape == (2, ) * 2 * n hh_ex = qu.ham_heis(n, cyclic=cyclic, j=j, b=bz) assert_allclose(qu.eigvalsh(hh_ex), qu.eigvalsh(hh_mpo.to_dense()), atol=1e-13)
def test_expand_mpo(self): h = MPO_ham_heis(12) assert h.site[0].dtype == float he = h.expand_bond_dimension(13) assert h.site[0].dtype == float assert max(he.site[6].shape) == 13 h.lower_ind_id = h.upper_ind_id t = h ^ ... he.upper_ind_id = he.lower_ind_id te = he ^ ... assert_allclose(t, te)
def test_expand_mpo(self, cyclic, rand_strength): h = MPO_ham_heis(12, cyclic=cyclic) assert h[0].dtype == float he = h.expand_bond_dimension(13, rand_strength=rand_strength) assert h[0].dtype == float assert max(he[6].shape) == 13 if cyclic: assert he.bond_size(0, -1) == 13 t = h.trace() te = he.trace() assert_allclose(t, te)
def test_expand_mpo(self, cyclic, rand_strength): h = MPO_ham_heis(12, cyclic=cyclic) assert h[0].dtype == float he = h.expand_bond_dimension(13, rand_strength=rand_strength) assert h[0].dtype == float assert max(he[6].shape) == 13 if cyclic: assert he.bond_size(0, -1) == 13 h.lower_ind_id = h.upper_ind_id t = h ^ ... he.upper_ind_id = he.lower_ind_id te = he ^ ... assert_allclose(t, te)
def test_heisenberg(self, n, l, gap): ham = MPO_ham_heis(n) dmrg = DMRG2(ham) dmrg.solve() gl = gap // 2 gr = gap // 2 + gap % 2 m = n // 2 sysa = range(m - l - gl, m - gl) sysb = range(m + gr, m + l + gr) assert max(sysa) + gap + 1 == min(sysb) ln = dmrg.state.logneg_subsys(sysa, sysb, approx_spectral_opts={'bsz': 16}, verbosity=2) # exact lne = logneg_subsys(groundstate(ham_heis(n, cyclic=False)), [2] * n, sysa, sysb) assert_allclose(lne, ln, rtol=0.1, atol=0.1)
def test_cyclic_solve_big_with_segmenting(self): n = 150 ham = MPO_ham_heis(n, cyclic=True) dmrg = DMRG2(ham, bond_dims=range(10, 30, 2)) dmrg.opts['periodic_segment_size'] = 1 / 3 assert dmrg.solve(tol=1, verbosity=2) assert dmrg.energy == pytest.approx(heisenberg_energy(n), 1e-3)
def test_dtypes(self, dtype): H = MPO_ham_heis(8).astype(dtype) dmrg = DMRG2(H) dmrg.opts['local_eig_backend'] = 'scipy' dmrg.solve(max_sweeps=3) res_dtype, = {t.dtype for t in dmrg.state} assert res_dtype == dtype
def test_realistic(self): ham = MPO_ham_heis(20) dmrg = DMRG2(ham, bond_dims=[4, 8]) dmrg.solve() sysa, sysb = range(3, 9), range(12, 17) rho_ab_pt = PTPTLazyMPS(dmrg.state, sysa, sysb) xf = approx_spectral_function(rho_ab_pt, lambda x: x, beta_tol=1e-6, tol=0.05, verbosity=2, max_bond=20) assert_allclose(1, xf, rtol=0.5, atol=0.1)
def test_realistic(self): seed_rand(42) ham = MPO_ham_heis(20) dmrg = DMRG2(ham, bond_dims=[2, 4]) dmrg.solve() rho_ab = dmrg.state.ptr(range(6, 14)) xf = approx_spectral_function(rho_ab, lambda x: x, tol=0.1, verbosity=2) assert_allclose(1.0, xf, rtol=0.6, atol=0.001)
def test_mpo_site_ham_heis(self): hh_mpo = MPO_ham_heis(5, tags=['foo']) assert hh_mpo.site[0].tags == {'I0', 'foo'} assert hh_mpo.site[3].tags == {'I3', 'foo'} assert hh_mpo.site[-1].tags == {'I4', 'foo'} assert hh_mpo.shape == (2, ) * 10 hh_ = (hh_mpo ^ ...).fuse({ 'k': ['k0', 'k1', 'k2', 'k3', 'k4'], 'b': ['b0', 'b1', 'b2', 'b3', 'b4'] }) hh = ham_heis(5, cyclic=False) assert_allclose(hh, hh_.data)
def test_realistic_ent(self): n = 12 sysa, sysb = range(3, 6), range(6, 8) ham = MPO_ham_heis(n) dmrg = DMRG2(ham, bond_dims=[2, 4]) dmrg.solve() rho_ab_pt = PTPTLazyMPS(dmrg.state, sysa, sysb) psi0 = dmrg.state.to_dense() lne = logneg_subsys(psi0, [2] * n, sysa=sysa, sysb=sysb) lnx = log2( approx_spectral_function(rho_ab_pt, abs, tol=0.1, verbosity=2)) assert_allclose(lne, lnx, rtol=0.5, atol=0.1)
def test_realistic_ent(self): n = 12 sysa, sysb = range(3, 6), range(6, 8) sysab = (*sysa, *sysb) ham = MPO_ham_heis(n) dmrg = DMRG2(ham, bond_dims=[10]) dmrg.solve() psi0 = dmrg.state.to_dense() lne = logneg_subsys(psi0, [2] * n, sysa=sysa, sysb=sysb) rho_ab = dmrg.state.ptr(sysab, rescale_sites=True) rho_ab_pt = rho_ab.partial_transpose(range(3)) lnx = log2( approx_spectral_function(rho_ab_pt, abs, tol=0.1, verbosity=2)) assert_allclose(lne, lnx, rtol=0.6, atol=0.1)
def test_single_explicit_sweep(self): h = MPO_ham_heis(5) dmrg = DMRG1(h, bond_dims=3) assert dmrg._k[0].dtype == float energy_tn = (dmrg._b | dmrg.ham | dmrg._k) e0 = energy_tn ^ ... assert abs(e0.imag) < 1e-13 de1 = dmrg.sweep_right() e1 = energy_tn ^ ... assert_allclose(de1, e1) assert abs(e1.imag) < 1e-13 de2 = dmrg.sweep_right() e2 = energy_tn ^ ... assert_allclose(de2, e2) assert abs(e2.imag) < 1e-13 # state is already left canonized after right sweep de3 = dmrg.sweep_left(canonize=False) e3 = energy_tn ^ ... assert_allclose(de3, e3) assert abs(e2.imag) < 1e-13 de4 = dmrg.sweep_left() e4 = energy_tn ^ ... assert_allclose(de4, e4) assert abs(e2.imag) < 1e-13 # test still normalized assert dmrg._k[0].dtype == float align_TN_1D(dmrg._k, dmrg._b, inplace=True) assert_allclose(abs(dmrg._b @ dmrg._k), 1) assert e1.real < e0.real assert e2.real < e1.real assert e3.real < e2.real assert e4.real < e3.real
def test_expand_mpo_limited(self, cyclic, rand_strength): h = MPO_ham_heis(12, cyclic=cyclic) he = h.expand_bond_dimension(3, rand_strength=rand_strength) # should do nothing assert max(he[6].shape) == 5
def test_expand_mpo_limited(self): h = MPO_ham_heis(12) he = h.expand_bond_dimension(3) # should do nothing assert max(he.site[6].shape) == 5