Ejemplo n.º 1
0
def test_contract():
    for i in range(5):
        x = np.random.randn(2, 2, 2, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))

        y = np.random.randn(2, 2, 2, 2, 2)
        yt = TreeTensor(accuracy=epsilon)
        yt.addTensor(ArrayTensor(y))

        zt = xt.contract([0, 1, 4], yt, [2, 3, 4])
        assert np.sum(
            (zt.array - np.einsum('ijklm,qwijm->klqw', x, y))**2) < epsilon

        zt = yt.contract([0, 1, 4], xt, [2, 3, 4])
        assert np.sum(
            (zt.array - np.einsum('ijklm,qwijm->klqw', y, x))**2) < epsilon

    for i in range(5):
        x = np.random.randn(2, 2, 3, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))

        y = np.random.randn(3, 2, 2, 2, 2)
        yt = TreeTensor(accuracy=epsilon)
        yt.addTensor(ArrayTensor(y))

        zt = xt.contract([0, 1, 4], yt, [2, 3, 4])
        assert np.sum(
            (zt.array - np.einsum('ijklm,qwijm->klqw', x, y))**2) < epsilon
Ejemplo n.º 2
0
def test_pathing():
    tn = TreeNetwork(accuracy=epsilon)

    n1 = Node(ArrayTensor(np.random.randn(3, 3, 3)))

    tn.addNode(n1)

    assert len(tn.pathBetween(n1, n1)) == 1

    n2 = Node(ArrayTensor(np.random.randn(3, 3, 3)))

    Link(n1.buckets[0], n2.buckets[0])

    tn.addNode(n2)

    assert len(tn.pathBetween(n1, n2)) == 2
    assert len(tn.pathBetween(n2, n1)) == 2
    assert tn.pathBetween(n1, n2) == [n1, n2]
    assert tn.pathBetween(n2, n1) == [n2, n1]

    n3 = Node(ArrayTensor(np.random.randn(3, 3, 3)))

    Link(n3.buckets[0], n2.buckets[2])

    tn.addNode(n3)

    assert len(tn.pathBetween(n1, n3)) == 3
    assert len(tn.pathBetween(n3, n1)) == 3
    assert tn.pathBetween(n1, n3) == [n1, n2, n3]
    assert tn.pathBetween(n3, n1) == [n3, n2, n1]
    assert tn.pathBetween(n2, n3) == [n2, n3]
    assert tn.pathBetween(n3, n2) == [n3, n2]
Ejemplo n.º 3
0
def test_mergeNode_ArrayTensor():
    for i in range(5):
        net = Network()

        x = np.random.randn(2, 3, 3)
        xt = ArrayTensor(x)
        n1 = Node(xt)
        xt = ArrayTensor(x)
        n2 = Node(xt)

        net.addNode(n1)
        Link(n1.buckets[0], n2.buckets[0])
        net.addNode(n2)

        net.mergeNodes(n1, n2)

        assert len(net.nodes) == 1
        assert len(net.buckets) == 4
        assert len(net.internalBuckets) == 0
        assert len(net.externalBuckets) == 4
        assert len(net.optimizedLinks) == 0

        arr, bdict = net.array
        assert arr.shape == (3, 3, 3, 3)
        for b in net.buckets:
            assert b.id in bdict
        for b1 in net.buckets:
            for b2 in net.buckets:
                if b1.id < b2.id:
                    assert bdict[b1.id] < bdict[b2.id]

        assert np.sum((arr - np.einsum('ijk,ilm->jklm', x, x))**2) < epsilon
Ejemplo n.º 4
0
def test_flatten():
    for i in range(5):
        x = np.random.randn(3, 3, 5)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))
        assert np.sum(
            (xt.flatten([0, 1]).array - np.reshape(x, (-1, 5)).T)**2) < epsilon

    for i in range(5):
        x = np.random.randn(2, 2, 2, 2, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))
        assert np.sum(
            (xt.flatten([1, 2]).array -
             np.transpose(np.reshape(x,
                                     (2, 4, 2, 2, 2)), axes=[0, 2, 3, 4, 1]))**
            2) < epsilon

    for i in range(5):
        x = np.random.randn(2, 2, 2, 2, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))
        assert np.sum((xt.flatten([2, 1]).array -
                       np.transpose(np.reshape(np.swapaxes(x, 1, 2),
                                               (2, 4, 2, 2, 2)),
                                    axes=[0, 2, 3, 4, 1]))**2) < epsilon
