def mZX(q, ix, iy): Gates.H(q, iy) Gates.CNOT(q, iy, ix) m = Measurement.measure(q, ix) Gates.CNOT(q, iy, ix) Gates.H(q, iy) print m, q
def check_state(self, q): Gates.H(q, 0) Gates.H(q, 1) m = Measurement.measure(q, 0) mm = Measurement.measure(q, 1) ii = 0 if m == 1 else 1 i = 0 if mm == 1 else 1 return i + 2 * ii
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 mXX(q, ix, iy): Gates.H(q, ix) print q Gates.H(q, iy) print q Gates.CNOT(q, iy, ix) print q m = Measurement.measure(q, ix) print m, q Gates.CNOT(q, iy, ix) print m, q Gates.H(q, iy) print m, q Gates.H(q, ix) print m, q
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
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 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 controlled_x(self, q, rnd): Gates.Z(q, 0) if Measurement.rr_measurement(q, [PauliZ(), PauliX()], [0, 1], rnd) == -1: Gates.Z(q, 1) Gates.H(q, 1) Gates.Z(q, 0)
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 check_bell(self, q): Gates.CNOT(q, 0, 1) Gates.H(q, 0) m = Measurement.measure(q, 0) mm = Measurement.measure(q, 1) i = 0 if m == 1 else 1 ii = 0 if mm == 1 else 1 return i + 2 * ii
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 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 all_superposition(self, q): for ix in range(q.length): Gates.H(q, ix)
def ghz_state(self, q): l = q.length Gates.H(q, 0) for ix in xrange(1, l): Gates.CNOT(q, 0, ix)
def generate_superposition(self, qs): for ix in range(qs.length): Gates.H(qs, ix)
def superposition(self, q): Gates.H(q, 0) Gates.H(q, 1)
def plus_state(self, q): return Gates.H(q, 0)
def basis_change(self, q): Gates.H(q, 0)
def test_two_qubit_gate_2(self): q = Qubits(2) Gates.H(q, 0) Gates.H(q, 1) self.two_qubit_gate_2(q) self.assert_signs(q, [(0, 1), (1, 1), (2, 1), (3, -1)])
def plus_minus_state(self, q, sign): assert q.length == 1 if sign < 0: Gates.X(q, 0) Gates.H(q, 0)
def ghz_state(self, q): Gates.H(q, 0) for ix in range(1, q.length): Gates.CNOT(q, 0, ix) return q
def copy_qubit(self, q, source, target, r): Gates.H(q, target) return Measurement.rr_measurement(q, [PauliZ(), PauliZ()], [target, source], r)
def superposition(self, q, bits): Gates.H(q, 0) for ix in range(1, len(bits)): if bits[ix]: Gates.CNOT(q, 0, ix)
def bell_state(self, q): Gates.H(q, 0) return Gates.CNOT(q, 0, 1)
def check_plus_minus(self, q): Gates.H(q, 0) return not self.single_qubit(q)
print q Gates.CNOT(q, iy, ix) print q m = Measurement.measure(q, ix) print m, q Gates.CNOT(q, iy, ix) print m, q Gates.H(q, iy) print m, q Gates.H(q, ix) print m, q def mZX(q, ix, iy): Gates.H(q, iy) Gates.CNOT(q, iy, ix) m = Measurement.measure(q, ix) Gates.CNOT(q, iy, ix) Gates.H(q, iy) print m, q q = Qubits(2) Gates.H(q, 0) Gates.H(q, 1) Gates.CY(q, 1, 0) Gates.CZ(q, 0, 1) Gates.Z(q, 0) Gates.Z(q, 1) print(q)