예제 #1
0
 def test_norm(self):
     a = Tensor(numpy.array([1, 2, 3]))
     e = Tensor(numpy.array([1j, 2j, 3j]))
     self.assertAlmostEqual(a.norm_squared, 14)
     self.assertAlmostEqual(e.norm_squared, 14)
     self.assertAlmostEqual((a + 2 * e).norm_squared, 70)
     c = normalize(Tensor(numpy.random.rand(*self.a)))
     self.assertAlmostEqual(c.norm_squared, 1)
예제 #2
0
 def test_copy(self):
     a = Tensor(numpy.array([1, 2]))
     b = a.copy()
     c = deepcopy(a)
     self.assertTrue(b._data is a._data)
     self.assertFalse(c._data is a._data)
     testing.assert_allclose(a.contract(), b.contract())
     testing.assert_allclose(a.contract(), c.contract())
예제 #3
0
 def test_mul_add_tran_conj(self):
     res_c = numpy.zeros(self.a[:2], dtype=complex)
     res = Tensor(copy(res_c))
     for _ in range(100):
         c1 = numpy.random.rand() + 1j * numpy.random.rand()
         a = numpy.random.rand(*(self.a[:2])) + 1j * \
             numpy.random.rand(*(self.a[:2]))
         c2 = numpy.random.rand() + 1j * numpy.random.rand()
         axes = numpy.random.permutation(2)
         res_c += c1 * numpy.transpose(numpy.conj(c2 * a), axes)
         res += c1 * (~(Tensor(a) * c2) % axes)
         testing.assert_allclose(res.contract(), res_c)
예제 #4
0
 def test_conjugation(self):
     a_R = numpy.random.rand(*(self.a))
     a_I = numpy.random.rand(*(self.a))
     a = a_R + a_I * 1j
     b = Tensor(a)
     testing.assert_allclose((~b).contract(), numpy.conj(a))
     testing.assert_allclose((~(~b)).contract(), a)
예제 #5
0
    def setUp(self):
        self.a = TensorNetwork()
        for i in range(5):
            self.a.open_edge(i)
        for i in range(5):
            for j in range(5, 10):
                self.a.add_node((i, j), [i, j], numpy.random.rand(2, 2))

        self.b = TensorNetwork()
        for i in range(5):
            self.b.open_edge(i)
        for i in range(5):
            for j in range(5, 10):
                self.b.add_node((i, j), [i, j], 1j * numpy.random.rand(2, 3))

        self.c = Tensor(numpy.random.rand(2, 2, 2, 2, 2))
예제 #6
0
 def test_multiplication_addition(self):
     a = numpy.random.rand(10, *(self.a))
     c = numpy.random.rand(10)
     b = [c[i] * Tensor(a[i]) for i in range(10)]
     res = TensorSum()
     res_c = numpy.zeros(self.a)
     for i in range(2):
         res.add_term(i, b[i])
         res_c += c[i] * a[i]
     testing.assert_allclose(res.contract(), res_c)
예제 #7
0
 def test_copy(self):
     c = TensorSum()
     a = Tensor(numpy.random.rand(2, 2, 2, 2, 2))
     for i in range(2):
         c.add_term(i, (a % numpy.random.permutation(5)).expand())
     d = c.copy()
     testing.assert_allclose(c.contract(), d.contract())
     e = deepcopy(c)
     testing.assert_allclose(c.contract(), e.contract())
     d.update_term(0, numpy.random.rand(2, 2, 2, 2, 2))
     self.assertFalse(numpy.allclose(c.contract(), d.contract()))
예제 #8
0
 def test_transpose(self):
     a = numpy.random.rand(*(self.a))
     axes = numpy.random.permutation(10)
     b = Tensor(a) % axes
     testing.assert_allclose(b.contract(), numpy.transpose(a, axes))
예제 #9
0
 def test_multiplication(self):
     a = numpy.random.rand(*(self.a))
     b = Tensor(a)
     c = numpy.random.rand()
     testing.assert_allclose(c * a, (c * b).contract())
예제 #10
0
 def test_data(self):
     a = numpy.random.rand(*(self.a))
     b = Tensor(a)
     testing.assert_allclose(b.contract(), a)
     self.assertEqual(self.a, b.shape)
