Ejemplo n.º 1
0
 def testIsUnitary(self):
     a = ComplexM(3, 3,
             [
               [(26, -52)  , (60, 24)  , (26, 0) ],
               [(9, 7)    , (1, 29)   , (14, 0)  ],
               [(48, -21) , (15, 22)  , (20, -22)],
             ]
         )
     b = ComplexM(2, 2, [[1, 0], [0, (0, 1)]])
     c = ComplexM(3, 3,
             [
               [(0.5, 0.5) , (0, 1 / sqrt(3)) , (3 / (2 * sqrt(15)), 1 / (2 * sqrt(15))) ],
               [(-0.5, 0)  , (1 / sqrt(3), 0) , (4 / (2 * sqrt(15)), 3 / (2 * sqrt(15))) ],
               [(0.5, 0)   , (0, -1 / sqrt(3)), (0, 5 / (2 * sqrt(15)))                  ],
             ]
         )
     d = ComplexM(3, 3,
             [
               [(2 ** -0.5, 0)  , (2 ** -0.5, 0) , (0, 0) ],
               [(0, -2 ** -0.5) , (0, 2 ** -0.5) , (0, 0) ],
               [(0, 0)          , (0, 0)         , (0, 1) ],
             ]
         )
     self.assertFalse(a.is_unitary())
     self.assertTrue(b.is_unitary())
Ejemplo n.º 2
0
    def testTrace(self):
        a = ComplexM(3, 3,
                [
                  [(3, 2)  , (0, 0)   , (5, -6) ],
                  [(1, 0)  , (-7, 2)   , (0, 1)  ],
                  [(1, 0)  , (4, 2)   , (1, -3.5)  ],
                ]
            )
        self.assertEqual(a.trace(), Complex(-3, 0.5))

        b = ComplexM(2, 3, a[0:2])
        with self.assertRaises(ValueError):
            b.trace()
Ejemplo n.º 3
0
    def testConjugate(self):
        a = ComplexM(3, 2, [ [(7, -8), (0,4)], [2, (9.4,3)],  [(0,1), (-3,-2)] ])
        b = ComplexM(3, 2, [ [(7, 8), (0,-4)], [2, (9.4,-3)],  [(0,-1), (-3,2)] ])

        self.assertEqual(b, a.conjugate())
        self.assertEqual(a, b.conjugate())
        self.assertEqual(a, a.conjugate().conjugate())
        self.assertEqual(b, b.conjugate().conjugate())
Ejemplo n.º 4
0
    def testTranspose(self):
        a = ComplexM(3, 2, [ [(7, -8), (0,4)], [2, (9.4,3)],  [(0,1), (-3,-2)] ])
        b = ComplexM(2, 3, [ [(7, -8), 2, (0,1)], [(0,4), (9.4,3), (-3,-2)]])

        self.assertEqual(b, a.transpose())
        self.assertEqual(a, b.transpose())
        self.assertEqual(a, a.transpose().transpose())
        self.assertEqual(b, b.transpose().transpose())
Ejemplo n.º 5
0
    def testMul_3x3(self):
        a   = ComplexM(3,3,
                [
                  [(3,2)  , (0,0)   , (5,-6) ],
                  [(1,0)  , (4,2)   , (0,1)  ],
                  [(4,-1) , (0,0)   , (4,0)  ],
                ]
            )
        b   = ComplexM(3,3,
                [
                  [(5,0)  , (2,-1)  , (6,-4) ],
                  [(0,0)  , (4,5)   , (2,0)  ],
                  [(7,-4) , (2,7)   , (0,0)  ],
                ]
            )
        res = ComplexM(3,3,
                [
                  [(26,-52)  , (60,24)  , (26,0) ],
                  [(9,7)    , (1,29)   , (14,0)  ],
                  [(48,-21) , (15,22)  , (20,-22)  ],
                ]
            )

        self.assertEqual(a*b, res)
        self.assertNotEqual(b*a, a*b)

        ident = a.get_identity()
        self.assertEqual(a, a*ident)
        self.assertEqual(b, b*ident)

        x = Complex(2,-1)
        self.assertEqual(x * (a*b), (x*a) * b)
        self.assertEqual(x * (a*b), a * (x*b))

        c = res
        self.assertEqual((a*b)*c, a*(b*c))
        self.assertEqual(a*(b+c), (a*b)+(a*c))
        self.assertEqual((a*b).transpose(), b.transpose() * a.transpose())
        self.assertEqual((a*b).adjoint(), b.adjoint() * a.adjoint())
        self.assertEqual((a*b).conjugate(), a.conjugate() * b.conjugate())
Ejemplo n.º 6
0
    def testIsHermitian(self):
        a = ComplexM(3,3,
                [
                  [(5, 0)  , (4, 5)  , (6, -16) ],
                  [(4, -5) , (13, 0) , (7, 0)   ],
                  [(6, 16) , (7, 0)  , (-2.1, 0)],
                ]
            )
        b = ComplexM(3, 3,
                [
                  [(5, 0)  , (4, 5)  , (6, -16) ],
                  [(4, 5)  , (13, 0) , (14, 0)  ],
                  [(6, 16) , (7, 0)  , (-2.1, 0)],
                ]
            )

        self.assertTrue(a.is_hermitian())
        self.assertFalse(b.is_hermitian())
        c = ComplexM(2, 3, a[0:2])
        self.assertFalse(c.is_hermitian())
