I am trying to implement a basic quantum computing emulator. In the chapter on Grover's algorithm, we're shown the following circuit:
They demonstrate Grover's algorithm with a function $f$ that picks out $101$, i.e. $f(101)=1$ and $0$ otherwise. They start with $\psi_{1}=[1, 0, 0, 0, 0, 0, 0, 0]^{T}$. The Hadamard gate (specifically H tensored with itself $n$ times) gives $\psi_{2}=1/\sqrt8 [1, 1, 1, 1, 1, 1, 1, 1]^{T}$. This is as far as I've come. They don't show explicitly how to get to $\psi_{3}$, which should be $1/\sqrt8 [1, 1, 1, 1, 1, -1, 1, 1]^{T}$. I am not sure how to interpret the circuit.
My best guess was to take the tensor product of $|0 \rangle=|000 \rangle$ and $|1 \rangle = [0, 1]^{T}$, then apply $I_{2^{n}} \otimes H$, then $U_{f}$. However, I have two problems:
The book says that, at that stage in the calculation, $\psi_{3} = 1/\sqrt8 [1, 1, 1, 1, 1, -1, 1, 1]^{T}$, which has length $8$, instead of $16$. I don't know how to "extract" the "top" qubits. Furthermore, my answer is $1/4[1, 1..., 1]^{T}$, which doesn't suggest the correct answer (especially given the fact that every entry is $1/4$).
Am I misinterpreting this circuit? What is the correct way to go from $\psi_{2}$ to $\psi_{3}$, from a programmatic point of view?