Пример #1
0
def simplestExample():
    shapeA = (300, 4, 5)
    shapeB = (300, 6)
    shapeC = (4, 6, 5)
    a = Tensor(labels=['a300', 'b4', 'c5'], data=np.ones(shapeA))
    b = Tensor(labels=['a300', 'd6'], data=np.ones(shapeB))
    c = Tensor(labels=['e4', 'd6', 'c5'], data=np.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('')
Пример #2
0
    def contract(self, tensorDict, removeTensorTag=True):
        self.lock()

        if (isinstance(tensorDict, dict)):
            tensorDict = TensorDict(tensorDict)

        # print(tensorDict.tensors)

        assert funcs.compareLists(
            self.tensorNames, list(tensorDict.tensors.keys())
        ), "Error: input tensorDict {} does not compatible with FTN {}.".format(
            list(tensorDict.tensors.keys()), self.tensorNames)

        localTensors = dict()
        for name in tensorDict.tensors:
            localTensors[name] = tensorDict.tensors[name].copy()

        for tensor, legName, newName in self.changeNameBefore:
            localTensors[tensor].renameLabel(legName, newName)

        for name in localTensors:
            localTensors[name].addTensorTag(name)

        for leg1, leg2 in self.links:
            tensorA, legA = leg1
            tensorB, legB = leg2
            makeLink(legA, legB, localTensors[tensorA], localTensors[tensorB])

        self.changed = False
        self.loadBondDims(localTensors)

        tensorList = [localTensors[name] for name in self.tensorNames]
        if (self.optimalSeq is None) or (self.realCost and self.changed):
            # print('getting optimal seq')
            self.optimalSeq = generateOptimalSequence(
                tensorList, bf=False, typicalDim=self.typicalDim)
            # print('optimal seq = {}'.format(self.optimalSeq))

        res = contractWithSequence(tensorList,
                                   seq=self.optimalSeq,
                                   inplace=True)
        # print(res)
        # print(tensorDict.tensors)

        for labelList, newLabel in self.outProductAfter:
            labelList = [
                self._dealOutProductLabel(label) for label in labelList
            ]
            # print(labelList, newLabel)
            # print(res.labels)
            res.outProduct(labelList, 'res-' + newLabel)
            # print(res.labels)

        for tensor, legName, newName in self.changeNameAfter:
            res.renameLabel(tensor + '-' + legName, tensor + '-' + newName)

        if (removeTensorTag):
            res.removeTensorTag()
        return res
Пример #3
0
    def test_TensorGraph(self):
        shapeA = (300, 4, 5)
        shapeB = (300, 6)
        shapeC = (4, 6, 5)
        a = Tensor(shape=shapeA,
                   labels=['a300', 'b4', 'c5'],
                   data=np.ones(shapeA))
        b = Tensor(shape=shapeB, labels=['a300', 'd6'], data=np.ones(shapeB))
        c = Tensor(shape=shapeC,
                   labels=['b4', 'd6', 'c5'],
                   data=np.ones(shapeC))

        makeLink(a.getLeg('a300'), b.getLeg('a300'))
        makeLink(a.getLeg('b4'), c.getLeg('b4'))
        makeLink(a.getLeg('c5'), c.getLeg('c5'))
        makeLink(b.getLeg('d6'), c.getLeg('d6'))

        tensorList = [a, b, c]

        tensorGraph = makeTensorGraph(tensorList)

        # if we use typical dim, then contract between 0 and 2 first is preferred
        # and this is not true if we consider the real bond dimension 300

        seq = tensorGraph.optimalContractSequence(typicalDim=None)
        self.assertListEqual(seq, [(0, 1), (2, 0)])
        self.assertEqual(tensorGraph.optimalCostResult(), 36120)

        seq = tensorGraph.optimalContractSequence(typicalDim=None, bf=True)
        self.assertEqual(tensorGraph.optimalCostResult(), 36120)

        # res1 = contractWithSequence(tensorList, seq = seq)

        seq = tensorGraph.optimalContractSequence(typicalDim=10)
        self.assertListEqual(seq, [(0, 2), (1, 0)])
        self.assertEqual(tensorGraph.optimalCostResult(), 10100)

        seq = tensorGraph.optimalContractSequence(typicalDim=10, bf=True)
        self.assertEqual(tensorGraph.optimalCostResult(), 10100)

        res2 = contractWithSequence(tensorList, seq=seq)
        self.assertEqual(
            res2.a**2,
            funcs.tupleProduct(shapeA) * funcs.tupleProduct(shapeB) *
            funcs.tupleProduct(shapeC))
Пример #4
0
 def toTensor(self):
     return contractWithSequence(self._tensors)