# Deustch-Jozsa algorithm for a specific problem, doesn't make sense?

I am trying to implement Deustch-Jozsa Algorithm where function f(x) = 0 for even and f(x) = 1 for odd, for a four-bit number. After writing the numbers out I found a pattern as described below Therefore as you can see the from the image, all I need to do is add the CNOT gate to the last register so to implement the oracle and here is my circuit that I have written for the same. Here is the circuit and output for the same  My question is shouldn't the output be '1111' since this is a balanced function. The number corresponding in the circuit is 2 = '0010' and the output is showcased for the same. I would highly appreciate breakdown. Also the does the circuit implement the oracle where f(x) = 0 is even and for

Your oracle is correct. The mistake here is that you initialized $$q_1$$ to $$|1\rangle$$ instead of the ancilla qubit. So the proper circuit here would be something like this:
This would give the state $$|1000\rangle$$ with probability $$1$$, which shows that $$f(x)$$ is balanced.
For your second question, recall that Deutsch-Jozsa algorithm guarantees that either the final measurement will give $$|0\rangle^{\otimes n}$$ with probability $$1$$, in which case our function is constant, or it will yield some other state, in which case we have a function that's balanced. Our measured state need not be $$|1\rangle^{\otimes n}$$ to deduce that $$f(x)$$ is balanced.
• @JaZZyCooL I don't understand what you mean by encoding an input. The only thing Deutsch-Jozsa expects is an oracle $U_f$ which maps $|x\rangle |y\rangle \to |x\rangle |y\oplus f(x)\rangle$ and given that it determines if $f$ is balanced or constant. It accepts no other input. Also, you need the ancilla to the state $H|1\rangle$ in order to induce phase kickback. Feb 15 at 15:36