Exemple #1
0
def test_lattice_order():
    s = site.SpinHalfSite('Sz')
    # yapf: disable
    square = lattice.Square(2, 2, s, order='default')
    order_default = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0]])
    npt.assert_equal(square.order, order_default)
    square = lattice.Square(4, 3, s, order='snake')
    order_snake = np.array([[0, 0, 0], [0, 1, 0], [0, 2, 0], [1, 2, 0], [1, 1, 0], [1, 0, 0],
                            [2, 0, 0], [2, 1, 0], [2, 2, 0], [3, 2, 0], [3, 1, 0], [3, 0, 0]])
    npt.assert_equal(square.order, order_snake)
    square = lattice.Square(2, 3, s, order=("standard", (True, False), (1, 0)))
    order_Fsnake = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 2, 0], [1, 2, 0]])
    npt.assert_equal(square.order, order_Fsnake)

    hc = lattice.Honeycomb(2, 3, s, order='default')
    order_hc_def = np.array([[0, 0, 0], [0, 1, 0], [0, 2, 0], [0, 0, 1], [0, 1, 1], [0, 2, 1],
                             [1, 0, 0], [1, 1, 0], [1, 2, 0], [1, 0, 1], [1, 1, 1], [1, 2, 1]])
    npt.assert_equal(hc.order, order_hc_def)
    hc = lattice.Honeycomb(2, 3, s, order=('standard', (True, False, False), (0.3, 0.1, -1.)))
    order_hc_mix = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 2, 0], [1, 2, 0],
                             [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1], [0, 2, 1], [1, 2, 1]])
    npt.assert_equal(hc.order, order_hc_mix)

    kag = lattice.Kagome(2, 3, s, order=('grouped', [[1], [0, 2]]))
    order_kag_gr = np.array([[0, 0, 1], [0, 1, 1], [0, 2, 1], [0, 0, 0], [0, 0, 2], [0, 1, 0],
                             [0, 1, 2], [0, 2, 0], [0, 2, 2],
                             [1, 0, 1], [1, 1, 1], [1, 2, 1], [1, 0, 0], [1, 0, 2], [1, 1, 0],
                             [1, 1, 2], [1, 2, 0], [1, 2, 2]])
    npt.assert_equal(kag.order, order_kag_gr)
Exemple #2
0
def test_number_nn():
    s = site.SpinHalfSite('Sz')
    chain = lattice.Chain(2, s)
    assert chain.number_nearest_neighbors() == 2
    assert chain.number_next_nearest_neighbors() == 2
    ladd = lattice.Ladder(2, s)
    assert ladd.number_nearest_neighbors(0) == 3
    assert ladd.number_nearest_neighbors(1) == 3
    assert ladd.number_next_nearest_neighbors(0) == 2
    assert ladd.number_next_nearest_neighbors(1) == 2
    square = lattice.Square(2, 2, s)
    assert square.number_nearest_neighbors() == 4
    assert square.number_next_nearest_neighbors() == 4
    triang = lattice.Triangular(2, 2, s)
    assert triang.number_nearest_neighbors() == 6
    assert triang.number_next_nearest_neighbors() == 6
    hc = lattice.Honeycomb(2, 2, s)
    assert hc.number_nearest_neighbors(0) == 3
    assert hc.number_nearest_neighbors(1) == 3
    assert hc.number_next_nearest_neighbors(0) == 6
    assert hc.number_next_nearest_neighbors(1) == 6
    kag = lattice.Kagome(2, 2, s)
    assert kag.number_nearest_neighbors(0) == 4
    assert kag.number_nearest_neighbors(1) == 4
    assert kag.number_nearest_neighbors(2) == 4
    assert kag.number_next_nearest_neighbors(0) == 4
    assert kag.number_next_nearest_neighbors(1) == 4
    assert kag.number_next_nearest_neighbors(2) == 4
