0

I want to compute the rotation distance between two $\mathrm{SU}(2)$ matrices. I tried to adapt this and I thought it was working, but then I discovered that numerical roundoff error can cause two matrices which should be approximately 0 degrees apart to be measured as 180 degrees apart.

import numpy as np
import numpy.linalg as la

phase = la.det(a) ** (-1.0 / 2.0)
a = phase * a
phase = la.det(b) ** (-1.0 / 2.0)
b = phase * b

ab = np.matmul(np.transpose(u1.conj()), b)
angle = np.rad2deg(np.absolute(np.arccos((np.trace(ab) - 0) / 2)))

While it looks like I could reimplement this using quaternions (I haven't tried yet), I'm wondering if this approach is mostly correct but has a bug or if this will never work. The issue seems to be something like this la.det(a) ** (-1.0 / 2.0) is producing different roots of unity or something like that. I don't have a test program to distribute, but I could prepare one if that would help.

amos
  • 113
  • The blog post you linked to is about rotations in $SO(3,\mathbb R)$, while you are talking about unitary matrices in $SU(2)$. How do you define distance in your setting? E.g. what is the distance between the identity matrix and $\operatorname{diag}(w,\bar w)$ for some complex number $w$ of unit modulus? – user1551 Jun 09 '20 at 12:58
  • I don't have a strict requirement for how distance is defined, but I do like using something that can be interpreted as an angle. Global phases in the matrices should be ignored. If it helps, the specific context here is single qubit gates. So, for your question, Z and S gates should have a distance of pi/2 since they are both rotations around the z axis. – amos Jun 09 '20 at 13:19
  • Probably much more complicated that what you're looking for, but some papers from H.E. Brandt use a particular notion of "distance" as a heuristic for minimizing the number of gates used in a quantum circuit. – Ben Grossmann Jun 09 '20 at 13:50
  • I suspect that your approach would work better if you compute the product and then fix the phase rather than dealing with the phases of $a,b$ separately. – Ben Grossmann Jun 09 '20 at 14:03
  • I think that in adapting the linked method, you will necessarily find some kind of ambiguity in $SU(2)$ that does not come up in $SO(3)$ because $SU(2)$ contains two copies of $SO(3)$ in some sense. – Ben Grossmann Jun 09 '20 at 14:11

1 Answers1

0

In short, I recommend two changes:

  1. adjust the phase of the product ab rather than handling a and b separately,
  2. 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$).

Ben Grossmann
  • 225,327
  • So your distance is not really a metric in the usual sense, that it has $d(I, cI) = 0$. – Arctic Char Jun 09 '20 at 15:05
  • Why don't we just do something like $\sqrt{\theta_1^2 + \theta_2^2}$? – Arctic Char Jun 09 '20 at 15:07
  • @ArcticChar It's a metric on the quotient space $SU(2)/\Bbb C$, i.e. over the equivalence classes of matrices "up to a global phase". I assume this is what the asker is looking for because of the code presented and because of the comment "global phases in the matrices should be ignored". – Ben Grossmann Jun 09 '20 at 15:38
  • @ArcticChar I assumed that we specifically want a function that can be computed without explicitly finding the eigenvalues. – Ben Grossmann Jun 09 '20 at 15:38
  • adjusting the phase of ab (instead of a and b individually) was sufficient to pass all my unit tests. it didn't occur to me to try that because I assumed that that a and b needed to be mutually compatible in some way before comparing them. – amos Jun 09 '20 at 15:59
  • It seems that you are working on $U(2)$ instead of $SU(2)$. Indeed $\det(A^*B)=1$ for all $A, B\in SU(2)$. – Arctic Char Jun 09 '20 at 16:24
  • @ArcticChar It's $SU(2)$ in the sense that ${\pm 1} \times U(2)/\Bbb C \cong SU(2)$, but keep in mind that in the context of quantum gates it is common to give a representive whose determinant is not necessarily $1$. For instance, the "phase" gate is given as $\operatorname{diag}(1,i)$, but the corresponding determinant-1 matrices are $\pm \operatorname{diag}(e^{-i\pi/4},e^{i\pi/4})$. – Ben Grossmann Jun 09 '20 at 16:43
  • @ArcticChar By the way my earlier comment should have said $U(2)/\Bbb C$. – Ben Grossmann Jun 09 '20 at 16:45
  • @amos I imagined as much. I'm glad that this works for you – Ben Grossmann Jun 09 '20 at 16:49
  • Okay, you better make that clear in your answer that you are making this identification. And we do not have $U(2)/\mathbb C \cong SU(2)$. The dimension does not match. It should be $U(2) /U(1) \cong SU(2)$. – Arctic Char Jun 09 '20 at 17:06
  • @ArcticChar First of all, what is the difference between $U(1)$ and $\Bbb C$? Second, what I intended was $({\pm 1} \times (U(2))/\Bbb C) \cong SU(2)$; perhaps the parentheses clarify what I meant. Finally, my answer as written addresses the needs of the asker, so I see no need to change it. – Ben Grossmann Jun 09 '20 at 17:09
  • Um? $U(1)$ is of real dimension one and $\mathbb C$ is of real dimension two. Whether as group or as topological space they are completely different. – Arctic Char Jun 09 '20 at 17:10
  • @ArcticChar Ah, I was indeed thinking of $U(1)$, thanks for the correction. Somehow it didn't occur to me that you might multiply by a number that has magnitude not equal to $1$. – Ben Grossmann Jun 09 '20 at 18:46
  • actually, I think there was a mistake in my implementation resulted in false positives, and I needed to do the abs of the argument to arccos. – amos Jun 10 '20 at 12:54
  • Just to clear up a minor confusion about the metric given. Is it square root of determinant of $A^{}B$ or we first compute square root of matrix product $A^{}B$, and then compute its determinant? I mean do we have $\sqrt(det (A^{}B))$ or $det(\sqrt(A^{}B) )$ ? – madeel Oct 30 '22 at 15:35
  • @madeel it’s the square root of the determinant – Ben Grossmann Oct 30 '22 at 18:02