Ejemplo n.º 5
0
def test_mergeLinks_TreeTensor_Compress():
    for i in range(5):
        net = Network()

        x = np.random.randn(2, 2, 2, 2, 3, 3)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))
        n1 = Node(xt)
        x = np.random.randn(2, 2, 2, 2, 3, 3)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))
        n2 = Node(xt)

        net.addNode(n1)
        Link(n1.buckets[0], n2.buckets[0])
        Link(n1.buckets[1], n2.buckets[1])
        net.addNode(n2)

        arr1, bdict1 = net.array

        net.mergeLinks(n1, compress=True, accuracy=epsilon)

        arr2, bdict2 = net.array

        assert np.sum((arr1 - arr2)**2) < epsilon
        assert np.sum((arr1 - arr2)**2) < epsilon
Ejemplo n.º 6
0
def test_getIndexFactor():
    for i in range(5):
        x = np.random.randn(3, 3, 3)
        xT = ArrayTensor(x)
        a, j = xT.getIndexFactor(0)
        assert np.sum((x / np.max(np.abs(x)) - a)**2) < epsilon
        assert j == 0
Ejemplo n.º 7
0
    def splitNode(self, node, ignore=None):
        '''
        Takes as input a Node and ensures that it is at most rank 3 by factoring rank 3 tensors
        out of it until what remains is rank 3. The factoring is done via a greedy algorithm,
        where the pair of indices with the least correlation with the rest are factored out.
        This is determined by explicitly tracing out all but those indices from the density
        matrix and computing the entropy.

        ignore may be None or a pair of indices.
        In the latter case, the pair of indices will be required to stay together.
        This is enforced by having the pair be the first one factored.
        '''
        nodes = []

        while node.tensor.rank > 3:
            self.removeNode(node)

            array = node.tensor.scaledArray

            s = []

            if ignore is not None:
                p = ignore
                ignore = None
            else:
                p = entropy(array)

            u, v, indices1, indices2 = splitArray(array,
                                                  p,
                                                  accuracy=self.accuracy)

            if u.shape[-1] > 1:
                b1 = Bucket()
                b2 = Bucket()
                n1 = Node(ArrayTensor(u, logScalar=node.tensor.logScalar / 2),
                          Buckets=[node.buckets[i] for i in indices1] + [b1])
                n2 = Node(ArrayTensor(v, logScalar=node.tensor.logScalar / 2),
                          Buckets=[b2] + [node.buckets[i] for i in indices2])
                # This line has to happen before addNode to prevent b1 and b2
                # from becoming externalBuckets
                _ = Link(b1, b2)
            else:
                # Cut link
                u = u[..., 0]
                v = v[0]
                n1 = Node(ArrayTensor(u, logScalar=node.tensor.logScalar / 2),
                          Buckets=[node.buckets[i] for i in indices1])
                n2 = Node(ArrayTensor(v, logScalar=node.tensor.logScalar / 2),
                          Buckets=[node.buckets[i] for i in indices2])

            self.addNode(n1)
            self.addNode(n2)
            nodes.append(n1)

            node = n2

        nodes.append(node)

        return nodes
Ejemplo n.º 8
0
def test_setIndexFactor():
    for i in range(5):
        x = np.random.randn(3, 3, 3)
        xT = ArrayTensor(x)
        y = np.random.randn(3, 3, 3)
        assert np.sum(
            (xT.setIndexFactor(0, y).array - y * np.exp(xT.logScalar))**
            2) < epsilon
Ejemplo n.º 9
0
def test_mergeBuckets():
    x = np.random.randn(2, 3, 3)
    xt = ArrayTensor(x)
    n = Node(xt)

    buckets = list(n.buckets)

    b = n.mergeBuckets(n.buckets[1:])

    assert n.tensor.rank == 2
    assert n.tensor.shape == (2, 9)
    assert len(n.buckets) == 2
    assert n.buckets[0] == buckets[0]
    assert n.buckets[1] != buckets[1]
    assert n.buckets[1] == b
    assert np.sum((np.reshape(x, (-1, 9)) - n.tensor.array)**2) < epsilon

    x = np.random.randn(2, 3, 3)
    xt = TreeTensor(accuracy=epsilon)
    xt.addTensor(ArrayTensor(x))
    n = Node(xt)

    buckets = list(n.buckets)

    b = n.mergeBuckets(n.buckets[1:])

    assert n.tensor.rank == 2
    assert n.tensor.shape == (2, 9)
    assert len(n.buckets) == 2
    assert n.buckets[0] == buckets[0]
    assert n.buckets[1] != buckets[1]
    assert n.buckets[1] == b
    assert np.sum((np.reshape(x, (-1, 9)) - n.tensor.array)**2) < epsilon

    x = np.random.randn(2, 2, 2, 2, 2, 2)
    xt = TreeTensor(accuracy=epsilon)
    xt.addTensor(ArrayTensor(x))
    n = Node(xt)

    buckets = list(n.buckets)

    b = n.mergeBuckets(n.buckets[4:])

    assert n.tensor.rank == 5
    assert n.tensor.shape == (2, 2, 2, 2, 4)
    assert len(n.buckets) == 5
    assert n.buckets[0] == buckets[0]
    assert n.buckets[-1] != buckets[-1]
    assert n.buckets[-1] == b
    assert np.sum((np.reshape(x, (2, 2, 2, 2, 4)) -
                   n.tensor.array)**2) < epsilon
