def test_double_site(): for site0, site1 in [[site.SpinHalfSite(None)] * 2, [site.SpinHalfSite('Sz')] * 2]: for charges in ['same', 'drop', 'independent']: ds = site.GroupedSite([site0, site1], charges=charges) ds.test_sanity() fs = site.FermionSite('N') ds = site.GroupedSite([fs, fs], ['a', 'b'], charges='same') assert ds.need_JW_string == set([op + 'a' for op in fs.need_JW_string] + [op + 'b' for op in fs.need_JW_string] + ['JW']) ss = site.GroupedSite([fs])
def test_double_site(): ss = site.SpinHalfSite('Sz') for site0, site1 in [[site.SpinHalfSite(None)] * 2, [ss] * 2]: for charges in ['same', 'drop', 'independent']: ds = site.DoubleSite(site0, site1, charges=charges) ds.test_sanity() fs = site.FermionSite('N') ds = site.DoubleSite(fs, fs, 'a', 'b', charges='same') assert ds.need_JW_string == set( [op + 'a' for op in fs.need_JW_string] + [op + 'b' for op in fs.need_JW_string])
def test_mps(): site_triv = site.SpinHalfSite(conserve=None) psi = mps.MPS.from_product_state([site_triv] * 4, [0, 1, 0, 1], bc='finite') psi.test_sanity() for L in [4, 2, 1]: print(L) state = (spin_half.state_indices(['up', 'down']) * L)[:L] psi = mps.MPS.from_product_state([spin_half] * L, state, bc='finite') psi.test_sanity() print(repr(psi)) print(str(psi)) psi2 = psi.copy() ov = psi.overlap(psi2) assert (abs(ov - 1.) < 1.e-15) if L > 1: npt.assert_equal(psi.entanglement_entropy(), 0.) # product state has no entanglement. E = psi.expectation_value('Sz') npt.assert_array_almost_equal_nulp(E, ([0.5, -0.5] * L)[:L], 100) C = psi.correlation_function('Sz', 'Sz') npt.assert_array_almost_equal_nulp(C, np.outer(E, E), 100) norm_err = psi.norm_test() assert (np.linalg.norm(norm_err) < 1.e-13) # example of doc in `from_product_state` L = 8 theta, phi = np.pi / 3, np.pi / 6 p_state = ["up", "down"] * (L // 2) # repeats entries L/2 times bloch_sphere_state = np.array([np.cos(theta / 2), np.exp(1.j * phi) * np.sin(theta / 2)]) p_state[L // 2] = bloch_sphere_state # replace one spin in center psi = mps.MPS.from_product_state([site_triv] * L, p_state, bc='finite', dtype=np.complex) eval_z = psi.expectation_value("Sigmaz") eval_x = psi.expectation_value("Sigmax") assert (eval_z[L // 2] - np.cos(theta)) < 1.e-12 assert (eval_x[L // 2] - np.sin(theta) * np.cos(phi)) < 1.e-12
def test_mps_add(): s = site.SpinHalfSite(conserve='Sz') u, d = 'up', 'down' psi1 = mps.MPS.from_product_state([s] * 4, [u, u, d, u], bc='finite') psi2 = mps.MPS.from_product_state([s] * 4, [u, d, u, u], bc='finite') npt.assert_equal(psi1.get_total_charge(True), [2]) psi_sum = psi1.add(psi2, 0.5**0.5, -0.5**0.5) npt.assert_almost_equal(psi_sum.norm, 1.) npt.assert_almost_equal(psi_sum.overlap(psi1), 0.5**0.5) npt.assert_almost_equal(psi_sum.overlap(psi2), -0.5**0.5) # check overlap with singlet state psi = mps.MPS.from_singlets(s, 4, [(1, 2)], lonely=[0, 3], up=u, down=d, bc='finite') npt.assert_almost_equal(psi_sum.overlap(psi), 1.) psi2_prime = mps.MPS.from_product_state([s] * 4, [u, u, u, u], bc='finite') npt.assert_equal(psi2_prime.get_total_charge(True), [4]) psi2_prime.apply_local_op(1, 'Sm', False, False) # now psi2_prime is psi2 up to gauging of charges. npt.assert_equal(psi2_prime.get_total_charge(True), [2]) # can MPS.add handle this? psi_sum_prime = psi1.add(psi2_prime, 0.5**0.5, -0.5**0.5) npt.assert_almost_equal(psi_sum_prime.overlap(psi), 1.)
def test_apply_op(bc, eps=1.e-13): s = site.SpinHalfSite(None) psi0 = mps.MPS.from_singlets(s, 3, [(0, 2)], lonely=[1], bc=bc, lonely_state='up') psi1 = psi0.copy() psi1.apply_local_op(1, 'Sigmax') #unitary psi1_expect = mps.MPS.from_singlets(s, 3, [(0, 2)], lonely=[1], bc=bc, lonely_state='down') psi1 = psi0.copy() psi1.apply_local_op(1, 'Sm') #non-unitary assert abs(psi1_expect.overlap(psi1) - 1.) < eps psi2 = psi0.copy() th = psi2.get_theta(0, 3).to_ndarray().reshape((8, )) s2 = 0.5**0.5 assert np.linalg.norm(th - [0., s2, 0., 0., -s2, 0., 0, 0.]) < eps psi2.apply_product_op(['Sigmax', 'Sm', 'Sigmax']) th = psi2.get_theta(0, 3).to_ndarray().reshape((8, )) assert np.linalg.norm(th - [0., 0., 0., -s2, 0., 0., s2, 0.]) < eps
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']))
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
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)
def test_increase_L(): s = site.SpinHalfSite(conserve='Sz') psi = mps.MPS.from_product_state([s] * 3, ['up', 'down', 'up'], bc='infinite') psi0 = psi.copy() psi.increase_L(9) psi.test_sanity() expval = psi.expectation_value('Sigmaz') npt.assert_equal(expval, [1., -1., 1.] * 3)
def test_spin_half_site_checks(): for conserve in [None, 'Sz', 'parity']: S = site.SpinHalfSite(conserve) S.test_sanity() if conserve != 'Sz': SxSy = ['Sx', 'Sy'] else: SxSy = None check_spin_site(S, SxSy=SxSy)
def test_InitialStateBuilder(): s0 = site.SpinHalfSite() lat = Chain(10, s0, bc_MPS='finite') psi1 = mps.InitialStateBuilder( lat, { 'method': 'lat_product_state', 'product_state': [['up'], ['down']], 'check_filling': 0.5, 'full_empty': ['up', 'down'], }).run() psi1.test_sanity() psi2 = mps.InitialStateBuilder( lat, { 'method': 'mps_product_state', 'product_state': ['up', 'down'] * 5, 'check_filling': 0.5, 'full_empty': ['up', 'down'], }).run() psi2.test_sanity() assert abs(psi1.overlap(psi2) - 1) < 1.e-14 psi3 = mps.InitialStateBuilder( lat, { 'method': 'fill_where', 'full_empty': ('up', 'down'), 'fill_where': "x_ind % 2 == 0", 'check_filling': 0.5, 'full_empty': ['up', 'down'], }).run() psi3.test_sanity() assert abs(psi1.overlap(psi3) - 1) < 1.e-14 psi4 = mps.InitialStateBuilder(lat, { 'method': 'randomized', 'randomized_from_method': 'lat_product_state', 'product_state': [['up'], ['down']], 'check_filling': 0.5, 'full_empty': ['up', 'down'], }, model_dtype=np.float64).run() assert psi4.dtype == np.float64 assert abs(psi4.overlap(psi1) - 1) > 0.1 # randomizing should lead to small overlap! psi5 = mps.InitialStateBuilder(lat, { 'method': 'randomized', 'randomized_from_method': 'lat_product_state', 'randomize_close_1': True, 'randomize_params': { 'N_steps': 2 }, 'product_state': [['up'], ['down']], 'check_filling': 0.5, 'full_empty': ['up', 'down'], }, model_dtype=np.complex128).run() assert psi5.dtype == np.complex128 assert 1.e-8 < abs(psi5.overlap(psi1) - 1) < 0.1 # but here we randomize only a bit
def test_mps_add(): s = site.SpinHalfSite(conserve='Sz') psi1 = mps.MPS.from_product_state([s] * 4, [0, 1, 0, 0], bc='finite') psi2 = mps.MPS.from_product_state([s] * 4, [0, 0, 1, 0], bc='finite') psi_sum = psi1.add(psi2, 0.5**0.5, -0.5**0.5) print(psi_sum) print(psi_sum._B[1]) print(psi_sum._B[2]) # check overlap with singlet state # TODO: doesn't work due to gauging of charges.... psi = mps.MPS.from_singlets(s, 4, [(1, 2)], lonely=[0, 3], up=0, down=1, bc='finite') print(psi.expectation_value('Sz'))
def test_roll_mps_unit_cell(): s = site.SpinHalfSite(conserve='Sz') psi = mps.MPS.from_product_state([s] * 4, ['down', 'up', 'up', 'up'], bc='infinite') psi1 = psi.copy() psi1.roll_mps_unit_cell(1) psi1.test_sanity() npt.assert_equal(psi.expectation_value('Sigmaz'), [-1., 1., 1., 1.]) npt.assert_equal(psi1.expectation_value('Sigmaz'), [1., -1., 1., 1.]) psi_m_1 = psi.copy() psi_m_1.roll_mps_unit_cell(-1) psi_m_1.test_sanity() npt.assert_equal(psi_m_1.expectation_value('Sigmaz'), [1., 1., 1., -1.])
def test_sample_measurements(eps=1.e-14, seed=5): spin_half = site.SpinHalfSite() u, d = spin_half.state_indices(['up', 'down']) spin_half.add_op('Pup', spin_half.Sz + 0.5 * spin_half.Id) psi = mps.MPS.from_singlets(spin_half, 6, [(0, 1), (2, 5)], lonely=[3, 4], bc='finite') rng = np.random.default_rng(seed) for i in range(4): sigmas, weight = psi.sample_measurements(3, 4, rng=rng) assert tuple(sigmas) == (u, u) assert abs(weight - 1) < eps sigmas, weight = psi.sample_measurements(0, 1, rng=rng) assert sigmas[0] == 1 - sigmas[1] print(sigmas) assert abs(weight - 0.5**0.5) < eps sigmas, weight = psi.sample_measurements(rng=rng) print(sigmas) assert sigmas[0] == 1 - sigmas[1] assert sigmas[2] == 1 - sigmas[5] sign = (+1 if sigmas[0] == u else -1) * (+1 if sigmas[2] == u else -1) print(sign, weight) assert abs(weight - 0.5 * sign) < eps sigmas, weight = psi.sample_measurements(ops=['Sz', 'Pup'], rng=rng) print(sigmas) assert sigmas[4] == 0.5 # Sz assert sigmas[3] == 1 # Pup spin_half = site.SpinHalfSite(conserve=None) assert tuple(spin_half.state_indices(['up', 'down'])) == (0, 1) x_basis = np.array([[1., 1], [1, -1]]) * 0.5**0.5 psi = mps.MPS.from_product_state([spin_half] * 4, [x_basis[0], x_basis[1], 0, 1]) for i in range(4): sigmas, weight = psi.sample_measurements( ops=['Sigmax', 'Sx', 'Sz', 'Sigmaz']) print(sigmas) npt.assert_allclose(sigmas, [1., -0.5, 0.5, -1.]) assert abs(abs(weight) - 1.) < eps
def test_enlarge_mps_unit_cell(): s = site.SpinHalfSite(conserve='Sz') psi = mps.MPS.from_product_state([s] * 3, ['up', 'down', 'up'], bc='infinite') psi0 = psi.copy() psi1 = psi.copy() with warnings.catch_warnings(): warnings.simplefilter("ignore", FutureWarning) psi0.increase_L(9) psi1.enlarge_mps_unit_cell(3) for psi in [psi0, psi1]: psi.test_sanity() expval = psi.expectation_value('Sigmaz') npt.assert_equal(expval, [1., -1., 1.] * 3)
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
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)
def test_mps_compress(method, eps=1.e-13): # Test compression of a sum of a state with itself L = 5 sites = [site.SpinHalfSite(conserve=None) for i in range(L)] plus_x = np.array([1., 1.]) / np.sqrt(2) minus_x = np.array([1., -1.]) / np.sqrt(2) psi = mps.MPS.from_product_state(sites, [plus_x for i in range(L)], bc='finite') psiOrth = mps.MPS.from_product_state(sites, [minus_x for i in range(L)], bc='finite') options = {'compression_method': method, 'trunc_params': {'chi_max': 30}} psiSum = psi.add(psi, .5, .5) psiSum.compress(options) assert (np.abs(psiSum.overlap(psi) - 1) < 1e-13) psiSum2 = psi.add(psiOrth, .5, .5) psiSum2.compress(options) psiSum2.test_sanity() assert (np.abs(psiSum2.overlap(psi) - .5) < 1e-13) assert (np.abs(psiSum2.overlap(psiOrth) - .5) < 1e-13)
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)
def test_group(): s = site.SpinHalfSite(conserve='parity') psi1 = mps.MPS.from_singlets(s, 6, [(1, 3), (2, 5)], lonely=[0, 4], bc='finite') psi2 = psi1.copy() print("group n=2") psi2.group_sites(n=2) assert psi2.L == psi1.L // 2 psi2.test_sanity() psi2.group_split({'chi_max': 2**3}) psi2.test_sanity() ov = psi1.overlap(psi2) assert abs(1. - ov) < 1.e-14 psi4 = psi1.copy() print("group n=4") psi4.group_sites(n=4) psi4.test_sanity() psi4.group_split({'chi_max': 2**3}) psi4.test_sanity() ov = psi1.overlap(psi4) assert abs(1. - ov) < 1.e-14
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)
def test_mps(): site_triv = site.SpinHalfSite(conserve=None) psi = mps.MPS.from_product_state([site_triv] * 4, [0, 1, 0, 1], bc='finite') psi.test_sanity() for L in [4, 2, 1]: print(L) state = (spin_half.state_indices(['up', 'down']) * L)[:L] psi = mps.MPS.from_product_state([spin_half] * L, state, bc='finite') psi.test_sanity() print(repr(psi)) print(str(psi)) psi2 = psi.copy() ov = psi.overlap(psi2) assert (abs(ov - 1.) < 1.e-15) if L > 1: npt.assert_equal(psi.entanglement_entropy(), 0.) # product state has no entanglement. E = psi.expectation_value('Sz') npt.assert_array_almost_equal_nulp(E, ([0.5, -0.5] * L)[:L], 100) C = psi.correlation_function('Sz', 'Sz') npt.assert_array_almost_equal_nulp(C, np.outer(E, E), 100) norm_err = psi.norm_test() assert (np.linalg.norm(norm_err) < 1.e-13)
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
def test_spin_half_site(): hcs = dict(Id='Id', JW='JW', Sx='Sx', Sy='Sy', Sz='Sz', Sp='Sm', Sm='Sp', Sigmax='Sigmax', Sigmay='Sigmay', Sigmaz='Sigmaz') sites = [] for conserve in [None, 'Sz', 'parity']: S = site.SpinHalfSite(conserve) S.test_sanity() for op in S.onsite_ops: assert S.hc_ops[op] == hcs[op] if conserve != 'Sz': SxSy = ['Sx', 'Sy'] else: SxSy = None check_spin_site(S, SxSy=SxSy) sites.append(S) check_same_operators(sites)
"""A collection of tests for :module:`tenpy.networks.terms`. """ # Copyright 2019 TeNPy Developers import numpy as np from tenpy.networks.terms import OnsiteTerms, CouplingTerms, MultiCouplingTerms from tenpy.networks import site spin_half = site.SpinHalfSite(conserve='Sz') 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
def test_spin_half_site(): sites = [] for conserve in [None, 'Sz', 'parity']: S = site.SpinHalfSite(conserve) sites.append(S) check_same_operators(sites)
def test_TrivialLattice(): s1 = site.SpinHalfSite('Sz') s2 = site.SpinSite(0.5, 'Sz') s3 = site.SpinSite(1.0, 'Sz') lat = lattice.TrivialLattice([s1, s2, s3, s2, s1]) lat.test_sanity()