Пример #1
0
def test_outer_vs_backend(dtype, backend):
    shape = (4, 3)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    testing_utils.check_contraction_dtype(backend, dtype)
    tensor1 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tensor2 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    result = tensornetwork.outer(tensor1, tensor2)
    backend_obj = backends.backend_factory.get_backend(backend)
    arrays = [t.array for t in [tensor1, tensor2]]
    backend_result = backend_obj.outer_product(*arrays)
    np.testing.assert_allclose(backend_result, result.array)
Пример #2
0
def test_ncon_vs_backend(dtype, backend):
    shape = (4, 3)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    testing_utils.check_contraction_dtype(backend, dtype)
    tensor1 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tensor2 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tensor3 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tensors = [tensor1, tensor2, tensor3]
    arrays = [tensor1.array, tensor2.array, tensor3.array]
    idxs = [[1, -1], [1, 2], [-2, 2]]
    result = ncon(tensors, idxs, backend=backend)
    old_result = tensornetwork.ncon(arrays, idxs, backend=backend)
    np.testing.assert_allclose(old_result, result.array)
Пример #3
0
def test_outer_invalid_backends(dtype, backend):
    backend_names = set(["jax", "numpy", "tensorflow", "pytorch"])
    this_name = set([backend])
    other_backend_names = list(backend_names - this_name)
    shape = (4, 3)
    dtype1 = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor1 = tensornetwork.ones(shape, backend=backend, dtype=dtype1)
    for other_backend in other_backend_names:
        dtype2 = testing_utils.np_dtype_to_backend(other_backend, dtype)
        tensor2 = tensornetwork.ones(shape,
                                     backend=other_backend,
                                     dtype=dtype2)
        with pytest.raises(ValueError):
            _ = tensornetwork.outer(tensor1, tensor2)
Пример #4
0
def test_tensordot_int_vs_backend(backend, dtype):
    """
  Tests that tensordot yields the same result as the backend equivalent.
  """
    shape = (4, 4, 4)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    testing_utils.check_contraction_dtype(backend, dtype)
    tensor1 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tensor2 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tensors = [tensor1, tensor2]
    dim = 1
    result = tensornetwork.tensordot(*tensors, dim)
    backend_obj = backends.backend_factory.get_backend(backend)
    arrays = [t.array for t in tensors]
    backend_result = backend_obj.tensordot(*arrays, axes=dim)
    np.testing.assert_allclose(backend_result, result.array)
Пример #5
0
def test_einsum_vs_backend(dtype, backend):
    shape = (4, 3)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    testing_utils.check_contraction_dtype(backend, dtype)
    tensor1 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tensor2 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tensor3 = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    result = tensornetwork.einsum("ba, bc, dc",
                                  tensor1,
                                  tensor2,
                                  tensor3,
                                  optimize=True)
    backend_obj = backends.backend_factory.get_backend(backend)
    arrays = [t.array for t in [tensor1, tensor2, tensor3]]
    backend_result = backend_obj.einsum("ba, bc, dc", *arrays, optimize=True)
    np.testing.assert_allclose(backend_result, result.array)
Пример #6
0
def test_ones(backend):
    """
  Tests tensornetwork.ones against np.ones.
  """
    shape = (5, 10, 3)
    backend_obj = backends.backend_factory.get_backend(backend)
    for dtype in dtypes[backend]["all"]:
        tnI = tensornetwork.ones(shape, dtype=dtype, backend=backend)
        npI = backend_obj.ones(shape, dtype=dtype)
        np.testing.assert_allclose(tnI.array, npI)
Пример #7
0
def test_norm_vs_backend(backend, dtype):
    shape = (6, 2, 6)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tn_result = linalg.norm(tensor)
    if backend is None:
        backend = backend_contextmanager.get_default_backend()
    backend_obj = backends.backend_factory.get_backend(backend)
    backend_result = backend_obj.norm(tensor.array)
    assert backend_result == tn_result
Пример #8
0
def test_reshape_vs_backend(backend, dtype):
    """
  Tests that reshape yields the same result as the backend equivalent.
  """
    shape = (3, 2, 4)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    result = tensornetwork.reshape(tensor, (6, 4))
    backend_obj = backends.backend_factory.get_backend(backend)
    backend_result = backend_obj.reshape(tensor.array, (6, 4))
    assert result.shape == backend_result.shape