Ejemplo n.º 10
0
def test_init():
    for i in range(5):
        x = np.random.randn(3, 3, 4)
        xt = ArrayTensor(x, logScalar=0)

        assert xt.shape == (3, 3, 4)
        assert xt.rank == 3
        assert xt.size == 3 * 3 * 4

        assert np.sum((xt.array - x)**2) < epsilon
        assert xt.logScalar == np.log(np.max(np.abs(x)))
        assert np.sum((xt.scaledArray - x / np.exp(xt.logScalar))**2) < epsilon

        xt2 = ArrayTensor(x, logScalar=1.44)
        assert xt2.logScalar == xt.logScalar + 1.44
Ejemplo n.º 11
0
def test_links():
    x = np.random.randn(2, 3, 3)
    xt = ArrayTensor(x)
    n1 = Node(xt)

    x = np.random.randn(2, 3, 3)
    xt = ArrayTensor(x)
    n2 = Node(xt)

    l1 = Link(n1.buckets[0], n2.buckets[0])

    assert n1.linkedBuckets[0].otherBucket == n2.buckets[0]

    l2 = Link(n2.buckets[1], n1.buckets[1])

    assert n1.linkedBuckets[1].otherBucket == n2.buckets[1]

    assert l1.bucket1.node == n1 or l1.bucket2.node == n1
    assert l1.bucket1.node == n2 or l1.bucket2.node == n2

    assert l1 in n1.findLinks(n2)
    assert l2 in n1.findLinks(n2)
    assert l1 in n2.findLinks(n1)
    assert l2 in n2.findLinks(n1)

    assert n1.findLink(n2) == l1 or n1.findLink(n2) == l2

    assert n1.indexConnecting(n2) == 0 or n1.indexConnecting(n2) == 1
    assert n2.indexConnecting(n1) == 0 or n2.indexConnecting(n1) == 1

    assert 0 in n1.indicesConnecting(n2)[0]
    assert 1 in n1.indicesConnecting(n2)[0]
    assert 2 not in n1.indicesConnecting(n2)[0]

    assert 0 in n2.indicesConnecting(n1)[0]
    assert 1 in n2.indicesConnecting(n1)[0]
    assert 2 not in n2.indicesConnecting(n1)[0]

    assert 0 in n1.indicesConnecting(n2)[1]
    assert 1 in n1.indicesConnecting(n2)[1]
    assert 2 not in n1.indicesConnecting(n2)[1]

    assert 0 in n2.indicesConnecting(n1)[1]
    assert 1 in n2.indicesConnecting(n1)[1]
    assert 2 not in n2.indicesConnecting(n1)[1]

    assert len(n1.connectedNodes) == 1
    assert n2 in n1.connectedNodes
Ejemplo n.º 12
0
    def flatten(self, inds):
        '''
        This method merges the listed external indices using a tree tensor
        by attaching the identity tensor to all of them and to a new
        external bucket. It then returns the new tree tensor.
        '''

        buckets = [self.externalBuckets[i] for i in inds]
        shape = [self.shape[i] for i in inds]

        # Create identity array
        shape.append(np.product(shape))
        iden = np.identity(shape[-1])
        iden = np.reshape(iden, shape)

        # Create Tree Tensor holding the identity
        tens = ArrayTensor(iden)
        tn = TreeTensor(self.accuracy)
        tn.addTensor(tens)

        # Contract the identity
        ttens = self.contract(inds, tn, list(range(len(buckets))))

        shape2 = [self.shape[i] for i in range(self.rank) if i not in inds]
        shape2.append(shape[-1])
        for i in range(len(shape2)):
            assert ttens.shape[i] == shape2[i]

        return ttens
Ejemplo n.º 13
0
def test_trace():
    for i in range(5):
        x = np.random.randn(2, 2, 2, 2, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))
        assert np.sum((xt.trace([0], [1]).array - np.einsum('iijklm->jklm', x))
                      **2) < epsilon
        assert np.sum(
            (xt.trace([0, 2], [5, 4]).array - np.einsum('ijklki->jl', x))**
            2) < epsilon
Ejemplo n.º 14
0
def test_init():
    x = np.random.randn(2, 3, 3)
    xt = ArrayTensor(x)
    n = Node(xt)

    assert len(n.buckets) == xt.rank
    for i, b in enumerate(n.buckets):
        assert b.node == n
        assert b.index == i
        assert n.bucketIndex(b) == i
