def test_kron(backend, dtype, pivots):
    """ Checks that Tensor.kron() works.
  """
    pivotA, pivotB = pivots
    shapeA = (2, 3, 4)
    shapeB = (1, 2)
    shapeC = (2, 3, 4, 1, 2)
    A, _ = testing_utils.safe_randn(shapeA, backend, dtype)
    if A is not None:
        B, _ = testing_utils.safe_randn(shapeB, backend, dtype)
        if pivotA is None and pivotB is None:
            C = tensornetwork.kron(A, B)
            matrixA = tensornetwork.pivot(A, pivot_axis=-1)
            matrixB = tensornetwork.pivot(B, pivot_axis=-1)
        elif pivotA is None:
            C = tensornetwork.kron(A, B, pivot_axisB=pivotB)
            matrixA = tensornetwork.pivot(A, pivot_axis=-1)
            matrixB = tensornetwork.pivot(B, pivot_axis=pivotB)
        elif pivotB is None:
            C = tensornetwork.kron(A, B, pivot_axisA=pivotA)
            matrixA = tensornetwork.pivot(A, pivot_axis=pivotA)
            matrixB = tensornetwork.pivot(B, pivot_axis=-1)
        else:
            C = tensornetwork.kron(A,
                                   B,
                                   pivot_axisA=pivotA,
                                   pivot_axisB=pivotB)
            matrixA = tensornetwork.pivot(A, pivot_axis=pivotA)
            matrixB = tensornetwork.pivot(B, pivot_axis=pivotB)

        Ctest = C.backend.einsum("ij,kl->ikjl", matrixA.array, matrixB.array)
        Ctest = C.backend.reshape(Ctest, shapeC)
        np.testing.assert_allclose(C.array, Ctest)
def test_kron_raises(backend):
    with tn.DefaultBackend(backend):
        A = tn.Node(np.ones((2, 2, 2)))
        B = tn.Node(np.ones((2, 2, 2)))
        with pytest.raises(
                ValueError,
                match="All operator tensors must have an even order."):
            tn.kron([A, B])
Beispiel #3
0
def H_XX(backend: Optional[BackendType] = None,
         dtype: Optional[DtypeType] = None) -> tn.Tensor:
    """
  Args:
    backend: The backend.
    dtype  : dtype of data.
  Returns:
    H      : The Hamiltonian, a tn.Tensor shape (2, 2, 2, 2).
  """
    X = sigX(backend=backend, dtype=dtype)
    Y = sigY(backend=backend, dtype=dtype)
    return tn.kron(X, X) + tn.kron(Y, Y).real
def test_operator_kron(backend):
    with tn.DefaultBackend(backend):
        X = np.array([[0, 1], [1, 0]], dtype=np.float32)
        Z = np.array([[1, 0], [0, -1]], dtype=np.float32)
        expected = np.kron(X, Z).reshape(2, 2, 2, 2)
        result = tn.kron([tn.Node(X), tn.Node(Z)])
        np.testing.assert_allclose(result.tensor, expected)
Beispiel #5
0
def H_XXZ(delta: float = 1,
          ud: float = 2.,
          scale: float = 1.,
          backend: Optional[BackendType] = None,
          dtype: Optional[DtypeType] = None) -> tn.Tensor:
    """
  H = (-1/(8scale))*[ud*[UD + DU] + delta*ZZ]

  Args:
    delta, ud, scale: Couplings.
    backend: The backend.
    dtype  : dtype of data.
  Returns:
    H      : The Hamiltonian, a tn.Tensor shape (2, 2, 2, 2).
  """
    U = sigU(backend=backend, dtype=dtype)
    D = sigD(backend=backend, dtype=dtype)
    Z = sigZ(backend=backend, dtype=dtype)
    UD = ud * (tn.kron(U, D) + tn.kron(D, U))
    return -(UD + delta * tn.kron(Z, Z)) / (8 * scale)
Beispiel #6
0
def H_ising(h: float,
            J: float = 1.,
            backend: Optional[BackendType] = None,
            dtype: Optional[DtypeType] = None) -> tn.Tensor:
    """
  The famous and beloved transverse-field Ising model,
  H = J * XX + h * ZI

  Args:
    h  : Transverse field.
    J  : Coupling strength, default 1.
    backend: The backend.
    dtype  : dtype of data.
  Returns:
    H      : The Hamiltonian, a tn.Tensor shape (2, 2, 2, 2).
  """
    X = sigX(backend=backend, dtype=dtype)
    Z = sigZ(backend=backend, dtype=dtype)
    Id = tn.eye(2, backend=backend, dtype=dtype)
    return J * tn.kron(X, X) + 0.5 * h * (tn.kron(Z, Id) + tn.kron(Id, Z))