예제 #1
0
 def test_xgate(self):
     """XGATE: Flip along √i axis. 1 => 0, 0 => 1"""
     z = Construct((1, 0))
     o = Construct((0, 1))
     zz = z.tensor(z)
     zo = z.tensor(o)
     oz = o.tensor(z)
     oo = o.tensor(o)
     rz = self.hadamard(z)
     ro = self.hadamard(o)
     cnot_tests = [
         ['Verify XGATE +|0>| = +|1>',
          self.xgate(z), o],
         ['Verify XGATE +|1>| = +|0>',
          self.xgate(o), z],
         ['Verify XGATE -|0>| = -|1>',
          self.xgate(-z), -o],
         ['Verify XGATE -|1>| = -|0>',
          self.xgate(-o), -z],
         ['Verify XGATE |00>| = |10>',
          self.xgate(zz), oz],
         ['Verify XGATE |01>| =-|11>',
          self.xgate(zo), -oo],
         ['Verify XGATE |10>| = |10>',
          self.xgate(oz), zz],
         ['Verify XGATE |11>| = |01>',
          self.xgate(oo), zo],
     ]
     for test in cnot_tests:
         title, calc, expect = test
         self.assertEqual(calc, expect)
예제 #2
0
    def test_16_steps(self):
        """16 Step Circle Walk test"""
        z = Construct((1, 0))
        steps = [
            (0, 1),
            (sqrt(1 / 2), -sqrt(1 / 2)),
            (-sqrt(1 / 2), sqrt(1 / 2)),
            (0, -1),
            (-1, 0),
            (-sqrt(1 / 2), -sqrt(1 / 2)),
            (-sqrt(1 / 2), -sqrt(1 / 2)),
            (-1, 0),
            (0, -1),
            (-sqrt(1 / 2), sqrt(1 / 2)),
            (sqrt(1 / 2), -sqrt(1 / 2)),
            (0, 1),
            (1, 0),
            (sqrt(1 / 2), sqrt(1 / 2)),
            (sqrt(1 / 2), sqrt(1 / 2)),
            (1, 0),
        ]
        starting = Construct((z.flat()))
        for i in range(16):
            if i % 2:
                gate = 'Hadamard'
                z2 = self.hadamard(z)
            else:
                gate = '   XGate'
                z2 = self.xgate(z)

            z = z2
            self.assertEqual(z, Construct(steps[i]))
        self.assertEqual(z, starting)
예제 #3
0
 def test_hadamard(self):
     """Hadamard gate test"""
     z = Construct((1, 0))
     o = Construct((0, 1))
     zz = z.tensor(z)
     zo = z.tensor(o)
     oz = o.tensor(z)
     oo = o.tensor(o)
     rz = self.hadamard(z)
     ro = self.hadamard(o)
     hadamard_tests = [
         ['Verify HADAMARD +| 0>| = + | +>',
          self.hadamard(z), rz],
         ['Verify HADAMARD +| 1>| = + | ->',
          self.hadamard(o), ro],
         ['Verify HADAMARD -| 0>| = - | +>',
          self.hadamard(-z), -rz],
         ['Verify HADAMARD -| 1>| = - | ->',
          self.hadamard(-o), -ro],
         [
             'Verify HADAMARD +|00>| = + |1+>',
             self.hadamard(zz),
             rz.tensor(z)
         ],
         [
             'Verify HADAMARD +|01>| = - |11>',
             self.hadamard(zo), -rz.tensor(o)
         ],
         [
             'Verify HADAMARD +|10>| = + |00>',
             self.hadamard(oz),
             ro.tensor(z)
         ],
         [
             'Verify HADAMARD +|11>| = + |01>',
             self.hadamard(oo),
             ro.tensor(o)
         ],
     ]
     for test in hadamard_tests:
         title, calc, expected = test
         self.assertEqual(calc, expected)
예제 #4
0
    def test_octonion_dimensions(self):
        """Check that a number intended to be a octonion says that it is"""
        for n in range(self.LOOPS):

            z = Construct([random() for _ in range(8)])
            self.assertFalse(z.is_complex())
            self.assertFalse(z.is_quaternion())
            self.assertTrue(z.is_octonion())
            self.assertFalse(z.is_sedenion())
            self.assertNotEqual(z.dimension(), 32)