Ejemplo n.º 15
0
def test_optimize():
    for i in range(5):
        x = np.random.randn(2, 2, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))

        y = np.random.randn(2, 2, 2, 2)
        yt = TreeTensor(accuracy=epsilon)
        yt.addTensor(ArrayTensor(y))

        zt = xt.contract([0], yt, [0])

        arr1 = zt.array

        zt.optimize()

        arr2 = zt.array

        assert np.sum((arr1 - arr2)**2) < epsilon
Ejemplo n.º 16
0
def test_IndexFactor():
    for i in range(5):
        x = np.random.randn(2, 3, 4, 5)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))

        y = np.random.randn(2, 3, 4, 5)
        yt = TreeTensor(accuracy=epsilon)
        yt.addTensor(ArrayTensor(y))

        # Compute inner product
        zt = xt.contract([0], yt, [0])

        # Generate a random unitary matrix
        r = np.random.randn(2, 2)
        r += r.T
        u = expm(1j * r)

        # Apply to factors on both x and y
        factX, indX = xt.getIndexFactor(0)
        factY, indY = yt.getIndexFactor(0)

        factX = np.tensordot(factX, u, axes=([indX], [0]))
        factY = np.tensordot(factY, np.conjugate(u.T), axes=([indY], [0]))

        permX = list(range(len(factX.shape) - 1))
        permY = list(range(len(factY.shape) - 1))
        permX.insert(indX, len(factX.shape) - 1)
        permY.insert(indY, len(factY.shape) - 1)

        factX = np.transpose(factX, axes=permX)
        factY = np.transpose(factY, axes=permY)

        xt = xt.setIndexFactor(0, factX)
        yt = yt.setIndexFactor(0, factY)

        assert xt.shape == (2, 3, 4, 5)
        assert yt.shape == (2, 3, 4, 5)

        zt2 = xt.contract([0], yt, [0])

        assert np.sum((zt.array - zt2.array)**2) < epsilon
Ejemplo n.º 17
0
    def __init__(self, dimension, rank, accuracy=0.0):
        super().__init__(accuracy)

        numLayers = layer(dimension)

        if rank == 0:
            self.addTensor(ArrayTensor(np.array(1.)))
        if rank == 1:
            self.addTensor(ArrayTensor(np.ones(dimension)))
        elif rank == 2:
            self.addTensor(ArrayTensor(np.identity(dimension)))
        else:
            numTensors = rank - 2

            buckets = []

            # Create identity array
            iden = np.zeros((dimension, dimension, dimension))
            for i in range(dimension):
                iden[i, i, i] = 1.0

            for i in range(numTensors):
                n = super().addTensor(ArrayTensor(iden))
                buckets = buckets + n.buckets

            while len(self.network.externalBuckets) > rank:
                b = buckets.pop(0)
                i = 0
                while buckets[i].node is b.node or len(
                        buckets[i].node.connectedNodes) > 0:
                    i += 1
                Link(b, buckets[i])

                self.externalBuckets.remove(b)
                self.externalBuckets.remove(buckets[i])
                self.network.externalBuckets.remove(b)
                self.network.externalBuckets.remove(buckets[i])
                self.network.internalBuckets.add(b)
                self.network.internalBuckets.add(buckets[i])

                buckets.remove(buckets[i])
Ejemplo n.º 18
0
def test_init():
    net = Network()

    assert len(net.nodes) == 0
    assert len(net.buckets) == 0
    assert len(net.internalBuckets) == 0
    assert len(net.externalBuckets) == 0
    assert len(net.optimizedLinks) == 0

    x = np.random.randn(2, 3, 3)
    xt = ArrayTensor(x)
    n1 = Node(xt)

    net.addNode(n1)

    assert len(net.nodes) == 1
    assert len(net.buckets) == 3
    assert len(net.internalBuckets) == 0
    assert len(net.externalBuckets) == 3
    assert len(net.optimizedLinks) == 0

    x = np.random.randn(2, 3, 3)
    xt = ArrayTensor(x)
    n2 = Node(xt)
    Link(n1.buckets[0], n2.buckets[0])

    net.addNode(n2)

    assert len(net.nodes) == 2
    assert len(net.buckets) == 6
    assert len(net.internalBuckets) == 2
    assert len(net.externalBuckets) == 4
    assert len(net.optimizedLinks) == 0

    net.removeNode(n1)

    assert len(net.nodes) == 1
    assert len(net.buckets) == 3
    assert len(net.internalBuckets) == 0
    assert len(net.externalBuckets) == 3
    assert len(net.optimizedLinks) == 0
