Пример #1
0
    def test_fullConnectedIsing(self):
        print('begin testing full connected Ising model:')
        latticeFC = completeGraph(n = 10, weight = 0.5)
        tensorNetwork = IsingTNFromUndirectedGraph(latticeFC)

        # seq = generateOptimalSequence(tensorNetwork, typicalDim = None)
        seq = generateGreedySequence(tensorNetwork)
        Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork, seq = seq)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        print('Z from MPS = {}'.format(ZMPS.single()))
        
        exactZ = exactZFromGraphIsing(latticeFC)
        print('exact Z = {}'.format(exactZ))

        self.assertTrue(funcs.floatEqual(Z.single(), exactZ, eps = 1e-3))
        self.assertTrue(funcs.floatEqual(ZMPS.single(), exactZ, eps = 1e-3))

        # latticeFC = completeGraph(n = 15, weight = 0.5)
        # tensorNetwork = IsingTNFromUndirectedGraph(latticeFC)

        # # seq = generateGreedySequence(tensorNetwork)
        # # Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork, seq = seq)

        # ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        # print('Z from MPS = {}'.format(ZMPS.single()))
        
        # exactZ = exactZFromGraphIsing(latticeFC)
        # print('exact Z = {}'.format(exactZ))

        print('')
Пример #2
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('')
Пример #3
0
def contractHandmadeTN():
    print('contractHandmadeTN():')
    a = Tensor(shape=(3, 5, 7), labels=['a3', 'a5', 'a7'])
    b = Tensor(shape=(2, 4, 5), labels=['b2', 'b4', 'b5'])
    c = Tensor(shape=(2, 7, 7, 7), labels=['c2', 'c71', 'c72', 'c73'])
    d = Tensor(shape=(7, 7, 3, 4), labels=['d71', 'd72', 'd3', 'd4'])
    e = Tensor(shape=(3, 3, 5), labels=['e31', 'e32', 'e5'])
    f = Tensor(shape=(2, 2, 5), labels=['f21', 'f22', 'f5'])
    g = Tensor(shape=(4, 4, 3, 3), labels=['g41', 'g42', 'g31', 'g32'])
    makeLink('a3', 'e31', a, e)
    makeLink('a5', 'b5', a, b)
    makeLink('a7', 'c72', a, c)

    makeLink('b2', 'f21', b, f)
    makeLink('b4', 'g41', b, g)

    makeLink('c2', 'f22', c, f)
    makeLink('c71', 'd72', c, d)
    makeLink('c73', 'd71', c, d)

    makeLink('d3', 'g31', d, g)
    makeLink('d4', 'g42', d, g)

    makeLink('e5', 'f5', e, f)
    makeLink('e32', 'g32', e, g)

    tensors = [a, b, d, c, g, f, e]

    res, _ = contractAndCostWithSequence(tensors)
    print('res from direct contraction = {}'.format(res.single()))

    mpsRes = contractWithMPS(tensors, chi=32)
    print('res from mps = {}'.format(mpsRes.single()))
    print('')
Пример #4
0
    def makeMPSContractionTest(self, tensors, eps=1e-8):
        res, cost = contractAndCostWithSequence(tensors)
        print('res = {}, cost = {}'.format(res.single(), cost))

        mpsRes = contractWithMPS(tensors, chi=32)
        print('res from mps = {}'.format(mpsRes.single()))

        eps = 1e-8
        self.assertTrue(
            funcs.floatEqual(res.single(), mpsRes.single(), eps=eps))
Пример #5
0
def squareIsingTest():
    print('squareIsingTest():')
    latticeFBC = squareLatticeFBC(n=4, m=4, weight=0.5)
    tensorNetwork = IsingTNFromUndirectedGraph(latticeFBC)

    Z, cost = contractAndCostWithSequence(tensorList=tensorNetwork)
    print('Z = {}, cost = {}'.format(Z.single(), cost))

    ZMPS = contractWithMPS(tensorList=tensorNetwork, chi=16)
    print('Z from MPS = {}'.format(ZMPS.single()))

    exactZ = exactZFromGraphIsing(latticeFBC)
    print('exact Z = {}'.format(exactZ))