Exemple #3
0
def test_IrregularLattice():
    s1 = site.SpinHalfSite('Sz')
    s2 = site.SpinSite(0.5, 'Sz')
    reg = lattice.Honeycomb(3, 3, [s1, s2], bc=['open', 'periodic'])
    ir = lattice.IrregularLattice(reg, [[1, 1, 0], [1, 1, 1], [0, 0, 0]],
                                  ([[1, 1, 2], [1, 1, 3]], [7, 10]), [s2, s2],
                                  [[-0.1, 0.0], [0.1, 0.0]])
    known = {  # written down by hand for this particular case
        (0, 1, (0, 0)): {'i': [5, 11, 0, 12, 1, 7, 13], 'j': [8, 14, 3, 15, 4, 10, 16]},
        (1, 0, (1, 0)): {'i': [ 2,  8,  4, 10], 'j': [5, 11, 7, 13]},
        (1, 0, (0, 1)): {'i': [ 2,  14,  3, 15, 10, 16], 'j': [0, 12, 1, 13, 5, 11]},
    }
    for (u0, u1, dx), expect in known.items():
        i, j, lat, sh = ir.possible_couplings(u0, u1, dx)
        print(i, j)
        sort = np.lexsort(lat.T)
        i = i[sort]
        j = j[sort]
        npt.assert_equal(i, np.array(expect['i']))
        npt.assert_equal(j, np.array(expect['j']))

        ops = [(None, dx, u1), (None, [0, 0], u0)]
        m_ji, m_lat_indices, m_coupling_shape = ir.possible_multi_couplings(ops)
        sort = np.lexsort(m_lat_indices.T)
        npt.assert_equal(m_ji[sort, 1], np.array(expect['i']))
        npt.assert_equal(m_ji[sort, 0], np.array(expect['j']))
Exemple #4
0
def test_possible_couplings():
    lat_reg = lattice.Honeycomb(2,
                                3, [None, None],
                                order="snake",
                                bc="periodic",
                                bc_MPS="infinite")
    lat_irreg = lattice.IrregularLattice(lat_reg, remove=[[0, 0, 0]])
    u0, u1 = 0, 1
    for lat in [lat_reg, lat_irreg]:
        for dx in [(0, 0), (0, 1), (2, 1), (-1, -1)]:
            print("dx =", dx)
            mps0, mps1, lat_indices, coupling_shape = lat.possible_couplings(u0, u1, dx)
            ops = [(None, [0, 0], u0), (None, dx, u1)]
            m_ijkl, m_lat_indices, m_coupling_shape = lat.possible_multi_couplings(ops)
            assert coupling_shape == m_coupling_shape
            if len(lat_indices) == 0:
                continue
            sort = np.lexsort(lat_indices.T)
            mps0, mps1, lat_indices = mps0[sort], mps1[sort], lat_indices[sort, :]
            assert m_ijkl.shape == (len(mps0), 2)
            m_sort = np.lexsort(m_lat_indices.T)
            m_ijkl, m_lat_indices = m_ijkl[m_sort, :], m_lat_indices[m_sort, :]
            npt.assert_equal(m_lat_indices, lat_indices)
            npt.assert_equal(mps0, m_ijkl[:, 0])
            npt.assert_equal(mps1, m_ijkl[:, 1])
Exemple #5
0
def test_pairs():
    lattices = [
        lattice.Chain(2, None),
        lattice.Ladder(2, None),
        lattice.Square(2, 2, None),
        lattice.Triangular(2, 2, None),
        lattice.Honeycomb(2, 2, None),
        lattice.Kagome(2, 2, None)
    ]
    for lat in lattices:
        print(lat.__class__.__name__)
        found_dist_pairs = lat.find_coupling_pairs(5, 3.)
        dists = sorted(found_dist_pairs.keys())
        for i, name in enumerate([
                'nearest_neighbors', 'next_nearest_neighbors', 'next_next_nearest_neighbors',
                'fourth_nearest_neighbors', 'fifth_nearest_neighbors'
        ]):
            if name not in lat.pairs:
                assert i > 2  # all of them should define up to next_next_nearest_neighbors
                continue
            print(name)
            defined_pairs = lat.pairs[name]
            found_pairs = found_dist_pairs[dists[i]]
            assert len(defined_pairs) == len(found_pairs)
            defined_pairs = pairs_with_reversed(defined_pairs)
            found_pairs = pairs_with_reversed(found_pairs)
            assert defined_pairs == found_pairs
