Solution (source code)

= Solution

Apply a <Pauli X gate> and then a <Hadamard gate> to the third qubit, and apply Hadamard gates to the first two. This prepares
$$
\left(\frac12\sum_{x\in B_2}|x\rangle\right)|-\rangle,
\qquad
|-\rangle=\frac{|0\rangle-|1\rangle}{\sqrt2}.
$$
One application of the <Boolean quantum oracle> now gives, by <quantum phase kickback>,
$$
U_f\left(\frac12\sum_x|x\rangle|-\rangle\right)
=\frac12\sum_x(-1)^{f(x)}|x\rangle|-\rangle
=|f\rangle|-\rangle.
$$
The answer qubit is unentangled and may be ignored. If all three qubits must be restored to the displayed register form, applying $H$ and then $X$ to the answer qubit maps $|-\rangle$ back to $|0\rangle$. Thus \b[the first register is exactly $|f\rangle$ after one use of $U_f$.]