Пример #6
0
def squareIsingTest(L=4):
    print('squareIsingTest(L = {}):'.format(L))

    weight = 0.5

    a = plaquetteIsingTensor(weight=weight, diamondForm=True)
    ctmrg = CTMRG(a, chi=16)
    ZCTMRG = ctmrg.getZ(L=L)
    print('CTMRG Z = {}'.format(ZCTMRG))

    latticeFBC = doubleSquareLatticeFBC(n=L, m=L, weight=weight)
    tensorNetwork = IsingTNFromUndirectedGraph(latticeFBC)

    if (L <= 6):
        Z, cost = contractAndCostWithSequence(tensorList=tensorNetwork,
                                              greedy=True)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

    ZMPS = contractWithMPS(tensorList=tensorNetwork, chi=16)
    print('Z from MPS = {}'.format(ZMPS.single()))

    if (L <= 3):
        exactZ = exactZFromGraphIsing(latticeFBC)
        print('exact Z = {}'.format(exactZ))
Пример #7
0
    def test_IsingFromGraph(self):
        
        latticePBC = squareLatticePBC(n = 4, m = 4, weight = 0.5)
        # for (4, 4) PBC case: the cost is low(13328 for full contraction), but the CPU time for calculating the optimal sequence is about one minute, so we test here with pre-calculated sequence
        tensorNetwork = IsingTNFromUndirectedGraph(latticePBC)

        # seq = generateOptimalSequence(tensorNetwork, typicalDim = None)
        seq = [(2, 3), (5, 6), (4, 7), (5, 4), (8, 12), (11, 15), (8, 11), (9, 13), (10, 14), (9, 10), (8, 9), (4, 8), (2, 4), (1, 2), (0, 1)] # pre-calculated
        # print('optimal seq = {}'.format(seq))
        Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork, seq = seq)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        print('Z from MPS = {}'.format(ZMPS.single()))
        
        exactZ = exactZFromGraphIsing(latticePBC)
        print('exact Z = {}'.format(exactZ))

        self.assertTrue(funcs.floatEqual(Z.single(), exactZ, eps = 1e-4))
        self.assertTrue(funcs.floatEqual(ZMPS.single(), exactZ, eps = 1e-4))

        latticeFBC = squareLatticeFBC(n = 4, m = 4, weight = 0.5)
        tensorNetwork = IsingTNFromUndirectedGraph(latticeFBC)

        # seq = generateOptimalSequence(tensorNetwork, typicalDim = None)
        seq = [(3, 7), (2, 3), (6, 2), (12, 13), (8, 12), (9, 8), (14, 15), (11, 14), (10, 11), (8, 10), (2, 8), (5, 2), (4, 2), (1, 2), (0, 1)] # pre-calculated
        # print('optimal seq = {}'.format(seq))
        Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork, seq = seq)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        # ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16, seq = seq)
        ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        print('Z from MPS = {}'.format(ZMPS.single()))
        
        exactZ = exactZFromGraphIsing(latticeFBC)
        print('exact Z = {}'.format(exactZ))

        self.assertTrue(funcs.floatEqual(Z.single(), exactZ, eps = 1e-6))
        self.assertTrue(funcs.floatEqual(ZMPS.single(), exactZ, eps = 1e-6))

        doubleLatticeFBC = doubleSquareLatticeFBC(n = 2, m = 2, weight = 0.5) # 12 tensors
        tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)

        # seq = [(1, 8), (4, 9), (5, 11), (10, 5), (4, 5), (3, 4), (1, 3), (7, 1), (2, 1), (6, 1), (0, 1)] # calculate on-the-fly is ok
        Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        exactZ = exactZFromGraphIsing(doubleLatticeFBC)
        print('exact Z = {}'.format(exactZ))

        ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        print('Z from MPS = {}'.format(ZMPS.single()))

        self.assertTrue(funcs.floatRelativeEqual(Z.single(), exactZ, eps = 1e-10))
        self.assertTrue(funcs.floatRelativeEqual(ZMPS.single(), exactZ, eps = 1e-10))

        # print(tensorNetwork)

        doubleLatticeFBC = doubleSquareLatticeFBC(n = 3, m = 3, weight = 0.5) # 24 tensors
        tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)

        seq = [(2, 15), (14, 2), (5, 2), (9, 20), (21, 9), (6, 9), (16, 6), (11, 23), (22, 11), (8, 11), (19, 8), (10, 8), (6, 8), (7, 6), (18, 6), (2, 6), (17, 2), (4, 2), (1, 2), (13, 1), (3, 1), (12, 1), (0, 1)]
        Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork, seq = seq)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        # exactZ = exactZFromGraphIsing(doubleLatticeFBC)
        # print('exact Z = {}'.format(exactZ))
        exactZ = 2694263494.5463686 # pre-calculated
        print('exact Z = {}'.format(exactZ))

        ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        print('Z from MPS = {}'.format(ZMPS.single()))

        self.assertTrue(funcs.floatRelativeEqual(Z.single(), exactZ, eps = 1e-10))
        self.assertTrue(funcs.floatRelativeEqual(ZMPS.single(), exactZ, eps = 1e-10))

        doubleLatticeFBC = doubleSquareLatticeFBC(n = 3, m = 3, weight = (0.5, 1.0)) # 24 tensors
        tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)

        seq = [(2, 15), (14, 2), (5, 2), (9, 20), (21, 9), (6, 9), (16, 6), (11, 23), (22, 11), (8, 11), (19, 8), (10, 8), (6, 8), (7, 6), (18, 6), (2, 6), (17, 2), (4, 2), (1, 2), (13, 1), (3, 1), (12, 1), (0, 1)]
        Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork, seq = seq)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        # exactZ = exactZFromGraphIsing(doubleLatticeFBC)
        # print('exact Z = {}'.format(exactZ))
        # exactZ = 2694263494.5463686 # pre-calculated
        # print('exact Z = {}'.format(exactZ))

        ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        print('Z from MPS = {}'.format(ZMPS.single()))

        self.assertTrue(funcs.floatRelativeEqual(Z.single(), ZMPS.single(), eps = 1e-10))
