In short, I recommend two changes:
- adjust the phase of the product
ab
rather than handling a
and b
separately,
- take the absolute value of the argument of
arccos
.
Also, we can make things slightly faster by noting that $\det(A^*B)$ has magnitude $1$, which means that $\det(A^*B)^{-1/2} = \overline{\det(A^*B)^{1/2}}$, where $\bar z$ dentoes the complex conjugate of $z$.
Here are the distances from $I$ for some common gates, as measured by the metric defined below:
- $X,Y,Z,H$: $\pi/2$
- $S: \pi/4$
Here's a derivation/interpretation of the resulting "distance" (metric over $U(2)/U(1)$).
Let $A,B$ denote our unitary matrices, and let $A^*$ denote the conjugate transpose of $A$. If we consider $A^*B$ to be our "difference rotation", it suffices to define the distance of $A^*B$ from the identity matrix $I$.
Note that $A^*B$ has eigenvalues $e^{i\theta_1},e^{i \theta_2}$ for some angles $\theta_1,\theta_2$, and we have $\theta_1 = \theta_2$ if and only if $A^*B$ is a multiple of the identity matrix. With that in mind, $|\theta_1 - \theta_2|$ gives us an angle corresponding to the "distance" of $A^*B$ from the identity. We further stipulate for this derivation that $\theta_1, \theta_2$ are such that $|\theta_1 - \theta_2| < 180^\circ$.
If we multiply $A^*B$ by one of the values of $\det(A^*B)^{-1/2}$ to get $M = \frac{A^*B}{\det(A^*B)^{1/2}}$, we shift these angles to either $0^\circ \pm \theta$ or $180^\circ \pm \theta$, where $\theta = \frac{|\theta_1 - \theta_2|}{2}$. In the first case, the trace of $M$ is positive. In the second, the trace is negative. In either case, the trace of $M$ is real.
In the first case, we find that
$$
\operatorname{tr}(M) = e^{i \theta} + e^{-i \theta} =
2 \cos \theta = 2 \cos \frac{|\theta_1 - \theta_2|}{2}.
$$
In this case, we could take our distance to be $\arccos(\frac 12\operatorname{tr}(M)) = \frac 12 |\theta_1 - \theta_2|$.
In the second case, we find that
$$
\operatorname{tr}(M) = e^{i(\pi +\theta)} + e^{i (\pi - \theta)} =
-[e^{i \theta} + e^{-i \theta}] =
-2 \cos \theta = -2 \cos \frac{|\theta_1 - \theta_2|}{2}.
$$
In this case, we could take our distance to be $\arccos(-\frac 12 \operatorname{tr}(M)) = \frac 12 |\theta_1 - \theta_2|$.
Putting everything together, we find that
$$
d(A,B) = \arccos\left(\left|\frac{\operatorname{tr}(A^*B)}{2 \det(A^*B)^{1/2}}\right| \right)
$$
(where $|x|$ denotes the absolute value of $x$) gives us a measure of the difference between two elements of $SU(2)$, up to a constant phase, which can be interpreted as an angle (from $0^\circ$ to $90^\circ$).
Z
andS
gates should have a distance of pi/2 since they are both rotations around the z axis. – amos Jun 09 '20 at 13:19