Solution (source code)

= Solution

Write $|\psi\rangle=\alpha|0\rangle+\beta|1\rangle$ and $t=e^{i\theta}$. Before measurement, the three <controlled-NOT gates> map a computational-basis component $|x,y,0\rangle$ to
$$
|x,y\mathbin\oplus x,y\mathbin\oplus x\rangle.
$$
The measured bit is therefore $b=y\mathbin\oplus x$. For $b=0$, the unnormalized state of the first qubit is
$$
\frac1{\sqrt2}(\alpha|0\rangle+t\beta|1\rangle)
=\frac1{\sqrt2}P(\theta)|\psi\rangle.
$$
For $b=1$, it is
$$
\frac1{\sqrt2}(t\alpha|0\rangle+\beta|1\rangle)
=\frac t{\sqrt2}P(-\theta)|\psi\rangle.
$$
Each branch has probability $1/2$; after normalization and removal of the irrelevant global phase $t$, the two outputs are
$$
\boxed{P(\theta)|\psi\rangle\quad\text{and}\quad
P(-\theta)|\psi\rangle}
$$
with equal probability. This is <probabilistic phase-gate injection>.