Finding the quaternion that rotates unit vectors $\mathbf{a}$ to $\mathbf{b}$ is an operation I’m using over-and-over on this blog and it’s really silly that I never explictly described it from standard vector viewpoint. Just spewing out the standard geometeric defs of the cross and dot products:

where $\lVert \ . \rVert~ $ denotes magnitude and since we’re limited ourselves to unit vectors reduces to:

with $\mathbf{n}$ the unit vector orthogonal to the plane that spans $\mathbf{a}$ and $\mathbf{b}$ with right-handed orientation from $\mathbf{a}$ to $\mathbf{b}$. Now if think of $\mathbf{n}$ as the axis of rotation and $\theta$ as the angle in an axis-angle representation then the rotation is the minimum magnitude angle that performs this operation (all the others can be described as a post composition of a rotation about $\mathbf{b}$).

To create a quaternion we can simply look-up the often cited axis-angle to quaternion equation:

If it wasn’t for that pesky “divide the angle by two” bit we’d be done. Luckily we can just apply trig half-angle identities to make everything fit:

The quaternion equivalent of computing the cross and dot products and shoving the result into a quaternion can be performed by:

Since conjugation negates the angle and the product sums the resulting angle is: $\func{\text{angle}}{\mathbf{b}} - \func{\text{angle}}{\mathbf{a}}$