Пример #1
0
def example():
    shapeA = (300, 400, 50)
    shapeB = (300, 600)
    shapeC = (400, 600, 50)
    a = Tensor(labels=['a3', 'b4', 'c5'], data=np.ones(shapeA))
    b = Tensor(labels=['a3', 'd6'], data=np.ones(shapeB))
    c = Tensor(labels=['e4', 'd6', 'c5'], data=np.ones(shapeC))

    # create tensors with labels

    makeLink('a3', 'a3', a, b)
    makeLink('c5', 'c5', a, c)
    makeLink('d6', 'd6', b, c)

    # make links via labels
    # note that labels can also be made between the "Leg" objects to avoid reused leg names, but here for simplicity from leg names

    # now we have a tensor network, we can generate the optimal sequence of this tensor list
    optimalSeq = generateOptimalSequence([a, b, c])
    print('optimal contraction sequence = {}'.format(optimalSeq))

    # if we do not have any knowledge in prior, we can contract the tensor list like
    res = contractTensorList([a, b, c])
    print(res)

    # if you already have a good sequence to use
    res, cost = contractAndCostWithSequence([a, b, c], seq=optimalSeq)
    print(res)
    print('contraction cost = {}'.format(cost))

    # if you want to save time / space by contract in place(note that after this you cannot contract them again, since their bonds between have been broken):
    res = contractWithSequence([a, b, c], seq=optimalSeq, inplace=True)
    print(res)
    print('')
Пример #2
0
def simplestExample():
    shapeA = (300, 4, 5)
    shapeB = (300, 6)
    shapeC = (4, 6, 5)
    a = Tensor(labels=['a300', 'b4', 'c5'], data=xplib.xp.ones(shapeA))
    b = Tensor(labels=['a300', 'd6'], data=xplib.xp.ones(shapeB))
    c = Tensor(labels=['e4', 'd6', 'c5'], data=xplib.xp.ones(shapeC))

    # create tensors with labels

    makeLink('a300', 'a300', a, b)
    makeLink('c5', 'c5', a, c)
    makeLink('d6', 'd6', b, c)

    # make links via labels
    # note that labels can also be made between the "Leg" objects to avoid reused leg names, but here for simplicity from leg names

    # now we have a tensor network, we can generate the optimal sequence of this tensor list
    optimalSeq = generateOptimalSequence([a, b, c])
    print('optimal contraction sequence = {}'.format(optimalSeq))

    # if we do not have any knowledge in prior, we can contract the tensor list like
    res = contractTensorList([a, b, c])
    print(res)

    # if you already have a good sequence to use
    res = contractWithSequence([a, b, c], seq=optimalSeq)
    print(res)

    # if you want to save time / space by contract in place(note that after this you cannot contract them again, since their bonds between have been broken):
    res = contractWithSequence([a, b, c], seq=optimalSeq, inplace=True)
    print(res)
    print('')

    # for reusable inplace contraction(which is our goal), refer to the use of CTL.tensornetwork.tensornetwork.FiniteTensorNetwork

    return res
Пример #3
0
    def test_outerProduct(self):
        a = Tensor(shape=(2, ), labels=['a'])
        b = Tensor(shape=(2, ), labels=['b'])

        op = contractTwoTensors(a, b, outProductWarning=False)
        self.assertTrue(funcs.compareLists(op.labels, ['a', 'b']))

        a = Tensor(shape=(2, 2, 2), labels=['a', 'b', 'c'])
        b = Tensor(shape=(2, ), labels=['x'])
        c = Tensor(shape=(2, ), labels=['y'])

        makeLink('a', 'x', a, b)
        makeLink('b', 'y', a, c)
        prod = contractTensorList([a, b, c], outProductWarning=False)
        self.assertTrue(funcs.compareLists(prod.labels, ['c']))

        dataA = np.random.random_sample((2, 2))
        dataB = np.random.random_sample((3, 3))
        a = Tensor(shape=(2, 2), labels=['a1', 'a2'], data=dataA)
        b = Tensor(shape=(3, 3), labels=['b1', 'b2'], data=dataB)

        prod = contractTensorList([a, b], outProductWarning=False)
        prod.reArrange(['a1', 'a2', 'b1', 'b2'])

        res = np.zeros((2, 2, 3, 3))
        for i in range(2):
            for j in range(2):
                for k in range(3):
                    for l in range(3):
                        res[(i, j, k, l)] = dataA[(i, j)] * dataB[(k, l)]
        # print(res, prod.a)
        self.assertTrue(funcs.floatArrayEqual(res, prod.a))

        a = Tensor(shape=(2, 2), labels=['a1', 'a2'], data=dataA)
        b = DiagonalTensor(shape=(3, 3),
                           labels=['b1', 'b2'],
                           data=np.diag(dataB))
        prod = contractTensorList([a, b], outProductWarning=False)
        prodData = prod.toTensor(['a1', 'a2', 'b1', 'b2'])
        # prod.reArrange(['a1', 'a2', 'b1', 'b2'])

        res = np.zeros((2, 2, 3, 3))
        for i in range(2):
            for j in range(2):
                for k in range(3):
                    # for l in range(3):
                    res[(i, j, k, k)] = dataA[(i, j)] * dataB[(k, k)]
        # print(res, prod.a)
        self.assertTrue(funcs.floatArrayEqual(res, prodData))

        dataA = np.random.random_sample((2, 2))
        dataB = np.random.random_sample(3)

        a = Tensor(shape=(2, 2), labels=['a1', 'a2'], data=dataA)
        b = DiagonalTensor(shape=(3, 3, 3),
                           labels=['b1', 'b2', 'b3'],
                           data=dataB)
        prod = contractTensorList([a, b], outProductWarning=False)
        prodData = prod.toTensor(['a1', 'a2', 'b1', 'b2', 'b3'])
        # prod.reArrange(['a1', 'a2', 'b1', 'b2'])

        res = np.zeros((2, 2, 3, 3, 3))
        for i in range(2):
            for j in range(2):
                for k in range(3):
                    # for l in range(3):
                    res[(i, j, k, k, k)] = dataA[(i, j)] * dataB[k]
        # print(res, prod.a)
        self.assertTrue(funcs.floatArrayEqual(res, prodData))