def test_projdiag(backend, dtype, chi): A = tn.randn((chi, chi), dtype=dtype, backend=backend, seed=10) B = tn.randn((chi, chi), dtype=dtype, backend=backend, seed=10) result = ct.projdiag(A, B) compare_val = tn.linalg.operations.ncon([A, B], [[1, 2], [1, 2]]) compare = compare_val * tn.eye(chi, dtype=dtype, backend=backend) rtol = (A.size + B.size) * np.finfo(dtype).eps np.testing.assert_allclose(result.array, compare.array, rtol=rtol)
def test_compute_hR_vs_expect(backend, dtype, chi, d): _, _, A_R, H = random_hermitian_system(backend, dtype, chi, d) I = tn.eye(chi, backend=backend, dtype=dtype) hR = ct.compute_hR(A_R, H) result = tn.abs(tn.trace(hR)) compare = ct.twositeexpect([A_R, I, A_R], H) rtol = (2*A_R.size + I.size + H.size) * np.finfo(dtype).eps np.testing.assert_allclose(result.array, compare.array, rtol=rtol)
def test_XopL_noX(backend, dtype, chi, d): A = tn.randn((chi, d, chi), dtype=dtype, backend=backend, seed=10) X = tn.eye(chi, dtype=dtype, backend=backend) result = ct.XopL(A, X) compare = tn.linalg.operations.ncon([A, A.conj()], [[1, 3, -2], [1, 3, -1]]) rtol = (2*A.size + X.size) * np.finfo(dtype).eps np.testing.assert_allclose(result.array, compare.array, rtol=rtol)
def test_polar(backend, dtype, chi): A = tn.randn((chi, chi), backend=backend, dtype=dtype, seed=10) U = polar.polarU(A) UUdag = U @ U.H UdagU = U.H @ U eye = tn.eye(chi, dtype, backend=backend) atol = A.size * np.finfo(dtype).eps np.testing.assert_allclose(eye.array, UUdag.array, atol=atol) np.testing.assert_allclose(UUdag.array, eye.array, atol=atol)
def test_polar_mps(backend, dtype, chi, d): A = tn.randn((chi, d, chi), backend=backend, dtype=dtype, seed=10) UR = polar.polarU(A, pivot_axis=1) UL = polar.polarU(A, pivot_axis=2) eye = tn.eye(chi, dtype, backend=backend) testR = ct.XopR(UR, eye) testL = ct.XopL(UL, eye) atol = A.size * np.finfo(dtype).eps np.testing.assert_allclose(eye.array, testR.array, atol=atol) np.testing.assert_allclose(eye.array, testL.array, atol=atol)
def test_inv_vs_backend(backend, dtype): shape = 6 dtype = testing_utils.np_dtype_to_backend(backend, dtype) tensor = tensornetwork.eye(shape, backend=backend, dtype=dtype) tn_result = linalg.inv(tensor) if backend is None: backend = backend_contextmanager.get_default_backend() backend_obj = backends.backend_factory.get_backend(backend) backend_result = backend_obj.inv(tensor.array) np.testing.assert_allclose(tn_result.array, backend_result)
def test_eye(backend): """ Tests tensornetwork.eye against np.eye. """ N = 4 M = 6 backend_obj = backends.backend_factory.get_backend(backend) for dtype in dtypes[backend]["all"]: tnI = tensornetwork.eye(N, dtype=dtype, M=M, backend=backend) npI = backend_obj.eye(N, dtype=dtype, M=M) np.testing.assert_allclose(tnI.array, npI)
def test_gauge_match_right(backend, dtype, chi, d): """ Gauge matching is a null op on a system gauge matched on the RHS. """ _, C, A_R, _ = random_hermitian_system(backend, dtype, chi, d) A_C = ct.leftmult(C, A_R) _, A_R2 = vumps.gauge_match(A_C, C, True) rtol = 2 * A_R.size * C.size * A_C.size * np.finfo(dtype).eps np.testing.assert_allclose(A_R.array, A_R2.array, rtol=rtol) I = tn.eye(chi, backend=backend, dtype=dtype) XAR2 = ct.XopR(A_R2, I) np.testing.assert_allclose(XAR2.array, I.array, atol=rtol)
def test_gauge_match_left(backend, dtype, chi, d): """ Gauge matching is a null op on a system gauge matched on the LHS. """ A_L, C, _, _ = random_hermitian_system(backend, dtype, chi, d) A_C = ct.rightmult(A_L, C) A_L2, _ = vumps.gauge_match(A_C, C, True) rtol = 2 * A_L.size * C.size * A_C.size * np.finfo(dtype).eps np.testing.assert_allclose(A_L.array, A_L2.array, rtol=rtol) I = tn.eye(chi, backend=backend, dtype=dtype) XAL2 = ct.XopL(A_L2, I) np.testing.assert_allclose(XAL2.array, I.array, atol=rtol)
def test_expm_vs_backend(backend, dtype): shape = 6 dtype = testing_utils.np_dtype_to_backend(backend, dtype) tensor = tensornetwork.eye(shape, backend=backend, dtype=dtype) if backend in ["pytorch"]: with pytest.raises(NotImplementedError): tn_result = linalg.expm(tensor) else: tn_result = linalg.expm(tensor) backend_obj = backends.backend_factory.get_backend(backend) if backend in ["pytorch"]: with pytest.raises(NotImplementedError): backend_result = backend_obj.expm(tensor.array) else: backend_result = backend_obj.expm(tensor.array) np.testing.assert_allclose(tn_result.array, backend_result)
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))