Ejemplo n.º 7
0
 def testNormalize(self):
     v = ComplexM(2, 1, [ [(1, 0)], [(0, 1)] ])
     w = ComplexM(2, 1, [ [(4, 0)], [(0, 4)] ])
     self.assertEqual(v.normalize(), w.normalize())
Ejemplo n.º 8
0
    def testTensor(self):
        v1 = ComplexM(3, 1, [[3], [4], [7]])
        v2 = ComplexM(2, 1, [[-1], [2]])
        res = ComplexM(6, 1, [[-3], [6], [-4], [8], [-7], [14]])

        self.assertEqual(res, v1.tensor(v2))
        self.assertNotEqual(res, v2.tensor(v1))

        a = ComplexM(3, 3,
            [
              [(3, 2) , (5, -1) , (0, 2) ],
              [(0, 0) , (12, 0) , (6, -3)],
              [(2, 0) , (4, 4)  , (9, 3) ],
            ]
        )
        b = ComplexM(3, 3,
            [
              [(1, 0)  , (3, 4) , (5, -7) ],
              [(10, 2) , (6, 0) , (2, 5)  ],
              [(0, 0)  , (1, 0) , (2, 9)  ]
            ]
        )
        c = ComplexM(9, 9,
            [
              [(3, 2)   , (1, 18)  , (29, -11) , (5, -1)   , (19, 17) , (18, -40) , (0, 2)    , (-8, 6)   , (14, 10)  ],
              [(26, 26) , (18, 12) , (-4, 19)  , (52, 0)   , (30, -6) , (15, 23)  , (-4, 20)  , (0, 12)   , (-10, 4)  ],
              [(0, 0)   , (3, 2)   , (-12, 31) , (0, 0)    , (5, -1)  , (19, 43)  , (0, 0)    , (0, 2)    , (-18, 4)  ],
              [(0, 0)   , (0, 0)   , (0, 0)    , (12, 0)   , (36, 48) , (60, -84) , (6, -3)   , (30, 15)  , (9, -57)  ],
              [(0, 0)   , (0, 0)   , (0, 0)    , (120, 24) , (72, 0)  , (24, 60)  , (66, -18) , (36, -18) , (27, 24)  ],
              [(0, 0)   , (0, 0)   , (0, 0)    , (0, 0)    , (12, 0)  , (24, 108) , (0, 0)    , (6, -3)   , (39, 48)  ],
              [(2, 0)   , (6, 8)   , (10, -14) , (4, 4)    , (-4, 28) , (48, -8)  , (9, 3)    , (15, 45)  , (66, -48) ],
              [(20, 4)  , (12, 0)  , (4, 10)   , (32, 48)  , (24, 24) , (-12, 28) , (84, 48)  , (54, 18)  , (3, 51)   ],
              [(0, 0)   , (2, 0)   , (4, 18)   , (0, 0)    , (4, 4)   , (-28, 44) , (0, 0)    , (9, 3)    , (-9, 87)  ],
            ]
        )
        self.assertEqual(c, a.tensor(b))
        self.assertNotEqual(c, b.tensor(a))

        a = ComplexM(2, 3,
                [
                  [(3, 2)  , (0, 0)   , (5, -6) ],
                  [(1, 0)  , (4, 2)   , (0, 1)  ],
                ]
            )
        b = ComplexM(3, 2,
                [
                  [(5, 0)  , (2, -1)],
                  [(0, 0)  , (4, 5)],
                  [(7, -4) , (2, 7)],
                ]
            )
        c = ComplexM(5, 1, [[(-2, -4)], [(3, -0.5)], [(2, -1)], [(4, -3)], [(6.5, 3)]])

        self.assertEqual(a.tensor(b).tensor(c), a.tensor((b.tensor(c))))
Ejemplo n.º 9
0
    def testDistance(self):
        v1 = ComplexM(3,1, [ [3], [1], [2] ])
        v2 = ComplexM(3,1, [ [2], [2], [-1] ])
        v3 = ComplexM(3,1, [ [4], [2], [-3] ])

        self.assertEqual(v1.distance(v2), sqrt(11))
        self.assertEqual(v1.distance(v1), 0)
        self.assertEqual(v2.distance(v2), 0)
        self.assertLess(v1.distance(v3), v1.distance(v2) + v1.distance(v3))
        self.assertEqual(v1.distance(v2), v2.distance(v1))

        with self.assertRaises(TypeError):
            v1.distance('foo')

        v4 = ComplexM(4,1, [ [4], [0], [-3], [-1] ])
        with self.assertRaises(ValueError):
            v1.distance(v4)

        a = ComplexM(3, 2, [ [(7, 8), (0,-4)], [2, (9.4,-3)],  [(0,-1), (-3,2)] ])
        b = ComplexM(3, 2, [ [(7, 8), (0,-4)], [2, (9.4,-3)],  [(0,-1), (-3,2)] ])
        with self.assertRaises(ValueError):
            a.distance(b)
