Solution (source code)

= Solution

Let $\Pi_g$ project onto the good-output subspace and let $|g\rangle=|\Psi_g\rangle$, $|b\rangle=|\Psi_b\rangle$. They are orthonormal because their supporting <computational basis> strings are disjoint. The verification <unitary operator> is $V=I-2\Pi_g$, so $V|g\rangle=-|g\rangle$ and $V|b\rangle=|b\rangle$.

Also $V_0=I-2|0\rangle\langle0|$. Since $A|0\rangle=|\Psi\rangle$, conjugating gives $AV_0A^\dagger=I-2|\Psi\rangle\langle\Psi|$. Thus
$$
Q=(2|\Psi\rangle\langle\Psi|-I)V.
$$
Both reflections preserve the span of $|g\rangle$ and $|b\rangle$: $V$ is diagonal there, and the other reflection only adds a multiple of $|\Psi\rangle$, already in that span. Direct substitution of $|\Psi\rangle=\sqrt p\,|g\rangle+\sqrt{1-p}\,|b\rangle$ yields
$$
\boxed{Q|g\rangle=(1-2p)|g\rangle-2\sqrt{p(1-p)}|b\rangle,\qquad Q|b\rangle=2\sqrt{p(1-p)}|g\rangle+(1-2p)|b\rangle.}
$$
The minus sign in the first image follows from applying $V$ before the state reflection. In the ordered basis $(|b\rangle,|g\rangle)$ the <matrix> is
$$
Q=\begin{pmatrix}1-2p&-2\sqrt{p(1-p)}\\2\sqrt{p(1-p)}&1-2p\end{pmatrix}.
$$
This exhibits the invariant plane without assumptions about the action of $A$ on the other input basis vectors.