I apologize in advance for any mistakes as I am new to this field and come from a programming, rather than mathematical/physical background.

I am looking for a way to decompose a given operator $U$ into $ZYZ$ rotations. After searching on the internet, I wrote this Julia function to perform this operation (V[1, 1] represents $V_{11}$, etc.):

decompose(U::Matrix{ComplexF64}) = begin
    α = atan(imag(det(U)),real(det(U)))/2
    V = exp(-im*α)*U
    θ₁ = magnitude(V[1, 1])≥magnitude(V[1, 2]) ? 2*acos(magnitude(V[1, 1])) : 2*asin(magnitude(V[1, 2]))
    if cos(θ₁/2) == 0
        θ₀ = atan(imag(V[2, 1]/sin(θ₁/2)), real(mag(V[2, 1]/sin(θ₁/2))))
        θ₀ = -θ₂
    elseif sin(θ₁/2) == 0
        θ₀ = atan(imag(V[2, 2]/cos(θ₁/2)), real(V[2, 2]/cos(θ₁/2)))
        θ₂ = θ₀
        θ₀ = atan(imag(V[2, 2]/cos(θ₁/2)), real(V[2, 2]/cos(θ₁/2)))+atan(imag(V[2, 1]/sin(θ₁/2)), real(V[2, 1]/sin(θ₁/2)))
        θ₂ = 2*atan(imag(V[2, 2]/cos(θ₁/2)), real(V[2, 2]/cos(θ₁/2)))-θ₀
    return α, θ₀, θ₁, θ₂

I then plug these values back into $e^{i*\alpha}*R_z(\theta_0)*R_y(\theta_1)*R_z(\theta_2)$, to 'recompose' the gate and check if the gate decomposed correctly. Here is the compose function for reference:

R_x(θ) = [cos(θ/2) -im*sin(θ/2); -im*sin(θ/2) cos(θ/2)]
R_y(θ) = [cos(θ/2) -sin(θ/2); sin(θ/2) cos(θ/2)]
R_z(θ) = [exp(-im*θ/2) 0; 0 exp(im*θ/2)]
compose(α, θ₀, θ₁, θ₂) = exp(im*α)*R_z(θ₀)*R_y(θ₁)*R_z(θ₂)

This seems to work for the Hadamard, Pauli-X, and Pauli-Z gates when I test it, but when I test it for S, T, and Pauli-Y gates, it does not seem to work.

For S, I expect $\begin{bmatrix}1 & 0 \\ 0 & i\end{bmatrix}$, but I receive $\begin{bmatrix}1 & 0 \\ 0 & 1\end{bmatrix}$.

For T, I expect $\begin{bmatrix}1 & 0 \\ 0 & \frac{\sqrt{2}}{2}+\frac{\sqrt{2}}{2}i\end{bmatrix}$, but I receive $\begin{bmatrix}1 & 0 \\ 0 & \sqrt{2}\end{bmatrix}$.

For Pauli-Y, I expect $\begin{bmatrix}0 & -i \\ i & 0\end{bmatrix}$, but I receive $\begin{bmatrix}0 & -1 \\ 1 & 0\end{bmatrix}$.

It seems that the 'recomposed' gate has the values of the original gate written as $\Re(z)+\Im(z)$ instead of $\Re(z)+\Im(z)i$. Does this make any difference functionally or are the gates still equivalent? If there is a difference, is it possible to decompose the original gate and then compose it once again while retaining the complex values?


1 Answer 1


The matrices should coincide when you check them. There is a mistake in the decompose function.

In the first if clause you define θ₀ twice, second time by using uninitialized θ₂.

BTW, instead of atan you could use the function angle of a complex number. And magnitude is just abs or norm from the LinearAlgebra package.

  • $\begingroup$ I fixed the error in the first if clause but it has no effect on the output for the T, S, & Y gates. Do you have any idea where the specific issue could be in the decomposition function? I rechecked my work and it seems to be correct. $\endgroup$ Commented Jul 31, 2022 at 16:43
  • $\begingroup$ Are you sure you wrote θ₂ = atan(imag(V[2, 1]/sin(θ₁/2)), real(mag(V[2, 1]/sin(θ₁/2)))); θ₀ = -θ₂? It fixes everything for me. $\endgroup$
    – Danylo Y
    Commented Jul 31, 2022 at 17:38
  • $\begingroup$ Turns out I had a mistake in another processing part of my code. Thank you for your help! $\endgroup$ Commented Aug 1, 2022 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.