Ejemplo n.º 19
0
def test_init():
    for i in range(5):
        x = np.random.randn(3, 3, 4)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))

        assert xt.shape == (3, 3, 4)
        assert xt.rank == 3
        assert xt.size == 3 * 3 * 4

        assert np.sum((xt.array - x)**2) < epsilon

    for i in range(5):
        x = np.random.randn(2, 2, 2, 2, 2, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))

        assert xt.shape == (2, 2, 2, 2, 2, 2, 2)
        assert xt.rank == 7
        assert xt.size == 2**7

        assert np.sum((xt.array - x)**2) < epsilon
Ejemplo n.º 20
0
def test_IndexFactor():
    for i in range(5):
        x = np.random.randn(3, 4, 5, 6)
        xt = ArrayTensor(x)

        y = np.random.randn(3, 4, 5, 6)
        yt = ArrayTensor(y)

        # Compute inner product
        zt = xt.contract([0], yt, [0])

        # Generate a random unitary matrix
        r = np.random.randn(3, 3)
        r += r.T
        u = expm(1j * r)

        assert np.sum(
            (np.identity(3) - np.dot(u, np.conjugate(u.T)))**2) < epsilon

        # Apply to factors on both x and y
        factX, indX = xt.getIndexFactor(0)
        factY, indY = yt.getIndexFactor(0)

        factX = np.tensordot(factX, u, axes=([indX], [0]))
        factY = np.tensordot(factY, np.conjugate(u.T), axes=([indY], [0]))

        factX = np.transpose(factX, axes=[3, 0, 1, 2])
        factY = np.transpose(factY, axes=[3, 0, 1, 2])

        xt = xt.setIndexFactor(0, factX)
        yt = yt.setIndexFactor(0, factY)

        assert xt.shape == (3, 4, 5, 6)
        assert yt.shape == (3, 4, 5, 6)

        zt2 = xt.contract([0], yt, [0])

        assert np.sum((zt.array - zt2.array)**2) < epsilon
Ejemplo n.º 21
0
def test_mergeLinks_ArrayTensor():
    for i in range(5):
        net = Network()

        x = np.random.randn(2, 2, 3, 3)
        xt = ArrayTensor(x)
        n1 = Node(xt)
        xt = ArrayTensor(x)
        n2 = Node(xt)

        net.addNode(n1)
        Link(n1.buckets[0], n2.buckets[0])
        Link(n1.buckets[1], n2.buckets[1])
        net.addNode(n2)

        arr1, bdict1 = net.array

        net.mergeLinks(n1)

        arr2, bdict2 = net.array

        assert np.sum((arr1 - arr2)**2) < epsilon
        assert np.sum((arr1 - arr2)**2) < epsilon
Ejemplo n.º 22
0
def test_mergeNode_TreeTensor():
    for i in range(5):
        net = Network()

        x = np.random.randn(2, 2, 2, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(x))
        n1 = Node(xt)
        y = np.random.randn(2, 2, 2, 2, 2)
        xt = TreeTensor(accuracy=epsilon)
        xt.addTensor(ArrayTensor(y))
        n2 = Node(xt)

        net.addNode(n1)
        Link(n1.buckets[0], n2.buckets[0])
        net.addNode(n2)

        net.mergeNodes(n1, n2)

        assert len(net.nodes) == 1
        assert len(net.buckets) == 8
        assert len(net.internalBuckets) == 0
        assert len(net.externalBuckets) == 8
        assert len(net.optimizedLinks) == 0

        arr, bdict = net.array
        assert arr.shape == (2, 2, 2, 2, 2, 2, 2, 2)
        for b in net.buckets:
            assert b.id in bdict
        for b1 in net.buckets:
            for b2 in net.buckets:
                if b1.id < b2.id:
                    assert bdict[b1.id] < bdict[b2.id]

        assert np.sum(
            (arr - np.einsum('ijklm,iqwer->jklmqwer', x, y))**2) < epsilon
Ejemplo n.º 23
0
def test_trace():
    for i in range(5):
        x = np.random.randn(3, 3, 5)
        xt = ArrayTensor(x)
        assert np.sum(
            (xt.trace([0], [1]).array - np.einsum('iij->j', x))**2) < epsilon

    for i in range(5):
        x = np.random.randn(3, 3, 5, 4, 4)
        xt = ArrayTensor(x)
        assert np.sum(
            (xt.trace([0, 3], [1, 4]).array - np.einsum('iijkk->j', x))**
            2) < epsilon
Ejemplo n.º 24
0
def test_flatten():
    for i in range(5):
        x = np.random.randn(3, 3, 5)
        xt = ArrayTensor(x)
        assert np.sum(
            (xt.flatten([0, 1]).array - np.reshape(x, (-1, 5)).T)**2) < epsilon

    for i in range(5):
        x = np.random.randn(3, 3, 5)
        xt = ArrayTensor(x)
        assert np.sum(
            (xt.flatten([1, 0]).array - np.reshape(np.swapaxes(x, 0, 1),
                                                   (-1, 5)).T)**2) < epsilon
