Exemplo n.º 1
0
    name_list_A = [f"A.{i}" for i in range(rank_A)]
    name_list_B = [f"B.{i}" for i in range(rank_B)]
    for i in range(rank_fuse):
        name_list_A[fuse_leg_A[i]] = f"{i}"
        name_list_B[fuse_leg_B[i]] = f"{i}"

    dim_A = np.random.randint(1, max_random, rank_A).tolist()
    dim_B = np.random.randint(1, max_random, rank_B).tolist()
    for i in range(rank_total):
        dim_A[total_leg_A[i]] = dim_B[total_leg_B[i]] = np.random.randint(
            2, max_random)

    A = Tensor(name_list_A, dim_A).test()
    B = Tensor(name_list_B, dim_B).test()
    C = A.contract(
        B, {(f"A.{contract_leg_A[i]}", f"B.{contract_leg_B[i]}")
            for i in range(rank_contract)})
    # print(repr(A))
    # print(repr(B))
    # print(repr(C))
    a = A.block[{}]
    b = B.block[{}]

    index_A = [chr(ord('a') + i) for i in range(rank_A)]
    index_B = [chr(ord('A') + i) for i in range(rank_B)]
    index_C = []
    for i in range(rank_total):
        index_A[total_leg_A[i]] = index_B[total_leg_B[i]]
    # fuse的顺序是按照name自身顺序来的
    for i in fuse_leg_A:
        index_C.append(index_A[i])
Exemplo n.º 2
0
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

import numpy as np
from TAT.Tensor import DNo as Tensor

max_random = 8

for _ in range(100):
    rank_A = np.random.randint(2, max_random)
    rank_B = np.random.randint(2, max_random)
    rank_contract = np.random.randint(1, np.min([rank_A, rank_B]))
    # print(rank_A, rank_B, rank_contract)

    contract_name_A = np.random.choice(range(rank_A), rank_contract, False)
    contract_name_B = np.random.choice(range(rank_B), rank_contract, False)

    dim_A = np.random.randint(1, max_random, size=rank_A)
    dim_B = np.random.randint(1, max_random, size=rank_B)
    dim_contract = np.random.randint(1, max_random, size=rank_contract)

    dim_A = [j if i not in contract_name_A else dim_contract[contract_name_A.tolist().index(i)] for i, j in enumerate(dim_A)]
    dim_B = [j if i not in contract_name_B else dim_contract[contract_name_B.tolist().index(i)] for i, j in enumerate(dim_B)]

    A = Tensor([f"A.{i}" for i in range(rank_A)], dim_A).randn()
    B = Tensor([f"B.{i}" for i in range(rank_B)], dim_B).randn()
    v_t = A.contract(B, {(f"A.{i}", f"B.{j}") for i, j in zip(contract_name_A, contract_name_B)}).block[{}]
    v_n = np.tensordot(A.block[{}], B.block[{}], [contract_name_A, contract_name_B])
    v_d = v_t - v_n
    print(np.max(np.abs(v_d)))