I tried to implement Deutsch algorithm using qiskit. The following is a code.
circ = QuantumCircuit(2, 2) # |q_1q_0> circ.x(0) circ.h([0,1]) # Oracle circ.barrier() circ.x(1) circ.barrier() circ.h(0) circ.measure([0,1], [0,1]) backend_sim = Aer.get_backend('qasm_simulator') job = execute(circ, backend_sim, shots=1024) result = job.result() counts = result.get_counts(circ) print(counts)
I expected that the first classical bit is 0 (that is, the function corresponding to that oracle is a constant function). But, the output is the following.
{'11': 496, '01': 528}
Why does the output imply the function is a balanced one?
Advertisement
Answer
Deutsch algorithm applies the X gate to the qubit you use for phase kickback trick, to prepare it in the |-⟩ state before applying the oracle. Your implementation applies it to the “data” qubit instead, so that the combined effect of the algorithm (after H gates cancel out) is just preparing the data qubit in |1⟩ state.