Ejemplo n.º 25
0
def test_contract():
    for i in range(5):
        x = np.random.randn(3, 4, 3)
        y = np.random.randn(3, 4, 3)

        xt = ArrayTensor(x)
        yt = ArrayTensor(y)

        zt = xt.contract(0, yt, 0)
        assert np.sum(
            (zt.array - np.einsum('ijk,ilm->jklm', x, y))**2) < epsilon

        zt = xt.contract(1, yt, 1)
        assert np.sum(
            (zt.array - np.einsum('jik,lim->jklm', x, y))**2) < epsilon

        zt = xt.contract(0, yt, 2)
        assert np.sum(
            (zt.array - np.einsum('ijk,lmi->jklm', x, y))**2) < epsilon
Ejemplo n.º 26
0
def PA2D(nX, nY, h, J, q, accuracy):
    network = Network()

    # Place to store the tensors
    lattice = [[] for i in range(nX)]
    bondL = [[] for i in range(nX)]

    # Each lattice site has seven indices of width five, and returns zero if
    # they are unequal and one otherwise.
    for i in range(nX):
        for j in range(nY):
            lattice[i].append(Node(IdentityTensor(2, 5, accuracy=accuracy)))

    arr = np.zeros((2, 2))

    # 2-point
    arr[0][0] = np.exp(-J)
    arr[1][1] = np.exp(-J)
    arr[0][1] = np.exp(J)
    arr[1][0] = np.exp(J)

    # 1-point
    arr[0] *= np.exp(h / 4)
    arr[1] *= np.exp(-h / 4)

    # Expand
    arr = np.einsum('ij,ik,il,ia->ijkla', arr, arr, arr, arr)

    # 3-point
    arr[1, 1, :, 1, :] = 0
    arr[1, 1, :, :, 1] = 0
    arr[1, :, 1, 1, :] = 0
    arr[1, :, 1, :, 1] = 0

    # 4-point
    arr[1, 1, 1, 1, :] = np.exp(q)
    arr[1, 1, 1, :, 1] = np.exp(q)
    arr[1, 1, :, 1, 1] = np.exp(q)
    arr[1, :, 1, 1, 1] = np.exp(q)

    # 5-point
    for j in range(2):
        for k in range(2):
            for l in range(2):
                for m in range(2):
                    if j + k + l + m >= 4:
                        arr[1, j, k, l, m] = 0

    # Make L-bonds
    for i in range(nX):
        for j in range(nY):
            bondL[i].append(Node(ArrayTensor(arr)))

    # Attach links
    for i in range(nX):
        for j in range(nY):
            Link(lattice[i][j].buckets[0], bondL[i][j].buckets[0])
            Link(lattice[i][j].buckets[1], bondL[(i + 1) % nX][j].buckets[1])
            Link(lattice[i][j].buckets[2], bondL[i - 1][j].buckets[2])
            Link(lattice[i][j].buckets[3], bondL[i][(j + 1) % nY].buckets[3])
            Link(lattice[i][j].buckets[4], bondL[i][j - 1].buckets[4])

    # Add to Network
    for i in range(nX):
        for j in range(nY):
            network.addNode(lattice[i][j])
            network.addNode(bondL[i][j])

    return network
Ejemplo n.º 27
0
 def setIndexFactor(self, ind, arr):
     tt = deepcopy(self)
     tt.externalBuckets[ind].node.tensor = ArrayTensor(
         arr, logScalar=tt.externalBuckets[ind].node.tensor.logScalar)
     return tt