Exemple #6
0
def test_lattice_order():
    s = site.SpinHalfSite('Sz')
    # yapf: disable
    square = lattice.Square(2, 2, s, 'default')
    order_default = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0]])
    npt.assert_equal(square.order, order_default)
    square = lattice.Square(4, 3, s, 'snake')
    order_snake = np.array([[0, 0, 0], [0, 1, 0], [0, 2, 0], [1, 2, 0], [1, 1, 0], [1, 0, 0],
                            [2, 0, 0], [2, 1, 0], [2, 2, 0], [3, 2, 0], [3, 1, 0], [3, 0, 0]])
    npt.assert_equal(square.order, order_snake)
    square = lattice.Square(2, 3, s, ((1, 0), (True, False)))
    order_Fsnake = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 2, 0], [1, 2, 0]])
    npt.assert_equal(square.order, order_Fsnake)

    hc = lattice.Honeycomb(2, 3, s, s, 'default')
    order_hc_def = np.array([[0, 0, 0], [0, 1, 0], [0, 2, 0], [0, 0, 1], [0, 1, 1], [0, 2, 1],
                             [1, 0, 0], [1, 1, 0], [1, 2, 0], [1, 0, 1], [1, 1, 1], [1, 2, 1]])
    npt.assert_equal(hc.order, order_hc_def)
    hc = lattice.Honeycomb(2, 3, s, s, ((0.3, 0.1, -1.), (True, False, False)))
    order_hc_mix = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 2, 0], [1, 2, 0],
                             [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1], [0, 2, 1], [1, 2, 1]])
    npt.assert_equal(hc.order, order_hc_mix)
Exemple #7
0
def test_number_nn():
    s = site.SpinHalfSite('Sz')
    chain = lattice.Chain(2, s)
    assert chain.number_nearest_neighbors() == 2
    assert chain.number_next_nearest_neighbors() == 2
    square = lattice.Square(2, 2, s)
    print(square.number_next_nearest_neighbors())
    assert square.number_nearest_neighbors() == 4
    assert square.number_next_nearest_neighbors() == 4
    hc = lattice.Honeycomb(2, 2, s, s)
    assert hc.number_nearest_neighbors(0) == 3
    assert hc.number_nearest_neighbors(1) == 3
    assert hc.number_next_nearest_neighbors(0) == 6
    assert hc.number_next_nearest_neighbors(1) == 6
Exemple #8
0
def test_index_conversion():
    from tenpy.networks.mps import MPS
    s = site.SpinHalfSite(conserve=None)
    state1 = [[[0, 1]]]  # 0=up, 1=down
    for order in ["snake", "default"]:
        lat = lattice.Honeycomb(2, 3, [s, s], order=order, bc_MPS="finite")
        psi1 = MPS.from_lat_product_state(lat, state1)
        sz1_mps = psi1.expectation_value("Sigmaz")
        sz1_lat = lat.mps2lat_values(sz1_mps)
        npt.assert_equal(sz1_lat[:, :, 0], +1.)
        npt.assert_equal(sz1_lat[:, :, 1], -1.)
        # and a random state
        state2 = np.random.random(lat.shape + (2, ))
        psi2 = MPS.from_lat_product_state(lat, state2)
        sz2_mps = psi2.expectation_value("Sigmaz")
        sz2_lat = lat.mps2lat_values(sz2_mps)
        expect_sz2 = np.sum(state2**2 * np.array([1., -1]), axis=-1)
        npt.assert_equal(sz2_lat, expect_sz2)
