def test_BlockSparseTensor_zeros(dtype, num_charges):
    np.random.seed(10)
    Ds = [8, 9, 10, 11]
    rank = 4
    flows = np.random.choice([True, False], size=rank, replace=True)
    indices = [
        Index(
            BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                       charge_types=[U1Charge] * num_charges), flows[n])
        for n in range(rank)
    ]
    arr = BlockSparseTensor.zeros(indices, dtype=dtype)
    np.testing.assert_allclose(arr.data, 0)
    np.testing.assert_allclose(Ds, arr.shape)
    np.testing.assert_allclose(arr.flat_flows, flows)
    for n in range(4):
        assert charge_equal(arr.charges[n][0], indices[n].flat_charges[0])
def test_randn(R, dtype, num_charges):
  np.random.seed(10)
  backend = symmetric_backend.SymmetricBackend()
  indices = [
      Index(
          BaseCharge(
              np.random.randint(-5, 6, (num_charges, 10)),
              charge_types=[U1Charge] * num_charges), False) for _ in range(R)
  ]
  actual = backend.randn(indices, dtype=dtype, seed=10)
  np.random.seed(10)
  expected = randn(indices, dtype=dtype)
  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_svd_decompositions(dtype, R, R1, num_charges):
    np.random.seed(10)
    D = 30
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, D)),
                   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_decomposition(bs, A, R1)
    u_dense, s_dense, v_dense, _ = np_decompositions.svd_decomposition(
        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)
Esempio n. 4
0
def test_add_sub(op, dtype, num_charges, chargetype):
    np.random.seed(10)
    indices = [
        Index(get_charge(chargetype, num_charges, 10), False) for _ in range(4)
    ]
    order = np.arange(4)
    np.random.shuffle(order)
    a = BlockSparseTensor.randn(indices, dtype=dtype)
    b = BlockSparseTensor.randn([indices[o] for o in order], dtype=dtype)
    npb = np.reshape(b.todense(), b.shape)
    npa = np.reshape(a.todense(), a.shape)

    c = a.transpose(order)
    npc = npa.transpose(order)
    d = op(c, b)
    npd = op(npc, npb)
    np.testing.assert_allclose(d.todense(), npd)
def test_rq_decomposition(dtype, R, R1, num_charges):
    np.random.seed(10)
    D = 30
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, D)),
                   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)

    r, q = decompositions.rq_decomposition(bs, A, R1)
    res = bs.tensordot(r, q, 1)
    r_dense, q_dense = np_decompositions.rq_decomposition(np, A.todense(), R1)
    res2 = np.tensordot(r_dense, q_dense, 1)
    np.testing.assert_almost_equal(res.todense(), res2)
def test_broadcast_left_multiplication_raises():
    np.random.seed(10)
    backend = symmetric_backend.SymmetricBackend()
    num_charges = 1
    Ds = [10, 30, 24]
    R = len(Ds)
    indices = [
        Index(
            BaseCharge(np.random.randint(-5, 6, (Ds[n], num_charges)),
                       charge_types=[U1Charge] * num_charges), False)
        for n in range(R)
    ]

    tensor1 = ChargeArray.random(indices=indices)
    tensor2 = backend.randn(indices)
    with pytest.raises(ValueError):
        backend.broadcast_left_multiplication(tensor1, tensor2)
def test_random_uniform_seed(dtype, num_charges):
    np.random.seed(10)
    R = 4
    backend = symmetric_backend.SymmetricBackend()
    indices = [
        Index(
            BaseCharge(np.random.randint(-5, 6, (10, num_charges)),
                       charge_types=[U1Charge] * num_charges), False)
        for _ in range(R)
    ]
    a = backend.random_uniform(indices, dtype=dtype, seed=10)
    b = backend.random_uniform(indices, dtype=dtype, seed=10)
    np.testing.assert_allclose(a.data, b.data)
    assert np.all([
        charge_equal(a._charges[n], b._charges[n])
        for n in range(len(a._charges))
    ])
def test_eigs_cache_exception():
    dtype = np.float64
    np.random.seed(10)
    backend = symmetric_backend.SymmetricBackend()
    D = 16
    index = Index(U1Charge.random(D, 0, 0), True)

    def mv(vec):
        raise ValueError()

    init = BlockSparseTensor.random([index], dtype=dtype)
    with pytest.raises(ValueError):
        backend.eigs(mv, [], init)
    cacher = get_cacher()
    assert not cacher.do_caching
    assert not get_caching_status()
    assert cacher.cache == {}
