def test_BaseDMRG_raises(): numpy_backend = backend_factory.get_backend('numpy') pytorch_backend = backend_factory.get_backend('pytorch') dtype = np.float64 N = 10 D = 10 Jz = np.ones(N - 1) Jxy = np.ones(N - 1) Bz = np.zeros(N) mpo = FiniteXXZ(Jz, Jxy, Bz, dtype=dtype, backend=numpy_backend) mps = FiniteMPS.random( [2] * (N - 1), [D] * (N - 2), dtype=dtype, backend=numpy_backend) with pytest.raises(ValueError): BaseDMRG(mps, mpo, numpy_backend.ones((1, 1, 1), dtype=dtype), numpy_backend.ones((1, 1, 1), dtype=dtype), 'name') mpo = FiniteXXZ(Jz, Jxy, Bz, dtype=np.float64, backend=numpy_backend) mps = FiniteMPS.random( [2] * N, [D] * (N - 1), dtype=np.float32, backend=numpy_backend) with pytest.raises( TypeError, match="mps.dtype = {} is different from " "mpo.dtype = {}".format(mps.dtype, mpo.dtype)): BaseDMRG(mps, mpo, numpy_backend.ones((1, 1, 1), dtype=dtype), numpy_backend.ones((1, 1, 1), dtype=dtype), 'name') mpo = FiniteXXZ(Jz, Jxy, Bz, dtype=np.float64, backend=numpy_backend) mps = FiniteMPS.random( [2] * N, [D] * (N - 1), dtype=np.float64, backend=pytorch_backend) with pytest.raises(TypeError, match="mps and mpo use different backends."): BaseDMRG(mps, mpo, numpy_backend.ones((1, 1, 1), dtype=dtype), numpy_backend.ones((1, 1, 1), dtype=dtype), 'name')
def test_BaseDMRG_position(backend_dtype_values): backend = backend_factory.get_backend(backend_dtype_values[0]) dtype = backend_dtype_values[1] N = 10 D = 10 Jz = np.ones(N - 1) Jxy = np.ones(N - 1) Bz = np.zeros(N) mpo = FiniteXXZ(Jz, Jxy, Bz, dtype=dtype, backend=backend) mps = FiniteMPS.random([2] * N, [D] * (N - 1), dtype=dtype, backend=backend) dmrg = BaseDMRG(mps, mpo, np.ones((1, 1, 1), dtype=dtype), np.ones((1, 1, 1), dtype=dtype), 'name') dmrg.position(N - 1) np.testing.assert_allclose(np.arange(N), sorted(list(dmrg.left_envs.keys()))) np.testing.assert_allclose([N - 1], list(dmrg.right_envs.keys())) assert dmrg.mps.center_position == N - 1 dmrg.position(0) np.testing.assert_allclose([0], list(dmrg.left_envs.keys())) np.testing.assert_allclose(np.arange(N), sorted(list(dmrg.right_envs.keys()))) assert dmrg.mps.center_position == 0 with pytest.raises(IndexError, match="site > length of mps"): dmrg.position(N) with pytest.raises(IndexError, match="site < 0"): dmrg.position(-1)
def test_BaseDMRG_init(backend_dtype_values): backend = backend_factory.get_backend(backend_dtype_values[0]) dtype = backend_dtype_values[1] N = 10 D = 10 Jz = np.ones(N - 1) Jxy = np.ones(N - 1) Bz = np.zeros(N) mpo = FiniteXXZ(Jz, Jxy, Bz, dtype=dtype, backend=backend) mps = FiniteMPS.random([2] * N, [D] * (N - 1), dtype=dtype, backend=backend) dmrg = BaseDMRG(mps, mpo, np.ones((1, 1, 1), dtype=dtype), np.ones((1, 1, 1), dtype=dtype), 'name') assert dmrg.name == 'name' assert dmrg.backend is backend
def test_compute_envs(backend_dtype_values): backend = backend_factory.get_backend(backend_dtype_values[0]) dtype = backend_dtype_values[1] N = 10 D = 10 Jz = np.ones(N - 1) Jxy = np.ones(N - 1) Bz = np.zeros(N) mpo = FiniteXXZ(Jz, Jxy, Bz, dtype=dtype, backend=backend) mps = FiniteMPS.random([2] * N, [D] * (N - 1), dtype=dtype, backend=backend) dmrg = BaseDMRG(mps, mpo, np.ones((1, 1, 1), dtype=dtype), np.ones((1, 1, 1), dtype=dtype), 'name') dmrg.position(5) dmrg.compute_left_envs() dmrg.compute_right_envs() np.testing.assert_allclose([0, 1, 2, 3, 4, 5], sorted(list(dmrg.left_envs.keys()))) np.testing.assert_allclose([5, 6, 7, 8, 9], sorted(list(dmrg.right_envs.keys())))
def test_BaseDMRG_raises_2(): backend = 'numpy' backend_obj = backend_factory.get_backend(backend) dtype = np.float64 N = 10 D = 10 Jz = np.ones(N - 1) Jxy = np.ones(N - 1) Bz = np.zeros(N) mpo = FiniteXXZ(Jz, Jxy, Bz, dtype=dtype, backend=backend) tensors = [np.random.randn(1, 2, D)] + [ np.random.randn(D, 2, D) for _ in range(N - 2) ] + [np.random.randn(D, 2, 1)] mps = FiniteMPS( tensors, center_position=None, canonicalize=False, backend=backend) with pytest.raises( ValueError, match="Found mps in non-canonical form. Please canonicalize mps."): BaseDMRG(mps, mpo, backend_obj.ones((1, 1, 1), dtype=dtype), backend_obj.ones((1, 1, 1), dtype=dtype), 'name')