Ejemplo n.º 28
0
def BayesTest2(observations, discreteG, discreteQ, discreteW, discreteH,
               accuracy):
    '''
    observations is a list of (k,M) pairs
    where k is the number of heads and M-k is the
    number of tails in a repeated Bernoulli coin toss.

    This model represents the likelihood

    L = M! p^k (1-p)^(M-k)/(k!(M-k)!)

    summed over all coins that were observed.

    Here we model

    p_i = min(g*h_i + q^w, 1)

    where each of g, q, w and h_i lie in [0,1]
    and have uniform priors. g, w and q are global parameters.

    discrete(G,W,Q) specify the g, w and q values to sample.
    discreteH is the same for h_i.
    '''

    network = Network()

    # Local tensors
    hs = []

    for i, obs in enumerate(observations):
        arr = np.zeros(
            (len(discreteG), len(discreteQ), len(discreteW), len(discreteH)))
        for j, gg in enumerate(discreteG):
            for k, qq in enumerate(discreteQ):
                for e, ww in enumerate(discreteW):
                    for l, h in enumerate(discreteH):
                        p = min(gg * h + qq**ww, 1)
                        arr[j, k, e,
                            l] = factorial(obs[1]) * p**obs[0] * (1 - p)**(
                                obs[1] - obs[0]) / (factorial(obs[0]) *
                                                    factorial(obs[1] - obs[0]))

        # Marginalizes over all of the individual distributions
        arr = np.sum(arr, axis=-1)
        h = Node(ArrayTensor(arr))
        hs.append(h)

    extG = [h.buckets[0] for h in hs]
    extW = [h.buckets[1] for h in hs]
    extQ = [h.buckets[2] for h in hs]

    nodes = []

    dimension = len(discreteG)
    while len(extG) > 1:
        iden = np.zeros((dimension, dimension, dimension))
        for i in range(dimension):
            iden[i, i, i] = 1.0
        n = Node(IdentityTensor(dimension, 3, accuracy=accuracy))
        nodes.append(n)
        Link(n.buckets[0], extG[0])
        Link(n.buckets[1], extG[1])
        extG.append(n.buckets[2])
        extG = extG[2:]

    dimension = len(discreteW)
    while len(extW) > 1:
        iden = np.zeros((dimension, dimension, dimension))
        for i in range(dimension):
            iden[i, i, i] = 1.0
        n = Node(IdentityTensor(dimension, 3, accuracy=accuracy))
        nodes.append(n)
        Link(n.buckets[0], extW[0])
        Link(n.buckets[1], extW[1])
        extW.append(n.buckets[2])
        extW = extW[2:]

    dimension = len(discreteQ)
    while len(extQ) > 1:
        iden = np.zeros((dimension, dimension, dimension))
        for i in range(dimension):
            iden[i, i, i] = 1.0
        n = Node(IdentityTensor(dimension, 3, accuracy=accuracy))
        nodes.append(n)
        Link(n.buckets[0], extQ[0])
        Link(n.buckets[1], extQ[1])
        extQ.append(n.buckets[2])
        extQ = extQ[2:]

    for h in hs:
        network.addNode(h)

    for n in nodes:
        network.addNode(n)

    return network
Ejemplo n.º 29
0
def BayesTest1(observations, discreteG, discreteQ, discreteW, discreteH,
               accuracy):
    '''
    observations is a list of (k,M) pairs
    where k is the number of heads and M-k is the
    number of tails in a repeated Bernoulli coin toss.

    This model represents the likelihood

    L = M! p^k (1-p)^(M-k)/(k!(M-k)!)

    summed over all coins that were observed.

    Here we model

    p_i = min(g*h_i + q^w, 1)

    where each of g, q, w and h_i lie in [0,1]
    and have uniform priors. g, w and q are global parameters.

    discrete(G,W,Q) specify the g, w and q values to sample.
    discreteH is the same for h_i.
    '''

    network = Network()

    # Global tensors
    n = len(observations)
    g = Node(IdentityTensor(len(discreteG), n + 1, accuracy=accuracy))
    q = Node(IdentityTensor(len(discreteQ), n + 1, accuracy=accuracy))
    w = Node(IdentityTensor(len(discreteW), n + 1, accuracy=accuracy))

    # Local tensors
    hs = []

    for i, obs in enumerate(observations):
        arr = np.zeros(
            (len(discreteG), len(discreteQ), len(discreteW), len(discreteH)))
        for j, gg in enumerate(discreteG):
            for k, qq in enumerate(discreteQ):
                for e, ww in enumerate(discreteW):
                    for l, h in enumerate(discreteH):
                        p = min(gg * h + qq**ww, 1)
                        arr[j, k, e,
                            l] = factorial(obs[1]) * p**obs[0] * (1 - p)**(
                                obs[1] - obs[0]) / (factorial(obs[0]) *
                                                    factorial(obs[1] - obs[0]))

        # Marginalizes over all of the individual distributions
        arr = np.sum(arr, axis=-1)
        h = Node(ArrayTensor(arr))
        hs.append(h)
        Link(h.buckets[0], g.buckets[i])
        Link(h.buckets[1], q.buckets[i])
        Link(h.buckets[2], w.buckets[i])

    # Assemble the network
    network.addNode(g)
    network.addNode(q)
    network.addNode(w)
    for h in hs:
        network.addNode(h)

    return network
