def test_position_truncation(backend): D, d, N = 10, 2, 10 tensors = [np.ones((1, d, D))] + [np.ones((D, d, D)) for _ in range(N - 2) ] + [np.ones((D, d, 1))] mps = BaseMPS(tensors, center_position=0, backend=backend) mps.position(N-1) mps.position(0, D=5) assert np.all(np.array(mps.bond_dimensions) <= 5)
def test_normalization(backend): D, d, N = 10, 2, 10 tensors = [np.random.randn(1, d, D) ] + [np.random.randn(D, d, D) for _ in range(N - 2)] + [np.random.randn(D, d, 1)] mps = BaseMPS(tensors, center_position=0, backend=backend) mps.position(len(mps) - 1) Z = mps.position(0, normalize=True) np.testing.assert_allclose(Z, 1.0)
def test_position_raises_error(backend): D, d, N = 10, 2, 10 tensors = [np.random.randn(1, d, D) ] + [np.random.randn(D, d, D) for _ in range(N - 2)] + [np.random.randn(D, d, 1)] mps = BaseMPS(tensors, center_position=0, backend=backend) with pytest.raises(ValueError): mps.position(-1) with pytest.raises(ValueError): mps.position(11)
def test_left_orthonormalization(backend_dtype_values): backend = backend_dtype_values[0] dtype = backend_dtype_values[1] D, d, N = 10, 2, 10 tensors = [get_random_np((1, d, D), dtype) ] + [get_random_np((D, d, D), dtype) for _ in range(N - 2)] + [get_random_np((D, d, 1), dtype)] mps = BaseMPS(tensors, center_position=N - 1, backend=backend) mps.position(0) mps.position(len(mps) - 1) assert all( abs(mps.check_orthonormality('left', site)) < 1E-12 for site in range(len(mps)))
def test_position_raises_error(backend): D, d, N = 10, 2, 10 tensors = [np.random.randn(1, d, D)] + [ np.random.randn(D, d, D) for _ in range(N - 2) ] + [np.random.randn(D, d, 1)] mps = BaseMPS(tensors, center_position=0, backend=backend) with pytest.raises( ValueError, match="site = -1 not between values" " 0 < site < N = 10"): mps.position(-1) with pytest.raises( ValueError, match="site = 11 not between values" " 0 < site < N = 10"): mps.position(11) mps = BaseMPS(tensors, center_position=None, backend=backend) with pytest.raises( ValueError, match="BaseMPS.center_position is" " `None`, cannot shift `center_position`." "Reset `center_position` manually or use `canonicalize`"): mps.position(1) mps = BaseMPS(tensors, center_position=0, backend=backend) with pytest.raises( ValueError, match="max_truncation_err"): mps.position(1, max_truncation_err=1.1)
def test_position_no_shift_no_normalization(backend): D, d, N = 4, 2, 6 tensors = [np.ones((1, d, D))] + [np.ones((D, d, D)) for _ in range(N - 2) ] + [np.ones((D, d, 1))] mps = BaseMPS(tensors, center_position=int(N / 2), backend=backend) Z = mps.position(int(N / 2), normalize=False) np.testing.assert_allclose(Z, 5.656854)
def test_position_shift_right(backend): D, d, N = 4, 2, 6 tensors = [np.ones((1, d, D))] + [np.ones((D, d, D)) for _ in range(N - 2) ] + [np.ones((D, d, 1))] mps = BaseMPS(tensors, center_position=int(N / 2), backend=backend) Z = mps.position(N - 1, normalize=True) np.testing.assert_allclose(Z, 2.828427)
def test_position_no_normalization(backend): D, d, N = 4, 2, 6 tensors = [np.ones((1, d, D))] + [np.ones((D, d, D)) for _ in range(N - 2) ] + [np.ones((D, d, 1))] mps = BaseMPS(tensors, center_position=0, backend=backend) Z = mps.position(len(mps) - 1, normalize=False) np.testing.assert_allclose(Z, 8192.0)