Ejemplo n.º 10
0
    def testNorm(self):
        v1 = ComplexM(4, 1, [ [(4, 3)], [(6,-4)], [(12,-7)], [(0,13)]])
        v2 = ComplexM(4, 1, [ [(2, 3)], [(2,-4)], [(1,-7)], [(3,13)]])

        self.assertEqual(v1.norm(), sqrt(439))
        self.assertGreater(v2.norm(), 0)
        self.assertLess((v1 + v2).norm(), v1.norm() + v2.norm())

        a = ComplexM(2, 2, [ [(3, 0), (5, 0)], [(2, 0), (3, 0)] ])

        self.assertEqual(a.norm(), sqrt(47))

        b = ComplexM(3, 2, [ [(3, 0), (5, 0)], [(2, 0), (3, 0)], [(4,0), (5,0)] ])
        with self.assertRaises(ValueError):
            b.norm()
Ejemplo n.º 11
0
 def testIsSquared(self):
     a = ComplexM(5,5, [[i*j for j in range(5)] for i in range(5)])
     self.assertTrue(a.is_squared())
     b = ComplexM(4,5, a[0:4])
     self.assertFalse(b.is_squared())
Ejemplo n.º 12
0
    def testVectorinner_product(self):
        v1 = ComplexM(3, 1, [[(2, -5)], [(1, 0)], [(3, 1)]])
        v2 = ComplexM(3, 1, [[(2, 1)], [(2, 3)], [(4, 14)]])
        v3 = ComplexM(3, 1, [[(0, -2)], [(-1, 0)], [(2, -3)]])
        c  = Complex(3, 3)
        vz = ComplexM(3, 1, [[0], [0], [0]])

        self.assertIsInstance(v1.inner_product(v2), Complex)
        # v ≠ 0 → ⟨v, v⟩ > 0
        self.assertGreater(v1.inner_product(v1).real_value, 0)
        self.assertEqual(v1.inner_product(v1).imaginary_value, 0)
        # ⟨v1, v2⟩ = 0 ↔ v = 0
        self.assertEqual(vz.inner_product(vz), Complex(0))
        # ⟨v1+v2, v3⟩ = ⟨v1, v3⟩ + ⟨v2, v3⟩
        self.assertEqual((v1 + v2).inner_product(v3), v1.inner_product(v3) + v2.inner_product(v3))
        # ⟨v1, v2+v3⟩ = ⟨v1, v2⟩ + ⟨v1, v3⟩
        self.assertEqual(v1.inner_product(v2 + v3), v1.inner_product(v2) + v1.inner_product(v3))
        # ⟨c v1, v2⟩ = conj(c) ⟨v1, v2⟩
        self.assertEqual((v1 * c).inner_product(v2), c.conjugate() * v1.inner_product(v2))
        # ⟨v1, c v2⟩ = c ⟨v2, v1⟩
        self.assertEqual(v1.inner_product(c * v2), c * v1.inner_product(v2))
        # ⟨v1, v2⟩ = conj(⟨v2, v1⟩)
        self.assertEqual(v1.inner_product(v2), v2.inner_product(v1).conjugate())
Ejemplo n.º 13
0
    def testMul_2x3(self):
        a   = ComplexM(2,3,
                [
                  [(3,2)  , (0,0)   , (5,-6) ],
                  [(1,0)  , (4,2)   , (0,1)  ],
                ]
            )
        b   = ComplexM(3,2,
                [
                  [(5,0)  , (2,-1)],
                  [(0,0)  , (4,5)],
                  [(7,-4) , (2,7)],
                ]
            )
        res = ComplexM(2,2,
                [
                  [(26,-52)  , (60,24)],
                  [(9,7)    , (1,29)],
                ]
            )

        self.assertEqual(a*b, res)
        self.assertNotEqual(b*a, a*b)

        self.assertIsNone(a.get_identity())
        self.assertIsNone(b.get_identity())
        self.assertIsNotNone(res.get_identity())

        x = Complex(4,-3.5)
        self.assertEqual(x * (a*b), (x*a) * b)
        self.assertEqual(x * (a*b), a * (x*b))

        self.assertEqual((a*b).transpose(), b.transpose() * a.transpose())
        self.assertEqual((a*b).adjoint(), b.adjoint() * a.adjoint())
        self.assertEqual((a*b).conjugate(), a.conjugate() * b.conjugate())
Ejemplo n.º 14
0
    def testIsVector(self):
        v = ComplexM(3, 1, [[(-1, -2)], [(1, 2)], [(-4, 2.5)]])
        a = ComplexM(1, 3, [[(-1, -2), (1, 2), (-4, 2.5)]])

        self.assertTrue(v.is_vector())
        self.assertFalse(a.is_vector())