Ejemplo n.º 30
0
def PA3D(nX, nY, nZ, h, J, q, accuracy):
    network = Network()

    # Place to store the tensors
    lattice = [[[] for j in range(nY)] for i in range(nX)]
    bondL = [[[] for j in range(nY)] for i in range(nX)]

    # Each lattice site has seven indices of width five, and returns zero if
    # they are unequal and one otherwise.
    for i in range(nX):
        for j in range(nY):
            for k in range(nZ):
                lattice[i][j].append(
                    Node(IdentityTensor(2, 7, accuracy=accuracy)))

    arr = np.zeros((2, 2))

    # 2-point
    arr[0][0] = np.exp(-J)
    arr[1][1] = np.exp(-J)
    arr[0][1] = np.exp(J)
    arr[1][0] = np.exp(J)

    # 1-point
    arr[0] *= np.exp(h / 6)
    arr[1] *= np.exp(-h / 6)

    # Expand
    arr = np.einsum('ij,ik,il,ia,ib,ic->ijklabc', arr, arr, arr, arr, arr, arr)

    # 3-point
    arr[1, 1, :, 1, :, :, :] = 0
    arr[1, 1, :, :, 1, :, :] = 0
    arr[1, 1, :, :, :, 1, :] = 0
    arr[1, 1, :, :, :, :, 1] = 0
    arr[1, :, 1, 1, :, :, :] = 0
    arr[1, :, 1, :, 1, :, :] = 0
    arr[1, :, 1, :, :, 1, :] = 0
    arr[1, :, 1, :, :, :, 1] = 0

    arr[1, 1, :, 1, :, :, :] = 0
    arr[1, :, 1, 1, :, :, :] = 0
    arr[1, :, :, 1, :, 1, :] = 0
    arr[1, :, :, 1, :, :, 1] = 0
    arr[1, 1, :, :, 1, :, :] = 0
    arr[1, :, 1, :, 1, :, :] = 0
    arr[1, :, :, :, 1, 1, :] = 0
    arr[1, :, :, :, 1, :, 1] = 0

    arr[1, 1, :, :, :, 1, :] = 0
    arr[1, :, 1, :, :, 1, :] = 0
    arr[1, :, :, 1, :, 1, :] = 0
    arr[1, :, :, :, 1, 1, :] = 0
    arr[1, 1, :, :, :, :, 1] = 0
    arr[1, :, 1, :, :, :, 1] = 0
    arr[1, :, :, 1, :, :, 1] = 0
    arr[1, :, :, :, 1, :, 1] = 0

    # 4-point
    arr[1, 1, 1, 1, :, :, :] = np.exp(q)
    arr[1, 1, 1, :, 1, :, :] = np.exp(q)
    arr[1, 1, 1, :, :, 1, :] = np.exp(q)
    arr[1, 1, 1, :, :, :, 1] = np.exp(q)
    arr[1, 1, :, 1, 1, :, :] = np.exp(q)
    arr[1, :, 1, 1, 1, :, :] = np.exp(q)
    arr[1, :, :, 1, 1, 1, :] = np.exp(q)
    arr[1, :, :, 1, 1, :, 1] = np.exp(q)
    arr[1, 1, :, :, :, 1, 1] = np.exp(q)
    arr[1, :, 1, :, :, 1, 1] = np.exp(q)
    arr[1, :, :, 1, :, 1, 1] = np.exp(q)
    arr[1, :, :, :, 1, 1, 1] = np.exp(q)

    # 5-point
    for j in range(2):
        for k in range(2):
            for l in range(2):
                for m in range(2):
                    for n in range(2):
                        for p in range(2):
                            if j + k + l + m + n + p >= 4:
                                arr[1, j, k, l, m, n, p] = 0

    t = ArrayTensor(arr)
    tt = TreeTensor(accuracy)
    tt.addTensor(t)

    # Make L-bonds
    for i in range(nX):
        for j in range(nY):
            for k in range(nZ):
                bondL[i][j].append(Node(deepcopy(tt)))

    # Attach links
    for i in range(nX):
        for j in range(nY):
            for k in range(nZ):
                Link(lattice[i][j][k].buckets[0], bondL[i][j][k].buckets[0])
                Link(lattice[i][j][k].buckets[1],
                     bondL[(i + 1) % nX][j][k].buckets[1])
                Link(lattice[i][j][k].buckets[2],
                     bondL[i - 1][j][k].buckets[2])
                Link(lattice[i][j][k].buckets[3],
                     bondL[i][(j + 1) % nY][k].buckets[3])
                Link(lattice[i][j][k].buckets[4],
                     bondL[i][j - 1][k].buckets[4])
                Link(lattice[i][j][k].buckets[5],
                     bondL[i][j][(k + 1) % nZ].buckets[5])
                Link(lattice[i][j][k].buckets[6],
                     bondL[i][j][k - 1].buckets[6])

    # Add to Network
    for i in range(nX):
        for j in range(nY):
            for k in range(nZ):
                network.addNode(lattice[i][j][k])
                network.addNode(bondL[i][j][k])

    return network