Exemple #9
0
def test_HelicalLattice():
    s = site.SpinHalfSite()
    honey = lattice.Honeycomb(2, 3, s, bc=['periodic', -1], bc_MPS='infinite', order='Cstyle')
    hel = lattice.HelicalLattice(honey, 2)
    strength = np.array([[1.5, 2.5, 1.5], [2.5, 1.5, 2.5]])

    def assert_same(i1, j1, s1, ij2, s2):
        """check that coupling and multi_coupling agree up to sorting order"""
        assert len(i1) == len(ij2)
        sort1 = np.lexsort(np.stack([i1, j1]))
        sort2 = np.lexsort(ij2.T)
        for a, b in zip(sort1, sort2):
            assert (i1[a], j1[a]) == tuple(ij2[b])
            assert s1[a] == s2[b]

    i, j, s = hel.possible_couplings(0, 1, [0, 0], strength)
    ijm, sm = hel.possible_multi_couplings([('X', [0, 0], 0), ('X', [0, 0], 1)], strength)
    assert np.all(i == [0, 2]) and np.all(j == [1, 3])
    assert np.all(s == [1.5, 2.5])
    assert_same(i, j, s, ijm, sm)

    i, j, s = hel.possible_couplings(0, 0, [1, 0], strength)
    ijm, sm = hel.possible_multi_couplings([('X', [0, 0], 0), ('X', [1, 0], 0)], strength)
    assert np.all(i == [0, 2]) and np.all(j == [6, 8])
    assert np.all(s == [1.5, 2.5])
    assert_same(i, j, s, ijm, sm)

    i, j, s = hel.possible_couplings(0, 0, [-1, 1], strength)
    assert np.all(i == [4, 6]) and np.all(j == [0, 2])
    assert np.all(s == [2.5, 1.5])  # swapped!
    ijm, sm = hel.possible_multi_couplings([('X', [0, 0], 0), ('X', [-1, 1], 0)], strength)
    assert_same(i, j, s, ijm, sm)
    ijm, sm = hel.possible_multi_couplings([('X', [1, 0], 0), ('X', [0, 1], 0)], strength)
    assert_same(i, j, s, ijm, sm)

    # test that MPS.from_lat_product_state checks translation invariance
    p_state = [[['up', 'down'], ['down', 'down'], ['up', 'down']],
               [['down', 'down'], ['up', 'down'], ['down', 'down']]]
    psi = MPS.from_lat_product_state(hel, p_state)
    p_state[0][2] = ['down', 'down']
    with pytest.raises(ValueError, match='.* not translation invariant .*'):
        psi = MPS.from_lat_product_state(hel, p_state)
Exemple #10
0
def test_number_nn():
    s = site.SpinHalfSite('Sz')
    chain = lattice.Chain(2, s)
    assert chain.count_neighbors() == 2
    assert chain.count_neighbors(key='next_nearest_neighbors') == 2
    ladd = lattice.Ladder(2, s)
    for u in [0, 1]:
        assert ladd.count_neighbors(u) == 3
        assert ladd.count_neighbors(u, key='next_nearest_neighbors') == 2
    square = lattice.Square(2, 2, s)
    assert square.count_neighbors() == 4
    assert square.count_neighbors(key='next_nearest_neighbors') == 4
    triang = lattice.Triangular(2, 2, s)
    assert triang.count_neighbors() == 6
    assert triang.count_neighbors(key='next_nearest_neighbors') == 6
    hc = lattice.Honeycomb(2, 2, s)
    for u in [0, 1]:
        assert hc.count_neighbors(u) == 3
        assert hc.count_neighbors(u, key='next_nearest_neighbors') == 6
    kag = lattice.Kagome(2, 2, s)
    for u in [0, 1, 2]:
        assert kag.count_neighbors(u) == 4
        assert kag.count_neighbors(u, key='next_nearest_neighbors') == 4