Esempio n. 9
0
def test_eig_prod(dtype, Ds, num_charges):
  np.random.seed(10)
  R = len(Ds)
  charges = [
      BaseCharge(
          np.random.randint(-5, 6, (num_charges, Ds[n]), dtype=np.int16),
          charge_types=[U1Charge] * num_charges) for n in range(R)
  ]
  flows = [False] * R
  inds = [Index(charges[n], flows[n]) for n in range(R)]

  A = BlockSparseTensor.random(
      inds + [i.copy().flip_flow() for i in inds], dtype=dtype)
  dims = np.prod(Ds)
  A = A.reshape([dims, dims])
  E, V = eig(A)
  A_ = V @ diag(E) @ inv(V)
  np.testing.assert_allclose(A.data, A_.data)
def test_ChargeArray_transpose_data(num_charges):
    Ds = np.array([8, 9, 10, 11])
    order = [2, 0, 1, 3]
    flows = [True, False, True, False]
    indices = [
        Index(
            BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                       charge_types=[U1Charge] * num_charges), flows[n])
        for n in range(4)
    ]
    arr = ChargeArray.random(indices)
    data = np.ascontiguousarray(np.transpose(np.reshape(arr.data, Ds), order))
    arr2 = arr.transpose(order).transpose_data()
    data3 = np.reshape(arr2.data, Ds[order])
    np.testing.assert_allclose(data, data3)
    np.testing.assert_allclose(arr2.shape, Ds[order])
    np.testing.assert_allclose(arr2._order, [[0], [1], [2], [3]])
    np.testing.assert_allclose(arr2.flows, [[True], [True], [False], [False]])
Esempio n. 11
0
def test_fromdense(num_charges, chargetype):
    np.random.seed(10)
    Ds = [8, 9, 10, 11]
    rank = 4
    flows = np.random.choice([True, False], size=rank, replace=True)
    charges = [get_charge(chargetype, num_charges, Ds[n]) for n in range(rank)]
    fused = fuse_charges(charges, flows)
    mask = fused == np.zeros((num_charges, 1))
    inds = np.nonzero(mask)[0]
    inds2 = np.nonzero(np.logical_not(mask))[0]
    indices = [Index(charges[n], flows[n]) for n in range(rank)]

    dense = np.random.random_sample(Ds)
    arr = BlockSparseTensor.fromdense(indices, dense)
    dense_arr = arr.todense()

    np.testing.assert_allclose(np.ravel(dense)[inds], arr.data)
    np.testing.assert_allclose(np.ravel(dense_arr)[inds2], 0)
Esempio n. 12
0
def test_item():
  t1 = BlockSparseTensor(
      data=np.array(1.0),
      charges=[],
      flows=[],
      order=[],
      check_consistency=False)
  assert t1.item() == 1
  Ds = [10, 11, 12, 13]
  charges = [U1Charge.random(Ds[n], -5, 5) for n in range(4)]
  flows = [True, False, True, False]
  inds = [Index(c, flows[n]) for n, c in enumerate(charges)]
  t2 = BlockSparseTensor.random(inds, dtype=np.float64)
  with pytest.raises(
      ValueError,
      match="can only convert an array of"
      " size 1 to a Python scalar"):
    t2.item()
Esempio n. 13
0
def test_qr_raises():
    np.random.seed(10)
    dtype = np.float64
    num_charges = 1
    Ds = [20, 21]
    R1 = 1
    R = len(Ds)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                   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)
    d1 = np.prod(Ds[:R1])
    d2 = np.prod(Ds[R1:])
    A = A.reshape([d1, d2])
    with pytest.raises(ValueError):
        qr(A, mode='fake_mode')
Esempio n. 14
0
def test_broadcast_right_multiplication(dtype, num_charges):
    np.random.seed(10)
    backend = symmetric_backend.SymmetricBackend()
    Ds = [10, 30, 24]
    R = len(Ds)
    indices = [
        Index(
            BaseCharge(np.random.randint(-5, 6, (Ds[n], num_charges)),
                       charge_types=[U1Charge] * num_charges), False)
        for n in range(R)
    ]
    tensor1 = backend.randn(indices, dtype=dtype)
    tensor2 = ChargeArray.random(indices=[indices[-1].copy().flip_flow()],
                                 dtype=dtype)
    t1dense = tensor1.todense()
    t2dense = tensor2.todense()
    out = backend.broadcast_right_multiplication(tensor1, tensor2)
    dense = t1dense * t2dense
    np.testing.assert_allclose(out.todense(), dense)
