def quantum_parallelism(f): U_f = qc.U_f(f, d=2) H = qc.Hadamard() phi = qc.zeros(2) phi = H(phi, qubit_indices=[0]) phi = U_f(phi)
def deutsch_algorithm(f): U_f = qc.U_f(f, d=2) H = qc.Hadamard() phi = H(qc.zeros()) * H(qc.ones()) phi = U_f(phi) phi = H(phi, qubit_indices=[0]) measurement = phi.measure(qubit_indices=0) return measurement
def test_U_f_unitary(self): num_tests = 10 for test_i in range(num_tests): d = np.random.randint(3, 7) f = random_boolean_function(d) Op = qc.U_f(f, d=d+1) Op_adj = get_adjoint(Op) I = qc.Identity(d+1) max_diff = max_absolute_difference(Op(Op_adj), I) self.assertLess(max_diff, epsilon) max_diff = max_absolute_difference(Op_adj(Op), I) self.assertLess(max_diff, epsilon)
def deutsch_jozsa_algorithm(d, f): # The operators we will need U_f = qc.U_f(f, d=d + 1) H_d = qc.Hadamard(d) H = qc.Hadamard() state = qc.zeros(d) * qc.ones(1) state = (H_d * H)(state) state = U_f(state) state = H_d(state, qubit_indices=range(d)) measurements = state.measure(qubit_indices=range(d)) return measurements
def test_U_f_basis_measurement(self): num_tests = 10 for test_i in range(num_tests): d = np.random.randint(1, 8) f = random_boolean_function(d) U = qc.U_f(f, d=d+1) bits = tuple(np.random.choice([0, 1], size=d, replace=True)) input_qubits = qc.bitstring(*bits) ans_qubit = qc.zeros() state = input_qubits * ans_qubit state = U(state) answer = f(*bits) measured_ans = state.measure(qubit_indices=d) self.assertEqual(answer, measured_ans)
def grover_algorithm(d, f): # The operators we will need Oracle = qc.U_f(f, d=d+1) H_d = qc.Hadamard(d) H = qc.Hadamard() N = 2**d zero_projector = np.zeros((N, N)) zero_projector[0, 0] = 1 Inversion = H_d((2 * qc.Operator.from_matrix(zero_projector) - qc.Identity(d))(H_d)) Grover = Inversion(Oracle, qubit_indices=range(d)) # Initial state state = qc.zeros(d) * qc.ones(1) state = (H_d * H)(state) # Number of Grover iterations angle_to_rotate = np.arccos(np.sqrt(1 / N)) rotation_angle = 2 * np.arcsin(np.sqrt(1 / N)) iterations = int(round(angle_to_rotate / rotation_angle)) for i in range(iterations): state = Grover(state) measurements = state.measure(qubit_indices=range(d)) return measurements