Solution (source code)

= Solution

Now $|a\rangle=|\psi\rangle$. Suppose $0<m<N$, and normalize the <orthogonal> good and bad components:
$$
|g\rangle=|\psi_1\rangle/\sin\chi,\qquad
|b\rangle=|\psi_0\rangle/\cos\chi,\qquad
|\psi\rangle=\sin\chi|g\rangle+\cos\chi|b\rangle.
$$
Their disjoint computational supports make $(|g\rangle,|b\rangle)$ an orthonormal basis. For $s=\sin\chi,c=\cos\chi$, the <two-dimensional matrix for phase amplitude amplification> is
$$
\boxed{Q=-\begin{pmatrix}
t(c^2+ps^2)&(p-1)sc\\
t(p-1)sc&s^2+pc^2
\end{pmatrix}_{(g,b)}.}
$$
To obtain its first column, the marked phase first multiplies $|g\rangle$ by $t$, then $I+(p-1)|\psi\rangle\langle\psi|$ adds its projection onto $|\psi\rangle$; the second column is obtained similarly without $t$. This also proves unitarity: the rank-one phase has <eigenvalues> $p,1$ on this plane, and the marked phase has <eigenvalues> $t,1$, all of unit modulus. Their negative product is unitary for any real phases.

Using the unnormalized pair $(|\psi_1\rangle,|\psi_0\rangle)$ instead would produce a differently scaled matrix, which is not in general unitary in the ordinary Euclidean metric. At $m=0$ or $m=N$ one component vanishes and the relevant search space is one-dimensional.