def test_diag(dtype, num_charges): np.random.seed(10) backend = symmetric_backend.SymmetricBackend() a = get_tensor(3, num_charges, dtype) with pytest.raises(ValueError): backend.diag(a) b = get_chargearray(num_charges, dtype) expected = diag(b) actual = backend.diag(b) np.testing.assert_allclose(expected.data, actual.data) assert np.all([ charge_equal(expected._charges[n], actual._charges[n]) for n in range(len(actual._charges)) ])
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_diagflat(dtype, num_charges): np.random.seed(10) backend = symmetric_backend.SymmetricBackend() a = get_tensor(3, num_charges, dtype) with pytest.raises(ValueError): backend.diagflat(a) b = get_chargearray(num_charges, dtype) expected = diag(b) actual = backend.diagflat(b) np.testing.assert_allclose(expected.data, actual.data) assert np.all([ charge_equal(expected._charges[n], actual._charges[n]) for n in range(len(actual._charges)) ]) with pytest.raises(NotImplementedError, match="Can't specify k with Symmetric backend"): actual = backend.diagflat(b, k=1)
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])