Example #1
0
    def test_fredkin_gate(self):
        q = Qubits(3)
        Gates.X(q, 0)
        Gates.X(q, 1)
        self.fredkin_gate(q)
        self.assert_signs(q, [(5, 1)])

        q = Qubits(3)
        Gates.X(q, 0)
        Gates.X(q, 2)
        self.fredkin_gate(q)
        self.assert_signs(q, [(6, 1)])

        for x in [True, False]:
            for y in [True, False]:
                for z in [True, False]:
                    if x and (y ^ z):
                        continue
                    q = Qubits(3)
                    if x:
                        Gates.X(q, 0)
                    if y:
                        Gates.X(q, 1)
                    if z:
                        Gates.X(q, 2)
                    self.fredkin_gate(q)
                    self.assert_signs(q, q.monomials())
Example #2
0
 def test_102_reset_qubit(self):
     q = Qubits(1)
     self.reset_qubit(q)
     self.assert_signs(q, [(0, 1)])
     q = Qubits(1)
     Gates.X(q, 0)
     self.reset_qubit(q)
     self.assert_signs(q, [(0, 1)])
Example #3
0
 def test_103_check_plus_minus(self):
     q = Qubits(1)
     Gates.H(q, 0)
     assert self.check_plus_minus(q) == True
     q = Qubits(1)
     Gates.X(q, 0)
     Gates.H(q, 0)
     assert self.check_plus_minus(q) == False
Example #4
0
    def test_two_qubit_gate(self):
        q = Qubits(2)
        Gates().H(q, 0)
        self.two_qubit_gate(q)
        self.assert_signs(q, [(0, 1), (3, 1)])

        q = Qubits(2)
        self.two_qubit_gate(q)
        self.assert_signs(q, [(0, 1)])
Example #5
0
    def test_amplitude_change(self):
        q = Qubits(1)
        self.amplitude_change(q, pi / 3)
        self.assert_signs(q, [(0, half), (1, half * sqrt(3))])

        q = Qubits(1)
        Gates.X(q, 0)
        self.amplitude_change(q, pi / 3)
        self.assert_signs(q, [(0, -half * sqrt(3)), (1, half)])
Example #6
0
    def test_superposition(self):
        q = Qubits(5)
        self.superposition_bitstrings(q, [True, False, False, True, True],
                                      [True, True, False, False, True])
        self.assert_signs(q, [(19, 1), (25, 1)])

        q = Qubits(5)
        self.superposition_bitstrings(q, [False, False, False, True, True],
                                      [False, True, False, False, True])
        self.assert_signs(q, [(3, 1), (9, 1)])

        q = Qubits(1)
        self.superposition_bitstrings(q, [True], [False])
        self.assert_signs(q, [(0, 1), (1, 1)])
Example #7
0
 def test_superposition(self):
     for ix in range(1, 5):
         print "superposition for {0:d}".format(ix)
         qs = Qubits(ix)
         self.generate_superposition(qs)
         print qs
         self.assert_qubits(qs)
Example #8
0
 def test_phase_flip(self):
     q = Qubits(1)
     self.phase_flip(q)
     self.assert_signs(q, [(0, 1)])
     Gates.X(q, 0)
     self.phase_flip(q)
     self.assert_signs(q, [(1, I)])
Example #9
0
 def test_phase_change(self):
     aaa = symbols('aaa')
     q = Qubits(1)
     self.phase_change(q, aaa)
     self.assert_signs(q, [(0, 1)])
     Gates.X(q, 0)
     self.phase_change(q, aaa)
     self.assert_signs(q, [(1, exp(I * aaa))])
Example #10
0
 def test_plus_minus_state(self):
     print("generating plus minus states")
     expectations = [[(0, 1), (1, 1)], [(0, 1), (1, -1)]]
     for exp, sign in zip(expectations, [1, -1]):
         q = Qubits(1)
         self.plus_minus_state(q, sign)
         self.assert_signs(q, exp)
     print("plus minus states solved")