Esempio n. 15
0
def test_qr_r_mode():
    Ds = [10, 11]
    dtype = np.float64
    np.random.seed(10)
    rank = len(Ds)
    charges = [
        BaseCharge(np.zeros((Ds[n], 1)), charge_types=[U1Charge] * 1)
        for n in range(rank)
    ]
    flows = [True] * rank
    A = BlockSparseTensor.random(
        [Index(charges[n], flows[n]) for n in range(rank)], dtype=dtype)
    d1 = np.prod(Ds[:1])
    d2 = np.prod(Ds[1:])
    A = A.reshape([d1, d2])
    R = qr(A, mode='r')
    R_np = np.linalg.qr(A.todense(), mode='r')
    np.testing.assert_allclose(np.abs(np.diag(R.todense())),
                               np.abs(np.diag(R_np)))
Esempio n. 16
0
def test_qr_prod(dtype, Ds, R1, mode, num_charges):
    np.random.seed(10)
    R = len(Ds)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                   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)
    d1 = np.prod(Ds[:R1])
    d2 = np.prod(Ds[R1:])
    A = A.reshape([d1, d2])
    Q, R = qr(A, mode=mode)
    A_ = Q @ R
    assert A_.dtype == A.dtype
    np.testing.assert_allclose(A.data, A_.data)
    for n in range(len(A._charges)):
        assert charge_equal(A_._charges[n], A._charges[n])
Esempio n. 17
0
def test_BlockSparseTensor_contiguous_3():
    Ds = [10, 11, 12, 13]
    charges = [U1Charge.random(Ds[n], -5, 5) for n in range(4)]
    flows = [True, False, True, False]
    inds = [Index(c, flows[n]) for n, c in enumerate(charges)]
    a = BlockSparseTensor.random(inds, dtype=np.float64)
    order_a = [0, 3, 1, 2]
    a = a.transpose(order_a)
    adense = a.todense()
    order_b = [0, 2, 1, 3]
    b = BlockSparseTensor.random([inds[n] for n in order_b], dtype=np.float64)
    b = b.transpose([0, 3, 2, 1])
    b.contiguous([0, 2, 1, 3], inplace=True)
    bdense = b.todense()
    c = a + b
    cdense = c.todense()
    np.testing.assert_allclose(a.flows, b.flows)
    np.testing.assert_allclose(a.flows, b.flows)
    np.testing.assert_allclose(adense + bdense, cdense)
def test_broadcast_left_multiplication(dtype, num_charges):
    np.random.seed(10)
    backend = symmetric_backend.SymmetricBackend()
    Ds = [10, 30, 24]
    R = len(Ds)
    indices = [
        Index(
            BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                       charge_types=[U1Charge] * num_charges), False)
        for n in range(R)
    ]

    tensor1 = ChargeArray.random(indices=[indices[0]], dtype=dtype)
    tensor2 = backend.randn(indices, dtype=dtype)
    t1dense = tensor1.todense()
    t2dense = tensor2.todense()
    out = backend.broadcast_left_multiplication(tensor1, tensor2)
    dense = np.reshape(t1dense, (10, 1, 1)) * t2dense
    np.testing.assert_allclose(out.todense(), dense)
Esempio n. 19
0
def test_svd_singvals(dtype, Ds, R1, num_charges):
    np.random.seed(10)
    R = len(Ds)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                   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)

    d1 = np.prod(Ds[:R1])
    d2 = np.prod(Ds[R1:])
    A = A.reshape([d1, d2])
    _, S1, _ = svd(A, full_matrices=False)
    S2 = svd(A, full_matrices=False, compute_uv=False)
    np.testing.assert_allclose(S1.data, S2.data)
    Sdense = np.linalg.svd(A.todense(), compute_uv=False)
    np.testing.assert_allclose(np.sort(Sdense[Sdense > 1E-15]),
                               np.sort(S2.data[S2.data > 0.0]))
Esempio n. 20
0
def test_svd_prod(dtype, Ds, R1, num_charges):
    np.random.seed(10)
    R = len(Ds)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                   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)
    d1 = np.prod(Ds[:R1])
    d2 = np.prod(Ds[R1:])
    A = A.reshape([d1, d2])

    U, S, V = svd(A, full_matrices=False)
    A_ = U @ diag(S) @ V
    assert A_.dtype == A.dtype
    np.testing.assert_allclose(A.data, A_.data)
    for n in range(len(A._charges)):
        assert charge_equal(A_._charges[n], A._charges[n])
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, (num_charges, D)),
                   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_decomposition(bs,
                                                   val,
                                                   1,
                                                   max_singular_values=40)
    assert S2.shape == S.shape
