Example #1
0
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 = FiniteMPS(tensors, center_position=0, backend=backend)
    mps.position(len(mps) - 1)
    Z = mps.position(0, normalize=True)
    np.testing.assert_allclose(Z, 1.0)
Example #2
0
def test_apply_one_site_gate(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 = FiniteMPS(tensors, center_position=0, backend=backend)
    gate = get_random_np((2, 2), dtype)
    mps.apply_one_site_gate(gate, 5)
    actual = np.transpose(np.tensordot(tensors[5], gate, ([1], [1])),
                          (0, 2, 1))
    np.testing.assert_allclose(mps.nodes[5].tensor, actual)
Example #3
0
def test_mps_init(backend, N, pos):
    D, d = 10, 2
    tensors = [np.random.randn(1, d, D)
               ] + [np.random.randn(D, d, D)
                    for _ in range(N - 2)] + [np.random.randn(D, d, 1)]
    with pytest.raises(ValueError):
        FiniteMPS(tensors, center_position=pos, backend=backend)
Example #4
0
def test_apply_two_site_gate(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 = FiniteMPS(tensors, center_position=0, backend=backend)
    gate = get_random_np((2, 2, 2, 2), dtype)
    mps.apply_two_site_gate(gate, 5, 6)
    tmp = np.tensordot(tensors[5], tensors[6], ([2], [0]))
    actual = np.transpose(np.tensordot(tmp, gate, ([1, 2], [2, 3])),
                          (0, 2, 3, 1))
    res = tn.contract_between(mps.nodes[5], mps.nodes[6])
    np.testing.assert_allclose(res.tensor, actual)
Example #5
0
def test_right_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 = FiniteMPS(tensors, center_position=0, backend=backend)

    mps.position(len(mps) - 1)
    mps.position(0)
    assert all([
        mps.check_orthonormality('right', site) < 1E-12
        for site in range(len(mps))
    ])
Example #6
0
def test_local_measurement(backend_dtype_values):
    backend = backend_dtype_values[0]
    dtype = backend_dtype_values[1]

    D, d, N = 1, 2, 10
    tensors_1 = [np.ones((1, d, D), dtype=dtype)] + [
        np.ones((D, d, D), dtype=dtype) for _ in range(N - 2)
    ] + [np.ones((D, d, 1), dtype=dtype)]
    mps_1 = FiniteMPS(tensors_1, center_position=0, backend=backend)

    tensors_2 = [np.zeros((1, d, D), dtype=dtype)] + [
        np.zeros((D, d, D), dtype=dtype) for _ in range(N - 2)
    ] + [np.zeros((D, d, 1), dtype=dtype)]
    for t in tensors_2:
        t[0, 0, 0] = 1
    mps_2 = FiniteMPS(tensors_2, center_position=0, backend=backend)

    sz = np.diag([0.5, -0.5]).astype(dtype)
    result_1 = np.array(mps_1.measure_local_operator([sz] * N, range(N)))
    result_2 = np.array(mps_2.measure_local_operator([sz] * N, range(N)))
    np.testing.assert_almost_equal(result_1, np.zeros(N))
    np.testing.assert_allclose(result_2, np.ones(N) * 0.5)
Example #7
0
def test_correlation_measurement(backend_dtype_values):
    backend = backend_dtype_values[0]
    dtype = backend_dtype_values[1]

    D, d, N = 1, 2, 10
    tensors_1 = [np.ones((1, d, D), dtype=dtype)] + [
        np.ones((D, d, D), dtype=dtype) for _ in range(N - 2)
    ] + [np.ones((D, d, 1), dtype=dtype)]
    mps_1 = FiniteMPS(tensors_1, center_position=0, backend=backend)
    mps_1.position(N - 1)
    mps_1.position(0)
    tensors_2 = [np.zeros((1, d, D), dtype=dtype)] + [
        np.zeros((D, d, D), dtype=dtype) for _ in range(N - 2)
    ] + [np.zeros((D, d, 1), dtype=dtype)]
    for t in tensors_2:
        t[0, 0, 0] = 1
    mps_2 = FiniteMPS(tensors_2, center_position=0, backend=backend)
    mps_2.position(N - 1)
    mps_2.position(0)

    sz = np.diag([0.5, -0.5]).astype(dtype)
    result_1 = np.array(
        mps_1.measure_two_body_correlator(sz, sz, N // 2, range(N)))
    result_2 = np.array(
        mps_2.measure_two_body_correlator(sz, sz, N // 2, range(N)))
    actual = np.zeros(N)
    actual[N // 2] = 0.25
    np.testing.assert_almost_equal(result_1, actual)
    np.testing.assert_allclose(result_2, np.ones(N) * 0.25)