As far as I have understood, "uncomputation" in quantum computing is a way to restore the working memory to its initial state, while keeping the result of the computation in another register. This trick is usually explained as follows:
Assume you have a classical function $f : \{0,1\}^n \rightarrow \{0,1\}$. Then there exists a unitary $U_f$ that maps state $|x\rangle|0\rangle |0\rangle$ to $|x\rangle |g(x)\rangle |f(x)\rangle$, where $|g(x)\rangle$ is the state of the working space at the end of the computation. Moreover, $|x\rangle |g(x)\rangle |f(x)\rangle$ is a pure state (not a superposition). You can now add an extra register $|0\rangle$ in which the result $f(x)$ of the computation is CNOT at the end, i.e. we get $|x\rangle |g(x)\rangle |f(x)\rangle |f(x)\rangle$. Finally, apply $U_f^{-1}$ to the first three registers of this state so as to obtain $|x\rangle |0\rangle |0\rangle |f(x)\rangle$. The working memory has been cleaned.
I'm fine with this procedure. However, it seems to me that it works because $|x\rangle |g(x)\rangle |f(x)\rangle$ is a pure state. It would also be ok for a superposition of pure states that all have the same register $|f(x)\rangle$ at the end (before we CNOT). But it doestn't work in the other cases. Consider for instance this circuit:
After the first Hadamard has been applied, $x_1$ contains the result of the computation (it is the same as $|f(x)\rangle$ previously, but now it is a superposition). It is CNOT with the extra register $x_2$ and then another Hadamard is applied. The final state is $(|00\rangle+|01\rangle+|10\rangle-|11\rangle)/2$. Obviously, the first qubit has not been restored to its initial state.
My first question is: am I right when I say that the uncomputation trick only works when the register that is copied is not in superposition? My second question is about the following algorithm taken from this paper from Scott Aaronson:
$\mathcal{Q}_*$ is a quantum circuit that takes some witness $|z\rangle$ as input, and outputs $0$ or $1$. It can be thought as a circuit for QMA, for which the real input $x$ has been fixed and we want to know if there exists a witness that is accepted with high probability (the idea of this paper is to only try with pure states witnesses $|z\rangle$). In this context, I don't understand why the "uncompute garbage" step restores the memory. I think that the register containing the output $b$ is CNOT in an extra register (which will be measured later), but since it's usually a superposition of $|0\rangle$ and $|1\rangle$ I don't understand why the uncomputation would work.