Ejemplo n.º 1
0
def test_get_diag(dtype, num_charges, Ds, flow):
    np.random.seed(10)
    np_flow = -np.int((np.int(flow) - 0.5) * 2)
    indices = [
        Index(
            BaseCharge(np.random.randint(-2, 3, (Ds[n], num_charges)),
                       charge_types=[U1Charge] * num_charges), flow)
        for n in range(2)
    ]
    arr = BlockSparseTensor.random(indices, dtype=dtype)
    fused = fuse_charges(arr.flat_charges, arr.flat_flows)
    inds = np.nonzero(fused == np.zeros((1, num_charges), dtype=np.int16))[0]
    # pylint: disable=no-member
    left, _ = np.divmod(inds, Ds[1])
    unique_charges = unique(np_flow *
                            (indices[0]._charges[0].charges[left, :]))
    diagonal = diag(arr)
    sparse_blocks, _, block_shapes = _find_diagonal_sparse_blocks(
        arr.flat_charges, arr.flat_flows, 1)
    data = np.concatenate([
        np.diag(np.reshape(arr.data[sparse_blocks[n]], block_shapes[:, n]))
        for n in range(len(sparse_blocks))
    ])
    np.testing.assert_allclose(data, diagonal.data)
    np.testing.assert_allclose(unique_charges,
                               diagonal.flat_charges[0].unique_charges)
Ejemplo n.º 2
0
def get_chargearray(num_charges, dtype=np.float64):
    D = np.random.randint(8, 12)
    charge = BaseCharge(np.random.randint(-5, 6, (num_charges, D)),
                        charge_types=[U1Charge] * num_charges)
    flow = False
    index = Index(charge, flow)
    return ChargeArray.random(indices=[index], dtype=dtype)
Ejemplo n.º 3
0
def test_BaseCharge_len():
    D = 300
    B = 5
    np.random.seed(10)
    q = np.random.randint(-B // 2, B // 2 + 1, (D, 2)).astype(np.int16)
    Q = BaseCharge(charges=q)
    assert len(Q) == 300
def test_ChargeArray_transpose_reshape_transpose_data(num_charges):
    Ds = np.array([8, 9, 10, 11])
    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)
    nparr = np.reshape(arr.data, Ds)

    arr2 = arr.transpose([2, 0, 1, 3])
    nparr2 = nparr.transpose([2, 0, 1, 3])
    arr3 = arr2.reshape([80, 99])
    nparr3 = nparr2.reshape([80, 99])
    arr4 = arr3.transpose([1, 0])
    nparr4 = nparr3.transpose([1, 0])

    arr5 = arr4.reshape([9, 11, 10, 8])
    nparr5 = nparr4.reshape([9, 11, 10, 8])
    np.testing.assert_allclose(arr3.transpose_data().data,
                               np.ascontiguousarray(nparr3).flat)
    np.testing.assert_allclose(arr4.transpose_data().data,
                               np.ascontiguousarray(nparr4).flat)
    np.testing.assert_allclose(arr5.transpose_data().data,
                               np.ascontiguousarray(nparr5).flat)
Ejemplo n.º 5
0
def test_pinv(dtype, num_charges):
    np.random.seed(10)
    R = 2
    D = 10
    charge = BaseCharge(np.random.randint(-5,
                                          6, (D, num_charges),
                                          dtype=np.int16),
                        charge_types=[U1Charge] * num_charges)
    flows = [True, False]
    A = BlockSparseTensor.random([Index(charge, flows[n]) for n in range(R)],
                                 (-0.5, 0.5),
                                 dtype=dtype)
    invA = pinv(A)
    left_eye = invA @ A

    blocks, _, shapes = _find_diagonal_sparse_blocks(left_eye.flat_charges,
                                                     left_eye.flat_flows, 1)
    for n, block in enumerate(blocks):
        t = np.reshape(left_eye.data[block], shapes[:, n])
        assert np.linalg.norm(t - np.eye(t.shape[0], t.shape[1])) < 1E-12

    right_eye = A @ invA
    blocks, _, shapes = _find_diagonal_sparse_blocks(right_eye.flat_charges,
                                                     right_eye.flat_flows, 1)
    for n, block in enumerate(blocks):
        t = np.reshape(right_eye.data[block], shapes[:, n])
        assert np.linalg.norm(t - np.eye(t.shape[0], t.shape[1])) < 1E-12
