def oracle(self, qs): assert qs.length == (len(self.b) + 1), "incorrect qubit length" for ix, b in enumerate(self.b): if b: Gates.CNOT(qs, ix, qs.length - 1) else: Gates.X(qs, ix) Gates.CNOT(qs, ix, qs.length - 1) Gates.X(qs, ix)
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
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)])
def all_bell_states(self, q, ix): Gates.H(q, 0) if ix >= 2: Gates.X(q, 1) Gates.CNOT(q, 0, 1) if ix % 2 == 1: Gates.Z(q, 0)
def w_state(self, q): Gates.X(q, 0) power = 1 missedpower = -1 while power < q.length: oldpower = power power = power * 2 if power > q.length: power = q.length missedpower = oldpower Gates.H(q, power - 1) for ix in range(oldpower - 1): index = oldpower + ix if ix < q.length and index < q.length: Gates.CCNOT(q, ix, power - 1, index) if ix < q.length and index < q.length: Gates.CNOT(q, index, ix) if index < q.length: Gates.CNOT(q, index, power - 1) Gates.CNOT(q, power - 1, oldpower - 1) if missedpower > -1: Gates.CCNOT(q, 0, q.length - 1, 1) Gates.CCNOT(q, 1, 0, q.length - 1) m = Measurement.rr_measure(q, q.length - 1, 0) Gates.CCNOT(q, 1, 0, q.length - 1) Gates.CNOT(q, q.length - 1, 0) Gates.CNOT(q, q.length - 1, 1) return q
def test_add_X_remove(self): q = Qubits.random_qubits(2) qq = q.clone() q.add_qubit() Gates.X(q, 0) q.remove_qubit() self.assert_qubits(q, qq)
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
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)])
def bell_state(self, q, index): assert q.length == 2 Gates.H(q, 0) Gates.CNOT(q, 0, 1) if index % 2 == 1: Gates.Z(q, 1) if index >= 2: Gates.X(q, 1)
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))])
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())
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)])
def superposition_bitstrings(self, q, bits1, bits2): ix, bitsa, bitsb = self.__find_one_zero(bits1, bits2) Gates.H(q, ix) for ii in range(0, len(bits1)): if ix == ii: continue if bitsb[ii]: Gates.X(q, ii) Gates.CNOT(q, ix, ii) if bitsa[ii]: Gates.CNOT(q, ix, ii)
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)])
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)])
def w_state(self, q): Gates.X(q, 0) power = 1 while power < q.length: oldpower = power power = power * 2 Gates.H(q, power - 1) for ix in range(oldpower - 1): index = oldpower + ix Gates.CCNOT(q, ix, power - 1, index) Gates.CNOT(q, index, ix) Gates.CNOT(q, index, power - 1) Gates.CNOT(q, power - 1, oldpower - 1) return q
def controlled_x_general(self, q): # Gates.CNOT(q, 0, 1) items = [0.1, 0.9] r1, r2, r3 = choice(items), choice(items), choice(items) bad = [[0.1, 0.9, 0.9], [0.9, 0.9, 0.1]] if [r1, r2, r3] in bad: r1, r2, r3 = [0.1, 0.1, 0.1] print r1, r2, r3 q.add_qubit() #print "before measurement", q p1 = self.copy_qubit(q, 1, 0, r1) #print "after measurement", q Gates.H(q, 0) Gates.H(q, 2) p2 = Measurement.rr_measurement(q, [PauliZ(), PauliZ()], [0, 2], r2) Gates.H(q, 0) Gates.H(q, 2) if p2 == -1: Gates.Z(q, 1) if p1 != Measurement.rr_measurement(q, [PauliZ()], [0], r3): Gates.X(q, 2) #print "after CNOT", q q.remove_qubit()
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
def state_flip(self, q): Gates.X(q, 0)
def minus_state(self, q): Gates.X(q, 0) self.plus_state(q)
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
def reset_qubit(self, q): if Measurement.measure(q, 0) == -1: Gates.X(q, 0)
def plus_minus_state(self, q, sign): assert q.length == 1 if sign < 0: Gates.X(q, 0) Gates.H(q, 0)