Пример #8
0
    def test_exactCTMRG(self):
        # test case for non-interacting Ising model
        weight = 0.0
        doubleLatticeFBC = doubleSquareLatticeFBC(n=3, m=3,
                                                  weight=weight)  # 24 tensors
        tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)

        seq = [(2, 15), (14, 2), (5, 2), (9, 20), (21, 9), (6, 9), (16, 6),
               (11, 23), (22, 11), (8, 11), (19, 8), (10, 8), (6, 8), (7, 6),
               (18, 6), (2, 6), (17, 2), (4, 2), (1, 2), (13, 1), (3, 1),
               (12, 1), (0, 1)]
        Z, cost = contractAndCostWithSequence(tensorList=tensorNetwork,
                                              seq=seq)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        # exactZ = 2694263494.5463686 # pre-calculated
        # print('exact Z = {}'.format(exactZ))

        # ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        # print('Z from MPS = {}'.format(ZMPS.single()))

        a = plaquetteIsingTensor(weight=weight, diamondForm=True)

        ctmrg = CTMRG(a, chi=16)
        # for i in range(1, 5):
        #     print('CTMRG Z(L = {}) = {}'.format(i, ctmrg.getZ(L = i)))
        # with self.assertWarns(RuntimeWarning):
        ZCTMRG = ctmrg.getZ(L=3)
        print('CTMRG Z = {}'.format(ZCTMRG))
        self.assertTrue(funcs.floatRelativeEqual(ZCTMRG, Z.single(),
                                                 eps=1e-10))

        # test case for Ising model
        weight = 0.5

        for nn in range(1, 3):
            doubleLatticeFBC = doubleSquareLatticeFBC(
                n=nn, m=nn, weight=weight)  # 24 tensors
            tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)
            Z, cost = contractAndCostWithSequence(tensorList=tensorNetwork)
            print('Z for L = {} is {}'.format(nn, Z.single()))

        doubleLatticeFBC = doubleSquareLatticeFBC(n=3, m=3,
                                                  weight=weight)  # 24 tensors
        tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)

        seq = [(2, 15), (14, 2), (5, 2), (9, 20), (21, 9), (6, 9), (16, 6),
               (11, 23), (22, 11), (8, 11), (19, 8), (10, 8), (6, 8), (7, 6),
               (18, 6), (2, 6), (17, 2), (4, 2), (1, 2), (13, 1), (3, 1),
               (12, 1), (0, 1)]
        Z, cost = contractAndCostWithSequence(tensorList=tensorNetwork,
                                              seq=seq)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        # exactZ = 2694263494.5463686 # pre-calculated
        # print('exact Z = {}'.format(exactZ))

        # ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        # print('Z from MPS = {}'.format(ZMPS.single()))

        a = plaquetteIsingTensor(weight=weight, diamondForm=True)
        # for i in range(1, 5):
        #     print('CTMRG Z(L = {}) = {}'.format(i, ctmrg.getSingleZ(L = i)))

        ctmrg = CTMRG(a, chi=16)
        # for i in range(1, 5):
        #     print('CTMRG Z(L = {}) = {}'.format(i, ctmrg.getZ(L = i)))
        # with self.assertWarns(RuntimeWarning):
        ZCTMRG = ctmrg.getZ(L=3)
        print('CTMRG Z = {}'.format(ZCTMRG))
        self.assertTrue(funcs.floatRelativeEqual(ZCTMRG, Z.single(),
                                                 eps=1e-10))

        weight = 0.5
        doubleLatticeFBC = doubleSquareLatticeFBC(n=5, m=5,
                                                  weight=weight)  # 24 tensors
        tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)

        Z, cost = contractAndCostWithSequence(tensorList=tensorNetwork,
                                              seq=None,
                                              greedy=True)
        print('Z = {}, cost = {}'.format(Z.single(), cost))

        # ZMPS = contractWithMPS(tensorList = tensorNetwork, chi = 16)
        # print('Z from MPS = {}'.format(ZMPS.single()))

        a = plaquetteIsingTensor(weight=weight, diamondForm=True)

        ctmrg = CTMRG(a, chi=16)
        ZCTMRG = ctmrg.getZ(L=5)
        print('CTMRG Z = {}'.format(ZCTMRG))
        self.assertTrue(funcs.floatRelativeEqual(ZCTMRG, Z.single(),
                                                 eps=1e-10))

        weight = 0.5
        doubleLatticeFBC = doubleSquareLatticeFBC(n=7, m=7,
                                                  weight=weight)  # 24 tensors
        tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)

        # Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork, seq = None, greedy = True)
        # print('Z = {}, cost = {}'.format(Z.single(), cost))

        ZMPS = contractWithMPS(tensorList=tensorNetwork, chi=16)
        print('Z from MPS = {}'.format(ZMPS.single()))

        a = plaquetteIsingTensor(weight=weight, diamondForm=True)

        ctmrg = CTMRG(a, chi=16)
        ZCTMRG = ctmrg.getZ(L=7)
        print('CTMRG Z = {}'.format(ZCTMRG))
        self.assertTrue(
            funcs.floatRelativeEqual(ZCTMRG, ZMPS.single(), eps=1e-10))

        weight = 0.7
        doubleLatticeFBC = doubleSquareLatticeFBC(n=6, m=6,
                                                  weight=weight)  # 24 tensors
        tensorNetwork = IsingTNFromUndirectedGraph(doubleLatticeFBC)

        # Z, cost = contractAndCostWithSequence(tensorList = tensorNetwork, seq = None, greedy = True)
        # print('Z = {}, cost = {}'.format(Z.single(), cost))

        ZMPS = contractWithMPS(tensorList=tensorNetwork, chi=16)
        print('Z from MPS = {}'.format(ZMPS.single()))

        a = plaquetteIsingTensor(weight=weight, diamondForm=True)

        ctmrg = CTMRG(a, chi=16)
        ZCTMRG = ctmrg.getZ(L=6)
        print('CTMRG Z = {}'.format(ZCTMRG))
        self.assertTrue(
            funcs.floatRelativeEqual(ZCTMRG, ZMPS.single(), eps=1e-10))
