1

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)

enter image description here

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?

astentx
  • 6,393
  • 2
  • 16
  • 25
ksk S
  • 13
  • 2

1 Answers1

2

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.

Mariia Mykhailova
  • 1,987
  • 1
  • 9
  • 18
  • Thank you so much. I misunderstood the algorithm. I corrected my code to `circ.h(0)` to `circ.h(1)`, then I got the output `{'00': 515, '01': 509}` and now I know the second classical bit shows the function type. – ksk S Aug 02 '21 at 00:42
  • @kskS I'm glad that helped! You can mark the answer as accepted to indicate that it resolves your problem – Mariia Mykhailova Aug 02 '21 at 04:58
  • 1
    Thank you for your comments. I marked your answer as accepted I guess :) – ksk S Aug 03 '21 at 00:48