def test_diagonal(Ds, dtype, num_charges, flow):
    np.random.seed(10)
    backend = symmetric_backend.SymmetricBackend()
    np_flow = -np.int((np.int(flow) - 0.5) * 2)
    indices = [
        Index(
            BaseCharge(np.random.randint(-2, 3, (Ds[n], num_charges)),
                       charge_types=[U1Charge] * num_charges), flow)
        for n in range(2)
    ]
    arr = BlockSparseTensor.random(indices, dtype=dtype)
    fused = fuse_charges(arr.flat_charges, arr.flat_flows)
    inds = np.nonzero(fused == np.zeros((1, num_charges), dtype=np.int16))[0]
    # pylint: disable=no-member
    left, _ = np.divmod(inds, Ds[1])
    unique = np.unique(np_flow * (indices[0]._charges[0].charges[left, :]),
                       axis=0)
    diagonal = backend.diagonal(arr)

    sparse_blocks, _, block_shapes = _find_diagonal_sparse_blocks(
        arr.flat_charges, arr.flat_flows, 1)
    data = np.concatenate([
        np.diag(np.reshape(arr.data[sparse_blocks[n]], block_shapes[:, n]))
        for n in range(len(sparse_blocks))
    ])
    np.testing.assert_allclose(data, diagonal.data)
    np.testing.assert_allclose(unique, diagonal.flat_charges[0].unique_charges)
    with pytest.raises(NotImplementedError):
        diagonal = backend.diagonal(arr, axis1=0)
    with pytest.raises(NotImplementedError):
        diagonal = backend.diagonal(arr, axis2=1)
    with pytest.raises(NotImplementedError):
        diagonal = backend.diagonal(arr, offset=1)
Ejemplo n.º 7
0
def get_contractable_tensors(R1, R2, cont, dtype, num_charges, DsA, Dscomm,
                             DsB):
    assert R1 >= cont
    assert R2 >= cont
    chargesA = [
        BaseCharge(np.random.randint(-5, 5, (num_charges, DsA[n])),
                   charge_types=[U1Charge] * num_charges)
        for n in range(R1 - cont)
    ]
    commoncharges = [
        BaseCharge(np.random.randint(-5, 5, (num_charges, Dscomm[n])),
                   charge_types=[U1Charge] * num_charges) for n in range(cont)
    ]
    chargesB = [
        BaseCharge(np.random.randint(-5, 5, (num_charges, DsB[n])),
                   charge_types=[U1Charge] * num_charges)
        for n in range(R2 - cont)
    ]
    #contracted indices
    indsA = np.random.choice(np.arange(R1), cont, replace=False)
    indsB = np.random.choice(np.arange(R2), cont, replace=False)

    flowsA = np.full(R1, False, dtype=np.bool)
    flowsB = np.full(R2, False, dtype=np.bool)
    flowsB[indsB] = True

    indicesA = [None for _ in range(R1)]
    indicesB = [None for _ in range(R2)]
    for n, ia in enumerate(indsA):
        indicesA[ia] = Index(commoncharges[n], flowsA[ia])
        indicesB[indsB[n]] = Index(commoncharges[n], flowsB[indsB[n]])
    compA = list(set(np.arange(R1)) - set(indsA))
    compB = list(set(np.arange(R2)) - set(indsB))

    for n, ca in enumerate(compA):
        indicesA[ca] = Index(chargesA[n], flowsA[ca])
    for n, cb in enumerate(compB):
        indicesB[cb] = Index(chargesB[n], flowsB[cb])
    indices_final = []
    for n in sorted(compA):
        indices_final.append(indicesA[n])
    for n in sorted(compB):
        indices_final.append(indicesB[n])
    A = BlockSparseTensor.random(indices=indicesA, dtype=dtype)
    B = BlockSparseTensor.random(indices=indicesB, dtype=dtype)
    return A, B, indsA, indsB
