def test_rightmult(backend, dtype, chi, d): lam = tn.randn((chi, chi), dtype=dtype, backend=backend, seed=10) gam = tn.randn((chi, d, chi), dtype=dtype, backend=backend, seed=10) result = ct.rightmult(gam, lam) compare = tn.linalg.operations.ncon([gam, lam], [[-1, -2, 1], [1, -3]]) np.testing.assert_allclose(result.array, compare.array)
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_gauge_transform(backend, dtype, chi, d): lamL = tn.randn((chi, chi), dtype=dtype, backend=backend, seed=10) lamR = tn.randn((chi, chi), dtype=dtype, backend=backend, seed=10) gam = tn.randn((chi, d, chi), dtype=dtype, backend=backend, seed=10) result = ct.gauge_transform(lamL, gam, lamR) compare = tn.linalg.operations.ncon([lamL, gam, lamR], [[-1, 1], [1, -2, 2], [2, -3]]) rtol = (lamL.size + lamR.size + gam.size) * np.finfo(dtype).eps np.testing.assert_allclose(result.array, compare.array, rtol=rtol)
def test_XopR(backend, dtype, chi, d): A = tn.randn((chi, d, chi), dtype=dtype, backend=backend, seed=10) X = tn.randn((chi, chi), dtype=dtype, backend=backend, seed=10) result = ct.XopR(A, X) compare = tn.linalg.operations.ncon([A, A.conj(), X], [[-2, 3, 1], [-1, 3, 2], [2, 1]]) rtol = (2*A.size + X.size) * np.finfo(dtype).eps np.testing.assert_allclose(result.array, compare.array, rtol=rtol)
def test_rho_loc(backend, dtype, chi, d): A = tn.randn((chi, d, chi), dtype=dtype, backend=backend, seed=10) B = tn.randn((chi, d, chi), dtype=dtype, backend=backend, seed=10) result = ct.rholoc(A, B) As = A.conj() Bs = B.conj() to_contract = [A, B, As, Bs] idxs = [(1, -3, 2), (2, -4, 3), (1, -1, 4), (4, -2, 3)] compare = tn.linalg.operations.ncon(to_contract, idxs).reshape((d**2, d**2)) rtol = 2 * (A.size + B.size) * np.finfo(dtype).eps np.testing.assert_allclose(result.array, compare.array, rtol=rtol)
def random_hermitian_system(backend, dtype, chi, d): """ Return A_L, C, A_R representing a normalized quantum state and a Hermitian H. """ A_1 = tn.randn((chi, d, chi), dtype=dtype, backend=backend, seed=10) A_L, _ = tn.linalg.linalg.qr(A_1, pivot_axis=2, non_negative_diagonal=True) C, A_R = tn.linalg.linalg.rq(A_L, pivot_axis=1, non_negative_diagonal=True) C /= tn.linalg.linalg.norm(C) H = tn.randn((d, d, d, d), dtype=dtype, backend=backend, seed=10) H = H.reshape((d*d, d*d)) H = 0.5*(H + H.H) H = H.reshape((d, d, d, d)) return (A_L, C, A_R, H)
def test_minimize_Hc(backend, dtype, chi, d): """ The result of minimize_Hc solves the eigenproblem for Hc. """ A_L, C, A_R, H = random_hermitian_system(backend, dtype, chi, d) LH = tn.randn((chi, chi), dtype=dtype, backend=backend) RH = tn.randn((chi, chi), dtype=dtype, backend=backend) LH = 0.5 * (LH + LH.H) RH = 0.5 * (RH + RH.H) params = tn_vumps.params.krylov_params(n_krylov=chi * chi) ev, newC = tn_vumps.vumps.minimize_Hc([A_L, C, A_R], [H, LH, RH], 1E-5, params) C_prime = ct.apply_Hc(newC, A_L, A_R, [H, LH, RH]) EC = ev * newC np.testing.assert_allclose(C_prime.array, EC.array, rtol=1E-4, atol=1E-4)
def test_minimize_HAc(backend, dtype, chi, d): """ The result of minimize_HAc solves the eigenproblem for HAc. """ A_L, C, A_R, H = random_hermitian_system(backend, dtype, chi, d) A_C = ct.leftmult(C, A_R) LH = tn.randn((chi, chi), dtype=dtype, backend=backend) LH = 0.5 * (LH + LH.H) RH = tn.randn((chi, chi), dtype=dtype, backend=backend) RH = 0.5 * (RH + RH.H) params = tn_vumps.params.krylov_params(n_krylov=chi * chi, max_restarts=20) ev, newAC = tn_vumps.vumps.minimize_HAc([A_L, C, A_R], A_C, [H, LH, RH], 1E-5, params) AC_prime = ct.apply_HAc(newAC, A_L, A_R, H, LH, RH) EAC = ev * newAC np.testing.assert_allclose(AC_prime.array, EAC.array, rtol=1E-4, atol=1E-4)
def vumps_initialization( d: int, chi: int, dtype: Optional[DtypeType] = None, backend: Optional[Text] = None ) -> Tuple[ThreeTensors, tn.Tensor, TwoTensors]: """ Generate a random uMPS in mixed canonical forms, along with the left dominant eV L of A_L and right dominant eV R of A_R. Args: d: Physical dimension. chi: Bond dimension. dtype: Data dtype of tensors. backend: The backend. Returns: mps = (A_L, C, A_R): A_L and A_R have shape (chi, d, chi), and are respectively left and right isometric. C is the (chi, chi) centre of orthogonality. A_C: A_L @ C. One of the equations vumps minimizes is A_L @ C = C @ A_R = A_C. fpoints = [rL, lR]: C^dag @ C and C @ C^dag respectively. Will converge to the left and right fixed points of A_R and A_L. Both are chi x chi. """ A_1 = tn.randn((chi, d, chi), dtype=dtype, backend=backend) A_L, _ = tn.linalg.linalg.qr(A_1, pivot_axis=2, non_negative_diagonal=True) C, A_R = tn.linalg.linalg.rq(A_L, pivot_axis=1, non_negative_diagonal=True) C /= tn.linalg.linalg.norm(C) A_C = ct.rightmult(A_L, C) L0, R0 = vumps_approximate_tm_eigs(C) fpoints = (L0, R0) mps = [A_L, C, A_R] benchmark.block_until_ready(R0) return (mps, A_C, fpoints)
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_gauge_match_minimizes(backend, dtype, chi, d): """ Gauge matching decreases the values of ||A_C - A_L C || and ||A_C - C A_R||. """ A_L, C, A_R, _ = random_hermitian_system(backend, dtype, chi, d) A_C = tn.randn((chi, d, chi), dtype=dtype, backend=backend) epsL = tn.linalg.linalg.norm(A_C - ct.rightmult(A_L, C)) epsR = tn.linalg.linalg.norm(A_C - ct.leftmult(C, A_R)) A_L2, A_R2 = vumps.gauge_match(A_C, C, True) epsL2 = tn.linalg.linalg.norm(A_C - ct.rightmult(A_L2, C)) epsR2 = tn.linalg.linalg.norm(A_C - ct.leftmult(C, A_R2)) assert epsL2 < epsL assert epsR2 < epsR
def test_randn(backend): """ Tests tensornetwork.randn against the backend code. """ shape = (5, 10, 3, 2) seed = int(time.time()) np.random.seed(seed=seed) backend_obj = backends.backend_factory.get_backend(backend) for dtype in dtypes[backend]["rand"]: tnI = tensornetwork.randn(shape, dtype=dtype, seed=seed, backend=backend) npI = backend_obj.randn(shape, dtype=dtype, seed=seed) np.testing.assert_allclose(tnI.array, npI)