# Implementing Deutsch's problem on IBM Q composer

I tried to implement the Deutsch's problem on IBM Q but didn't get the results as expected.

So here is the circuit for the Deutsch algorithm and this is my implementation on the composer: so let me explain to you what I did/thought:

$$|x\rangle$$ is qubit q(1)

$$|y\rangle$$ is q(2)

with qubit q(0) I wanted to realize the oracle function $$f(x)$$, which has - as implemented - the properties that $$f(0) = 0$$ and $$f(1) = 1$$. As I used the cnot gate on the initial $$|0\rangle$$ state of q(0) -> so $$f$$ is balanced on q(0) I should have f(x) after the first CNOT gate. q(0) (which is $$f(|x\rangle)$$ now determines what happens with q(2) using a flipped CNOT, as it is technically not possible to use the normal CNOT gate.

Then I measure $$|x\rangle$$ i.e. q(1) which should be in a pure state (here $$|0\rangle$$ as $$f$$ is balanced) - but it is not: so any ideas what went wrong here? Maybe (probably) my implementation of $$U_{f}$$ is wrong, but how can this be realized then?

TL;DR: There are 8 possible outcomes, each with equal probability of being read. The final state has q read as 0 in 4 of the outcomes, and read as 1 in the other 4 outcomes. Since you are only measuring q, you only see the two results, one with q as 0 and one with q as 1. They are both around 50% because each outcome had an equal probability of being read.

Let's go through your circuit and what the state should be at each step.

X-Gate on q

The state will now be |100$$\rangle$$

Just a simple bit flip on q, from 0 to 1.

H-Gate on q and H-Gate on q

After these gates, the state will be $$\frac{1}{\sqrt4}$$|000$$\rangle$$ + $$\frac{1}{\sqrt4}$$|010$$\rangle$$ + $$\frac{1}{\sqrt4}$$|100$$\rangle$$ + $$\frac{1}{\sqrt4}$$|110$$\rangle$$

q and q both get put into superposition. They both can be either 0 or 1, leading to 4 possible outcomes.

CNOT from q to q

After this gate, the state will be $$\frac{1}{\sqrt4}$$|000$$\rangle$$ + $$\frac{1}{\sqrt4}$$|011$$\rangle$$ + $$\frac{1}{\sqrt4}$$|100$$\rangle$$ + $$\frac{1}{\sqrt4}$$|111$$\rangle$$

q and q become entangled. q's outcome depends on q. The two will either be 00 or 11, and since q is still in superposition, there are still 4 possible outcomes.

H-Gate on q and H-Gate on q

After these gates, the state will be $$\frac{1}{\sqrt4}$$|100$$\rangle$$ + $$\frac{1}{\sqrt4}$$|101$$\rangle$$ + $$\frac{1}{\sqrt4}$$|110$$\rangle$$ + $$\frac{1}{\sqrt4}$$|111$$\rangle$$

The H-Gate on q cancels the first one out, leaving q's outcome as 1. The H-Gate on q puts it in superposition, meaning it can be either 0 or 1. There are still 4 outcomes, because although we lost two possible outcomes with q going back to just being 1, we gained two possible outcomes since q can be either 0 or 1.

CNOT from q to q

After this gate, the state will still be $$\frac{1}{\sqrt4}$$|100$$\rangle$$ + $$\frac{1}{\sqrt4}$$|101$$\rangle$$ + $$\frac{1}{\sqrt4}$$|110$$\rangle$$ + $$\frac{1}{\sqrt4}$$|111$$\rangle$$

This gate essentially does not change the state. What ends up happening is when q is 1, a bit flip will be applied to q. However, at the moment q is always 1, so each possible outcome will have q's bit flipped. Since each outcome has an equal probability of being read, the bit flip just provides us with the state possible outcomes, each with the same probabilities.

H-Gate on q and H-Gate on q

After these gates, the state will be $$\frac{1}{\sqrt4}$$|000$$\rangle$$ + $$\frac{1}{\sqrt4}$$|011$$\rangle$$ + $$\frac{1}{\sqrt4}$$|100$$\rangle$$ + $$\frac{1}{\sqrt4}$$|111$$\rangle$$

The H-Gate on q puts it back in superposition, meaning it can be either 0 or 1. The H-Gate on q cancels out the first one, meaning it is either 0 or 1 depending on q. q and q will either be 00 or 11, and q will either be 0 or 1, providing us with 4 possible outcomes.

H-Gate on q

After this gate, the state will be $$\frac{1}{\sqrt8}$$|000$$\rangle$$ + $$\frac{1}{\sqrt8}$$|001$$\rangle$$ + $$\frac{1}{\sqrt8}$$|010$$\rangle$$ + $$\frac{1}{\sqrt8}$$|011$$\rangle$$ + $$\frac{1}{\sqrt8}$$|100$$\rangle$$ + $$\frac{1}{\sqrt8}$$|101$$\rangle$$ + $$\frac{1}{\sqrt8}$$|110$$\rangle$$ + $$\frac{1}{\sqrt8}$$|111$$\rangle$$

This final H-gate sets q's outcome as either 0 or 1, and this change won't affect q's outcome. q's outcome is still either 0 or 1 depending on what q at the CNOT gate. q is still in superposition, so it is either 0 or 1 as well. Since all 3 of the qubits can be either 0 or 1, there are $$2^3$$, or 8, possible outcomes.

The last H-Gate on q does not cancel out the first H-Gate (which would end up setting q to only be 0) because of the entanglement between q and q.

Unfortunately, I do not know exactly how to fix your circuit to have it do what you want it to, as I am not very knowledgable on the algorithm itself. However, this article may provide some more information on the topic.