Write the normalized input as , where and . Use a data register, an -qubit eigenvalue register, and a flag quantum ancilla. Begin with .
Apply exact quantum phase estimation to . Its controlled evolutions and inverse quantum Fourier transform produce
Apply the HHL controlled reciprocal rotation, with :
The function of is computed by quantum arithmetic; the rotation is a quantum variable rotation. On a zero eigenvalue label with no input amplitude, define any unitary action, for example no rotation.
Run the inverse phase-estimation circuit. Because each amplitude multiplier depends only on , the eigenvalue register is reset to on both flag branches. Measuring the flag and conditioning on outcome one leaves
The successful unnormalized branch is , giving the probability in the preceding part. The operations independent of are the Hadamard gates preparing the phase register, the inverse quantum Fourier transform, the reversible reciprocal/angle arithmetic, the controlled fixed-angle rotations, and the final flag measurement. Uncomputation removes all arithmetic workspace; the -dependent controlled evolutions and the -dependent state-preparation circuit supply the input-specific operations.
Use reversible quantum arithmetic on an ancillary work register. On each computational-basis branch, compute
where
Because the classical algorithms for and are efficient, they can be made reversible with polynomial overhead. Reversible division, square root, and inverse cosine to the retained binary precision likewise use gates under the question's precision convention. Uncompute the and work registers, leaving