Ejemplo n.º 8
0
def test_BaseCharge_charges():
    D = 100
    B = 6
    np.random.seed(10)
    charges = np.random.randint(-B // 2, B // 2 + 1, (D, 2)).astype(np.int16)

    q1 = BaseCharge(charges)
    np.testing.assert_allclose(q1.charges, charges)
def test_eye_dtype(dtype, num_charges):
    np.random.seed(10)
    backend = symmetric_backend.SymmetricBackend()
    index = Index(
        BaseCharge(np.random.randint(-5, 6, (num_charges, 100)),
                   charge_types=[U1Charge] * num_charges), False)
    actual = backend.eye(index, dtype=dtype)
    assert actual.dtype == dtype
def get_square_matrix(num_charges, dtype=np.float64):
    D = np.random.randint(40, 60)
    charges = BaseCharge(np.random.randint(-5, 6, (num_charges, D)),
                         charge_types=[U1Charge] * num_charges)

    flows = [False, True]
    indices = [Index(charges, flows[n]) for n in range(2)]
    return BlockSparseTensor.random(indices=indices, dtype=dtype)
Ejemplo n.º 11
0
def get_hermitian_matrix(num_charges, dtype=np.float64):
    D = np.random.randint(40, 60)
    charges = BaseCharge(np.random.randint(-5, 6, (D, num_charges)),
                         charge_types=[U1Charge] * num_charges)

    flows = [False, True]
    indices = [Index(charges, flows[n]) for n in range(2)]
    A = BlockSparseTensor.random(indices=indices, dtype=dtype)
    return A + A.conj().T
def get_tensor(R, num_charges, dtype=np.float64):
    Ds = np.random.randint(8, 12, R)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                   charge_types=[U1Charge] * num_charges) for n in range(R)
    ]
    flows = list(np.full(R, fill_value=False, dtype=np.bool))
    indices = [Index(charges[n], flows[n]) for n in range(R)]
    return BlockSparseTensor.random(indices=indices, dtype=dtype)
Ejemplo n.º 13
0
def test_eq__raises():
    np.random.seed(10)
    num_charges = 2
    charge = BaseCharge(np.random.randint(-2, 3, (30, num_charges)),
                        charge_types=[U1Charge] * num_charges)
    with pytest.raises(ValueError):
        _ = charge == np.random.randint(-1,
                                        1, (2, num_charges + 1),
                                        dtype=np.int16)
Ejemplo n.º 14
0
def test_BaseCharge_unique():
    D = 3000
    B = 5
    np.random.seed(10)
    q = np.random.randint(-B // 2, B // 2 + 1, (2, D)).astype(np.int16)
    Q = BaseCharge(charges=q, charge_types=[U1Charge, U1Charge])
    expected = np.unique(q,
                         return_index=True,
                         return_inverse=True,
                         return_counts=True,
                         axis=1)
    actual = Q.unique(return_index=True,
                      return_inverse=True,
                      return_counts=True)
    assert np.all(actual[0].charges == expected[0])
    assert np.all(actual[1] == expected[1])
    assert np.all(actual[2] == expected[2])
    assert np.all(actual[3] == expected[3])
def get_zeros(shape, num_charges, dtype=np.float64):
    R = len(shape)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, shape[n])),
                   charge_types=[U1Charge] * num_charges) for n in range(R)
    ]
    flows = list(np.full(R, fill_value=False, dtype=np.bool))
    indices = [Index(charges[n], flows[n]) for n in range(R)]
    return BlockSparseTensor.zeros(indices=indices, dtype=dtype)