예제 #5
0
    def test_32ion(self):
        """Check that a number intended to be 32 dimensional knows that it is"""
        for n in range(self.LOOPS):

            z = Construct([random() for _ in range(32)])
            self.assertFalse(z.is_complex())
            self.assertFalse(z.is_quaternion())
            self.assertFalse(z.is_octonion())
            self.assertFalse(z.is_sedenion())
            self.assertEqual(z.dimension(), 32)
예제 #6
0
 def test_constant_0(self):
     z = Construct((1, 0))
     o = Construct((0, 1))
     control = Construct(z.flat())
     target = Construct(z.flat())
     self.assertEqual(control, z)
     self.assertEqual(target, z)
     control = Construct(o.flat())
     target = Construct(z.flat())
     self.assertEqual(control, o)
     self.assertEqual(target, z)
예제 #7
0
 def test_identity(self):
     z = Construct((1, 0))
     o = Construct((0, 1))
     control = z.copy()
     target = z.copy()
     self.cnot(control, target)
     self.assertEqual(control, z)
     self.assertEqual(target, z)
     control = o.copy()
     target = z.copy()
     self.cnot(control, target)
     self.assertEqual(control, o)
     self.assertEqual(target, z)
예제 #8
0
 def test_constant_1(self):
     z = Construct((1, 0))
     o = Construct((0, 1))
     control = z.copy()
     target = z.copy()
     target = self.xgate(target)
     self.assertEqual(control, z)
     self.assertEqual(target, o)
     control = o.copy()
     target = z.copy()
     target = self.xgate(target)
     self.assertEqual(control, o)
     self.assertEqual(target, o)
예제 #9
0
 def test_tensor(self):
     z1 = Construct((1, 2))
     z2 = Construct((3, 4))
     z = Construct((1, 0))
     o = Construct((0, 1))
     brakets = [
         [z1.tensor(z2), (3, 4, 6, 8)],
         [o.tensor(z.tensor(z)), (0, 0, 0, 0, 1, 0, 0, 0)],
         [o.tensor(z.tensor(o)), (0, 0, 0, 0, 0, 1, 0, 0)],
         [o.tensor(o.tensor(z)), (0, 0, 0, 0, 0, 0, 1, 0)],
         [o.tensor(o.tensor(o)), (0, 0, 0, 0, 0, 0, 0, 1)],
         [
             o.tensor(z.tensor(z.tensor(z))),
             (0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(z.tensor(o))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(o.tensor(z))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(o.tensor(o))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)
         ],
         [
             o.tensor(o.tensor(z.tensor(z))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
         ],
         [
             o.tensor(o.tensor(z.tensor(o))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0)
         ],
         [
             o.tensor(o.tensor(o.tensor(z))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)
         ],
         [
             o.tensor(o.tensor(o.tensor(o))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
         ],
         [
             o.tensor(z.tensor(z.tensor(z.tensor(z)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(z.tensor(z.tensor(o)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(z.tensor(o.tensor(z)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(z.tensor(o.tensor(o)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(o.tensor(z.tensor(z)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(o.tensor(z.tensor(o)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(o.tensor(o.tensor(z)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(z.tensor(o.tensor(o.tensor(o)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(o.tensor(z.tensor(z.tensor(z)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(o.tensor(z.tensor(z.tensor(o)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(o.tensor(z.tensor(o.tensor(z)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)
         ],
         [
             o.tensor(o.tensor(z.tensor(o.tensor(o)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)
         ],
         [
             o.tensor(o.tensor(o.tensor(z.tensor(z)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
         ],
         [
             o.tensor(o.tensor(o.tensor(z.tensor(o)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0)
         ],
         [
             o.tensor(o.tensor(o.tensor(o.tensor(z)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)
         ],
         [
             o.tensor(o.tensor(o.tensor(o.tensor(o)))),
             (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
         ],
     ]
     for item in brakets:
         calc, expected = item
         expect = Construct(expected)
         self.assertEqual(calc, expect)