Example #11
0
 def test_bva(self):
     bits = [True, True, False]
     oracle = OracleE2(bits).oracle
     value = self.check_bva(len(bits), oracle)
     voracle = OracleE2(value).oracle
     q = Qubits(len(bits) + 1)
     vv = voracle(q)
     bv = oracle(q)
     assert vv == bv, "incorrect returned bitstring"
     bits = [True, True, False, True]
     oracle = OracleE2(bits).oracle
     value = self.check_bva(len(bits), oracle)
     voracle = OracleE2(value).oracle
     q = Qubits(len(bits) + 1)
     vv = voracle(q)
     bv = oracle(q)
     assert vv == bv, "incorrect returned bitstring"
Example #12
0
 def check_bva(self, n, oracle):
     q = Qubits(n + 1)
     oracle(q)
     m = Measurement.measure(q, n)
     v = 1 if m == -1 else 0
     r = [True] * n
     if n % 2 != v:
         r[-1] = False
     return r
Example #13
0
 def s_state(self, ix):
     s = [
         [half, -half, -half, -half],
         [-half, half, -half, -half],
         [-half, -half, half, -half],
         [-half, -half, -half, half],
     ]
     q = Qubits(2)
     v = Matrix(s[ix])
     q.v = v
     return q
Example #14
0
 def test_ghz_states(self):
     print("testing ghz states")
     total = 5
     expectations = map(lambda x: [(0, 1), (2**x - 1, 1)],
                        xrange(1, total + 1))
     for exp, ix in zip(expectations, xrange(1, total + 1)):
         q = Qubits(ix)
         self.ghz_state(q)
         print q
         print exp
         self.assert_signs(q, exp)
     print("ghz states equal")
Example #15
0
 def test_basis_change(self):
     q = Qubits(1)
     self.basis_change(q)
     self.assert_signs(q, [(0, 1), (1, 1)])
     self.basis_change(q)
     self.assert_signs(q, [(0, 1)])
     q.v[0] = 0
     q.v[1] = 1
     self.basis_change(q)
     self.assert_signs(q, [(0, 1), (1, -1)])
     self.basis_change(q)
     self.assert_signs(q, [(1, 1)])
Example #16
0
    def test_two_qubit_gate_3(self):
        q = Qubits(2)
        # Assert that |01> -> |10>
        Gates.X(q, 1)
        self.two_qubit_gate_3(q)
        self.assert_signs(q, [(2, 1)])

        # Assert that |10> -> |01>
        self.two_qubit_gate_3(q)
        self.assert_signs(q, [(1, 1)])

        # Assert that |00> -> |00>
        q = Qubits(2)
        self.two_qubit_gate_3(q)
        self.assert_signs(q, [(0, 1)])

        # Assert that |11> -> |11>
        Gates.X(q, 0)
        Gates.X(q, 1)
        self.assert_signs(q, [(3, 1)])
        self.two_qubit_gate_3(q)
        self.assert_signs(q, [(3, 1)])
Example #17
0
 def test_bell_states(self):
     print("checking bell states")
     expectations = [
         [(0, 1), (3, 1)],
         [(0, 1), (3, -1)],
         [(1, 1), (2, 1)],
         [(1, 1), (2, -1)],
     ]
     for ix, ex in enumerate(expectations):
         q = Qubits(2)
         self.bell_state(q, ix)
         self.assert_signs(q, ex)
     print("bell states equal")
Example #18
0
 def test_teleportation(self):
   q = Qubits(3)
   alice = 0
   bob = 1
   message = 2
   Gates.H(q, message)
   # entangle alice with bob
   Gates.H(q, alice)
   Gates.CNOT(q, alice, bob)
   # entangle message with alice
   print Gates.CNOT(q, message, alice)
   print Gates.H(q, message)
   am = Measurement.measure(q, alice)
   mm = Measurement.measure(q, message)
   print am == -1, mm == -1, q