Ejemplo n.º 16
0
def get_charge(chargetype, num_charges, D):
    if chargetype == "U1":
        out = BaseCharge(np.random.randint(-5, 6, (D, num_charges)),
                         charge_types=[U1Charge] * num_charges)
    if chargetype == "Z2":
        out = BaseCharge(np.random.randint(0, 2, (D, num_charges)),
                         charge_types=[Z2Charge] * num_charges)
    if chargetype == "mixed":
        n1 = num_charges // 2 if num_charges > 1 else 1
        out = BaseCharge(np.random.randint(-5, 6, (D, n1)),
                         charge_types=[U1Charge] * n1)

        if num_charges > 1:
            n2 = num_charges - n1
            out = out @ BaseCharge(np.random.randint(0, 2, (D, n2)),
                                   charge_types=[Z2Charge] * n2)

    return out
def test_ChargeArray_todense(dtype, num_charges):
    Ds = [8, 9, 10, 11]
    indices = [
        Index(
            BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                       charge_types=[U1Charge] * num_charges), False)
        for n in range(4)
    ]
    arr = ChargeArray.random(indices, dtype=dtype)
    np.testing.assert_allclose(arr.todense(), np.reshape(arr.data, Ds))
Ejemplo n.º 18
0
def test_BaseCharge_generic():
    D = 300
    B = 5
    np.random.seed(10)
    q = np.random.randint(-B // 2, B // 2 + 1, (D, 2)).astype(np.int16)
    unique_charges = np.unique(q, axis=0)
    Q = BaseCharge(charges=q)
    assert Q.dim == 300
    assert Q.num_symmetries == 2
    assert Q.num_unique == unique_charges.shape[0]
Ejemplo n.º 19
0
def get_charge(chargetype, num_charges, D):
    if chargetype == "U1":
        return BaseCharge(np.random.randint(-5, 6, (num_charges, D)),
                          charge_types=[U1Charge] * num_charges)
    if chargetype == "Z2":
        return BaseCharge(np.random.randint(0, 2, (num_charges, D)),
                          charge_types=[Z2Charge] * num_charges)
    if chargetype == "mixed":
        n1 = num_charges // 2 if num_charges > 1 else 1
        c = BaseCharge(np.random.randint(-5, 6, (n1, D)),
                       charge_types=[U1Charge] * n1)

        if num_charges > 1:
            n2 = num_charges - n1
            c = c @ BaseCharge(np.random.randint(0, 2, (n2, D)),
                               charge_types=[Z2Charge] * n2)

        return c
    return None
def test_zeros_dtype(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.zeros(indices, dtype=dtype)
    assert actual.dtype == dtype
Ejemplo n.º 21
0
def test_reduce_charges_2():
  left_charges = np.asarray([[-2, 0, 1, 0, 0], [-3, 0, 2, 1,
                                                0]]).astype(np.int16).T
  right_charges = np.asarray([[-1, 0, 2, 1], [-2, 2, 7, 0]]).astype(np.int16).T
  target_charge = np.zeros((1, 2), dtype=np.int16)
  fused_charges = fuse_ndarray_charges(left_charges, right_charges,
                                       [U1Charge, U1Charge])
  dense_positions = reduce_charges([
      BaseCharge(left_charges, charge_types=[U1Charge, U1Charge]),
      BaseCharge(right_charges, charge_types=[U1Charge, U1Charge])
  ], [False, False],
                                   target_charge,
                                   return_locations=True)

  np.testing.assert_allclose(dense_positions[0].charges, 0)
  #pylint: disable=no-member
  np.testing.assert_allclose(
      dense_positions[1],
      np.nonzero(np.logical_and.reduce(fused_charges == target_charge,
                                       axis=1))[0])
Ejemplo n.º 22
0
def test_trace_tensor(dtype, num_charges, D1, D2):
    np.random.seed(10)
    charge1 = BaseCharge(np.random.randint(-5,
                                           6, (num_charges, D1),
                                           dtype=np.int16),
                         charge_types=[U1Charge] * num_charges)
    charge2 = BaseCharge(np.random.randint(-5,
                                           6, (num_charges, D2),
                                           dtype=np.int16),
                         charge_types=[U1Charge] * num_charges)
    indices = [
        Index(charge1, False),
        Index(charge2, False),
        Index(charge1, True)
    ]
    tensor = BlockSparseTensor.random(indices, dtype=dtype)
    res = trace(tensor, (0, 2))
    assert res.sparse_shape[0] == indices[1]
    res_dense = np.trace(tensor.todense(), axis1=0, axis2=2)
    np.testing.assert_allclose(res.todense(), res_dense)
def test_random_uniform_non_zero_imag(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.random_uniform(indices, dtype=dtype, seed=10)
    assert np.linalg.norm(np.imag(actual.data)) != 0.0
Ejemplo n.º 24
0
def test_eye(dtype, num_charges, D):
  charge = BaseCharge(
      np.random.randint(-5, 6, (num_charges, D), dtype=np.int16),
      charge_types=[U1Charge] * num_charges)
  flow = False
  index = Index(charge, flow)
  A = eye(index, dtype=dtype)
  blocks, _, shapes = _find_diagonal_sparse_blocks(A.flat_charges, A.flat_flows,
                                                   1)
  for n, block in enumerate(blocks):
    t = np.reshape(A.data[block], shapes[:, n])
    np.testing.assert_almost_equal(t, np.eye(t.shape[0], t.shape[1]))
Ejemplo n.º 25
0
def test_trace_matrix(dtype, num_charges, D):
  np.random.seed(10)
  R = 2
  charge = BaseCharge(
      np.random.randint(-5, 6, (num_charges, D), dtype=np.int16),
      charge_types=[U1Charge] * num_charges)
  flows = [True, False]
  matrix = BlockSparseTensor.random([Index(charge, flows[n]) for n in range(R)],
                                    dtype=dtype)
  res = trace(matrix)
  res_dense = np.trace(matrix.todense())
  np.testing.assert_allclose(res.data, res_dense)
Ejemplo n.º 26
0
def test_pinv_raises():
  num_charges = 1
  np.random.seed(10)
  R = 3
  D = 10
  charge = BaseCharge(
      np.random.randint(-5, 6, (num_charges, D), dtype=np.int16),
      charge_types=[U1Charge] * num_charges)
  A = BlockSparseTensor.random([Index(charge, False) for n in range(R)],
                               (-0.5, 0.5))
  with pytest.raises(ValueError):
    pinv(A)
Ejemplo n.º 27
0
def test_BaseCharge_single_unique():
    D = 30
    np.random.seed(10)
    q = np.ones((D, 2), dtype=np.int16)
    Q = BaseCharge(charges=q, charge_types=[U1Charge, U1Charge])
    expected = np.unique(q,
                         return_index=True,
                         return_inverse=True,
                         return_counts=True,
                         axis=0)
    actual = Q.unique(return_index=True,
                      return_inverse=True,
                      return_counts=True)
    assert np.all(actual[0].charges == expected[0])
    assert np.all(actual[1] == expected[1])
    assert np.all(actual[2] == expected[2])
    assert np.all(actual[3] == expected[3])

    expected = np.unique(q, axis=0)
    actual = Q.unique()
    assert np.all(actual.charges == expected)
def test_random_uniform_dtype(dtype, num_charges):
    np.random.seed(10)
    R = 4
    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.random_uniform(indices, dtype=dtype, seed=10)
    assert actual.dtype == dtype
def test_eye(dtype, num_charges):
    np.random.seed(10)
    backend = symmetric_backend.SymmetricBackend()
    index = Index(
        BaseCharge(np.random.randint(-5, 6, (num_charges, 100)),
                   charge_types=[U1Charge] * num_charges), False)
    actual = backend.eye(index, dtype=dtype)
    expected = eye(index, 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))
    ])
Ejemplo n.º 30
0
def test_get_empty_diag(dtype, num_charges, Ds):
    np.random.seed(10)
    indices = [
        Index(
            BaseCharge(np.random.randint(-2, 3, (num_charges, Ds[n])),
                       charge_types=[U1Charge] * num_charges), False)
        for n in range(2)
    ]
    arr = BlockSparseTensor.random(indices, dtype=dtype)
    diagonal = diag(arr)
    np.testing.assert_allclose([], diagonal.data)
    for c in diagonal.flat_charges:
        assert len(c) == 0