Пример #9
0
def test_rq_default(backend, dtype):
    shape = (3, 6, 4, 2)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    split_axis = 1
    tn_result = linalg.rq(tensor, split_axis)
    result2 = linalg.rq(tensor, split_axis, non_negative_diagonal=False)
    tn_arrays = [t.array for t in tn_result]
    arrays2 = [t.array for t in result2]
    for tn_arr, arr2 in zip(tn_arrays, arrays2):
        np.testing.assert_allclose(tn_arr, arr2)
Пример #10
0
def test_sqrt_vs_backend(backend, dtype):
    shape = (4, 5, 6)
    dtype_b = testing_utils.np_dtype_to_backend(backend, dtype)
    backend_obj = backends.backend_factory.get_backend(backend)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype_b)
    if (backend == "pytorch" and dtype == np.float16):
        pytest.skip("Prod not supported with this dtype and backend.")
    else:
        backend_result = backend_obj.sqrt(tensor.array)
        tn_result = tensornetwork.sqrt(tensor).array
        np.testing.assert_allclose(backend_result, tn_result)
Пример #11
0
def test_tensordot_invalid_backend_raises_value_error(backend, dtype):
    """
  Tests that tensordot raises ValueError when fed Tensors with different
  backends. Other failure modes are tested at the backend level.
  """
    backend_names = set(["jax", "numpy", "tensorflow", "pytorch"])
    this_name = set([backend])
    other_backend_names = list(backend_names - this_name)
    shape = (4, 4, 4)
    dtype1 = testing_utils.np_dtype_to_backend(backend, dtype)
    testing_utils.check_contraction_dtype(backend, dtype1)
    tensor1 = tensornetwork.ones(shape, backend=backend, dtype=dtype1)
    for other_backend in other_backend_names:
        dtype2 = testing_utils.np_dtype_to_backend(other_backend, dtype)
        testing_utils.check_contraction_dtype(other_backend, dtype2)
        tensor2 = tensornetwork.ones(shape,
                                     backend=other_backend,
                                     dtype=dtype2)
        with pytest.raises(ValueError):
            _ = tensornetwork.tensordot(tensor1, tensor2,
                                        [[2, 0, 1], [1, 2, 0]])
Пример #12
0
def test_eigh_vs_backend(backend, dtype):
    shape = (3, 6, 4, 4)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    tn_result = linalg.eigh(tensor)
    if backend is None:
        backend = backend_contextmanager.get_default_backend()
    backend_obj = backends.backend_factory.get_backend(backend)
    backend_result = backend_obj.eigh(tensor.array)
    tn_arrays = [t.array for t in tn_result]
    for tn_arr, backend_arr in zip(tn_arrays, backend_result):
        np.testing.assert_allclose(tn_arr, backend_arr)
Пример #13
0
def test_ncon_invalid_backends(dtype, backend):
    backend_names = set(["jax", "numpy", "tensorflow", "pytorch"])
    this_name = set([backend])
    other_backend_names = list(backend_names - this_name)
    shape = (4, 3)
    dtype1 = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor1 = tensornetwork.ones(shape, backend=backend, dtype=dtype1)
    for other_backend in other_backend_names:
        dtype2 = testing_utils.np_dtype_to_backend(other_backend, dtype)
        tensor2 = tensornetwork.ones(shape,
                                     backend=other_backend,
                                     dtype=dtype2)
        for other_other_backend in backend_names:
            dtype3 = testing_utils.np_dtype_to_backend(other_other_backend,
                                                       dtype)
            tensor3 = tensornetwork.zeros(shape,
                                          backend=other_other_backend,
                                          dtype=dtype3)
            tensors = [tensor1, tensor2, tensor3]
            idxs = [[1, -1], [1, 2], [-2, 2]]
            with pytest.raises(ValueError):
                _ = ncon(tensors, idxs)
Пример #14
0
def test_rq_vs_backend(backend, dtype):
    shape = (3, 6, 4, 2)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    split_axis = 1
    tn_result = linalg.rq(tensor, split_axis, non_negative_diagonal=False)
    if backend is None:
        backend = backend_contextmanager.get_default_backend()
    backend_obj = backends.backend_factory.get_backend(backend)
    backend_result = backend_obj.rq(tensor.array, split_axis)
    tn_arrays = [t.array for t in tn_result]
    for tn_arr, backend_arr in zip(tn_arrays, backend_result):
        np.testing.assert_allclose(tn_arr, backend_arr)
