Skip to content

Commit

Permalink
Make sure XYX angles are in (-pi, pi).
Browse files Browse the repository at this point in the history
  • Loading branch information
gribeill committed Apr 14, 2020
1 parent 83fba4e commit 1d07267
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions QGL/Euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ def tracedist(A, B, tol=np.sqrt(_eps)):
#### END FUNCTIONS COPIED FROM PYGSTI

def is_close(A, B, tol=np.sqrt(_eps)):
"""Check if two matrices are close in the sense of trace distance.
"""Check if two matrices are close in the sense of trace distance.
"""
if tracedist(A, B) < tol:
return True
return True
else:
A[np.abs(A) < tol] = 0.0
B[np.abs(B) < tol] = 0.0
A /= np.exp(1j*np.angle(A[0,0]))
B /= np.exp(1j*np.angle(B[0,0]))
return tracedist(A, B) < tol
return ((tracedist(A, B) < tol) or (tracedist(A, -1.0*B) < tol))

def haar_unitary(d):
"""Generate a Haar-random unitary matrix of dimension d.
Expand Down Expand Up @@ -128,6 +130,13 @@ def zyz_angles(U):
λ = (a - b) * 0.5
return (ϕ, θ, λ)

def _mod_2pi(angle):
if angle > np.pi:
angle -= 2*np.pi
if angle < -np.pi:
angle += 2*np.pi
return angle

def xyx_angles(U):
"""Euler angles for a unitary matrix U in the sequence X-Y-X.
Note that angles are returned in matrix multiplication, not circuit order.
Expand All @@ -136,7 +145,7 @@ def xyx_angles(U):
"""
H = np.array([[1., 1.], [1., -1.]], dtype=np.complex128)/np.sqrt(2)
ϕ, θ, λ = zyz_angles(H@U@H)
return (ϕ, -1.0*θ, λ)
return (_mod_2pi(ϕ), _mod_2pi(-1.0*θ), _mod_2pi(λ))

def diatomic_angles(U):
ϕ, θ, λ = zyz_angles(U)
Expand Down

0 comments on commit 1d07267

Please sign in to comment.