예제 #11
0
 def test_equal(self):
     a = Tensor(numpy.array([2, 4]))
     b = 2 * Tensor(numpy.array([1, 2]))
     c = Tensor(numpy.array([1, 1])) + Tensor(numpy.array([1, 3]))
     testing.assert_allclose(a.contract(), c.contract())
     testing.assert_allclose(a.contract(), b.contract())
예제 #12
0
 def test_incompatible_dimensions(self):
     a = Tensor(numpy.array([1, 2]))
     b = Tensor(numpy.array([1, 2, 3]))
     with self.assertRaises(ValueError):
         a + b
예제 #13
0
 def test_identifier(self):
     cnt = TensorValued.id_count
     for _ in range(100):
         a = Tensor()
     self.assertEqual(a.identifier, cnt + 100)
예제 #14
0
class TensorSumTestCase(unittest.TestCase):

    def setUp(self):
        self.a = TensorNetwork()
        for i in range(5):
            self.a.open_edge(i)
        for i in range(5):
            for j in range(5, 10):
                self.a.add_node((i, j), [i, j], numpy.random.rand(2, 2))

        self.b = TensorNetwork()
        for i in range(5):
            self.b.open_edge(i)
        for i in range(5):
            for j in range(5, 10):
                self.b.add_node((i, j), [i, j], 1j * numpy.random.rand(2, 3))

        self.c = Tensor(numpy.random.rand(2, 2, 2, 2, 2))

    def test_addition(self):
        c = self.a + ~(self.b) + 8 * self.c
        self.assertEqual(type(c), TensorSum)
        self.assertTrue(numpy.allclose(c.contract(),
                                       self.a.contract()
                                       + numpy.conj(self.b.contract())
                                       + 8 * self.c.contract()))

    def test_associativity(self):
        c = (self.a + self.b) + self.c
        d = self.a + (self.b + self.c)
        e = (self.a + (self.c - self.b)) + (self.a + (2 * self.b - self.a))
        self.assertTrue(numpy.allclose(c.contract(), d.contract()))
        self.assertTrue(numpy.allclose(c.contract(), e.contract()))

    def test_transpose(self):
        axesA = (2, 4, 0, 1, 3)
        axesB = (3, 4, 1, 0, 2)
        axesC = (3, 0, 1, 2, 4)
        axes_ = (1, 3, 0, 4, 2)
        c = (self.a % axesA
             + self.b % axesB
             + self.c % axesC) % axes_
        data = numpy.transpose(numpy.transpose(self.a.contract(), axesA)
                               + numpy.transpose(self.b.contract(), axesB)
                               + numpy.transpose(self.c.contract(), axesC), axes_)
        self.assertTrue(numpy.allclose(c.contract(), data))

    def test_add_and_remove_term(self):
        c = TensorSum()
        for i in range(100):
            c.add_term(i, self.a % numpy.random.permutation(5))
        choice = numpy.random.choice(100, 30, replace=False)
        for j in choice:
            c.remove_term(j)
        with self.assertRaises(KeyError):
            c.remove_term(choice[0])
        with self.assertRaises(KeyError):
            c.add_term(0, None)
            c.add_term(0, None)

    def test_shape_cache(self):
        c = TensorSum()
        misshaped = numpy.random.rand(2, 2, 3, 2, 2)
        c.add_term(0, self.a)
        c.add_term(1, self.a)
        c.remove_term(0)
        with self.assertRaises(ValueError):
            c.add_term(2, misshaped)
        c.add_term(2, self.a)
        c.remove_term(1)
        c.remove_term(2)
        # Now that C is empty we can add any shape to it
        c.add_term(0, misshaped)
        c.add_term(1, misshaped)
        with self.assertRaises(ValueError):
            c.add_term(2, self.a)
        # The function update_term does not fail early, so the user can update all tensors one-by-one
        c.update_term(0, self.a)
        c.update_term(1, self.a)
        c.add_term(2, self.a)

    def test_copy(self):
        c = TensorSum()
        a = Tensor(numpy.random.rand(2, 2, 2, 2, 2))
        for i in range(2):
            c.add_term(i, (a % numpy.random.permutation(5)).expand())
        d = c.copy()
        testing.assert_allclose(c.contract(), d.contract())
        e = deepcopy(c)
        testing.assert_allclose(c.contract(), e.contract())
        d.update_term(0, numpy.random.rand(2, 2, 2, 2, 2))
        self.assertFalse(numpy.allclose(c.contract(), d.contract()))