Beispiel #1
0
    def test_FreeBondFTN(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=['e4', 'd6', 'c5'],
                   data=np.ones(shapeC))

        tensorDict = TensorDict()
        tensorDict.setTensor('a', a)
        tensorDict.setTensor('b', b)
        tensorDict.setTensor('c', c)

        FTN = FiniteTensorNetwork(['a', 'b'], realCost=True)
        FTN.addTensor('c')

        FTN.addLink('a', 'a300', 'b', 'a300')
        FTN.addLink('a', 'c5', 'c', 'c5')
        FTN.addLink('b', 'd6', 'c', 'd6')

        result = FTN.contract(tensorDict, removeTensorTag=False)
        self.assertTrue(funcs.compareLists(result.labels, ['a-b4', 'c-e4']))
        self.assertEqual(int(result.a[0][1]), 9000)

        result = FTN.contract(tensorDict, removeTensorTag=True)
        self.assertTrue(funcs.compareLists(result.labels, ['b4', 'e4']))
        self.assertEqual(int(result.a[0][1]), 9000)

        FTN.unlock()
        FTN.addPostNameChange('c', 'e4', 'e4FromC')
        FTN.addPreNameChange('a', 'b4', 'b4FromA')
        FTN.addPreNameChange('a', 'a300', 'a3')
        FTN.removePreNameChange('a', 'a300', 'a3')
        FTN.addPostNameChange('a', 'd6', 'foo')
        FTN.removePostNameChange('a', 'd6', 'foo')

        result = FTN.contract(tensorDict, removeTensorTag=True)
        # print(result.labels)
        self.assertTrue(
            funcs.compareLists(result.labels, ['b4FromA', 'e4FromC']))
        self.assertEqual(int(result.a[0][1]), 9000)

        FTN.unlock()
        FTN.removePostNameChange('c', 'e4', 'e4FromC')
        FTN.addPreNameChange('c', 'e4', 'e4FromC')
        FTN.addPostOutProduct([('a', 'b4FromA'), ('c', 'e4FromC')], 'out')
        result = FTN.contract(tensorDict, removeTensorTag=True)

        self.assertListEqual(result.labels, ['out'])
        self.assertEqual(result.shape[0], 16)
Beispiel #2
0
    def test_FTN(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))

        tensorDict = TensorDict()
        tensorDict.setTensor('a', a)
        tensorDict.setTensor('b', b)
        tensorDict.setTensor('c', c)

        FTN = FiniteTensorNetwork(['a', 'b'], realCost=True)
        FTN.addTensor('c')

        FTN.addLink('a', 'a300', 'b', 'a300')
        FTN.addLink('a', 'b4', 'c', 'b4')
        FTN.addLink('a', 'c5', 'c', 'c5')
        FTN.addLink('b', 'd6', 'c', 'd6')

        result = FTN.contract(tensorDict)
        self.assertEqual(int(result.a), 36000)
        self.assertListEqual(FTN.optimalSeq, [(0, 1), (2, 0)])

        result2 = FTN.contract(tensorDict)
        self.assertEqual(int(result2.a), 36000)
        self.assertListEqual(FTN.optimalSeq, [(0, 1), (2, 0)])

        newShapeA = (3, 4, 5)
        newShapeB = (3, 6)

        newA = Tensor(shape=newShapeA,
                      labels=['a300', 'b4', 'c5'],
                      data=np.ones(newShapeA))
        newB = Tensor(shape=newShapeB,
                      labels=['a300', 'd6'],
                      data=np.ones(newShapeB))

        tensorDict.setTensor('a', newA)
        tensorDict.setTensor('b', newB)

        result3 = FTN.contract(tensorDict)
        self.assertEqual(int(result3.a), 360)
        self.assertListEqual(FTN.optimalSeq, [(0, 2), (1, 0)])
        self.assertEqual(FTN.bondDims['a-a300'], 3)
        self.assertEqual(FTN.tensorCount, 3)
Beispiel #3
0
def HOTRGGiltTN():
    ATensorNames = ["aul", "aur", "adl", "adr"]
    QTensorNames = [
        'qbll', 'qbrl', 'qall', 'qarl', 'qblr', 'qbrr', 'qalr', 'qarr', 'qbu',
        'qbd', 'qau', 'qad'
    ]  # i(circle), o(line)
    wTensorNames = ['wll', 'wlr', 'wrl', 'wrr']  # u, d, o
    vTensorNames = ['vu', 'vd']  # l, r, o

    tensorNames = ATensorNames + QTensorNames + wTensorNames + vTensorNames

    FTN = FiniteTensorNetwork(tensorNames=tensorNames)

    # FTN.addLink('aul', 'adl', 'd', 'u')
    # FTN.addLink('aur', 'adr', 'd', 'u')

    # up / down bonds
    FTN.addLink('vu', 'l', 'qbu', 'o')
    FTN.addLink('vu', 'r', 'qau', 'o')
    FTN.addLink('vd', 'l', 'qbd', 'o')
    FTN.addLink('vd', 'r', 'qad', 'o')

    # left / right bonds
    FTN.addLink('wll', 'u', 'qbll', 'o')
    FTN.addLink('wll', 'd', 'qall', 'o')
    FTN.addLink('wrr', 'u', 'qbrr', 'o')
    FTN.addLink('wrr', 'd', 'qarr', 'o')

    # A left bonds
    FTN.addLink('aul', 'u', 'qbu', 'i')
    FTN.addLink('adl', 'd', 'qbd', 'i')
    FTN.addLink('aul', 'd', 'adl', 'u')
    FTN.addLink('aul', 'l', 'qbll', 'i')
    FTN.addLink('adl', 'l', 'qall', 'i')
    FTN.addLink('aul', 'r', 'qbrl', 'i')
    FTN.addLink('adl', 'r', 'qarl', 'i')

    # A right bonds
    FTN.addLink('aur', 'u', 'qau', 'i')
    FTN.addLink('adr', 'd', 'qad', 'i')
    FTN.addLink('aur', 'd', 'adr', 'u')
    FTN.addLink('aur', 'l', 'qblr', 'i')
    FTN.addLink('adr', 'l', 'qalr', 'i')
    FTN.addLink('aur', 'r', 'qbrr', 'i')
    FTN.addLink('adr', 'r', 'qarr', 'i')

    # center w bonds
    FTN.addLink('wlr', 'u', 'qbrl', 'o')
    FTN.addLink('wlr', 'd', 'qarl', 'o')
    FTN.addLink('wrl', 'u', 'qblr', 'o')
    FTN.addLink('wrl', 'd', 'qalr', 'o')
    FTN.addLink('wlr', 'o', 'wrl', 'o')

    tensorDict = TensorDict()
    chi = 10
    for ATensorName in ATensorNames:
        tensorDict.setTensor(
            ATensorName,
            Tensor(data=np.random.randn(chi, chi, chi, chi),
                   labels=['u', 'l', 'd', 'r']))
    for QTensorName in QTensorNames:
        tensorDict.setTensor(
            QTensorName,
            Tensor(data=np.random.randn(chi, chi), labels=['i', 'o']))
    for wTensorName in wTensorNames:
        tensorDict.setTensor(
            wTensorName,
            Tensor(data=np.random.randn(chi, chi, chi), labels=['u', 'd',
                                                                'o']))
    for vTensorName in vTensorNames:
        tensorDict.setTensor(
            vTensorName,
            Tensor(data=np.random.randn(chi, chi, chi), labels=['l', 'r',
                                                                'o']))

    res = FTN.contract(tensorDict)
    print(res)