Esempio n. 1
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
Esempio n. 2
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