Esempio n. 22
0
def test_add_sub_raises(op):
    np.random.seed(10)
    Ds1 = [3, 4, 5, 6]
    Ds2 = [4, 5, 6, 7]

    indices1 = [
        Index(U1Charge.random(dimension=Ds1[n], minval=-5, maxval=5), False)
        for n in range(4)
    ]
    indices2 = [
        Index(U1Charge.random(dimension=Ds2[n], minval=-5, maxval=5), False)
        for n in range(4)
    ]
    a = BlockSparseTensor.randn(indices1)
    b = BlockSparseTensor.randn(indices2)
    with pytest.raises(TypeError):
        op(a, np.array([1, 2, 3]))
    with pytest.raises(ValueError):
        op(a, b)

    Ds3 = [3, 3, 3, 3]
    Ds4 = [9, 9]
    indices3 = [
        Index(U1Charge.random(dimension=Ds3[n], minval=-5, maxval=5), False)
        for n in range(4)
    ]
    indices4 = [
        Index(U1Charge.random(dimension=Ds4[n], minval=-5, maxval=5), False)
        for n in range(2)
    ]
    c = BlockSparseTensor.randn(indices3).reshape([9, 9])
    d = BlockSparseTensor.randn(indices4)
    with pytest.raises(ValueError):
        op(c, d)

    Ds5 = [200, 200]
    Ds6 = [200, 200]
    indices5 = [
        Index(U1Charge.random(dimension=Ds5[n], minval=-5, maxval=5), False)
        for n in range(2)
    ]
    indices6 = [
        Index(U1Charge.random(dimension=Ds6[n], minval=-5, maxval=5), False)
        for n in range(2)
    ]
    e = BlockSparseTensor.randn(indices5)
    f = BlockSparseTensor.randn(indices6)
    with pytest.raises(ValueError):
        op(e, f)
Esempio n. 23
0
def test_like_init(fun, val, dtype, num_charges):
    np.random.seed(10)
    Ds = [8, 9, 10, 11]
    rank = 4
    flows = np.random.choice([True, False], size=rank, replace=True)
    indices = [
        Index(
            BaseCharge(np.random.randint(-5, 6, (Ds[n], num_charges)),
                       charge_types=[U1Charge] * num_charges), flows[n])
        for n in range(rank)
    ]
    arr = randn(indices, dtype=dtype)
    arr2 = fun(arr)
    assert arr.dtype == arr2.dtype
    np.testing.assert_allclose(arr.shape, arr2.shape)
    np.testing.assert_allclose(arr.flat_flows, arr2.flat_flows)
    for n in range(4):
        assert charge_equal(arr.charges[n][0], arr2.charges[n][0])
    if val is not None:
        np.testing.assert_allclose(arr2.data, val)
Esempio n. 24
0
def test_eigh_prod(dtype, Ds, num_charges):
  np.random.seed(10)
  R = len(Ds)
  charges = [
      BaseCharge(
          np.random.randint(-5, 6, (num_charges, Ds[n]), dtype=np.int16),
          charge_types=[U1Charge] * num_charges) for n in range(R)
  ]
  flows = [False] * R
  inds = [Index(charges[n], flows[n]) for n in range(R)]
  A = BlockSparseTensor.random(
      inds + [i.copy().flip_flow() for i in inds], dtype=dtype)
  dims = np.prod(Ds)
  A = A.reshape([dims, dims])
  B = A + A.T.conj()
  E, V = eigh(B)
  B_ = V @ diag(E) @ V.conj().T
  np.testing.assert_allclose(B.data, B_.data)
  for n in range(len(B._charges)):
    assert charge_equal(B_._charges[n], B._charges[n])
Esempio n. 25
0
def test_index_eq():
    q1 = U1Charge(np.array([-1, -2, 0, 8, 7]))
    q2 = U1Charge(np.array([-1, -2, 0, 8, 7]))
    q3 = U1Charge(np.array([-1, 0, 8, 7]))
    i1 = Index(charges=q1, flow=False)
    i2 = Index(charges=q2, flow=False)
    i3 = Index(charges=q3, flow=False)
    i4 = Index(charges=[q1, q2], flow=[False, True])
    i5 = Index(charges=[q1, q2], flow=[False, True])
    i6 = Index(charges=[q1, q2], flow=[False, False])
    assert i1 == i2
    assert i1 != i3
    assert i1 != i4
    assert i4 == i5
    assert i5 != i6