Example #19
0
 def check_bva(self, n, oracle):
     q = Qubits(n + 1)
     Gates.X(q, n)
     for ix in xrange(n + 1):
         Gates.H(q, ix)
     oracle(q)
     for ix in xrange(n):
         Gates.H(q, ix)
     r = [True] * n
     for ix in xrange(n):
         m = Measurement.measure(q, ix)
         if m == -1:
             Gates.X(q, ix)
         r[ix] = (m != 1)
     return r
Example #20
0
    def test_10_superposition_bitstring(self):
        self.assert_qubits(self.superposition_bitstring(Qubits(1), [True]),
                           self.plus_state(Qubits(1)))
        self.assert_qubits(
            self.superposition_bitstring(Qubits(2), [True, True]),
            self.bell_state(Qubits(2)))
        self.assert_qubits(
            self.superposition_bitstring(Qubits(3), [True, True, True]),
            self.ghz_state(Qubits(3)))

        q = Qubits(2)
        b = [True, False]
        self.assert_signs(self.superposition_bitstring(q, b), [(0, 1), (2, 1)])

        q = Qubits(3)
        b = [True, False, True]
        self.assert_signs(self.superposition_bitstring(q, b), [(0, 1), (5, 1)])

        q = Qubits(6)
        b = [True, False, True, True, False, False]
        self.assert_signs(self.superposition_bitstring(q, b), [(0, 1),
                                                               (44, 1)])
Example #21
0
 def test_toffoli_gate(self):
     for x in [True, False]:
         for y in [True, False]:
             for z in [True, False]:
                 q = Qubits(3)
                 m = 0
                 if x:
                     Gates.X(q, 0)
                     m = m + 4
                 if y:
                     Gates.X(q, 1)
                     m = m + 2
                 if z:
                     Gates.X(q, 2)
                 if x and y:
                     if not z:
                         m = m + 1
                 elif z:
                     m = m + 1
                 self.toffoli_gate(q)
                 self.assert_signs(q, [(m, 1)])
Example #22
0
 def test_105_zz_or_oo(self):
     q = Qubits(2)
     assert self.zz_or_oo(q) == False
     Gates.X(q, 0)
     Gates.X(q, 1)
     assert self.zz_or_oo(q) == True
Example #23
0
 def test_02_minus_state(self):
     q = Qubits(1)
     self.minus_state(q)
     self.assert_signs(q, [(0, 1), (1, -1)])
Example #24
0
 def test_03_unequal_superposition(self):
     q = Qubits(1)
     self.unequal_superposition(q, -2 * pi / 3)
     self.assert_signs(q, [(0, cos(pi / 3)), (1, sin(pi / 3))])
Example #25
0
 def test_04_superposition(self):
     q = Qubits(2)
     self.superposition(q)
     self.assert_signs(q, [(0, 1), (1, 1), (2, 1), (3, 1)])
Example #26
0
 def test_05_superposition_basis(self):
     q = Qubits(2)
     self.superposition_basis(q)
     self.assert_signs(q, [(0, half), (1, I / 2), (2, -half), (3, -I / 2)])
Example #27
0
    def test_state_distinction(self):
        q = Qubits.w(5)
        assert self.state_distinction(q) == 1

        q = Qubits(5)
        assert self.state_distinction(q) == 0
Example #28
0
 def test_06_bell_states(self):
     q = Qubits(2)
     self.bell_state(q)
     self.assert_signs(q, [(0, 1), (3, 1)])
Example #29
0
 def test_superposition(self):
     q = Qubits(5)
     self.superposition(q, [True, False, False, True, True])
     self.assert_signs(q, [(0, 1), (19, 1)])
Example #30
0
 def test_101_single_qubit(self):
     q = Qubits(1)
     assert self.single_qubit(q) == False
     q = Qubits(1)
     Gates.X(q, 0)
     assert self.single_qubit(q) == True