def test_MPO_addition(): for bc in ['infinite', 'finite']: print('bc = ', bc, '-' * 40) s = spin_half ot1 = OnsiteTerms(4) ct1 = CouplingTerms(4) ct1.add_coupling_term(2., 2, 3, 'Sm', 'Sp') ct1.add_coupling_term(2., 2, 3, 'Sp', 'Sm') ct1.add_coupling_term(2., 1, 2, 'Sz', 'Sz') ot1.add_onsite_term(3., 1, 'Sz') H1 = mpo.MPOGraph.from_terms(ot1, ct1, [s] * 4, bc).build_MPO() ot2 = OnsiteTerms(4) ct2 = CouplingTerms(4) ct2.add_coupling_term(4., 0, 2, 'Sz', 'Sz') ct2.add_coupling_term(4., 1, 2, 'Sz', 'Sz') ot2.add_onsite_term(5., 1, 'Sz') H2 = mpo.MPOGraph.from_terms(ot2, ct2, [s] * 4, bc).build_MPO() H12_sum = H1 + H2 ot12 = OnsiteTerms(4) ot12 += ot1 ot12 += ot2 ct12 = CouplingTerms(4) ct12 += ct1 ct12 += ct2 H12 = mpo.MPOGraph.from_terms(ot12, ct12, [s] * 4, bc).build_MPO() assert H12.is_equal(H12_sum)
def test_MPO_expectation_value(): s = spin_half psi1 = mps.MPS.from_singlets(s, 6, [(1, 3), (2, 5)], lonely=[0, 4], bc='infinite') psi1.test_sanity() ot = OnsiteTerms(4) ot.add_onsite_term(0.1, 0, 'Sz') # -> 0.5 ot.add_onsite_term(0.2, 3, 'Sz') # -> 0. ct = CouplingTerms(4) # note: ct.L != psi1.L ct.add_coupling_term(1., 2, 3, 'Sz', 'Sz') # -> 0. ct.add_coupling_term(1.5, 1, 3, 'Sz', 'Sz') # -> 1.5*(-0.25) ct.add_coupling_term(2.5, 0, 6, 'Sz', 'Sz') # -> 2.5*0.25 H = mpo.MPOGraph.from_terms(ot, ct, [s] * 4, 'infinite').build_MPO() ev = H.expectation_value(psi1) desired_ev = (0.1 * 0.5 + 0.2 * 0. + 1. * 0. + 1.5 * -0.25 + 2.5 * 0.25) / H.L assert abs(ev - desired_ev) < 1.e-8 grid = [ [s.Id, s.Sz, 3 * s.Sz], [None, 0.1 * s.Id, s.Sz], [None, None, s.Id], ] L = 1 exp_dec_H = mpo.MPO.from_grids([s] * L, [grid] * L, bc='infinite', IdL=0, IdR=2) ev = exp_dec_H.expectation_value(psi1) desired_ev = 3 * 0.5 + 0.25 * 0.1**(4 - 1) + 0.25 * 0.1**( 6 - 1) + 0.25 * 0.1**(10 - 1) # values > 1.e-15 assert abs(ev - desired_ev) < 1.e-15 L = 3 exp_dec_H = mpo.MPO.from_grids([s] * L, [grid] * L, bc='infinite', IdL=0, IdR=2) ev = exp_dec_H.expectation_value(psi1) desired_ev = ( desired_ev + # first site 3 * 0. - 0.25 * 0.1**(3 - 1 - 1) + # second site 3 * 0. - 0.25 * 0.1**(5 - 2 - 1)) / 3. print("ev = ", ev, "desired", desired_ev) assert abs(ev - desired_ev) < 1.e-14
def test_MPO_hermitian(): s = spin_half ot = OnsiteTerms(4) ct = CouplingTerms(4) ct.add_coupling_term(1., 2, 3, 'Sm', 'Sp') H = mpo.MPOGraph.from_terms(ot, ct, [s] * 4, 'infinite').build_MPO() # assert not H.is_hermitian() ct.add_coupling_term(1., 2, 3, 'Sp', 'Sm') H = mpo.MPOGraph.from_terms(ot, ct, [s] * 4, 'infinite').build_MPO() # assert H.is_hermitian() ct.add_coupling_term(1., 3, 18, 'Sm', 'Sp') H = mpo.MPOGraph.from_terms(ot, ct, [s] * 4, 'infinite').build_MPO() assert not H.is_hermitian() ct.add_coupling_term(1., 3, 18, 'Sp', 'Sm') H = mpo.MPOGraph.from_terms(ot, ct, [s] * 4, 'infinite').build_MPO() assert H.is_hermitian()
def test_onsite_terms(): L = 6 strength1 = np.arange(1., 1. + L * 0.25, 0.25) o1 = OnsiteTerms(L) for i in [1, 0, 3]: o1.add_onsite_term(strength1[i], i, "X_{i:d}".format(i=i)) assert o1.onsite_terms == [{"X_0": strength1[0]}, {"X_1": strength1[1]}, {}, {"X_3": strength1[3]}, {}, {}] # yapf: disable strength2 = np.arange(2., 2. + L * 0.25, 0.25) o2 = OnsiteTerms(L) for i in [1, 4, 3, 5]: o2.add_onsite_term(strength2[i], i, "Y_{i:d}".format(i=i)) o2.add_onsite_term(strength2[3], 3, "X_3") # add to previous part o2.add_onsite_term(-strength1[1], 1, "X_1") # remove previous part o1 += o2 assert o1.onsite_terms == [{"X_0": strength1[0]}, {"X_1": 0., "Y_1": strength2[1]}, {}, {"X_3": strength1[3] + strength2[3], "Y_3": strength2[3]}, {"Y_4": strength2[4]}, {"Y_5": strength2[5]}] # yapf: disable o1.remove_zeros() assert o1.onsite_terms == [{"X_0": strength1[0]}, {"Y_1": strength2[1]}, {}, {"X_3": strength1[3]+ strength2[3], "Y_3": strength2[3]}, {"Y_4": strength2[4]}, {"Y_5": strength2[5]}] # yapf: disable # convert to term_list tl = o1.to_TermList() assert tl.terms == [[("X_0", 0)], [("Y_1", 1)], [("X_3", 3)], [("Y_3", 3)], [("Y_4", 4)], [("Y_5", 5)]] o3, c3 = tl.to_OnsiteTerms_CouplingTerms([None] * L) assert o3.onsite_terms == o1.onsite_terms
def test_MPO_expectation_value(tol=1.e-15): s = spin_half psi1 = mps.MPS.from_singlets(s, 6, [(1, 3), (2, 5)], lonely=[0, 4], bc='infinite') psi1.test_sanity() ot = OnsiteTerms(4) # H.L != psi.L, consider L = lcm(4, 6) = 12 ot.add_onsite_term(0.1, 0, 'Sz') # -> 0.5 * 2 (sites 0, 4, not 8) ot.add_onsite_term(0.2, 3, 'Sz') # -> 0. (not sites 4, 7, 11) ct = CouplingTerms(4) # note: ct.L != psi1.L ct.add_coupling_term(1., 2, 3, 'Sz', 'Sz') # -> 0. (not 2-3, 6-7, 10-11) ct.add_coupling_term(1.5, 1, 3, 'Sz', 'Sz') # -> 1.5*(-0.25) (1-3, not 5-7, not 9-11) ct.add_coupling_term(2.5, 0, 6, 'Sz', 'Sz') # -> 2.5*0.25*3 (0-6, 4-10, not 8-14) H = mpo.MPOGraph.from_terms((ot, ct), [s] * 4, 'infinite').build_MPO() desired_ev = (0.1 * 0.5 * 2 + 0.2 * 0. + 1. * 0. + 1.5 * -0.25 + 2.5 * 0.25 * 2) / 12 ev_power = H.expectation_value_power(psi1, tol=tol) assert abs(ev_power - desired_ev) < tol ev_TM = H.expectation_value_TM(psi1, tol=tol) assert abs(ev_TM - desired_ev) < tol ev = H.expectation_value(psi1, tol) assert abs(ev - desired_ev) < tol # now with exponentially decaying term grid = [ [s.Id, s.Sz, 3 * s.Sz], [None, 0.1 * s.Id, s.Sz], [None, None, s.Id], ] L = 1 exp_dec_H = mpo.MPO.from_grids([s] * L, [grid] * L, bc='infinite', IdL=0, IdR=2) desired_ev = ( 3 * 0.5 * 2 + # Sz onsite 0.25 * ( 0.1**3 + 0.1**5 + 0.1**9 + 0.1**11 + 0.1**15 + # Z_0 Z_i 0.1**1 + 0.1**5 + 0.1**7 + 0.1**11 + 0.1**13) + # Z_4 Z_i -0.25 * ( 0.1**1 + # Z_1 Z_3 0.1**2) # Z_2 Z_4 + 0. # other sites ) / 6. # values > 1.e-15 ev_power = exp_dec_H.expectation_value_power(psi1, tol=tol) ev_TM = exp_dec_H.expectation_value_TM(psi1, tol=tol) assert abs(ev_power - desired_ev) < tol assert abs(ev_TM - desired_ev) < tol ev = exp_dec_H.expectation_value(psi1, tol=tol) assert abs(ev - desired_ev) < tol L = 3 # should give exactly the same answer! exp_dec_H = mpo.MPO.from_grids([s] * L, [grid] * L, bc='infinite', IdL=0, IdR=2) ev_power = exp_dec_H.expectation_value_power(psi1, tol=tol) ev_TM = exp_dec_H.expectation_value_TM(psi1, tol=tol) assert abs(ev_power - desired_ev) < tol assert abs(ev_TM - desired_ev) < tol ev = exp_dec_H.expectation_value(psi1, tol=tol) assert abs(ev - desired_ev) < tol