Example #1
0
def test_multi_sites_combine_charges():
    spin = site.SpinSite(0.5, 'Sz')
    spin1 = site.SpinSite(1, 'Sz')
    ferm = site.SpinHalfFermionSite(cons_N='N', cons_Sz='Sz')
    spin_ops = {
        op_name: get_site_op_flat(spin, op_name)
        for op_name in spin.opnames
    }
    spin1_ops = {
        op_name: get_site_op_flat(spin1, op_name)
        for op_name in spin1.opnames
    }
    ferm_ops = {
        op_name: get_site_op_flat(ferm, op_name)
        for op_name in ferm.opnames
    }
    site.multi_sites_combine_charges([spin, ferm])
    assert tuple(spin.leg.chinfo.names) == ('2*Sz', 'N', 'Sz')
    spin.test_sanity()
    ferm.test_sanity()
    for op_name, op_flat in spin_ops.items():
        op_flat2 = get_site_op_flat(spin, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in ferm_ops.items():
        op_flat2 = get_site_op_flat(ferm, op_name)
        npt.assert_equal(op_flat, op_flat2)

    spin = site.SpinSite(0.5, 'Sz')
    ferm = site.SpinHalfFermionSite(cons_N='N', cons_Sz='Sz')
    site.multi_sites_combine_charges([ferm, spin],
                                     same_charges=[[(0, 'Sz'), (1, '2*Sz')]])
    assert tuple(ferm.leg.chinfo.names) == ('N', 'Sz')
    spin.test_sanity()
    ferm.test_sanity()
    for op_name, op_flat in spin_ops.items():
        op_flat2 = get_site_op_flat(spin, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in ferm_ops.items():
        op_flat2 = get_site_op_flat(ferm, op_name)
        npt.assert_equal(op_flat, op_flat2)

    # and finally a few more, changing orders as well
    ferm = site.SpinHalfFermionSite(cons_N='N', cons_Sz='Sz')
    spin = site.SpinSite(0.5, 'Sz')
    spin1 = site.SpinSite(1, 'Sz')
    site.multi_sites_combine_charges([ferm, spin1, spin],
                                     same_charges=[[(0, 'Sz'), (2, '2*Sz')]])
    assert tuple(ferm.leg.chinfo.names) == ('N', 'Sz', '2*Sz')
    spin.test_sanity()
    ferm.test_sanity()
    spin1.test_sanity()
    for op_name, op_flat in spin_ops.items():
        op_flat2 = get_site_op_flat(spin, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in ferm_ops.items():
        op_flat2 = get_site_op_flat(ferm, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in spin1_ops.items():
        op_flat2 = get_site_op_flat(spin1, op_name)
        npt.assert_equal(op_flat, op_flat2)
Example #2
0
def test_spin_half_fermion_site():
    for cons_N, cons_Sz in it.product(['N', 'parity', None], ['Sz', 'parity', None]):
        print("conserve ", repr(cons_N), repr(cons_Sz))
        S = site.SpinHalfFermionSite(cons_N, cons_Sz)
        S.test_sanity()
        Id = S.Id.to_ndarray()
        JW = S.JW.to_ndarray()
        Cu, Cd = S.Cu.to_ndarray(), S.Cd.to_ndarray()
        Cdu, Cdd = S.Cdu.to_ndarray(), S.Cdd.to_ndarray()
        Nu, Nd, Ntot = S.Nu.to_ndarray(), S.Nd.to_ndarray(), S.Ntot.to_ndarray()
        npt.assert_equal(np.dot(Cdu, Cu), Nu)
        npt.assert_equal(np.dot(Cdd, Cd), Nd)
        npt.assert_equal(Nu + Nd, Ntot)
        npt.assert_equal(np.dot(Nu, Nd), S.NuNd.to_ndarray())
        npt.assert_equal(anticommutator(Cdu, Cu), Id)
        npt.assert_equal(anticommutator(Cdd, Cd), Id)
        # anti-commutate with Jordan-Wigner
        npt.assert_equal(np.dot(Cu, JW), -np.dot(JW, Cu))
        npt.assert_equal(np.dot(Cd, JW), -np.dot(JW, Cd))
        npt.assert_equal(np.dot(Cdu, JW), -np.dot(JW, Cdu))
        npt.assert_equal(np.dot(Cdd, JW), -np.dot(JW, Cdd))
        # anti-commute Cu with Cd
        npt.assert_equal(np.dot(Cu, Cd), -np.dot(Cd, Cu))
        npt.assert_equal(np.dot(Cu, Cdd), -np.dot(Cdd, Cu))
        npt.assert_equal(np.dot(Cdu, Cd), -np.dot(Cd, Cdu))
        npt.assert_equal(np.dot(Cdu, Cdd), -np.dot(Cdd, Cdu))
        if cons_Sz != 'Sz':
            SxSy = ['Sx', 'Sy']
        else:
            SxSy = None
        check_spin_site(S, SxSy=SxSy)
Example #3
0
def test_expectation_value_term():
    s = spin_half
    psi1 = mps.MPS.from_singlets(s, 6, [(1, 3), (2, 5)], lonely=[0, 4], bc='finite')
    ev = psi1.expectation_value_term([('Sz', 2), ('Sz', 3)])
    assert abs(0. - ev) < 1.e-14
    ev = psi1.expectation_value_term([('Sz', 1), ('Sz', 3)])
    assert abs(-0.25 - ev) < 1.e-14
    ev = psi1.expectation_value_term([('Sz', 3), ('Sz', 1), ('Sz', 4)])
    assert abs(-0.25 * 0.5 - ev) < 1.e-14
    fs = site.SpinHalfFermionSite()
    # check fermionic signs
    psi2 = mps.MPS.from_product_state([fs] * 4, ['empty', 'up', 'down', 'full'], bc="infinite")
    ev = psi2.expectation_value_term([('Cu', 2), ('Nu', 1), ('Cdu', 2)])
    assert 1. == ev
    ev2 = psi2.expectation_value_term([('Cu', 2), ('Cd', 1), ('Cdd', 1), ('Cdu', 2)])
    assert ev2 == ev
    ev3 = psi2.expectation_value_term([('Cd', 1), ('Cu', 2), ('Cdd', 1), ('Cdu', 2)])
    assert ev3 == -ev2
    # over the infinite MPS boundary
    ev = psi2.expectation_value_term([('Nu', 1), ('Nd', 4)])  # should be zero
    assert abs(ev) == 0.
    ev = psi2.expectation_value_term([('Nu', 1), ('Nd', 6)])
    assert abs(ev) == 1.
    # terms_sum
    pref = np.random.random([5])
    term_list = TermList(
        [[('Nd', 0)], [('Nu', 1), ('Nd', 2)], [('Nd', 2),
                                               ('Nu', 5)], [('Nu Nd', 3)], [('Nu', 1),
                                                                            ('Nu', 5)]], pref)
    desired = sum(pref[1:])
    assert desired == sum(
        [psi2.expectation_value_term(term) * strength for term, strength in term_list])
    evsum, _ = psi2.expectation_value_terms_sum(term_list)
    assert abs(evsum - desired) < 1.e-14
Example #4
0
def test_spin_half_fermion_site_ops():
    sites = []
    for cons_n, cons_sz in it.product(['N', 'parity', None],
                                      ['Sz', 'parity', None]):
        S = site.SpinHalfFermionSite(cons_n, cons_sz)
        sites.append(S)
    check_same_operators(sites)
Example #5
0
def test_correlation_function():
    s = spin_half
    psi1 = mps.MPS.from_singlets(s, 6, [(1, 3), (2, 5)], lonely=[0, 4], bc='finite')
    corr1 = psi1.correlation_function('Sz', 'Sz')
    corr1_exact = 0.25 * np.array([[ 1.,  0.,  0.,  0.,  1.,  0.],
                                   [ 0.,  1.,  0., -1.,  0.,  0.],
                                   [ 0.,  0.,  1.,  0.,  0., -1.],
                                   [ 0., -1.,  0.,  1.,  0.,  0.],
                                   [ 1.,  0.,  0.,  0.,  1.,  0.],
                                   [ 0.,  0., -1.,  0.,  0.,  1.]])  # yapf: disable
    npt.assert_almost_equal(corr1, corr1_exact)
    corr1 = psi1.term_correlation_function_right([('Sz', 0)], [('Sz', 0)])
    npt.assert_almost_equal(corr1, corr1_exact[0, 1:])
    corr1 = psi1.term_correlation_function_right([('Sz', 0)], [('Sz', 1)])
    npt.assert_almost_equal(corr1, corr1_exact[0, 1:])
    corr1 = psi1.term_correlation_function_right([('Sz', 1)], [('Sz', 1)])
    npt.assert_almost_equal(corr1, corr1_exact[1, 2:])
    corr1 = psi1.term_correlation_function_right([('Sz', 1)], [('Sz', -1)])
    npt.assert_almost_equal(corr1, corr1_exact[1, 2:-1])

    corr1 = psi1.term_correlation_function_left([('Sz', 0)], [('Sz', 0)], range(0, 5), 5)
    npt.assert_almost_equal(corr1[::-1], corr1_exact[:-1, 5])
    corr1 = psi1.term_correlation_function_left([('Sz', 1)], [('Sz', 1)], range(0, 4), 4)
    npt.assert_almost_equal(corr1[::-1], corr1_exact[1:-1, 5])

    # check fermionic signs
    fs = site.SpinHalfFermionSite()
    psi2 = mps.MPS.from_product_state([fs] * 4, ['empty', 'up', 'down', 'full'], bc="infinite")
    corr2 = psi2.correlation_function('Cdu', 'Cu')
    corr2_exact = np.array([[ 0.,  0.,  0.,  0.],
                            [ 0.,  1.,  0.,  0.],
                            [ 0.,  0.,  0.,  0.],
                            [ 0.,  0.,  0.,  1.]])  # yapf: disable
    npt.assert_almost_equal(corr2, corr2_exact)
    psi3 = psi2.copy()
    from tenpy.algorithms.tebd import RandomUnitaryEvolution
    RandomUnitaryEvolution(psi3, {'N_steps': 4}).run()

    corr3 = psi3.correlation_function('Cdu', 'Cu')
    corr3_d = psi3.correlation_function('Cu', 'Cdu')
    npt.assert_almost_equal(np.diag(corr3) + np.diag(corr3_d), 1.)
    corr3 = corr3 - np.diag(np.diag(corr3))  # remove diagonal
    corr3_d = corr3_d - np.diag(np.diag(corr3_d))
    npt.assert_array_almost_equal(corr3, -corr3_d.T)  # check anti-commutation of operators

    corr = psi3.term_correlation_function_right([('Cdu', 0)], [('Cu', 0)], j_R=range(1, 4))
    npt.assert_almost_equal(corr, corr3[0, 1:])
    corr3_long = psi3.correlation_function('Cdu', 'Cu', [0], range(4, 11 * 4, 4)).flatten()
    corr3_long2 = psi3.term_correlation_function_right([('Cdu', 0)], [('Cu', 0)])
    npt.assert_array_almost_equal(corr3_long, corr3_long2)
Example #6
0
def test_spin_half_fermion_site():
    hcs = dict(Id='Id', JW='JW', JWu='JWu', JWd='JWd',
               Cu='Cdu', Cdu='Cu', Cd='Cdd', Cdd='Cd',
               Nu='Nu', Nd='Nd', NuNd='NuNd', Ntot='Ntot', dN='dN',
               Sx='Sx', Sy='Sy', Sz='Sz', Sp='Sm', Sm='Sp')  # yapf: disable
    sites = []
    for cons_N, cons_Sz in it.product(['N', 'parity', None],
                                      ['Sz', 'parity', None]):
        print("conserve ", repr(cons_N), repr(cons_Sz))
        S = site.SpinHalfFermionSite(cons_N, cons_Sz)
        S.test_sanity()
        for op in S.onsite_ops:
            assert S.hc_ops[op] == hcs[op]
        Id = S.Id.to_ndarray()
        JW = S.JW.to_ndarray()
        Cu, Cd = S.Cu.to_ndarray(), S.Cd.to_ndarray()
        Cdu, Cdd = S.Cdu.to_ndarray(), S.Cdd.to_ndarray()
        Nu, Nd, Ntot = S.Nu.to_ndarray(), S.Nd.to_ndarray(), S.Ntot.to_ndarray(
        )
        npt.assert_equal(np.dot(Cdu, Cu), Nu)
        npt.assert_equal(np.dot(Cdd, Cd), Nd)
        npt.assert_equal(Nu + Nd, Ntot)
        npt.assert_equal(np.dot(Nu, Nd), S.NuNd.to_ndarray())
        npt.assert_equal(anticommutator(Cdu, Cu), Id)
        npt.assert_equal(anticommutator(Cdd, Cd), Id)
        # anti-commutate with Jordan-Wigner
        npt.assert_equal(np.dot(Cu, JW), -np.dot(JW, Cu))
        npt.assert_equal(np.dot(Cd, JW), -np.dot(JW, Cd))
        npt.assert_equal(np.dot(Cdu, JW), -np.dot(JW, Cdu))
        npt.assert_equal(np.dot(Cdd, JW), -np.dot(JW, Cdd))
        # anti-commute Cu with Cd
        npt.assert_equal(np.dot(Cu, Cd), -np.dot(Cd, Cu))
        npt.assert_equal(np.dot(Cu, Cdd), -np.dot(Cdd, Cu))
        npt.assert_equal(np.dot(Cdu, Cd), -np.dot(Cd, Cdu))
        npt.assert_equal(np.dot(Cdu, Cdd), -np.dot(Cdd, Cdu))
        if cons_Sz != 'Sz':
            SxSy = ['Sx', 'Sy']
        else:
            SxSy = None
        check_spin_site(S, SxSy=SxSy)
        sites.append(S)
    check_same_operators(sites)
Example #7
0
def test_correlation_function():
    s = spin_half
    Pup = s.Id.copy()
    Pup[s.state_labels['down'], s.state_labels['down']] = 0.
    Pdown = s.Id.copy()
    Pdown[s.state_labels['up'], s.state_labels['up']] = 0.
    s.add_op('Pup', Pup, need_JW=False, hc='Pup')
    s.add_op('Pdown', Pdown, need_JW=False, hc='Pdown')
    psi1 = mps.MPS.from_singlets(s,
                                 6, [(1, 3), (2, 5)],
                                 lonely=[0, 4],
                                 bc='finite')
    corr1 = psi1.correlation_function('Sz', 'Sz')
    corr1_exact = 0.25 * np.array([[ 1.,  0.,  0.,  0.,  1.,  0.],
                                   [ 0.,  1.,  0., -1.,  0.,  0.],
                                   [ 0.,  0.,  1.,  0.,  0., -1.],
                                   [ 0., -1.,  0.,  1.,  0.,  0.],
                                   [ 1.,  0.,  0.,  0.,  1.,  0.],
                                   [ 0.,  0., -1.,  0.,  0.,  1.]])  # yapf: disable
    npt.assert_almost_equal(corr1, corr1_exact)
    corr1 = psi1.term_correlation_function_right([('Sz', 0)], [('Sz', 0)])
    npt.assert_almost_equal(corr1, corr1_exact[0, 1:])
    corr1 = psi1.term_correlation_function_right([('Sz', 0)], [('Sz', 1)])
    npt.assert_almost_equal(corr1, corr1_exact[0, 1:])
    corr1 = psi1.term_correlation_function_right([('Sz', 1)], [('Sz', 1)])
    npt.assert_almost_equal(corr1, corr1_exact[1, 2:])
    corr1 = psi1.term_correlation_function_right([('Sz', 1)], [('Sz', -1)])
    npt.assert_almost_equal(corr1, corr1_exact[1, 2:-1])

    corr1 = psi1.term_correlation_function_left([('Sz', 0)], [('Sz', 0)],
                                                range(0, 5), 5)
    npt.assert_almost_equal(corr1[::-1], corr1_exact[:-1, 5])
    corr1 = psi1.term_correlation_function_left([('Sz', 1)], [('Sz', 1)],
                                                range(0, 4), 4)
    npt.assert_almost_equal(corr1[::-1], corr1_exact[1:-1, 5])

    Sz = TermList([[('Pup', 0)], [('Pdown', 0)]],
                  [0.5, -0.5])  # complicated way to write Sz
    corr1 = psi1.term_list_correlation_function_right(Sz, Sz)
    # check term_list_correlation_function_right for terms with different qtotal
    npt.assert_almost_equal(corr1, corr1_exact[0, 1:])
    Sx = TermList([[('Sp', 0)], [('Sm', 0)]],
                  [0.5, +0.5])  # complicated way to write Sx_0
    Sy = TermList([[('Sp', 1)], [('Sm', 1)]],
                  [-0.5j, +0.5j])  # complicated way to write Sy_1
    corrxx = psi1.term_list_correlation_function_right(Sx, Sx)
    npt.assert_almost_equal(corrxx, np.zeros((5, )))  # Sx_0 gives 0
    corrxx = psi1.term_list_correlation_function_right(Sx, Sx, 1)
    npt.assert_almost_equal(corrxx, 0.25 * np.array([0., -1., 0., 0.]))
    corrxy = psi1.term_list_correlation_function_right(Sx, Sy, 1, range(1, 5))
    npt.assert_almost_equal(corrxy, np.zeros((4, )))

    # check fermionic signs
    fs = site.SpinHalfFermionSite()
    psi2 = mps.MPS.from_product_state([fs] * 4,
                                      ['empty', 'up', 'down', 'full'],
                                      bc="infinite")
    corr2 = psi2.correlation_function('Cdu', 'Cu')
    corr2_exact = np.array([[ 0.,  0.,  0.,  0.],
                            [ 0.,  1.,  0.,  0.],
                            [ 0.,  0.,  0.,  0.],
                            [ 0.,  0.,  0.,  1.]])  # yapf: disable
    npt.assert_almost_equal(corr2, corr2_exact)
    psi3 = psi2.copy()
    from tenpy.algorithms.tebd import RandomUnitaryEvolution
    RandomUnitaryEvolution(psi3, {'N_steps': 4}).run()

    corr3 = psi3.correlation_function('Cdu', 'Cu')
    corr3_d = psi3.correlation_function('Cu', 'Cdu')
    npt.assert_almost_equal(np.diag(corr3) + np.diag(corr3_d), 1.)
    corr3 = corr3 - np.diag(np.diag(corr3))  # remove diagonal
    corr3_d = corr3_d - np.diag(np.diag(corr3_d))
    npt.assert_array_almost_equal(
        corr3, -corr3_d.T)  # check anti-commutation of operators

    corr = psi3.term_correlation_function_right([('Cdu', 0)], [('Cu', 0)],
                                                j_R=range(1, 4))
    npt.assert_almost_equal(corr, corr3[0, 1:])
    corr3_long = psi3.correlation_function('Cdu', 'Cu', [0],
                                           range(4, 11 * 4, 4)).flatten()
    corr3_long2 = psi3.term_correlation_function_right([('Cdu', 0)],
                                                       [('Cu', 0)])
    npt.assert_array_almost_equal(corr3_long, corr3_long2)
    term1 = TermList([[('Cdu', 0)]], [1.])
    term2 = TermList([[('Cu', 0)], [('Ntot', -1)]],
                     [1., 2.])  # N shouldn't contribute!
    corr3_long3 = psi3.term_list_correlation_function_right(term1, term2)
Example #8
0
def test_set_common_charges():
    spin = site.SpinSite(0.5, 'Sz')
    spin1 = site.SpinSite(1, 'Sz')
    ferm = site.SpinHalfFermionSite(cons_N='N', cons_Sz='Sz')
    boson = site.BosonSite(2, 'N')
    spin_ops = {
        op_name: get_site_op_flat(spin, op_name)
        for op_name in spin.opnames
    }
    spin1_ops = {
        op_name: get_site_op_flat(spin1, op_name)
        for op_name in spin1.opnames
    }
    ferm_ops = {
        op_name: get_site_op_flat(ferm, op_name)
        for op_name in ferm.opnames
    }
    boson_ops = {
        op_name: get_site_op_flat(boson, op_name)
        for op_name in boson.opnames
    }
    site.set_common_charges([spin, ferm])
    assert tuple(spin.leg.chinfo.names) == ('2*Sz', 'N')
    spin.test_sanity()
    ferm.test_sanity()
    for op_name, op_flat in spin_ops.items():
        op_flat2 = get_site_op_flat(spin, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in ferm_ops.items():
        op_flat2 = get_site_op_flat(ferm, op_name)
        npt.assert_equal(op_flat, op_flat2)

    spin = site.SpinSite(0.5, 'Sz')
    ferm = site.SpinHalfFermionSite(cons_N='N', cons_Sz='Sz')
    site.set_common_charges([ferm, spin],
                            new_charges=[[(1, 0, '2*Sz'), (1, 1, '2*Sz')]])
    assert tuple(ferm.leg.chinfo.names) == ('2*Sz', )
    spin.test_sanity()
    ferm.test_sanity()
    for op_name, op_flat in spin_ops.items():
        op_flat2 = get_site_op_flat(spin, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in ferm_ops.items():
        op_flat2 = get_site_op_flat(ferm, op_name)
        npt.assert_equal(op_flat, op_flat2)

    # and finally a few more, changing orders as well
    ferm = site.SpinHalfFermionSite(cons_N='N', cons_Sz='Sz')
    spin = site.SpinSite(0.5, 'Sz')
    spin1 = site.SpinSite(1, 'Sz')
    boson = site.BosonSite(2, 'N')

    site.set_common_charges(
        [ferm, spin1, spin, boson],
        new_charges=[[(1, 0, '2*Sz'), (1, 2, '2*Sz')],
                     [(2, 0, 'N'), (1, 3, 'N')], [(0.5, 1, '2*Sz')]],
        new_names=['2*(Sz_f + Sz_spin-half)', '2*N_f+N_b', 'Sz_spin-1'])
    assert tuple(ferm.leg.chinfo.names) == ('2*(Sz_f + Sz_spin-half)',
                                            '2*N_f+N_b', 'Sz_spin-1')
    spin.test_sanity()
    ferm.test_sanity()
    spin1.test_sanity()
    boson.test_sanity()
    for op_name, op_flat in spin_ops.items():
        op_flat2 = get_site_op_flat(spin, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in ferm_ops.items():
        op_flat2 = get_site_op_flat(ferm, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in spin1_ops.items():
        op_flat2 = get_site_op_flat(spin1, op_name)
        npt.assert_equal(op_flat, op_flat2)
    for op_name, op_flat in boson_ops.items():
        op_flat2 = get_site_op_flat(boson, op_name)
        npt.assert_equal(op_flat, op_flat2)