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 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 mZZ(q, ix, iy): Gates.CNOT(q, iy, ix) print q m = Measurement.measure(q, ix) print m, q Gates.CNOT(q, iy, ix) print m, q
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 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 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 test_add_CNOT_remove(self): q = Qubits.random_qubits(2) qq = q.clone() q.add_qubit() Gates.CNOT(q, 1, 0) q.remove_qubit() self.assert_qubits(q, qq)
def oracle(self, qs): assert qs.length == (len(self.b) + 1), "incorrect qubit length" assert self.called == False, "can only call oracle once" for ix, b in enumerate(self.b): if b: Gates.CNOT(qs, ix, qs.length - 1) self.called = True
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 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 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_state(self, q): Gates.H(q, 0) Gates.CNOT(q, 0, 1) Gates.H(q, 0) 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 check_state(self, q, rnd): l = q.length if l % 2 == 1: m = Measurement.rr_measure(q, l - 1, rnd) if m == -1: m = Measurement.measure(q, 0) if m == 1: return 1 if m == -1: return 0 return l = l - 1 for ix in xrange(1, l): Gates.CNOT(q, ix, 0) for ix in xrange(1, l): Gates.CNOT(q, 0, ix) # if l % 2 == 1: # Gates.H(q, 0) m = Measurement.measure(q, 0) if m == -1: return 1 else: return 0
def test_06_controlled_x(self): """// Task 6. Controlled X gate with |0⟩ target // Input: Two unentangled qubits (stored in an array of length 2). // The first qubit will be in state |ψ⟩ = α |0⟩ + β |1⟩, the second - in state |0⟩ // (this can be written as two-qubit state (α|0⟩ + β|1⟩) ⊕ |0⟩). // Goal: Change the two-qubit state to α |00⟩ + β |11⟩ using only single-qubit gates and joint measurements. // Do not use two-qubit gates. // You do not need to allocate extra qubits.""" for _ in range(20): for rnd in [0.1, 0.9]: a, b = self.random_unit_vector(2) q = Qubits.create(2, [(0, a), (2, b)]) qq = q.clone() Gates.CNOT(qq, 0, 1) self.controlled_x(q, rnd) self.assert_qubits(q, qq)
def ghz_state(self, q): l = q.length Gates.H(q, 0) for ix in xrange(1, l): Gates.CNOT(q, 0, ix)
def two_qubit_gate_3(self, q): Gates.CNOT(q, 0, 1) Gates.CNOT(q, 1, 0) Gates.CNOT(q, 0, 1)
def bell_state(self, q): Gates.H(q, 0) return Gates.CNOT(q, 0, 1)
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 ghz_state(self, q): Gates.H(q, 0) for ix in range(1, q.length): Gates.CNOT(q, 0, ix) return q