def svd( self, tensor: Tensor, pivot_axis: int = -1, max_singular_values: Optional[int] = None, max_truncation_error: Optional[float] = None, relative: Optional[bool] = False ) -> Tuple[Tensor, Tensor, Tensor, Tensor]: return decompositions.svd(self.bs, tensor, pivot_axis, max_singular_values, max_truncation_error, relative)
def test_singular_values(dtype, R, R1, num_charges): np.random.seed(10) D = 30 charges = [ BaseCharge(np.random.randint(-5, 6, (D, num_charges)), charge_types=[U1Charge] * num_charges) for n in range(R) ] flows = [True] * R A = BlockSparseTensor.random( [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype) _, s, _, _ = decompositions.svd(bs, A, R1) _, s_dense, _, _ = np_decompositions.svd(np, A.todense(), R1) np.testing.assert_almost_equal(np.sort(s.todense()), np.sort(s_dense[s_dense > 1E-13]))
def test_max_singular_values(dtype, R, R1, num_charges): np.random.seed(10) D = 30 max_singular_values = 12 charges = [ BaseCharge(np.random.randint(-5, 6, (D, num_charges)), charge_types=[U1Charge] * num_charges) for n in range(R) ] flows = [True] * R A = BlockSparseTensor.random( [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype) _, s, _, _ = decompositions.svd(bs, A, R1, max_singular_values=max_singular_values) assert len(s.data) <= max_singular_values
def test_svds(dtype, R, R1, num_charges): np.random.seed(10) D = 30 charges = [ BaseCharge(np.random.randint(-5, 6, (D, num_charges)), charge_types=[U1Charge] * num_charges) for n in range(R) ] flows = [True] * R A = BlockSparseTensor.random( [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype) u, s, v, _ = decompositions.svd(bs, A, R1) u_dense, s_dense, v_dense, _ = np_decompositions.svd(np, A.todense(), R1) res1 = bs.tensordot(bs.tensordot(u, bs.diag(s), 1), v, 1) res2 = np.tensordot(np.tensordot(u_dense, np.diag(s_dense), 1), v_dense, 1) np.testing.assert_almost_equal(res1.todense(), res2)
def test_max_singular_values_larger_than_bond_dimension(dtype, num_charges): np.random.seed(10) R = 2 D = 30 charges = [ BaseCharge(np.random.randint(-5, 6, (D, num_charges)), charge_types=[U1Charge] * num_charges) for n in range(R) ] flows = [True] * R random_matrix = BlockSparseTensor.random( [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype) U, S, V = bs.svd(random_matrix, full_matrices=False) S.data = np.array(range(len(S.data))) val = U @ bs.diag(S) @ V _, S2, _, _ = decompositions.svd(bs, val, 1, max_singular_values=40) assert S2.shape == S.shape
def test_max_truncation_error(dtype, num_charges): np.random.seed(10) R = 2 D = 30 charges = [ BaseCharge(np.random.randint(-5, 6, (D, num_charges)), charge_types=[U1Charge] * num_charges) for n in range(R) ] flows = [True] * R random_matrix = BlockSparseTensor.random( [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype) U, S, V = bs.svd(random_matrix, full_matrices=False) svals = np.array(range(1, len(S.data) + 1)).astype(np.float64) S.data = svals[::-1] val = U @ bs.diag(S) @ V trunc = 8 mask = np.sqrt(np.cumsum(np.square(svals))) >= trunc _, S2, _, _ = decompositions.svd(bs, val, 1, max_truncation_error=trunc) np.testing.assert_allclose(S2.data, svals[mask][::-1])