Esempio n. 26
0
def test_ChargeArray_arithmetic_raises():
    np.random.seed(10)
    dtype = np.float64
    D = 10
    rank = 3
    charges = [U1Charge.random(D, -2, 2) for _ in range(rank)]
    flows = np.random.choice([True, False], size=rank, replace=True)
    inds = [Index(c, f) for c, f in zip(charges, flows)]
    T = ChargeArray.random(inds, dtype=dtype)
    with pytest.raises(NotImplementedError):
        T - T
    with pytest.raises(NotImplementedError):
        T + T
    with pytest.raises(NotImplementedError):
        -T
    with pytest.raises(NotImplementedError):
        T * 5
    with pytest.raises(NotImplementedError):
        5 * T
    with pytest.raises(NotImplementedError):
        T / 5
Esempio n. 27
0
def test_tn_transpose_reshape():
  np.random.seed(10)
  Ds = np.array([8, 9, 10, 11])
  flows = [True, False, True, False]
  indices = [Index(U1Charge.random(-5, 5, Ds[n]), flows[n]) for n in range(4)]
  arr = BlockSparseTensor.random(indices)
  arr2 = transpose(arr, [2, 0, 1, 3])
  arr3 = reshape(arr2, [80, 99])
  np.testing.assert_allclose(arr3.shape, [80, 99])
  np.testing.assert_allclose(arr3._order, [[2, 0], [1, 3]])
  np.testing.assert_allclose(arr3.flows, [[True, True], [False, False]])

  arr4 = transpose(arr3, [1, 0])
  np.testing.assert_allclose(arr4.shape, [99, 80])
  np.testing.assert_allclose(arr4._order, [[1, 3], [2, 0]])
  np.testing.assert_allclose(arr4.flows, [[False, False], [True, True]])

  arr5 = reshape(arr4, [9, 11, 10, 8])
  np.testing.assert_allclose(arr5.shape, [9, 11, 10, 8])
  np.testing.assert_allclose(arr5._order, [[1], [3], [2], [0]])
  np.testing.assert_allclose(arr5.flows, [[False], [False], [True], [True]])
Esempio n. 28
0
def test_tn_reshape(dtype):
  np.random.seed(10)
  Ds = [8, 9, 10, 11]
  indices = [Index(U1Charge.random(-5, 5, Ds[n]), False) for n in range(4)]
  arr = BlockSparseTensor.random(indices, dtype=dtype)
  arr2 = reshape(arr, [72, 110])
  for n in range(2):
    for m in range(2):
      assert charge_equal(arr2.charges[n][m], indices[n * 2 + m].charges)
  np.testing.assert_allclose(arr2.shape, [72, 110])
  np.testing.assert_allclose(arr2._order, [[0, 1], [2, 3]])
  np.testing.assert_allclose(arr2.flows, [[False, False], [False, False]])
  assert arr2.ndim == 2
  arr3 = reshape(arr, Ds)
  for n in range(4):
    assert charge_equal(arr3.charges[n][0], indices[n].charges)

  np.testing.assert_allclose(arr3.shape, Ds)
  np.testing.assert_allclose(arr3._order, [[0], [1], [2], [3]])
  np.testing.assert_allclose(arr3.flows, [[False], [False], [False], [False]])
  assert arr3.ndim == 4
Esempio n. 29
0
def test_create_diag(dtype, num_charges):
    np.random.seed(10)
    D = 200
    index = Index(
        BaseCharge(np.random.randint(-2, 3, (num_charges, D)),
                   charge_types=[U1Charge] * num_charges), False)

    arr = ChargeArray.random([index], dtype=dtype)
    diagarr = diag(arr)
    dense = np.ravel(diagarr.todense())
    np.testing.assert_allclose(np.sort(dense[dense != 0.0]),
                               np.sort(diagarr.data[diagarr.data != 0.0]))

    sparse_blocks, charges, block_shapes = _find_diagonal_sparse_blocks(
        diagarr.flat_charges, diagarr.flat_flows, 1)
    #in range(index._charges[0].unique_charges.shape[1]):
    for n, block in enumerate(sparse_blocks):
        shape = block_shapes[:, n]
        block_diag = np.diag(np.reshape(diagarr.data[block], shape))
        np.testing.assert_allclose(
            arr.data[np.squeeze(index._charges[0] == charges[n])], block_diag)
Esempio n. 30
0
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])