Пример #9
0
    def test_contraction(self):
        self.showTestCaseBegin("diagonal tensor contraction")
        # print('Begin test diagonalTensor contraction.')
        a = DiagonalTensor(shape = (2, 2), labels = ['a', 'b'])
        b = Tensor(shape = (2, ), labels = ['x'])
        c = Tensor(shape = (2, ), labels = ['y'])

        makeLink('a', 'x', a, b)
        makeLink('b', 'y', a, c)
        seq = generateOptimalSequence([a, b, c], typicalDim = 10)
        # print('optimal sequence = {}'.format(seq))
        prod, cost = contractAndCostWithSequence([a, b, c], seq = seq)
        # print('cost = {}'.format(cost))
        # prod = contractTensorList([a, b, c], outProductWarning = False)
        self.assertTrue(funcs.compareLists(prod.labels, []))
        self.assertListEqual(seq, [(0, 2), (1, 0)])
        self.assertEqual(cost, 4.0)

        # if we use Tensor instead of DiagonalTensor for a
        # then the cost should be 12.0, and the order should be (1, 2), (0, 1)
        # the optimal cost of diagonal tensors can be achieved if we use diagonal nature for contraction

        a = DiagonalTensor(shape = (2, 2, 2), labels = ['a', 'b', 'c'])
        b = DiagonalTensor(shape = (2, 2), labels = ['x', 'y'])
        makeLink('a', 'x', a, b)
        prod, cost = contractAndCostWithSequence([a, b])
        self.assertEqual(cost, 2)
        self.assertTrue(funcs.compareLists(prod.labels, ['b', 'c', 'y']))

        aData = np.array([[[1, 0], [0, 0]], [[0, 0], [0, 3]]])
        bData = np.random.random_sample(2)
        cData = np.random.random_sample(2)

        a = DiagonalTensor(data = aData, labels = ['a', 'b', 'c'])
        b = Tensor(data = bData, labels = ['x'])
        c = Tensor(data = cData, labels = ['y'])

        makeLink('a', 'x', a, b)
        makeLink('b', 'y', a, c)

        res1, _ = contractAndCostWithSequence([a, b, c])
        # print('seq = {}'.format(generateOptimalSequence([a, b, c])))

        a = Tensor(data = aData, labels = ['a', 'b', 'c'])
        b = Tensor(data = bData, labels = ['x'])
        c = Tensor(data = cData, labels = ['y'])

        makeLink('a', 'x', a, b)
        makeLink('b', 'y', a, c)

        res2, _ = contractAndCostWithSequence([a, b, c])
        # self.assertListEqual(list(res1.a), list(res2.a))
        self.assertTrue(funcs.floatArrayEqual(res1.a, res2.a))

        # print(cost1, cost2)

        # print(res1.a, res2.a)

        # test diagonal tensor contraction
        a = DiagonalTensor(shape = (2, 2), labels = ['a1', 'a2'])
        b = DiagonalTensor(shape = (2, 2, 2), labels = ['b1', 'b2', 'b3'])
        makeLink('a1', 'b1', a, b)
        res = a @ b
        self.assertTupleEqual(res.shape, (2, 2, 2))
        self.assertEqual(res.dim, 3)
        self.assertTrue(res.diagonalFlag)
        self.assertTrue((res.a == np.ones(2)).all())

        # test for diagonal * diagonal contraction cost(just O(length))
        a = DiagonalTensor(shape = (2, 2), labels = ['a1', 'a2'])
        b = DiagonalTensor(shape = 2, labels = ['b1', 'b2']) # deduce dim
        makeLink('a1', 'b2', a, b)

        cost, _ = contractCost(a, b)
        self.assertEqual(cost, 2.0)

        res, cost = contractAndCostWithSequence([a, b])
        self.assertEqual(res.dim, 2)
        self.assertEqual(res._length, 2)
        self.assertTupleEqual(res.shape, (2, 2))
        self.assertEqual(cost, 2.0)
        self.assertTrue(res.diagonalFlag)

        a = DiagonalTensor(shape = (2, 2), labels = ['a1', 'a2'])
        b = Tensor(shape = (2, 3, 3), labels = ['b1', 'b2', 'b3']) # deduce dim
        makeLink('a1', 'b1', a, b)

        cost, _ = contractCost(a, b)
        self.assertEqual(cost, 18.0)

        res, cost = contractAndCostWithSequence([a, b])
        # print(res)
        self.assertEqual(res.dim, 3)
        self.assertTrue(funcs.compareLists(list(res.shape), [2, 3, 3]))
        self.assertEqual(cost, 18.0)
        self.showTestCaseEnd("diagonal tensor contraction")