def test_104_check_a_b(self): for aaa in [pi / 6, pi / 3, pi, pi + pi / 3, pi + pi / 6]: q = Qubits.create(1, [(0, cos(aaa)), (1, sin(aaa))]) assert self.check_a_b(q, aaa) == True q = Qubits.create(1, [(0, -sin(aaa)), (1, cos(aaa))]) assert self.check_a_b(q, aaa) == False
def test_07_controlled_x_general(self): for ix in range(20): a, b, c, d = self.random_unit_vector(4) q = Qubits.create(2, [(0, a), (1, b), (2, c), (3, d)]) target = Qubits.create(2, [(0, a), (1, b), (2, d), (3, c)]) print "before", q self.controlled_x_general(q) print "after", q self.assert_qubits(q, target)
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 test_05_different_basis(self): """// Task 5. Parity measurement in different basis // Input: Two qubits (stored in an array) which are guaranteed to be // either in superposition α|00⟩ + β|01⟩ + β|10⟩ + α|11⟩ // or in superposition α|00⟩ - β|01⟩ + β|10⟩ - α|11⟩. // Output: 0 if qubits were in the first superposition, // 1 if they were in the second superposition. // The state of the qubits at the end of the operation should be the same as the starting state.""" for _ in range(10): for mul in [1, -1]: a, b = self.random_unit_vector(2) a = a / sqrt(2.0) b = b / sqrt(2.0) q = Qubits.create(2, [(0, a), (1, mul * b), (2, b), (3, mul * a)]) qq = q.clone() d = self.different_basis(q) self.assert_qubits(q, qq) expected = 0 if mul == 1 else 1 assert d == expected
def test_106_basis_state_measurement(self): for ix in range(4): q = Qubits.create(2, [(ix, 1)]) assert self.basis_state_measurement(q) == ix