Пример #15
0
def projdiag(A: tn.Tensor, B: tn.Tensor) -> tn.Tensor:
    """
  2   2
  |---|
  |   |
  A   B  otimes I(chi, chi)
  |   |
  |---|
  1   1
  Contract A with B to find <A|B>, and put the result on the main diagonal.
  """
    val = tn.linalg.operations.ncon([A, B], [[1, 2], [1, 2]])
    one = tn.ones(A.shape[0], dtype=A.dtype, backend=A.backend)
    return tn.diagflat(val * one)
Пример #16
0
def test_take_slice_vs_backend(backend, dtype):
    """
  Tests that take_slice yields the same result as the backend equivalent.
  """
    shape = (5, 6, 7)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    start_indices = (1, 2, 3)
    slice_sizes = (2, 3, 3)
    result = tensornetwork.take_slice(tensor, start_indices, slice_sizes)
    backend_obj = backends.backend_factory.get_backend(backend)
    backend_result = backend_obj.slice(tensor.array, start_indices,
                                       slice_sizes)
    assert result.shape == backend_result.shape
Пример #17
0
def test_einsum_invalid_backends(dtype, backend):
    backend_names = set(["jax", "numpy", "tensorflow", "pytorch"])
    this_name = set([backend])
    other_backend_names = list(backend_names - this_name)
    shape = (4, 3)
    dtype1 = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor1 = tensornetwork.ones(shape, backend=backend, dtype=dtype1)
    for other_backend in other_backend_names:
        dtype2 = testing_utils.np_dtype_to_backend(other_backend, dtype)
        tensor2 = tensornetwork.ones(shape,
                                     backend=other_backend,
                                     dtype=dtype2)
        for other_other_backend in backend_names:
            dtype3 = testing_utils.np_dtype_to_backend(other_other_backend,
                                                       dtype)
            tensor3 = tensornetwork.zeros(shape,
                                          backend=other_other_backend,
                                          dtype=dtype3)
            with pytest.raises(ValueError):
                _ = tensornetwork.einsum("ba, bc, dc",
                                         tensor1,
                                         tensor2,
                                         tensor3,
                                         optimize=True)
Пример #18
0
def test_unary_ops_vs_backend(backend, dtype, fname):
    shape = (4, 5, 6)
    dtype_b = testing_utils.np_dtype_to_backend(backend, dtype)
    backend_obj = backends.backend_factory.get_backend(backend)
    backend_func = getattr(backend_obj, fname)
    tn_func = getattr(tensornetwork.linalg.operations, fname)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype_b)
    if backend == "pytorch" and fname in ["sin", "log", "exp", "cos"]:
        with pytest.raises(NotImplementedError):
            backend_result = backend_func(tensor.array)
        with pytest.raises(NotImplementedError):
            tn_result = tn_func(tensor).array
    else:
        backend_result = backend_func(tensor.array)
        tn_result = tn_func(tensor).array
        np.testing.assert_allclose(backend_result, tn_result)
Пример #19
0
def test_svd_vs_backend(backend, dtype):
    shape = (3, 6, 4, 2)
    dtype = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype)
    split_axis = 1
    max_singular_values = 5
    max_trunc_error = 0.1
    relative = True
    tn_result = linalg.svd(tensor,
                           split_axis,
                           max_singular_values=max_singular_values,
                           max_truncation_error=max_trunc_error,
                           relative=relative)
    if backend is None:
        backend = backend_contextmanager.get_default_backend()
    backend_obj = backends.backend_factory.get_backend(backend)
    backend_result = backend_obj.svd(tensor.array,
                                     split_axis,
                                     max_singular_values=max_singular_values,
                                     max_truncation_error=max_trunc_error,
                                     relative=relative)
    tn_arrays = [t.array for t in tn_result]
    for tn_arr, backend_arr in zip(tn_arrays, backend_result):
        np.testing.assert_allclose(tn_arr, backend_arr)
Пример #20
0
def test_shape(backend, dtype):
    shape = (4, 5, 6)
    dtype_b = testing_utils.np_dtype_to_backend(backend, dtype)
    tensor = tensornetwork.ones(shape, backend=backend, dtype=dtype_b)
    tn_result = tensornetwork.shape(tensor)
    assert tensor.shape == tn_result