% !Tex program = pdflatex
% Chapter 4 - Quantum circuit
\ifx\allfiles\undefined
\documentclass[en]{sol-man}
\begin{document}
\fi
\chapter{Quantum circuit}

\section{Quantum algorithm}

\section{Single qubit operations}

\begin{exe}
    In Exercise 2.11, which you should do now if you haven't already done it, you compute the eigenvectors of the Pauli matrices. Find the points on the Bloch sphere which corresponding to the normalized eigenvectors of the different Pauli matrices.
\end{exe}
\begin{sol}
    The normalized eigenvectors of $X$ are
    \begin{align}
        \frac{1}{\sqrt{2}}(\lvert 0\rangle+\lvert 1\rangle),\quad\frac{1}{\sqrt{2}}(\lvert 0\rangle-\lvert 1\rangle),
    \end{align}
    which corresponds to the intersection points of the Bloch sphere and the positive $x$ axis, and of the Bloch sphere and the negative $x$ axis.\\
    The normalized eigenvectors of $Y$ are
    \begin{align}
        \frac{1}{\sqrt{2}}(\lvert 0\rangle+i\lvert 1\rangle),\quad\frac{1}{\sqrt{2}}(\lvert 0\rangle-i\lvert 1\rangle),
    \end{align}
    which corresponds to the intersection points of the Bloch sphere and the positive $y$ axis, and of the Bloch sphere and the negative $y$ axis.\\
    The normalized eigenvectors of $Z$ are
    \begin{align}
        \lvert 0\rangle,\quad\lvert 1\rangle,
    \end{align}
    which corresponds to the intersection points of the Bloch sphere and the positive $z$ axis, and of the Bloch sphere and the negative $z$ axis.
\end{sol}

\begin{exe}
    Let $x$ be a real number and $A$ a matrix such that $A^2=I$. Show that
    \begin{align}
        \label{E4.2}
        \exp(iAx)=\cos(x)I+i\sin(x)A.
    \end{align}
    Use this result to verify Equation (4.4) through (4.6)\footnote{Equation 4.4: $R_x(\theta)\equiv e^{-i\theta X/2}=\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}X=\begin{bmatrix}
        \cos\frac{\theta}{2}&-i\sin\frac{\theta}{2}\\
        -i\sin\frac{\theta}{2}&\cos\frac{\theta}{2}
    \end{bmatrix}$;\\
    Equation 4.5: $R_y(\theta)\equiv e^{-i\theta Y/2}=\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}Y=\begin{bmatrix}
        \cos\frac{\theta}{2}&-\sin\frac{\theta}{2}\\
        \sin\frac{\theta}{2}&\cos\frac{\theta}{2}
    \end{bmatrix}$;\\
    Equation 4.6: $R_z(\theta)\equiv e^{-i\theta Z/2}=\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}Z=\begin{bmatrix}
        e^{-i\theta/2}&0\\
        0&e^{i\theta/2}
    \end{bmatrix}$.}.
\end{exe}
\begin{pf}
    Similar to Exercise 2.35 and Problem 2.1, the left side of Equation \eqref{E4.2} is
    \begin{align}
        \exp(iAx)=\sum_{n=0}^{\infty}\frac{1}{n!}(iAx)^n=\sum_{n=1}^{\infty}\frac{(-1)^n}{(2n)!}(Ax)^{2n}+\sum_{n=0}^{\infty}\frac{i(-1)^n}{(2n+1)!}(Ax)^{2n+1}.
    \end{align}
    Note that
    \begin{align}
        A^2=I,
    \end{align}
    so
    \begin{align}
        \exp(iAx)=\sum_{n=0}^{\infty}\frac{(-1)^n}{(2n)!}+\sum_{n=0}^{\infty}\frac{i(-1)^n}{(2n+1)!}Ax^{2n+1}=\cos(x)I+i\sin(x)A,
    \end{align}
    which equals the right side of Equation \eqref{E4.2}.
    Therefore, Equation \eqref{E4.2} holds.

    Using the above result,
    \begin{align}
        \notag R_x(\theta)=&e^{-i\theta X/2}=\cos\left(-\frac{\theta}{2}\right)I+i\sin\left(-\frac{\theta}{2}\right)X=\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}X=\cos\frac{\theta}{2}\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix}-i\sin\frac{\theta}{2}\begin{bmatrix}
            0&1\\
            1&0
        \end{bmatrix}\\
        =&\begin{bmatrix}
            \cos\frac{\theta}{2}&-i\sin\frac{\theta}{2}\\
            -i\sin\frac{\theta}{2}&\cos\frac{\theta}{2}
        \end{bmatrix},
    \end{align}
    which verifies Equation (4.4),
    \begin{align}
        \notag R_Y(\theta)=&e^{-i\theta Y/2}=\cos\left(-\frac{\theta}{2}\right)I+i\sin\left(-\frac{\theta}{2}\right)Y=\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}Y=\cos\frac{\theta}{2}\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix}-i\sin\frac{\theta}{2}\begin{bmatrix}
            0&-i\\
            i&0
        \end{bmatrix}\\
        =&\begin{bmatrix}
            \cos\frac{\theta}{2}&-\sin\frac{\theta}{2}\\
            \sin\frac{\theta}{2}&\cos\frac{\theta}{2}
        \end{bmatrix},
    \end{align}
    which verifies Equation (4.5), and
    \begin{align}
        \notag R_Z(\theta)=&e^{-i\theta Z/2}=\cos\left(-\frac{\theta}{2}\right)I+i\sin\left(-\frac{\theta}{2}\right)Z=\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}Z=\cos\frac{\theta}{2}\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix}-i\sin\frac{\theta}{2}\begin{bmatrix}
            1&0\\
            0&-1
        \end{bmatrix}\\
        =&\begin{bmatrix}
            \cos\frac{\theta}{2}-i\sin\frac{\theta}{2}&0\\
            0&\cos\frac{\theta}{2}+i\sin\frac{\theta}{2}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\theta/2}&0\\
            0&e^{i\theta/2}
        \end{bmatrix},
    \end{align}
    which verifies Equation (4.6).
\end{pf}

\begin{exe}
    Show that, up to a global phase, the $\pi/8$ gate satisfies $T=R_z(\pi/4)$.
\end{exe}
\begin{pf}
    Using the conclusion proved in the previous exercise,
    \begin{align}
        R_z(\pi/4)=\begin{bmatrix}
            e^{-i\pi/8}&0\\
            0&e^{i\pi/8}
        \end{bmatrix}=\exp(-i\pi/8)T,
    \end{align}
    so the $\pi/8$ gate satisfies $T=R_z(\pi/4)$ up to a global phase.
\end{pf}

\begin{exe}
    Express the Hadamard gate $H$ as a product of $R_x$ and $R_z$ rotations and $e^{i\varphi}$ for some $\varphi$.
\end{exe}
\begin{pf}
    Since
    \begin{align}
        \notag e^{i\pi/2}R_z\left(\frac{\pi}{2}\right)R_x\left(\frac{\pi}{2}\right)R_z\left(\frac{\pi}{2}\right)=&e^{i\pi/2}\begin{bmatrix}
            e^{-i\pi/4}&0\\
            0&e^{i\pi/4}
        \end{bmatrix}\begin{bmatrix}
            \cos\frac{\pi}{4}&-i\sin\frac{\pi}{4}\\
            -i\sin\frac{\pi}{4}&\cos\frac{\pi}{4}
        \end{bmatrix}\begin{bmatrix}
            e^{-i\pi/4}&0\\
            0&e^{i\pi/4}
        \end{bmatrix}\\
        \notag=&e^{i\pi/2}\begin{bmatrix}
            e^{-i\pi/4}\cos\frac{\pi}{4}&-ie^{-i\pi/4}\sin\frac{\pi}{4}\\
            -ie^{i\pi/4}\sin\frac{\pi}{4}&e^{i\pi/4}\cos\frac{\pi}{4}
        \end{bmatrix}\begin{bmatrix}
            e^{-i\pi/4}&0\\
            0&e^{i\pi/4}
        \end{bmatrix}\\
        \notag=&e^{i\pi/2}\begin{bmatrix}
            e^{-i\pi/2}\cos\frac{\pi}{4}&-i\sin\frac{\pi}{4}\\
            -i\sin\frac{\pi}{4}&e^{i\pi/2}\cos\frac{\pi}{4}
        \end{bmatrix}=\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}=H,
    \end{align}
    the Hadamard gate $H$ can be expressed as a product of $R_x$ and $R_z$ rotations and $e^{i\varphi}$.
\end{pf}

\begin{exe}
    Prove that $(\hat{n}\cdot\vec{\sigma})^2=I$, and use this to verify Equation (4.8)\footnote{$R_{\hat{n}}(\theta)\equiv\exp(-i\theta\hat{n}\cdot\vec{\sigma}/2)=\cos\left(\frac{\theta}{2}\right)I-i\sin\left(\frac{\theta}{2}\right)(n_xX+n_yY+n_zZ)$.}.
\end{exe}
\begin{pf}
    Similar to Exercise 2.35,
    \begin{gather}
        \hat{n}\cdot\vec{\sigma}=n_x\sigma_1+n_y\sigma_2+n_z\sigma_3=n_x\begin{bmatrix}
            0&1\\
            1&0
        \end{bmatrix}+n_y\begin{bmatrix}
            0&-i\\
            i&0
        \end{bmatrix}+n_z\begin{bmatrix}
            1&0\\
            0&-1
        \end{bmatrix}=\begin{bmatrix}
            n_z&n_x-in_y\\
            n_x+in_y&-n_z
        \end{bmatrix},\\
        \Longrightarrow(\hat{n}\cdot\vec{\sigma})^2=\begin{bmatrix}
            n_z&n_x-in_y\\
            n_x+in_y&-n_z
        \end{bmatrix}\begin{bmatrix}
            n_z&n_x-in_y\\
            n_x+in_y&-n_z
        \end{bmatrix}=\begin{bmatrix}
            n_x^2+n_y^2+n_z^2&0\\
            0&n_x^2+n_y^2+n_z^2
        \end{bmatrix}=\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix}=I.
    \end{gather}

    Using the above result,
    \begin{align}
        \notag R_{\hat{n}}(\theta)=&\exp(-i\theta\hat{n}\cdot\vec{\sigma}/2)=\sum_{k=0}^{\infty}\frac{1}{k!}(-i\theta\hat{n}\cdot\vec{\sigma}/2)=\sum_{k=0}^{\infty}\frac{1}{(2k)!}(-i\theta\hat{n}\cdot\vec{\sigma}/2)^{2k}+\sum_{k=0}^{\infty}\frac{1}{(2k+1)!}(-i\theta\hat{n}\cdot\vec{\sigma}/2)^{2k+1}\\
        \notag=&\sum_{k=0}^{\infty}\frac{(-1)^k}{(2k)!}\left(\frac{\theta}{2}\right)^{2k}+\sum_{k=0}^{\infty}\frac{-i(-1)^k}{(2k+1)!}\left(\frac{\theta}{2}\right)^{2k+1}(\hat{n}\cdot\vec{\sigma})=\cos\left(\frac{\theta}{2}\right)I-i\sin\left(\frac{\theta}{2}\right)(\hat{n}\cdot\vec{\sigma})\\
        =&\cos\left(\frac{\theta}{2}\right)I-i\sin\left(\frac{\theta}{2}\right)(n_xX+n_yY+n_zZ),
    \end{align}
    which verifies Equation (4.8).
\end{pf}

\begin{exe}[Bloch sphere interpretation of rotations]
    One reason why the $R_{\hat{n}}(\theta)$ operators are referred to as rotation operators is the following fact, which you are to prove. Suppose a single qubit has a state represented by Bloch vector $\vec{\lambda}$. Then the effect of the rotation $R_{\hat{n}}(\theta)$ on the state is to rotate it by an angle $\theta$ about the $\hat{n}$ axis of the Bloch sphere. This fact explains the rather mysterious looking factor of two in the definition of the rotation matrices.
\end{exe}
\begin{pf}
    \footnote{\url{http://www.vcpc.univie.ac.at/~ian/hotlist/qc/talks/bloch-sphere-rotations.pdf}}Suppose $\hat{n}$ forms angle $\theta$ with the $z$ axis and the projection of $\hat{n}$ onto the $xy$ plane forms angle $\varphi$ with the $x$ axis, i.e.
    \begin{align}
        \label{E4.8}
        \hat{n}=n_x\hat{x}+n_y\hat{y}+n_z\hat{z}=\sin\theta\cos\varphi\hat{x}+\sin\theta\sin\varphi\hat{y}+\cos\theta\hat{z}.
    \end{align}
    Rotating the Bloch vector $\vec{\lambda}$ about $\hat{n}$ axis by angle $\alpha$ is equivalent to the following operation sequence:
    \begin{itemize}
        \item[1.] Rotating about $z$ axis by angle $-\varphi$ so that $\hat{n}$ is rotated onto $xz$ plane;
        \item[2.] Rotating about $y$ axis by angle $-\theta$ so that $\hat{n}$ coincides with $z$ axis;
        \item[3.] Rotating about $z$ axis by angle $\alpha$ so $\vec{\lambda}$ is rotated about $\hat{n}$ axis by angle $\alpha$;
        \item[4.] Rotating about $y$ axis by angle $\theta$ to counteract operation step 2.;
        \item[5.] Rotating about $z$ axis by angle $\theta$ to counteract operation step 1. so that $\hat{n}$ is rotated back to its original position,
    \end{itemize}
    i.e.,
    \begin{align}
        \notag&R_z(\varphi)R_y(\theta)R_z(\alpha)R_y(-\theta)R_z(-\varphi)\\
        \notag=&\left[\cos\frac{\varphi}{2}I-i\sin\frac{\varphi}{2}Z\right]\left[\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}Y\right]\left[\cos\frac{\alpha}{2}I-i\sin\frac{\alpha}{2}Z\right]\left[\cos\frac{\theta}{2}I+i\sin\frac{\theta}{2}Y\right]\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\left[\cos\frac{\varphi}{2}I-i\sin\frac{\varphi}{2}Z\right]\left[\cos\frac{\theta}{2}\cos\frac{\alpha}{2}I-i\cos\frac{\theta}{2}\sin\frac{\alpha}{2}Z-i\sin\frac{\theta}{2}\cos\frac{\alpha}{2}Y-\sin\frac{\theta}{2}\sin\frac{\alpha}{2}YZ\right]\left[\cos\frac{\theta}{2}I+i\sin\frac{\theta}{2}Y\right]\\
        \notag&\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\left[\cos\frac{\varphi}{2}I-i\sin\frac{\varphi}{2}Z\right]\left[\cos\frac{\theta}{2}\cos\frac{\alpha}{2}I-i\cos\frac{\theta}{2}\sin\frac{\alpha}{2}Z-i\sin\frac{\theta}{2}\cos\frac{\alpha}{2}Y-i\sin\frac{\theta}{2}\sin\frac{\alpha}{2}X\right]\left[\cos\frac{\theta}{2}I+i\sin\frac{\theta}{2}Y\right]\\
        \notag&\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\left[\cos\frac{\varphi}{2}I-i\sin\frac{\varphi}{2}Z\right]\left[\cos^2\frac{\theta}{2}\cos\frac{\alpha}{2}I-i\cos^2\frac{\theta}{2}\sin\frac{\alpha}{2}Z-i\sin\frac{\theta}{2}\cos\frac{\theta}{2}\cos\frac{\alpha}{2}Y-i\sin\frac{\theta}{2}\cos\frac{\theta}{2}\sin\frac{\alpha}{2}X\right.\\
        &\left.+i\cos\frac{\theta}{2}\sin\frac{\theta}{2}\cos\frac{\alpha}{2}Y+\cos\frac{\theta}{2}\sin\frac{\theta}{2}\sin\frac{\alpha}{2}ZY+\sin^2\frac{\theta}{2}\cos\frac{\alpha}{2}Y^2+\sin^2\frac{\theta}{2}\sin\frac{\alpha}{2}XY\right]\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\left[\cos\frac{\varphi}{2}I-i\sin\frac{\varphi}{2}Z\right]\left[\cos^2\frac{\theta}{2}\cos\frac{\alpha}{2}I-i\cos^2\frac{\theta}{2}\sin\frac{\alpha}{2}Z-i\sin\frac{\theta}{2}\cos\frac{\theta}{2}\cos\frac{\alpha}{2}Y-i\sin\frac{\theta}{2}\cos\frac{\theta}{2}\sin\frac{\alpha}{2}X\right.\\
        \notag&\left.+i\cos\frac{\theta}{2}\sin\frac{\theta}{2}\cos\frac{\alpha}{2}Y-i\cos\frac{\theta}{2}\sin\frac{\theta}{2}\sin\frac{\alpha}{2}X+\sin^2\frac{\theta}{2}\cos\frac{\alpha}{2}I+i\sin^2\frac{\theta}{2}\sin\frac{\alpha}{2}Z\right]\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\left[\cos\frac{\varphi}{2}I-i\sin\frac{\varphi}{2}Z\right]\left[\cos\frac{\alpha}{2}I-i\cos\theta\sin\frac{\alpha}{2}Z-i\sin\theta\sin\frac{\alpha}{2}X\right]\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\left[\cos\frac{\varphi}{2}\cos\frac{\alpha}{2}I-i\cos\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}Z-i\cos\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}X-i\sin\frac{\varphi}{2}\cos\frac{\alpha}{2}Z-\sin\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}Z^2\right.\\
        \notag&\left.-\sin\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}ZX\right]\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\left[\cos\frac{\varphi}{2}\cos\frac{\alpha}{2}I-i\cos\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}Z-i\cos\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}X-i\sin\frac{\varphi}{2}\cos\frac{\alpha}{2}Z-\sin\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}I-i\sin\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}Y\right]\\
        \notag&\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\left[\left(\cos\frac{\varphi}{2}\cos\frac{\alpha}{2}-\sin\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}\right)I-i\left(\cos\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}+\sin\frac{\varphi}{2}\cos\frac{\alpha}{2}\right)Z-i\cos\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}X\right.\\
        \notag&\left.-i\sin\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}Y\right]\left[\cos\frac{\varphi}{2}I+i\sin\frac{\varphi}{2}Z\right]\\
        \notag=&\cos\frac{\varphi}{2}\left(\cos\frac{\varphi}{2}\cos\frac{\alpha}{2}-\sin\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}\right)I-i\cos\frac{\varphi}{2}\left(\cos\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}+\sin\frac{\varphi}{2}\cos\frac{\alpha}{2}\right)Z-i\cos^2\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}X\\
        \notag&-i\sin\frac{\varphi}{2}\cos\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}Y+i\sin\frac{\varphi}{2}\left(\cos\frac{\varphi}{2}\cos\frac{\alpha}{2}-\sin\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}\right)Z+\sin\frac{\varphi}{2}\left(\cos\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}+\sin\frac{\varphi}{2}\cos\frac{\alpha}{2}\right)Z^2\\
        \notag&+\cos\frac{\varphi}{2}\sin\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}XZ+\sin^2\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}YZ\\
        \notag=&\cos\frac{\varphi}{2}\left(\cos\frac{\varphi}{2}\cos\frac{\alpha}{2}-\sin\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}\right)I-i\cos\frac{\varphi}{2}\left(\cos\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}+\sin\frac{\varphi}{2}\cos\frac{\alpha}{2}\right)Z-i\cos^2\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}X\\
        \notag&-i\sin\frac{\varphi}{2}\cos\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}Y+i\sin\frac{\varphi}{2}\left(\cos\frac{\varphi}{2}\cos\frac{\alpha}{2}-\sin\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}\right)Z+\sin\frac{\varphi}{2}\left(\cos\frac{\varphi}{2}\cos\theta\sin\frac{\alpha}{2}+\sin\frac{\varphi}{2}\cos\frac{\alpha}{2}\right)I\\
        \notag&-i\cos\frac{\varphi}{2}\sin\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}Y+i\sin^2\frac{\varphi}{2}\sin\theta\sin\frac{\alpha}{2}X\\
        =&\cos\frac{\alpha}{2}I-i\sin\frac{\alpha}{2}\left(\cos\varphi\sin\theta X+\sin\varphi\sin\theta Y+\cos\theta Z\right)=\cos\frac{\alpha}{2}I-i(n_xX+n_yY+n_zZ),
    \end{align}
    which equals $R_{\hat{n}}(\alpha)$.
    Therefore, the effect of the rotation $R_{\hat{n}}(\theta)$ on the state is to rotate it by an angle $\theta$ about the $\hat{n}$ axis of the Bloch sphere.
\end{pf}

\begin{exe}
    Show that $XYX=-Y$ and use this to prove that $XR_y(\theta)X=R_y(-\theta)$.
\end{exe}
\begin{pf}
    \begin{align}
        XYX=iZX=i^2Y=-Y.
    \end{align}
    In this way,
    \begin{align}
        \notag XR_y(\theta)X=&X\left(\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}Y\right)X=\left(\cos\frac{\theta}{2}X-i\sin\frac{\theta}{2}XY\right)X=\left(\cos\frac{\theta}{2}X+\sin\frac{\theta}{2}Z\right)X=\cos\frac{\theta}{2}X^2+\sin\frac{\theta}{2}ZX\\
        =&\cos\frac{\theta}{2}I+i\sin\frac{\theta}{2}Y=R_y(-\theta).
    \end{align}
\end{pf}

\begin{exe}
    An arbitrary single qubit unitary operator can be written in the form
    \begin{align}
        U=\exp(i\alpha)R_{\hat{n}}(\theta)
    \end{align}
    for some real number $\alpha$ and $\theta$, and a real three-dimensional unit vector $\hat{n}$.
    \begin{itemize}
        \item[1.] Prove this fact.
        \item[2.] Find values for $\alpha$, $\theta$, and $\hat{n}$ giving the Hadamard gate $H$.
        \item[3.] Find value for $\alpha$, $\theta$, and $\hat{n}$ giving the phase gate
        \begin{align}
            S=\begin{bmatrix}
                1&0\\
                0&i
            \end{bmatrix}.
        \end{align}
    \end{itemize}
\end{exe}
\begin{sol}
    \begin{itemize}
        \item[1.] Suppose an arbitrary qubit unitary operator is
        \begin{align}
            U=\begin{bmatrix}
                a&b\\
                c&d
            \end{bmatrix}.
        \end{align}
        where $a$, $b$, $c$, and $d$ are complex numbers. Since $U$ is unitary,
        \begin{gather}
            UU^{\dagger}=\begin{bmatrix}
                a&b\\
                c&d
            \end{bmatrix}\begin{bmatrix}
                a^*&c^*\\
                b^*&d^*
            \end{bmatrix}=\begin{bmatrix}
                aa^*+bb^*&ac^*+bd^*\\
                a^*c+b^*d&cc^*+dd^*
            \end{bmatrix}=I=\begin{bmatrix}
                1&0\\
                0&1
            \end{bmatrix},\\
            \Longrightarrow aa^*+bb^*=cc^*+dd^*=1,\quad ac^*+bd^*=0,\\
            \Longrightarrow c=\mp b^*,\quad d=\pm a^*,\quad\abs{a}^2+\abs{b}^2=1.
        \end{gather}
        Hence $U$ can be written as
        \begin{align}
            U=\begin{bmatrix}
                a&b\\
                \mp b^*&\pm a^*
            \end{bmatrix}=\left\{\begin{array}{ll}
                \begin{bmatrix}
                    a&b\\
                    -b^*&a^*
                \end{bmatrix},&\text{for }\det(U)=1;\\
                \begin{bmatrix}
                    a&b\\
                    b^*&-a^*
                \end{bmatrix},&\text{for }\det(U)=-1,\\
            \end{array}\right.
        \end{align}
        where $\abs{a}^2+\abs{b}^2=1$.
        Without loss of generality, we can find real number $\alpha$, and $\theta$, and a real three-dimensional unit vector $\hat{n}=n_x\hat{x}+n_y\hat{y}+z\hat{z}$ so that
        \begin{align}
            a=&e^{i\alpha}\left(\cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}\right),\\
            b=&-e^{i\alpha}(in_x+n_y)\sin\frac{\theta}{2},\\
            e^{i\alpha}=&\left\{\begin{array}{ll}
                1,&\text{for }\det(U)=1;\\
                i,&\text{for }\det(U)=-1,\\
            \end{array}\right.
        \end{align}
        which satisfies
        \begin{align}
            \abs{a}^2+\abs{b}^2=\cos^2\frac{\theta}{2}+n_z^2\sin^2\frac{\theta}{2}+n_x^2\sin^2\frac{\theta}{2}+n_y^2\sin^2\frac{\theta}{2}=\cos^2\frac{\theta}{2}+\sin^2\frac{\theta}{2}=1,
        \end{align}
        for $n_x^2+n_y^2+n_z^2=1$.
        In this way, the single qubit unitary operator can be written in the form
        \begin{align}
            \notag U=&\begin{bmatrix}
                a&b\\
                \mp b^*&\pm a^*
            \end{bmatrix}=\begin{bmatrix}
                e^{i\alpha}\left(\cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}\right)&-e^{i\alpha}(in_x+n_y)\sin\frac{\theta}{2}\\
                \pm e^{-i\alpha}(-in_x+n_y)\sin\frac{\theta}{2}&\pm e^{-i\alpha}\left(\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}\right)
            \end{bmatrix}\\
            \notag=&\left\{\begin{array}{ll}
                \begin{bmatrix}
                    e^{i\alpha}\left(\cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}\right)&-e^{i\alpha}(in_x+n_y)\sin\frac{\theta}{2}\\
                    e^{-i\alpha}(-in_x+n_y)\sin\frac{\theta}{2}&e^{-i\alpha}\left(\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}\right)
                \end{bmatrix},&\text{for }\det(U)=1\\
                \begin{bmatrix}
                    e^{i\alpha}\left(\cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}\right)&-e^{i\alpha}(in_x+n_y)\sin\frac{\theta}{2}\\
                    -e^{-i\alpha}(-in_x+n_y)\sin\frac{\theta}{2}&-e^{-i\alpha}\left(\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}\right)
                \end{bmatrix},&\text{for }\det(U)=-1
            \end{array}\right.\\
            \notag=&\left\{\begin{array}{ll}
                \begin{bmatrix}
                    \cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}&-(in_x+n_y)\sin\frac{\theta}{2}\\
                    (-in_x+n_y)\sin\frac{\theta}{2}&\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}
                \end{bmatrix},&\text{for }\det(U)=1\\
                \begin{bmatrix}
                    i\left(\cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}\right)&-i(in_x+n_y)\sin\frac{\theta}{2}\\
                    i(-in_x+n_y)\sin\frac{\theta}{2}&i\left(\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}\right)
                \end{bmatrix},&\text{for }\det(U)=-1
            \end{array}\right.\\
            \notag=&\left\{\begin{array}{ll}
                \begin{bmatrix}
                    \cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}&-(in_x+n_y)\sin\frac{\theta}{2}\\
                    (-in_x+n_y)\sin\frac{\theta}{2}&\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}
                \end{bmatrix},&\text{for }\det(U)=1\\
                i\begin{bmatrix}
                    \cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}&-(in_x+n_y)\sin\frac{\theta}{2}\\
                    (-in_x+n_y)\sin\frac{\theta}{2}&\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}
                \end{bmatrix},&\text{for }\det(U)=-1
            \end{array}\right.\\
            \notag=&e^{i\alpha}\begin{bmatrix}
                \cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}&-(in_x+n_y)\sin\frac{\theta}{2}\\
                (-in_x+n_y)\sin\frac{\theta}{2}&\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}
            \end{bmatrix}\\
            \notag=&e^{i\alpha}\left\{\cos\theta\begin{bmatrix}
                1&0\\
                0&1
            \end{bmatrix}-i\sin\theta\left(n_x\begin{bmatrix}
                0&1\\
                1&0
            \end{bmatrix}+n_y\begin{bmatrix}
                0&-i\\
                i&0
            \end{bmatrix}+n_z\begin{bmatrix}
                1&0\\
                0&-1
            \end{bmatrix}\right)\right\}\\
            \notag=&e^{i\alpha}\left[\cos\frac{\theta}{2}I-i\sin\frac{\theta}{2}\left(n_xX+n_yY+n_zZ\right)\right]\\
            =&\exp(i\alpha)R_{\hat{n}}(\theta).
        \end{align}
        \item[2.] The Hadamard gate is
        \begin{align}
            H=\frac{1}{\sqrt{2}}\begin{bmatrix}
                1&1\\
                1&-1
            \end{bmatrix},
        \end{align}
        whose determinant is
        \begin{align}
            \det(H)=\abs{\begin{matrix}
                \frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}\\
                \frac{1}{\sqrt{2}}&-\frac{1}{\sqrt{2}}
            \end{matrix}}=-1,
        \end{align}
        so we choose
        \begin{gather}
            \begin{align}
                a=&e^{i\alpha}\left(\cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}\right)=\frac{1}{\sqrt{2}},\\
                b=&-e^{i\alpha}(in_x+n_y)\sin\frac{\theta}{2}=\frac{1}{\sqrt{2}},\\
                e^{i\alpha}=&i,
            \end{align}\\
            \Longrightarrow\alpha=\frac{\pi}{2},\quad\theta=\pi,\quad\hat{n}=n_x\hat{x}+n_y\hat{y}+n_z\hat{z}=\frac{1}{\sqrt{2}}\hat{x}+\frac{1}{\sqrt{2}}\hat{z}.
        \end{gather}
        \item[3.] For phase gate $S$,
        \begin{gather}
            \begin{align}
                e^{i\alpha}\left(\cos\frac{\theta}{2}-in_z\sin\frac{\theta}{2}\right)=&1,\\
                -e^{i\alpha}\left(in_x+n_y\right)\sin\frac{\theta}{2}=0,\\
                e^{-i\alpha}(-in_x+n_y)\sin\frac{\theta}{2}=0,\\
                e^{i\alpha}\left(\cos\frac{\theta}{2}+in_z\sin\frac{\theta}{2}\right)=i,
            \end{align}\\
            \Longrightarrow\alpha=\frac{\pi}{4},\quad\theta=\frac{\pi}{2},\quad\hat{n}=\hat{z}.
        \end{gather}
    \end{itemize}
\end{sol}

\begin{exe}
    Explain why any single qubit unitary operator may be written in the form (4.12)\footnote{$U=\begin{bmatrix}
        e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
        e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
    \end{bmatrix}$.}.
\end{exe}
\begin{sol}
    Suppose an arbitrary qubit unitary operator is
        \begin{align}
            U=\begin{bmatrix}
                a&b\\
                c&d
            \end{bmatrix}.
        \end{align}
        where $a$, $b$, $c$, and $d$ are complex numbers. Since $U$ is unitary,
        \begin{gather}
            UU^{\dagger}=\begin{bmatrix}
                a&b\\
                c&d
            \end{bmatrix}\begin{bmatrix}
                a^*&c^*\\
                b^*&d^*
            \end{bmatrix}=\begin{bmatrix}
                aa^*+bb^*&ac^*+bd^*\\
                a^*c+b^*d&cc^*+dd^*
            \end{bmatrix}=I=\begin{bmatrix}
                1&0\\
                0&1
            \end{bmatrix},\\
            \Longrightarrow aa^*+bb^*=cc^*+dd^*=1,\quad ac^*+bd^*=0.
        \end{gather}
        Since
        \begin{align}
            \notag&\begin{bmatrix}
                e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
                e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
            \end{bmatrix}\begin{bmatrix}
                e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
                e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
            \end{bmatrix}^{\dagger}\\
            \notag=&\begin{bmatrix}
                e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
                e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
            \end{bmatrix}\begin{bmatrix}
                e^{-i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&e^{-i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}\\
                -e^{-i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}&e^{-i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
            \end{bmatrix}\\
            \notag=&\begin{bmatrix}
                e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}e^{-i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}+e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}e^{-i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2},\\
                e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}e^{-i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}e^{-i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2};\\
                e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}e^{-i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}-e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}e^{-i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2},\\
                e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}e^{-i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}+e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}e^{-i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
            \end{bmatrix}\\
            =&\begin{bmatrix}
                1&0\\
                0&1
            \end{bmatrix},
        \end{align}
        the matrix $\begin{bmatrix}
            e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
            e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
        \end{bmatrix}$ is also unitary.
        For arbitrary $a$, $b$, $c$, and $d$ such that $U$ is unitary, let
        \begin{align}
            a=&e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2},\\
            b=&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2},\\
            c=&e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2},\\
            d=&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2},
        \end{align}
        i.e., set
        \begin{align}
            \alpha=&-\frac{i}{2}\ln(ad-bc),\\
            \beta=&\frac{i}{2}\ln\left(-\frac{ab}{cd}\right),\\
            \delta=&\frac{i}{2}\ln\left(-\frac{ac}{bd}\right),\\
            \gamma=&\arccos\abs{ad+bc}.
        \end{align}
        Then
        \begin{align}
            U=\begin{bmatrix}
                e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
                e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
            \end{bmatrix}.
        \end{align}
        Therefore, any single unitary operator may be written in the form (4.12).
\end{sol}

\begin{exe}[X-Y decomposition of rotations]
    Give a decomposition analogous to Theorem 4.1\footnote{\label{Thm-4.1-Z-Y-decomp-for-a-single-qubit}($Z-Y$ decomposition for single qubit) Suppose $U$ is a unitary operation on a single qubit. Then there exist real numbers $\alpha$, $\beta$, $\gamma$ and $\delta$ such that $U=e^{i\alpha}R_z(\beta)R_y(\gamma)R_z(\delta)$.} but using $R_x$ instead of $R_z$.
\end{exe}
\begin{sol}
    Suppose an arbitrary qubit unitary operator is
    \begin{align}
        U=\begin{bmatrix}
            a&b\\
            c&d
        \end{bmatrix},
    \end{align}
    where $a$, $b$, $c$, and $d$ are complex numbers. Since $U$ is unitary,
    \begin{gather}
        UU^{\dagger}=\begin{bmatrix}
            a&b\\
            c&d
        \end{bmatrix}\begin{bmatrix}
            a^*&c^*\\
            b^*&d^*
        \end{bmatrix}=\begin{bmatrix}
            aa^*+bb^*&ac^*+bd^*\\
            a^*c+b^*d&cc^*+dd^*
        \end{bmatrix}=I=\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix},\\
        \Longrightarrow aa^*+bb^*=cc^*+dd^*=1,\quad ac^*+bd^*=0.
    \end{gather}
    The $X-Y$ decomposition of rotation is
    \begin{align}
        \notag&e^{i\alpha}R_z(\beta)R_x(\gamma)R_z(\delta)=e^{i\alpha}\begin{bmatrix}
            \cos\frac{\beta}{2}&-i\sin\frac{\beta}{2}\\
            -i\sin\frac{\beta}{2}&\cos\frac{\beta}{2}
        \end{bmatrix}\begin{bmatrix}
            \cos\frac{\gamma}{2}&-\sin\frac{\gamma}{2}\\
            \sin\frac{\gamma}{2}&\cos\frac{\gamma}{2}
        \end{bmatrix}\begin{bmatrix}
            \cos\frac{\delta}{2}&-i\sin\frac{\delta}{2}\\
            -i\sin\frac{\delta}{2}&\cos\frac{\delta}{2}
        \end{bmatrix}\\
        \notag&=e^{i\alpha}\begin{bmatrix}
            \cos\frac{\beta}{2}\cos\frac{\gamma}{2}-i\sin\frac{\beta}{2}\sin\frac{\gamma}{2}&-\cos\frac{\beta}{2}\sin\frac{\gamma}{2}-i\sin\frac{\beta}{2}\cos\frac{\gamma}{2}\\
            -i\sin\frac{\beta}{2}\cos\frac{\gamma}{2}+\cos\frac{\beta}{2}\sin\frac{\gamma}{2}&i\sin\frac{\beta}{2}\sin\frac{\gamma}{2}+\cos\frac{\beta}{2}\cos\frac{\gamma}{2}
        \end{bmatrix}\begin{bmatrix}
            \cos\frac{\delta}{2}&-i\sin\frac{\delta}{2}\\
            -i\sin\frac{\delta}{2}&\cos\frac{\delta}{2}
        \end{bmatrix}\\
        \notag&=e^{i\alpha}\\
        \notag&\left[\begin{smallmatrix}
            \left(\cos\frac{\beta}{2}\cos\frac{\gamma}{2}-i\sin\frac{\beta}{2}\sin\frac{\gamma}{2}\right)\cos\frac{\delta}{2}-i\left(-\cos\frac{\beta}{2}\sin\frac{\gamma}{2}-i\sin\frac{\beta}{2}\cos\frac{\gamma}{2}\right)\sin\frac{\delta}{2}&-i\left(\cos\frac{\beta}{2}\cos\frac{\gamma}{2}-i\sin\frac{\beta}{2}\sin\frac{\gamma}{2}\right)\sin\frac{\delta}{2}+\left(-\cos\frac{\beta}{2}\sin\frac{\gamma}{2}-i\sin\frac{\beta}{2}\cos\frac{\gamma}{2}\right)\cos\frac{\delta}{2}\\
            \left(-i\sin\frac{\beta}{2}\cos\frac{\gamma}{2}+\cos\frac{\beta}{2}\sin\frac{\gamma}{2}\right)\cos\frac{\delta}{2}-i\left(i\sin\frac{\beta}{2}\sin\frac{\gamma}{2}+\cos\frac{\beta}{2}\cos\frac{\gamma}{2}\right)\sin\frac{\delta}{2}&-i\left(-i\sin\frac{\beta}{2}\cos\frac{\gamma}{2}+\cos\frac{\beta}{2}\sin\frac{\gamma}{2}\right)\sin\frac{\delta}{2}+\left(i\sin\frac{\beta}{2}\sin\frac{\gamma}{2}+\cos\frac{\beta}{2}\cos\frac{\gamma}{2}\right)\cos\frac{\delta}{2}
        \end{smallmatrix}\right]\\
        \notag&=e^{i\alpha}\\
        \notag&\left[\begin{smallmatrix}
            \cos\frac{\gamma}{2}\left(\cos\frac{\beta}{2}\cos\frac{\delta}{2}-\sin\frac{\beta}{2}\sin\frac{\delta}{2}\right)+i\sin\frac{\gamma}{2}\left(-\sin\frac{\beta}{2}\cos\frac{\delta}{2}+\cos\frac{\beta}{2}\sin\frac{\delta}{2}\right)&\sin\frac{\gamma}{2}\left(-\sin\frac{\beta}{2}\sin\frac{\delta}{2}-\cos\frac{\beta}{2}\cos\frac{\delta}{2}\right)+i\cos\frac{\gamma}{2}\left(-\cos\frac{\beta}{2}\sin\frac{\delta}{2}-\sin\frac{\beta}{2}\cos\frac{\delta}{2}\right)\\
            \sin\frac{\gamma}{2}\left(\cos\frac{\beta}{2}\cos\frac{\delta}{2}+\sin\frac{\beta}{2}\sin\frac{\delta}{2}\right)+i\cos\frac{\gamma}{2}\left(-\sin\frac{\beta}{2}\cos\frac{\delta}{2}-\cos\frac{\beta}{2}\sin\frac{\delta}{2}\right)&\cos\frac{\gamma}{2}\left(-\sin\frac{\beta}{2}\sin\frac{\delta}{2}+\cos\frac{\beta}{2}\cos\frac{\delta}{2}\right)+i\sin\frac{\gamma}{2}\left(-\cos\frac{\beta}{2}\sin\frac{\delta}{2}+\sin\frac{\beta}{2}\cos\frac{\delta}{2}\right)
        \end{smallmatrix}\right]\\
        \notag&=e^{i\alpha}\begin{bmatrix}
            \cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}&-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\\
            \sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}&\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}
        \end{bmatrix}\\
    \end{align}
    Since
    \begin{align}
        \notag&e^{i\alpha}R_z(\beta)R_x(\gamma)R_z(\delta)[e^{i\alpha}R_z(\beta)R_x(\gamma)R_z(\delta)]^{\dagger}\\
        \notag=&e^{i\alpha}\begin{bmatrix}
            \cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}&-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\\
            \sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}&\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}
        \end{bmatrix}\times\\
        \notag&e^{-i\alpha}\begin{bmatrix}
            \cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}&-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\\
            \sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}&\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}
        \end{bmatrix}^{\dagger}\\
        \notag=&\begin{bmatrix}
            \cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}&-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\\
            \sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}&\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}
        \end{bmatrix}\times\\
        \notag&\begin{bmatrix}
            \cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}&\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}+i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\\
            -\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}+i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}&\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}
        \end{bmatrix}\\
        \notag=&\left[\begin{smallmatrix}
            \left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right)\left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}\right)+\left(-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right)\left(-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}+i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right),\\
            \left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right)\left(\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}+i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right)+\left(-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right)\left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right);\\
            \left(\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right)\left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right)+\left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right)\left(-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}+i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right),\\
            \left(\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right)\left(\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}+i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right)+\left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right)\left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right)
        \end{smallmatrix}\right]\\
        \notag=&\left[\begin{smallmatrix}
            \cos^2\frac{\gamma}{2}\cos^2\frac{\beta+\delta}{2}+\sin^2\frac{\gamma}{2}\sin^2\frac{\beta-\delta}{2}+\sin^2\frac{\gamma}{2}\cos^2\frac{\beta-\delta}{2}+\cos^2\frac{\gamma}{2}\sin^2\frac{\beta+\delta}{2}&0\\
            0&\sin^2\frac{\gamma}{2}\cos^2\frac{\beta-\delta}{2}+\cos^2\frac{\gamma}{2}\sin^2\frac{\beta+\delta}{2}+\cos^2\frac{\gamma}{2}\cos^2\frac{\beta+\delta}{2}+\sin^2\frac{\gamma}{2}\sin^2\frac{\beta-\delta}{2}
        \end{smallmatrix}\right]\\
        =&\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix}=I.
    \end{align}
    the $X-Y$ decomposition of rotations is also unitary.
    For arbitrary $a$, $b$, $c$, and $d$ such that $U$ is unitary, let
    \begin{align}
        a=&e^{i\alpha}\left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}-i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right),\\
        b=&e^{i\alpha}\left(-\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right),\\
        c=&e^{i\alpha}\left(\sin\frac{\gamma}{2}\cos\frac{\beta-\delta}{2}-i\cos\frac{\gamma}{2}\sin\frac{\beta+\delta}{2}\right),\\
        d=&e^{i\alpha}\left(\cos\frac{\gamma}{2}\cos\frac{\beta+\delta}{2}+i\sin\frac{\gamma}{2}\sin\frac{\beta-\delta}{2}\right),
    \end{align}
    i.e., set
    \begin{align}
        \alpha=&-\frac{i}{2}\ln(ad-bc),\\
        \gamma=&\arccos\frac{a^2-b^2-c^2+b^2}{2(ad-bc)},\\
        \beta=&i\left(\arctan\frac{b+c}{a+d}-\arctan\frac{a-d}{b-c}\right),\\
        \delta=&i\left(\arctan\frac{b+c}{a+d}+\arctan\frac{a-d}{b-c}\right).
    \end{align}
    Then
    \begin{align}
        U=e^{i\alpha}R_z(\beta)R_x(\gamma)R_z(\delta),
    \end{align}
    which is a $X-Y$ decomposition of rotations analogous to Theorem 4.1.
\end{sol}

\begin{exe}
    Suppose $\hat{m}$ and $\hat{n}$ are non-parallel real unit vectors in three dimensions. Use Theorem 4.1 to show that an arbitrary single qubit unitary $U$ may be written
    \begin{align}
        U=e^{i\alpha}R_{\hat{n}}(\beta)R_{\hat{m}}(\gamma)R_{\hat{n}}(\delta),
    \end{align}
    for appropriate choices of $\alpha$, $\beta$, $\gamma$ and $\delta$.
\end{exe}
\begin{pf}
    There is something wrong with this Exercise. Imagine that $\hat{m}$ and $\hat{n}$ are almost parallel but not parallel. In this case, we can not rotate a Bloch vector from paralleling with $\hat{n}$ to anti-paralleling with $\hat{n}$ by first rotating the vector about $\hat{n}$ by some angle $\delta$, then about $\hat{m}$ by some angle $\gamma$, and finally about $\hat{n}$ by some angle $\beta$, with a phase $\alpha$ added. However, this rotation can be represented by unitary operator $U=X$. Therefore, some single qubit unitary $U$ may not be written
    \begin{align}
        U=e^{i\alpha}R_{\hat{n}}(\beta)R_{\hat{m}}(\gamma)R_{\hat{n}}(\delta),
    \end{align}
    with any choices of $\alpha$, $\beta$, $\gamma$ and $\delta$.
\end{pf}

\begin{exe}
    Give $A$, $B$, $C$, and $\alpha$ for the Hadamard gate.
\end{exe}
\begin{sol}
    The Hadamard gate can be written as
    \begin{align}
        H\equiv\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}=e^{i\alpha}R_z(\beta)R_y(\gamma)R_z(\delta)=\begin{bmatrix}
            e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
            e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
        \end{bmatrix},
    \end{align}
    i.e.,
    \begin{align}
        e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}=&\frac{1}{\sqrt{2}},\\
        -e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}=&\frac{1}{\sqrt{2}},\\
        e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}=&\frac{1}{\sqrt{2}},\\
        e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}=&-\frac{1}{\sqrt{2}},
    \end{align}
    so
    \begin{align}
        \alpha=&\frac{\pi}{2},\\
        \beta=&0,\\
        \delta=&\pi,\\
        \gamma=&\frac{\pi}{2}.
    \end{align}
    In this way, for the Hadamard gate,
    \begin{align}
        H=e^{i\alpha}AXBXC,
    \end{align}
    where
    \begin{align}
        \alpha=&\frac{\pi}{2},\\
        A=&R_z(\beta)R_y(\gamma/2)=\begin{bmatrix}
            e^{-i\beta/2}&0\\
            0&e^{i\beta/2}
        \end{bmatrix}\begin{bmatrix}
            \cos\frac{\gamma}{4}&-\sin\frac{\gamma}{4}\\
            \sin\frac{\gamma}{4}&\cos\frac{\gamma}{4}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\beta/2}\cos\frac{\gamma}{4}&-e^{-i\beta/2}\sin\frac{\gamma}{4}\\
            e^{i\beta/2}\sin\frac{\gamma}{4}&e^{i\beta/2}\cos\frac{\gamma}{4}
        \end{bmatrix}=\begin{bmatrix}
            \cos\frac{\pi}{8}&-\sin\frac{\pi}{8}\\
            \sin\frac{\pi}{8}&\cos\frac{\pi}{8}
        \end{bmatrix},\\
        \notag B=&R_y(-\gamma/2)R_z(-(\delta+\beta)/2)=\begin{bmatrix}
            \cos\left(-\frac{\gamma}{4}\right)&-\sin\left(-\frac{\gamma}{4}\right)\\
            \sin\left(-\frac{\gamma}{4}\right)&\cos\left(-\frac{\gamma}{4}\right)
        \end{bmatrix}\begin{bmatrix}
            e^{i(\delta+\beta)/4}&0\\
            0&e^{-i(\delta+\beta)/4}
        \end{bmatrix}=\begin{bmatrix}
            e^{i(\delta+\beta)/4}\cos\frac{\gamma}{4}&e^{-i(\delta+\beta)/4}\sin\frac{\gamma}{4}\\
            -e^{i(\delta+\beta)/4}\sin\frac{\gamma}{4}&e^{-i(\delta+\beta)/4}\cos\frac{\gamma}{4}
        \end{bmatrix}\\
        =&\begin{bmatrix}
            e^{i\pi/4}\cos\frac{\pi}{8}&e^{-i\pi/4}\sin\frac{\pi}{8}\\
            -e^{i\pi/4}\sin\frac{\pi}{8}&e^{-i\pi/4}\cos\frac{\pi}{8}
        \end{bmatrix},\\
        C=&R_z((\delta-\beta)/2)=\begin{bmatrix}
            e^{-i(\delta-\beta)/4}&0\\
            0&e^{i(\delta-\beta)/4}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\pi/4}&0\\
            0&e^{i\pi/4}
        \end{bmatrix}
    \end{align}
\end{sol}

\begin{exe}[Circuit identities]
    It is useful to be able to simplify circuit by inspection, using well-known identities. Prove the following three identities:
    \begin{align}
        HXH=Z;\quad HYH=-Y,\quad HZH=X.
    \end{align}
\end{exe}
\begin{pf}
    \begin{align}
        HXH=&\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}\begin{bmatrix}
            0&1\\
            1&0
        \end{bmatrix}\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}=\begin{bmatrix}
            1&0\\
            0&-1
        \end{bmatrix}=Z,\\
        HYH=&\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}\begin{bmatrix}
            0&-i\\
            i&0
        \end{bmatrix}\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}=\begin{bmatrix}
            0&i\\
            -i&0
        \end{bmatrix}=-Y,\\
        HZH=&\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}\begin{bmatrix}
            1&0\\
            0&-1
        \end{bmatrix}\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}=\begin{bmatrix}
            0&1\\
            1&0
        \end{bmatrix}=X.
    \end{align}
\end{pf}

\begin{exe}
    Use the previous exercise to show that $HTH=R_x(\pi/4)$, up to a global phase.
\end{exe}
\begin{pf}
    Using the conclusion obtained in Exercise 4.3 and the previous Exercise, we have
    \begin{align}
        \notag HTH=&\exp(i\pi/8)HR_z(\pi/4)H=\exp(i\pi/8)H\left(\cos\frac{\pi}{8}I-i\sin\frac{\pi}{8}Z\right)H=\exp(i\pi/8)\left(\cos\frac{\pi}{8}I-i\sin\frac{\pi}{8}X\right)\\
        =&\exp(i\pi/8)R_x(\pi/4).
    \end{align}

    Therefore, $HTH=R_x(\pi/4)$ up to a global phase.
\end{pf}

\begin{exe}[Composition of single qubit operations]
    The Bloch representation gives a nice way to visualize the effect of composing two rotations.
    \begin{itemize}
        \item[(1)] Prove that if a rotation through an angle $\beta_1$ about the axis $\hat{n}_1$ is followed by a rotation through an angle $\beta_2$ about an axis $\hat{n}_2$, then the overall rotation is through an angle $\beta_{12}$ about an axis $\hat{n}_{12}$ given by
        \begin{align}
            c_{12}=&c_1c_2-s_1s_2\hat{n}_1\cdot\hat{n}_2\\
            s_{12}\hat{n}_{12}=&s_1c_2\hat{n}_1+c_1s_2\hat{n}_2-s_1s_2\hat{n}_2\times\hat{n}_1,
        \end{align}
        where $c_i=\cos(\beta_i/2)$, $s_i=\sin(\beta_i/2)$, $c_{12}=\cos(\beta_{12}/2)$, and $s_{12}=\sin(\beta_{12}/2)$.
        \item[(2)] Show that if $\beta_1=\beta_2$ and $\hat{n}_1=\hat{z}$ these equations simplify to
        \begin{align}
            c_{12}=&c^2-s^2\hat{z}\cdot\hat{n}_2\\
            s_{12}\hat{n}_{12}=&sc(\hat{z}+\hat{n})-s^2\hat{n}_2\times\hat{z},
        \end{align}
        where $c=c_1$ and $s=s_1$.
    \end{itemize}
\end{exe}
\begin{pf}
    \begin{itemize}
        \item[(1)] A rotation through an angle $\beta_1$ about the axis $\hat{n}_1$ followed by a rotation through an angle $\beta_2$ about an axis $\hat{n}_2$ is given by
        \begin{align}
            \notag R_{\hat{n}_2}(\beta_2)R_{\hat{n}_1}(\beta_1)=&\left[c_2I-is_2(n_{2x}X+n_{2y}Y+n_{2z}Z)\right]\left[c_1I-is_1(n_{1x}X+n_{1y}Y+n_{1z}Z)\right]\\
            \notag=&c_2c_1I-ic_2s_1(n_{1x}X+n_{1y}Y+n_{1z}Z)-is_2c_1(n_{2x}X+n_{2y}Y+n_{2z}Z)\\
            \notag&-s_2s_1(n_{2x}X+n_{2y}Y+n_{2z}Z)(n_{1x}X+n_{1y}Y+n_{1z}Z)\\
            \notag=&c_2c_1I-ic_2s_1(n_{1x}X+n_{1y}Y+n_{1z}Z)-is_2c_1(n_{2x}X+n_{2y}Y+n_{2z}Z)\\
            \notag&-s_2s_1(n_{2x}n_{1x}XX+n_{2x}n_{1y}XY+n_{2x}n_{1z}XZ+n_{2y}n_{1x}YX+n_{2y}n_{1y}YY+n_{2y}n_{1z}YZ\\
            \notag&+n_{2z}n_{1x}ZX+n_{2z}n_{1y}ZY+n_{2z}n_{1z}ZZ)\\
            \notag=&c_2c_1I-ic_2s_1(n_{1x}X+n_{1y}Y+n_{1z}Z)-is_2c_1(n_{2x}X+n_{2y}Y+n_{2z}Z)\\
            \notag&-s_2s_1(n_{2x}n_{1x}I+in_{2x}n_{1y}Z-in_{2x}n_{1z}Y-in_{2y}n_{1x}Z+n_{2y}n_{1y}I+in_{2y}n_{1z}X\\
            \notag&+in_{2z}n_{1x}Y-in_{2z}n_{1y}X+n_{2z}n_{1z}I)\\
            \notag=&[c_2c_1-s_2s_1(n_{1x}n_{2x}+n_{2y}n_{1y}+n_{2z}n_{1z})]I-i[c_2s_1n_{1x}+s_2c_1n_{2x}+s_2s_1(n_{2y}n_{1z}-n_{2z}n_{1y})]X\\
            \notag&-i[c_2s_1n_{1y}+s_2c_1n_{2y}+s_2s_1(-n_{2x}n_{1z}+n_{2z}n_{1x})]Y\\
            &-i[c_2s_1n_{1z}+s_2c_1n_{2z}+s_2s_1(n_{2x}n_{1y}-n_{2y}n_1x)]Z.
        \end{align}
        The rotation through an angle $\beta_{12}$ about an axis $\hat{n}_{12}$ where
        \begin{align}
            c_{12}=&c_1c_2-s_1s_2\hat{n}_1\hat{n}_2,\\
            s_{12}\hat{n}_{12}=&s_1c_2\hat{n}_1+c_1s_2\hat{n}_2-s_1s_2\hat{n}_2\times\hat{n}_1,
        \end{align}
        i.e.,
        \begin{align}
            c_{12}=&c_1c_2-s_1s_2(n_{1x}n_{2x}+n_{1y}n_{2y}+n_{1z}n_{2z}),\\
            s_{12}n_{12x}=&s_1c_2n_{1x}+c_1s_2n_{2x}-s_1s_2(n_{2y}n_{1z}-n_{2z}n_{1y}),\\
            s_{12}n_{12y}=&s_1c_2n_{1y}+c_1s_2n_{2y}-s_1s_2(n_{2z}n_{1x}-n_{2x}n_{1z}),\\
            s_{12}n_{12z}=&s_1c_2n_{1z}+c_1s_2n_{2z}-s_1s_2(n_{2x}n_{1y}-n_{2y}n_{1x}),
        \end{align}
        is given by
        \begin{align}
            \notag R_{\hat{n}_{12}}(\beta_{12})=&c_{12}I-is_{12}(n_{12x}X+n_{12y}Y+n_{12z}Z)\\
            \notag=&[c_1c_2-s_1s_2(n_{1x}n_{2x}+n_{1y}n_{2y}+n_{1z}n_{2z})]I-i[s_1c_2n_{1x}+c_1s_2n_{2x}-s_1s_2(n_{2y}n_{1z}-n_{2z}n_{1y})]X\\
            &-i[s_1c_2n_{1y}+c_1s_2n_{2y}-s_1s_2(n_{2z}n_{1x}-n_{2x}n_{1z})]Y-i[s_1c_2n_{1z}+c_1s_2n_{2z}-s_1s_2(n_{2x}n_{1y}-n_{2y}n_{1x})]Z.
        \end{align}
        Therefore, if a rotation through an angle $\beta_1$ about the axis $\hat{n}_1$ is followed by a rotation through an angle $\beta_2$ about an axis $\hat{n}_2$, then the overall rotation is through an angle $\beta_{12}$ about an axis $\hat{n}_{12}$.
        \item[(b)] If $\beta_1=\beta_2$ and $\hat{n}_1=\hat{z}$, then $c=c_1=c_2$, $s=s_1=s_2$, $n_{1x}=n_{1y}=0$, $n_{1z}=1$, and these equations simplify to
        \begin{align}
            c_{12}=&c_1c_2-s_1s_2\hat{n}_1\hat{n}_2=c^2-s^2\hat{z}\cdot\hat{n}_2,\\
            s_{12}\hat{n}_{12}=&s_1c_2\hat{n}_1+c_1s_2\hat{n}_2-s_1s_2\hat{n}_2\times\hat{n}_1=sc(\hat{z}+\hat{n})-s^2\hat{n}_2\times\hat{z}.
        \end{align}
    \end{itemize}
\end{pf}

\section{Controlled operations}

\begin{exe}[Matrix representation of multi-qubit gates]
    What is the $4\times 4$ unitary matrix for the circuit
    \begin{center}
        \begin{quantikz}
            \lstick{$x_2$} & \gate{H} & \qw \\
            \lstick{$x_1$} & \qw & \qw
        \end{quantikz}
    \end{center}
        in the computational basis? What is the unitary matrix for the circuit
    \begin{center}
        \begin{quantikz}
            \lstick{$x_2$} & \qw & \qw\\
            \lstick{$x_1$} & \gate{H} & \qw
        \end{quantikz}
    \end{center}
    in the computational basis?
\end{exe}
\begin{sol}
    For the first quantum circuit, the $4\times 4$ unitary is
    \begin{align}
        I\otimes H=\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix}\otimes\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}=\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1&0&0\\
            1&-1&0&0\\
            0&0&1&1\\
            0&0&1&-1
        \end{bmatrix}.
    \end{align}
    The unitary matrix for the second quantum circuit is
    \begin{align}
        H\otimes I=\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}\otimes\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix}=\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&0&1&0\\
            0&1&0&1\\
            1&0&-1&0\\
            0&1&0&-1
        \end{bmatrix}.
    \end{align}
\end{sol}

\begin{exe}[Building CNOT from from controlled-$Z$ gates]
    Construct a CNOT gate from one controlled-$Z$ gate, that is, the gate whose action in the computational basis is specified by the unitary matrix
    \[
        \begin{bmatrix}
            1&0&0&0\\
            0&1&0&0\\
            0&0&1&0\\
            0&0&0&-1
        \end{bmatrix},
    \]
    and two Hadamard gates, specifying the control and target qubits.
\end{exe}
\begin{sol}
    Since
    \begin{align}
        (I\otimes H)\begin{bmatrix}
            1&0&0&0\\
            0&1&0&0\\
            0&0&1&0\\
            0&0&0&-1
        \end{bmatrix}(I\otimes H)=\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1&0&0\\
            1&-1&0&0\\
            0&0&1&1\\
            0&0&1&-1
        \end{bmatrix}\begin{bmatrix}
            1&0&0&0\\
            0&1&0&0\\
            0&0&1&0\\
            0&0&0&-1
        \end{bmatrix}\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1&0&0\\
            1&-1&0&0\\
            0&0&1&1\\
            0&0&1&-1
        \end{bmatrix}=\begin{bmatrix}
            1&0&0&0\\
            0&1&0&0\\
            0&0&0&1\\
            0&0&1&0
        \end{bmatrix},
    \end{align}
    \begin{center}
        \begin{quantikz}
            \qw & \ctrl{1} & \qw\\
            \qw & \targ{} & \qw
        \end{quantikz}
        $=$
        \begin{quantikz}
            \qw & \qw & \ctrl{1} & \qw & \qw\\
            \qw & \gate{H} & \gate{Z} & \gate{H} & \qw
        \end{quantikz}
    \end{center}
\end{sol}

\begin{exe}
    Show that
    \begin{center}
        \begin{quantikz}
            \qw & \ctrl{1} & \qw\\
            \qw & \gate{Z} & \qw
        \end{quantikz}
        $=$
        \begin{quantikz}
            \qw & \gate{Z} & \qw\\
            \qw & \ctrl{-1} & \qw
        \end{quantikz}
    \end{center}
\end{exe}
\begin{pf}
    Since the unitary matrices of the two quantum circuits are both
    \begin{align}
        \begin{bmatrix}
            1&0&0&0\\
            0&1&0&0\\
            0&0&1&0\\
            0&0&0&-1
        \end{bmatrix},
    \end{align}
    these two quantum circuits equal.
\end{pf}

\begin{exe}[CNOT action on density matrices]
    The CNOT gate is a simple permutation whose action on density matrix $\rho$ is to rearrange the elements in the matrix. Write out this action explicitly in the computational basis.
\end{exe}
\begin{sol}
    The CNOT gate exchanges the coefficients of $\lvert 10\rangle\langle 10\rvert$ component and $\lvert 11\rangle\langle 11\rvert$ component, $\lvert 10\rangle\langle 11\rvert$ component and $\lvert 11\rangle\langle 10\rvert$ component.
\end{sol}

\begin{exe}[CNOT basis transformations]
    Unlike ideal classical gates, ideal quantum gates do not have (as electrical engineers say) `high-impedance' inputs. In fact, the role of role of `control' and `target' are arbitrary -- they depend on what basis you think of a device as operating in. We have described how the CNOT behaves with respect to the computational basis, and in this description the state of the control qubit is not changed. However, if we work in a different basis then the control qubit \emph{does} change: we will show that its phase is flipped depending on the state of the `target' qubits! Show that
    \begin{center}
        \begin{quantikz}
            \qw & \gate{H} & \ctrl{1} & \gate{H} & \qw\\
            \qw & \gate{H} & \targ{} & \gate{H} & \qw
        \end{quantikz}
        $=$
        \begin{quantikz}
            \qw & \targ{} & \qw\\
            \qw & \ctrl{-1} & \qw
        \end{quantikz}
    \end{center}
    Introducing basis states $\lvert\pm\rangle=(\lvert 0\rangle\pm\lvert 1\rangle)/\sqrt{2}$, use this circuit identity to show that the effect of a CNOT with the first qubit as control and the second qubit as target is as follows:
    \begin{align}
        \label{E4.20-1}
        \lvert+\rangle\lvert+\rangle\rightarrow&\lvert+\rangle\lvert+\rangle\\
        \lvert-\rangle\lvert+\rangle\rightarrow&\lvert-\rangle\lvert+\rangle\\
        \lvert+\rangle\lvert-\rangle\rightarrow&\lvert-\rangle\lvert-\rangle\\
        \label{E4.20-4}
        \lvert-\rangle\lvert-\rangle\rightarrow&\lvert+\rangle\lvert-\rangle,
    \end{align}
    Thus, with respect to this new basis, the state of the target qubit is not changed, while the state of the control qubit is flipped if the target starts as $\lvert-\rangle$, otherwise it is left alone. That is, in this basis, targe and control have essentially interchanged roles!
\end{exe}
\begin{pf}
    Since
    \begin{align}
        \notag(H\otimes H)\begin{bmatrix}
            1&0&0&0\\
            0&1&0&0\\
            0&0&0&1\\
            0&0&1&0
        \end{bmatrix}(H\otimes H)=&\left(\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}\otimes\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}\right)\begin{bmatrix}
            1&0&0&0\\
            0&1&0&0\\
            0&0&0&1\\
            0&0&1&0
        \end{bmatrix}\left(\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}\otimes\frac{1}{\sqrt{2}}\begin{bmatrix}
            1&1\\
            1&-1
        \end{bmatrix}\right)\\
        =&\frac{1}{2}\begin{bmatrix}
            1&1&1&1\\
            1&-1&1&-1\\
            1&1&-1&-1\\
            1&-1&-1&1
        \end{bmatrix}\begin{bmatrix}
            1&0&0&0\\
            0&1&0&0\\
            0&0&0&1\\
            0&0&1&0
        \end{bmatrix}\frac{1}{2}\begin{bmatrix}
            1&1&1&1\\
            1&-1&1&-1\\
            1&1&-1&-1\\
            1&-1&-1&1
        \end{bmatrix}=\begin{bmatrix}
            1&0&0&0\\
            0&0&0&1\\
            0&0&1&0\\
            0&1&0&0
        \end{bmatrix},
    \end{align}
    \begin{center}
        \begin{quantikz}
            \qw & \gate{H} & \ctrl{1} & \gate{H} & \qw\\
            \qw & \gate{H} & \targ{} & \gate{H} & \qw
        \end{quantikz}
        $=$
        \begin{quantikz}
            \qw & \targ{} & \qw\\
            \qw & \ctrl{-1} & \qw
        \end{quantikz}
    \end{center}
    A CNOT with the first qubit as control and the second qubit as target
    \begin{center}
        \begin{quantikz}
            \qw & \ctrl{1} & \qw\\
            \qw & \targ{} & \qw
        \end{quantikz}
    \end{center}
    is equivalent to
    \begin{center}
        \begin{quantikz}
            \qw & \gate{H} & \gate{H} & \ctrl{1} & \gate{H} & \gate{H} & \qw\\
            \qw & \gate{H} & \gate{H} & \targ{} & \gate{H} & \gate{H} & \qw
        \end{quantikz}
        $=$
        \begin{quantikz}
            \qw & \gate{H} & \targ{} & \gate{H} & \qw\\
            \qw & \gate{H} & \ctrl{-1} & \gate{H} & \qw
        \end{quantikz}
    \end{center}
    Inputting the state given by from \eqref{E4.20-1} to \eqref{E4.20-4} to the above circuit, the state changing process is shown in Table \ref{E4.20-table}.
    \begin{table}[h]
        \centering
        \caption{State changing process for different inputs into CNOT.}
        \label{E4.20-table}
        \begin{tabular}{|c|c|c|c|c|}
        \hline
        & Input & After the $1$st set of $H$ gates & After the CNOT gate & Output \\ \hline
        \multirow{4}{*}{States} & $\lvert++\rangle$ & $\lvert 00\rangle$ & $\lvert 00\rangle$ & $\lvert++\rangle$ \\ \cline{2-5}
        & $\lvert-+\rangle$ & $\lvert 10\rangle$ & $\lvert 10\rangle$ & $\lvert-+\rangle$ \\ \cline{2-5}
        & $\lvert+-\rangle$ & $\lvert 01\rangle$ & $\lvert 11\rangle$ & $\lvert--\rangle$ \\ \cline{2-5}
        & $\lvert--\rangle$ & $\lvert 11\rangle$ & $\lvert 01\rangle$ & $\lvert+-\rangle$ \\ \hline
        \end{tabular}
    \end{table}

    Therefore, the effect of a CNOT with the first qubit as control and the second qubit as target is as given by from \eqref{E4.20-1} to \eqref{E4.20-4}.
\end{pf}

\begin{exe}
    Verify that Figure 4.8\footnote{\label{Fig-4.8-Circuit-for-the-C2(U)-gate}\begin{quantikz}
    \qw & \ctrl{1} & \qw\\
    \qw & \ctrl{1} & \qw\\
    \qw & \gate{H} & \qw
    \end{quantikz}$=$\begin{quantikz}
    \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \ctrl{2} & \qw\\
    \qw & \ctrl{1} & \targ{} & \ctrl{1} & \targ{} & \qw & \qw\\
    \qw & \gate{V} & \qw & \gate{V^{\dagger}} & \qw & \gate{V} & \qw
    \end{quantikz}\\Figure 4.8. Circuit for the $C^2(U)$ gate. $V$ is any unitary operator satisfying $V^2=U$. The special case $V=(1-i)(1+iX)/2$ corresponds to the Toffoli gate.} implements the $C^2(U)$ operation.
\end{exe}
\begin{pf}
    Inputting the computational basis states to the Figure 4.8\footref{Fig-4.8-Circuit-for-the-C2(U)-gate}, the state changing process is shown in Table \ref{E4.21-state-changing-progress}.
    \begin{table}[h]
        \centering
        \caption{State changing process for the computational basis stats inputs into Figure 4.8\textsuperscript{\ref{Fig-4.8-Circuit-for-the-C2(U)-gate}}.}
        \label{E4.21-state-changing-progress}
        \begin{tabular}{|c|c|c|c|c|c|c|}
        \hline
        & Input & After the $1$st $V$ & After the $1$st CNOT & After $V^{\dagger}$ & After the $2$nd CNOT & After the $2$nd $V$ \\ \hline
        \multirow{8}{*}{States} & $\lvert 000\rangle$ & $\lvert 000\rangle$ & $\lvert 000\rangle$ & $\lvert 000\rangle$ & $\lvert 000\rangle$ & $\lvert 000\rangle$ \\ \cline{2-7}
         & $\lvert 001\rangle$ & $\lvert 001\rangle$ & $\lvert 001\rangle$ & $\lvert 001\rangle$ & $\lvert 001\rangle$ & $\lvert 001\rangle$ \\ \cline{2-7}
         & $\lvert 010\rangle$ & $\lvert 01\rangle\otimes V\lvert 0\rangle$ & $\lvert 01\rangle\otimes V\lvert 0\rangle$ & \begin{tabular}[c]{@{}c@{}}$\lvert 01\rangle\otimes V^{\dagger}V\lvert 0\rangle$\\ $=\lvert 010\rangle$\end{tabular} & \begin{tabular}[c]{@{}c@{}}$\lvert 01\rangle\otimes V^{\dagger}V\lvert 0\rangle$\\ $=\lvert 010\rangle$\end{tabular} & $\lvert 010\rangle$ \\ \cline{2-7}
         & $\lvert 011\rangle$ & $\lvert 01\rangle\otimes V\lvert 1\rangle$ & $\lvert 01\rangle\otimes V\lvert 1\rangle$ & \begin{tabular}[c]{@{}c@{}}$\lvert 01\rangle\otimes V^{\dagger}V\lvert 1\rangle$\\ $=\lvert 011\rangle$\end{tabular} & \begin{tabular}[c]{@{}c@{}}$\lvert 01\rangle\otimes V^{\dagger}V\lvert 1\rangle$\\ $=\lvert 011\rangle$\end{tabular} & $\lvert 011\rangle$ \\ \cline{2-7}
         & $\lvert 100\rangle$ & $\lvert 100\rangle$ & $\lvert 110\rangle$ & $\lvert 11\rangle V^{\dagger}\lvert 0\rangle$ & $\lvert 10\rangle V^{\dagger}\lvert 0\rangle$ & \begin{tabular}[c]{@{}c@{}}$\lvert 10\rangle VV^{\dagger}\lvert 0\rangle$\\ $=\lvert 100\rangle$\end{tabular} \\ \cline{2-7}
         & $\lvert 101\rangle$ & $\lvert 101\rangle$ & $\lvert 111\rangle$ & $\lvert 11\rangle V^{\dagger}\lvert 1\rangle$ & $\lvert 10\rangle V^{\dagger}\lvert 1\rangle$ & \begin{tabular}[c]{@{}c@{}}$\lvert 10\rangle VV^{\dagger}\lvert 1\rangle$\\ $=\lvert 101\rangle$\end{tabular} \\ \cline{2-7}
         & $\lvert 110\rangle$ & $\lvert 11\rangle\otimes V\lvert 0\rangle$ & $\lvert 10\rangle\otimes V\lvert 0\rangle$ & $\lvert 10\rangle\otimes V\lvert 0\rangle$ & $\lvert 11\rangle\otimes V\lvert 0\rangle$ & \begin{tabular}[c]{@{}c@{}}$\lvert 11\rangle\otimes V^2\lvert 0\rangle$\\ $=\lvert 11\rangle\otimes U\lvert 0\rangle$\end{tabular} \\ \cline{2-7}
         & $\lvert 111\rangle$ & $\lvert 11\rangle\otimes V\lvert 1\rangle$ & $\lvert 10\rangle\otimes V\lvert 1\rangle$ & $\lvert 10\rangle\otimes V\lvert 1\rangle$ & $\lvert 11\rangle\otimes V\lvert 1\rangle$ & \begin{tabular}[c]{@{}c@{}}$\lvert 11\rangle\otimes V^2\lvert 0\rangle$\\ $=\lvert 11\rangle\otimes U\lvert 1\rangle$\end{tabular} \\ \hline
        \end{tabular}
    \end{table}

    The truth table of $C^2(U)$ operation is shown in Table \ref{E4.21-C2U-truth-table}.
    \begin{table}[h]
        \centering
        \caption{Truth table of $C^2(U)$.}
        \label{E4.21-C2U-truth-table}
        \begin{tabular}{|c|c|c|c|c|c|}
        \hline
        \multicolumn{3}{|c|}{Inputs} & \multicolumn{3}{c|}{Outputs} \\ \hline
        Control 1 & Control 2 & Target & Control 1 & Control 2 & Target \\ \hline
        $\lvert 0\rangle$ & $\lvert 0\rangle$ & $\lvert 0\rangle$ & $\lvert 0\rangle$ & $\lvert 0\rangle$ & $\lvert 0\rangle$ \\ \hline
        $\lvert 0\rangle$ & $\lvert 0\rangle$ & $\lvert 1\rangle$ & $\lvert 0\rangle$ & $\lvert 0\rangle$ & $\lvert 1\rangle$ \\ \hline
        $\lvert 0\rangle$ & $\lvert 1\rangle$ & $\lvert 0\rangle$ & $\lvert 0\rangle$ & $\lvert 1\rangle$ & $\lvert 0\rangle$ \\ \hline
        $\lvert 0\rangle$ & $\lvert 1\rangle$ & $\lvert 1\rangle$ & $\lvert 0\rangle$ & $\lvert 1\rangle$ & $\lvert 1\rangle$ \\ \hline
        $\lvert 1\rangle$ & $\lvert 0\rangle$ & $\lvert 0\rangle$ & $\lvert 1\rangle$ & $\lvert 0\rangle$ & $\lvert 0\rangle$ \\ \hline
        $\lvert 1\rangle$ & $\lvert 0\rangle$ & $\lvert 1\rangle$ & $\lvert 1\rangle$ & $\lvert 0\rangle$ & $\lvert 1\rangle$ \\ \hline
        $\lvert 1\rangle$ & $\lvert 1\rangle$ & $\lvert 0\rangle$ & $\lvert 1\rangle$ & $\lvert 1\rangle$ & $U\lvert 0\rangle$ \\ \hline
        $\lvert 1\rangle$ & $\lvert 1\rangle$ & $\lvert 1\rangle$ & $\lvert 1\rangle$ & $\lvert 1\rangle$ & $U\lvert 1\rangle$ \\ \hline
        \end{tabular}
    \end{table}

    Therefore, Figure 4.8\footref{Fig-4.8-Circuit-for-the-C2(U)-gate} implements the $C^2(U)$ operation.
\end{pf}

\begin{exe}
    Prove that a $C^2(U)$ gate (for any single qubit unitary $U$) can be constructed using at most eight one-qubit gates, and six controlled-NOTs.
\end{exe}
\begin{pf}
    \footnote{Reference: \url{https://quantumcomputing.stackexchange.com/questions/7082/how-to-reduce-circuit-elements-of-a-decomposed-c2u-operation}}Using the equation in Figure 4.6\footnote{\label{Fig-4.6-controlled-U-construction}\begin{quantikz}
        \qw & \ctrl{1} & \qw\\
        \qw & \gate{U} & \qw
    \end{quantikz}$=$\begin{tikzcd}[ampersand replacement=\&]
        \qw \& \qw \& \ctrl{1} \& \qw \& \ctrl{1} \& \gate{\begin{bmatrix}1&0\\0&e^{i\alpha}\end{bmatrix}} \& \qw\\
        \qw \& \gate{C} \& \targ{} \& \gate{B} \& \targ{} \& \gate{A} \& \qw
    \end{tikzcd}} and the conclusion obtained in the last Exercise, we can transform the $C^2(U)$ gate into
    \begin{center}
        \begin{quantikz}
            \qw & \ctrl{1} & \qw\\
            \qw & \ctrl{1} & \qw\\
            \qw & \gate{U} & \qw
        \end{quantikz}$=$
        \begin{quantikz}
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \ctrl{2} & \qw\\
            \qw & \ctrl{1} & \targ{} & \ctrl{1} & \targ{} & \qw & \qw\\
            \qw & \gate{V} & \qw & \gate{V^{\dagger}} & \qw & \gate{V} & \qw
        \end{quantikz}
    \end{center}
    \begin{center}
        \tiny
        $=$\begin{quantikz}
            \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \ctrl{2} & \qw & \ctrl{2} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \qw\\
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \targ{} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{-i\alpha}\end{smallmatrix}\right]} & \ctrl{1} & \qw & \ctrl{1} & \qw & \targ{} & \qw & \qw & \qw & \qw & \qw\\
            \qw & \gate{C} & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw & \gate{A^{\dagger}} & \targ{} & \gate{B^{\dagger}} & \targ{} & \gate{C^{\dagger}} & \gate{C} & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw
        \end{quantikz}
    \end{center}
    \begin{center}
        \footnotesize
        $=$\begin{quantikz}
            \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \qw & \ctrl{1} & \ctrl{2} & \qw & \ctrl{2} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \qw\\
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \targ{} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{-i\alpha}\end{smallmatrix}\right]} & \ctrl{1} & \qw & \ctrl{1} & \targ{} & \qw & \qw & \qw & \qw & \qw\\
            \qw & \gate{C} & \targ{} & \gate{B} & \targ{} & \qw & \qw & \qw & \targ{} & \gate{B^{\dagger}} & \targ{} & \qw & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw
        \end{quantikz}
    \end{center}
    where $U=V^2$, $V=\exp(i\alpha)AXBXC$ and $ABC=I$.
    Moving the original $6$th CNOT gate in the above quantum circuit diagram to right before the original $4$th CNOT gate and adding two CNOT gate for compensation, we get
    \begin{center}
        \tiny
        \begin{quantikz}
            \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \qw & \ctrl{2} & \qw & \qw & \ctrl{2} & \ctrl{2} & \qw & \ctrl{2} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \qw\\
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \targ{} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{-i\alpha}\end{smallmatrix}\right]} & \targ{} & \ctrl{1} & \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \qw & \qw & \qw\\
            \qw & \gate{C} & \targ{} & \gate{B} & \targ{} & \qw & \qw & \qw & \qw & \targ{} & \targ{} & \gate{B^{\dagger}} & \targ{} & \targ{} & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw
        \end{quantikz}
    \end{center}
    This kind of moving of CNOT gate is valid due to Corollary \ref{E4.22-cor-1}, which we will show below.
    Using Corollary \ref{E4.22-cor-2}, we can cancel the $8$th and $9$th CNOT gates and get
    \begin{center}
        \begin{quantikz}
            \qw & \qw & \qw & \qw & \qw & \qw\gategroup[wires=2,steps=4,style={dashed}]{} & \ctrl{1} & \qw & \ctrl{1} & \qw & \ctrl{2} & \qw & \qw & \qw & \ctrl{2} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \qw\\
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \targ{} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{-i\alpha}\end{smallmatrix}\right]} & \targ{} & \ctrl{1} & \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \qw\\
            \qw & \gate{C} & \targ{} & \gate{B} & \targ{} & \qw & \qw & \qw & \qw & \targ{} & \targ{} & \gate{B^{\dagger}} & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw
        \end{quantikz}
    \end{center}
    Since the part in dashed frame is diagonal(Corollary \ref{E4.22-cor-3}) and thus commute with arbitrary operator, we can move it to the end of the circuit and get
    \begin{center}
        \begin{quantikz}
            \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{2} & \qw & \qw & \qw & \ctrl{2} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \qw\gategroup[wires=2,steps=4,style={dashed}]{} & \ctrl{1} & \qw & \ctrl{1} & \qw\\
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \ctrl{1} & \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \targ{} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{-i\alpha}\end{smallmatrix}\right]} & \targ{} & \qw\\
            \qw & \gate{C} & \targ{} & \gate{B} & \targ{} & \targ{} & \targ{} & \gate{B^{\dagger}} & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw & \qw & \qw & \qw & \qw
        \end{quantikz}
    \end{center}
    Again, using Corollary \ref{E4.22-cor-2}, we cancel the $2$nd and $3$rd CNOT gates and get
    \begin{center}
        \begin{quantikz}
            \qw & \qw & \qw & \qw & \ctrl{2} & \qw & \qw & \qw & \ctrl{2} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \ctrl{1} & \qw & \ctrl{1} & \qw\\
            \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \ctrl{1} & \qw & \qw & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \targ{} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{-i\alpha}\end{smallmatrix}\right]} & \targ{} & \qw\\
            \qw & \gate{C} & \targ{} & \gate{B} & \targ{} & \gate{B^{\dagger}} & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw & \qw & \qw & \qw
        \end{quantikz}
    \end{center}
    which contains eight one-qubit gates, and six controlled-NOTs.

    Therefore, a $C^2(U)$ gate (for any single qubit unitary U) can be constructed using at most eight one-qubit, and six controlled-NOTs.
    \begin{cor}
        \label{E4.22-cor-1}
        \begin{quantikz}
            \qw & \qw & \ctrl{1} & \qw\\
            \qw & \ctrl{1} & \targ{} & \qw\\
            \qw & \targ{} & \qw & \qw
        \end{quantikz}$=$
        \begin{quantikz}
            \qw & \ctrl{1} & \qw & \ctrl{2} & \qw\\
            \qw & \targ{} & \ctrl{1} & \qw & \qw\\
            \qw & \qw & \targ{} & \targ{} & \qw
        \end{quantikz}
    \end{cor}
    \begin{pf}
        Inputting the computational basis states to the right and left side, the state changing process is shown in Table \ref{E4.22-cor-1-left} and Table \ref{E4.22-cor-1-right}, respectively, which generate the same output.
        \begin{table}[h]
            \centering
            \caption{State changing process for the computational basis states inputs into the left side of Corollary \ref{E4.22-cor-1}.}
            \label{E4.22-cor-1-left}
            \begin{tabular}{|c|c|c|c|}
            \hline
             & Input & After the $1$st CNOT & Output \\ \hline
            \multirow{8}{*}{States} & $\lvert 000\rangle$ & $\lvert 000\rangle$ & $\lvert 000\rangle$ \\ \cline{2-4}
             & $\lvert 001\rangle$ & $\lvert 001\rangle$ & $\lvert 001\rangle$ \\ \cline{2-4}
             & $\lvert 010\rangle$ & $\lvert 011\rangle$ & $\lvert 010\rangle$ \\ \cline{2-4}
             & $\lvert 011\rangle$ & $\lvert 010\rangle$ & $\lvert 011\rangle$ \\ \cline{2-4}
             & $\lvert 100\rangle$ & $\lvert 100\rangle$ & $\lvert 100\rangle$ \\ \cline{2-4}
             & $\lvert 101\rangle$ & $\lvert 101\rangle$ & $\lvert 101\rangle$ \\ \cline{2-4}
             & $\lvert 110\rangle$ & $\lvert 111\rangle$ & $\lvert 110\rangle$ \\ \cline{2-4}
             & $\lvert 111\rangle$ & $\lvert 110\rangle$ & $\lvert 111\rangle$ \\ \hline
            \end{tabular}
        \end{table}
        \begin{table}[h]
            \centering
            \caption{State changing process for the computational basis states inputs into the right side of Corollary \ref{E4.22-cor-1}.}
            \label{E4.22-cor-1-right}
            \begin{tabular}{|c|c|c|c|c|}
            \hline
            & Input & After the $1$st CNOT & After the $2$nd CNOT & Output \\ \hline
            \multirow{8}{*}{States} & $\lvert 000\rangle$ & $\lvert 000\rangle$ & $\lvert 000\rangle$ & $\lvert 000\rangle$ \\ \cline{2-5}
            & $\lvert 001\rangle$ & $\lvert 001\rangle$ & $\lvert 001\rangle$ & $\lvert 001\rangle$ \\ \cline{2-5}
            & $\lvert 010\rangle$ & $\lvert 010\rangle$ & $\lvert 011\rangle$ & $\lvert 011\rangle$ \\ \cline{2-5}
            & $\lvert 011\rangle$ & $\lvert 011\rangle$ & $\lvert 010\rangle$ & $\lvert 010\rangle$ \\ \cline{2-5}
            & $\lvert 100\rangle$ & $\lvert 110\rangle$ & $\lvert 111\rangle$ & $\lvert 110\rangle$ \\ \cline{2-5}
            & $\lvert 101\rangle$ & $\lvert 111\rangle$ & $\lvert 110\rangle$ & $\lvert 111\rangle$ \\ \cline{2-5}
            & $\lvert 110\rangle$ & $\lvert 100\rangle$ & $\lvert 100\rangle$ & $\lvert 101\rangle$ \\ \cline{2-5}
            & $\lvert 111\rangle$ & $\lvert 101\rangle$ & $\lvert 101\rangle$ & $\lvert 100\rangle$ \\ \hline
            \end{tabular}
        \end{table}
    \end{pf}
    \begin{cor}
        \label{E4.22-cor-2}
        \begin{quantikz}
            \qw & \ctrl{1} & \ctrl{1} & \qw\\
            \qw & \targ{} & \targ{} & \qw
        \end{quantikz} vanishes.
    \end{cor}
    \begin{pf}
        Inputting the computational basis states to the above quantum circuit, the state changing process is shown in Table \ref{E4.22-cor-2-quantum-circuit}, where the inputs and outputs are the same, so this quantum circuit vanishes.
        \begin{table}[h]
            \centering
            \caption{State changing process for the computational basis states inputs into the quantum circuit of Corollary \ref{E4.22-cor-2}.}
            \label{E4.22-cor-2-quantum-circuit}
            \begin{tabular}{|c|c|c|c|}
            \hline
             & Input & After the $1$st CNOT & Output \\ \hline
            \multirow{4}{*}{States} & $\lvert 00\rangle$ & $\lvert 00\rangle$ & $\lvert 00\rangle$ \\ \cline{2-4}
             & $\lvert 01\rangle$ & $\lvert 01\rangle$ & $\lvert 01\rangle$ \\ \cline{2-4}
             & $\lvert 10\rangle$ & $\lvert 11\rangle$ & $\lvert 10\rangle$ \\ \cline{2-4}
             & $\lvert 11\rangle$ & $\lvert 10\rangle$ & $\lvert 11\rangle$ \\ \hline
            \end{tabular}
        \end{table}
    \end{pf}
    \begin{cor}
        \label{E4.22-cor-3}
        \begin{quantikz}
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \qw\\
            \qw & \gate{\left[\begin{smallmatrix}1&0\\0&e^{i\alpha}\end{smallmatrix}\right]} & \targ{} & \gate{\left[\begin{smallmatrix}1&0\\0&e^{-i\alpha}\end{smallmatrix}\right]} & \targ{} & \qw
        \end{quantikz} is diagonal.
    \end{cor}
    \begin{pf}
        The unitary matrix of the above quantum circuit is
        \begin{align}
            \notag&\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&1&0\\
                0&0&0&e^{i\alpha}
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&1\\
                0&0&1&0
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&1&0\\
                0&0&0&e^{-i\alpha}
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&1\\
                0&0&1&0
            \end{bmatrix}=\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&1\\
                0&0&e^{i\alpha}&0
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&1&0\\
                0&0&0&e^{-i\alpha}
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&1\\
                0&0&1&0
            \end{bmatrix}\\
            =&\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&e^{-i\alpha}\\
                0&0&e^{i\alpha}&0\\
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&1\\
                0&0&1&0
            \end{bmatrix}=\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&e^{i\alpha}&0\\
                0&0&0&e^{-i\alpha}
            \end{bmatrix},
        \end{align}
        which is diagonal.
    \end{pf}
\end{pf}

\begin{exe}
    Construct a $C^1(U)$ gate for $U=R_x(\theta)$ and $U=R_y(\theta)$, using only CNOT and single qubit gates. Can you reduce the number of single qubit gates needed in the construction from three to two?
\end{exe}
\begin{sol}
    For $U=R_x(\theta)$, using Theorem 4.1\footref{Thm-4.1-Z-Y-decomp-for-a-single-qubit}, it can be written as
    \begin{align}
        U=R_x(\theta)=\begin{bmatrix}
            \cos\frac{\theta}{2}&-i\sin\frac{\theta}{2}\\
            -i\sin\frac{\theta}{2}&\cos\frac{\theta}{2}
        \end{bmatrix}=e^{i\alpha}R_z(\beta)R_y(\gamma)R_z(\delta)=\begin{bmatrix}
            e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
            e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
        \end{bmatrix},
    \end{align}
    i.e.,
    \begin{align}
        e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}=&\cos\frac{\theta}{2},\\
        -e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}=&-i\sin\frac{\theta}{2},\\
        e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}=&-i\sin\frac{\theta}{2},\\
        e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}=&\cos\frac{\theta}{2},
    \end{align}
    so
    \begin{align}
        \alpha=&0,\\
        \beta=&-\frac{\pi}{2},\\
        \delta=&\frac{\pi}{2},\\
        \gamma=&\theta.
    \end{align}
    Using the Corollary 4.2 in textbook\footnote{\label{Cor-4.2-U=exp(i alpha)AXBXC}Suppose $U$ is a unitary gate on a single qubit. Then there exist unitary operators $A$, $B$, $C$ on a single qubit such that $ABC=I$ and $U=e^{i\alpha}AXBXC$, where $\alpha$ is some overall phase factor, $A=R_z(\beta)R_y(\gamma/2)$, $B=R_y(-\gamma/2)R_z(-(\delta+\beta)/2)$, $C=R_z((\delta-\beta)/2)$.},
    \begin{align}
        U=R_x(\theta)=e^{i\alpha}AXBXC,
    \end{align}
    where
    \begin{align}
        \alpha=&0,\\
        A=&R_z(\beta)R_y(\gamma/2)=\begin{bmatrix}
            e^{-i\beta/2}&0\\
            0&e^{i\beta/2}
        \end{bmatrix}\begin{bmatrix}
            \cos\frac{\gamma}{4}&-\sin\frac{\gamma}{4}\\
            \sin\frac{\gamma}{4}&\cos\frac{\gamma}{4}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\beta/2}\cos\frac{\gamma}{4}&-e^{-i\beta/2}\sin\frac{\gamma}{4}\\
            e^{i\beta/2}\sin\frac{\gamma}{4}&e^{i\beta/2}\cos\frac{\gamma}{4}
        \end{bmatrix}=\begin{bmatrix}
            e^{i\pi/4}\cos\frac{\theta}{4}&-e^{i\pi/4}\sin\frac{\theta}{4}\\
            e^{-i\pi/4}\sin\frac{\theta}{4}&e^{-i\pi/4}\cos\frac{\theta}{4}
        \end{bmatrix},\\
        \notag B=&R_y(-\gamma/2)R_z(-(\delta+\beta)/2)=\begin{bmatrix}
            \cos\left(-\frac{\gamma}{4}\right)&-\sin\left(-\frac{\gamma}{4}\right)\\
            \sin\left(-\frac{\gamma}{4}\right)&\cos\left(-\frac{\gamma}{4}\right)
        \end{bmatrix}\begin{bmatrix}
            e^{i(\delta+\beta)/4}&0\\
            0&e^{-i(\delta+\beta)/4}
        \end{bmatrix}=\begin{bmatrix}
            e^{i(\delta+\beta)/4}\cos\frac{\gamma}{4}&e^{-i(\delta+\beta)/4}\sin\frac{\gamma}{4}\\
            -e^{i(\delta+\beta)/4}\sin\frac{\gamma}{4}&e^{-i(\delta+\beta)/4}\cos\frac{\gamma}{4}
        \end{bmatrix}\\
        =&\begin{bmatrix}
            \cos\frac{\theta}{4}&\sin\frac{\theta}{4}\\
            -\sin\frac{\theta}{4}&\cos\frac{\theta}{4}
        \end{bmatrix},\\
        C=&R_z((\delta-\beta)/2)=\begin{bmatrix}
            e^{-i(\delta-\beta)/4}&0\\
            0&e^{i(\delta-\beta)/4}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\pi/4}&0\\
            0&e^{i\pi/4}
        \end{bmatrix}.
    \end{align}
    Using the equation in Figure 4.6\footref{Fig-4.6-controlled-U-construction}, we can transform the $C^1(U)$ gate into
    \begin{center}
        \begin{quantikz}
            \qw & \ctrl{1} & \qw\\
            \qw & \gate{U} & \qw
        \end{quantikz}$=$\begin{quantikz}
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \qw & \qw\\
            \qw & \gate{C} & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw
        \end{quantikz}
    \end{center}
    Using the Corollary \ref{E4.23-cor-1} which we will show bellow, we can exchange the $C$ gate and the CNOT, and reduce the number of single qubit gates needed in the construction from three to two,
    \begin{center}
        \begin{quantikz}
            \qw & \ctrl{1} & \qw & \ctrl{1} & \qw & \qw\\
            \qw & \targ{} & \gate{D} & \targ{} & \gate{A} & \qw\\
        \end{quantikz}
    \end{center}
    where the unitary matrix for $D$ gate is
    \begin{align}
        D=CB=\begin{bmatrix}
            e^{-i\pi/4}\cos\frac{\theta}{4}&e^{-i\pi/4}\sin\frac{\theta}{4}\\
            -e^{i\pi/4}\sin\frac{\theta}{4}&e^{i\pi/4}\sin\frac{\theta}{4}
        \end{bmatrix}.
    \end{align}
    \begin{cor}
        \label{E4.23-cor-1}
        \begin{quantikz}
            \qw & \qw & \ctrl{1} & \qw\\
            \qw & \gate{C} & \targ{} & \qw
        \end{quantikz}$=$\begin{quantikz}
            \qw & \ctrl{1} & \qw & \qw\\
            \qw & \targ{} & \gate{C} & \qw
        \end{quantikz}
    \end{cor}
    \begin{pf}
        The unitary matrix for the left side of the Corollary \ref{E4.23-cor-1} is
        \begin{align}
            C\times\text{CNOT}=\begin{bmatrix}
                e^{-i\pi/4}&0&0&0\\
                0&e^{i\pi/4}&0&0\\
                0&0&e^{-i\pi/4}&0\\
                0&0&0&e^{i\pi/4}
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&1\\
                0&0&1&0
            \end{bmatrix}=\begin{bmatrix}
                e^{-i\pi/4}&0&0&0\\
                0&e^{i\pi/4}&0&0\\
                0&0&0&e^{-\pi/4}\\
                0&0&e^{i\pi/4}&0
            \end{bmatrix},
        \end{align}
        and the matrix for the right side of the Corollary \ref{E4.23-cor-1} is
        \begin{align}
            \text{CNOT}\times C=\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&1\\
                0&0&1&0
            \end{bmatrix}\begin{bmatrix}
                e^{-i\pi/4}&0&0&0\\
                0&e^{i\pi/4}&0&0\\
                0&0&e^{-i\pi/4}&0\\
                0&0&0&e^{i\pi/4}
            \end{bmatrix}=\begin{bmatrix}
                e^{-i\pi/4}&0&0&0\\
                0&e^{i\pi/4}&0&0\\
                0&0&0&e^{-\pi/4}\\
                0&0&e^{i\pi/4}&0
            \end{bmatrix}
        \end{align}
        which are the same. Therefore, the Corollary \ref{E4.23-cor-1} holds.
    \end{pf}

    For $U=R_y(\theta)$, using Theorem 4.1\footref{Thm-4.1-Z-Y-decomp-for-a-single-qubit}, it can be written as
    \begin{align}
        U=R_y(\theta)=\begin{bmatrix}
            \cos\frac{\theta}{2}&-\sin\frac{\theta}{2}\\
            \sin\frac{\theta}{2}&\cos\frac{\theta}{2}
        \end{bmatrix}=e^{i\alpha}R_z(\beta)R_y(\gamma)R_z(\delta)=\begin{bmatrix}
            e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}&-e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\
            e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}&e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}
        \end{bmatrix}
    \end{align}
    i.e.,
    \begin{align}
        e^{i(\alpha-\beta/2-\delta/2)}\cos\frac{\gamma}{2}=&\cos\frac{\theta}{2},\\
        -e^{i(\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}=&-\sin\frac{\theta}{2},\\
        e^{i(\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2}=&\sin\frac{\theta}{2},\\
        e^{i(\alpha+\beta/2+\delta/2)}\cos\frac{\gamma}{2}=&\cos\frac{\theta}{2},
    \end{align}
    so
    \begin{align}
        \alpha=&0,\\
        \beta=&0,\\
        \delta=&0,\\
        \gamma=&\theta.
    \end{align}
    Using the Corollary 4.2\footref{Cor-4.2-U=exp(i alpha)AXBXC} in textbook,
    \begin{align}
        U=R_y(\theta)=e^{i\alpha}AXBXC,
    \end{align}
    where
    \begin{align}
        \alpha=&0,\\
        A=&R_z(\beta)R_y(\gamma/2)=\begin{bmatrix}
            e^{-i\beta/2}&0\\
            0&e^{i\beta/2}
        \end{bmatrix}\begin{bmatrix}
            \cos\frac{\gamma}{4}&-\sin\frac{\gamma}{4}\\
            \sin\frac{\gamma}{4}&\cos\frac{\gamma}{4}
        \end{bmatrix}=\begin{bmatrix}
            e^{-i\beta/2}\cos\frac{\gamma}{4}&-e^{-i\beta/2}\sin\frac{\gamma}{4}\\
            e^{i\beta/2}\sin\frac{\gamma}{4}&e^{i\beta/2}\cos\frac{\gamma}{4}
        \end{bmatrix}=\begin{bmatrix}
            \cos\frac{\theta}{4}&-\sin\frac{\theta}{4}\\
            \sin\frac{\theta}{4}&\cos\frac{\theta}{4}
        \end{bmatrix},\\
        \notag B=&R_y(-\gamma/2)R_z(-(\delta+\beta)/2)=\begin{bmatrix}
            \cos\left(-\frac{\gamma}{4}\right)&-\sin\left(-\frac{\gamma}{4}\right)\\
            \sin\left(-\frac{\gamma}{4}\right)&\cos\left(-\frac{\gamma}{4}\right)
        \end{bmatrix}\begin{bmatrix}
            e^{i(\delta+\beta)/4}&0\\
            0&e^{-i(\delta+\beta)/4}
        \end{bmatrix}=\begin{bmatrix}
            e^{i(\delta+\beta)/4}\cos\frac{\gamma}{4}&e^{-i(\delta+\beta)/4}\sin\frac{\gamma}{4}\\
            -e^{i(\delta+\beta)/4}\sin\frac{\gamma}{4}&e^{-i(\delta+\beta)/4}\cos\frac{\gamma}{4}
        \end{bmatrix}\\
        =&\begin{bmatrix}
            \cos\frac{\theta}{4}&\sin\frac{\theta}{4}\\
            -\sin\frac{\theta}{4}&\cos\frac{\theta}{4}
        \end{bmatrix},\\
        C=&R_z((\delta-\beta)/2)=\begin{bmatrix}
            e^{-i(\delta-\beta)/4}&0\\
            0&e^{i(\delta-\beta)/4}
        \end{bmatrix}=\begin{bmatrix}
            1&0\\
            0&1
        \end{bmatrix}.
    \end{align}
    Using the equation in Figure 4.6\footref{Fig-4.6-controlled-U-construction}, we can transform the $C^1(U)$ gate into
    \begin{center}
        \begin{quantikz}
            \qw & \ctrl{1} & \qw\\
            \qw & \gate{U} & \qw
        \end{quantikz}$=$\begin{quantikz}
            \qw & \ctrl{1} & \qw & \ctrl{1} & \qw & \qw\\
            \qw & \targ{} & \gate{B} & \targ{} & \gate{A} & \qw
        \end{quantikz}
    \end{center}
    where there is only two single qubit gates.
\end{sol}

\begin{exe}
    Verify that Figure 4.9\footnote{\label{Fig-4.9-Implementation-of-Toffoli-gate-using-Hadamard-phase-controlled-NOT-and-pi/8-gates}\begin{quantikz}
        \qw & \ctrl{1} & \qw\\
        \qw & \ctrl{1} & \qw\\
        \qw & \targ{} & \qw
    \end{quantikz}$=$\begin{quantikz}
        \qw & \qw & \qw & \qw & \ctrl{2} & \qw & \qw & \qw & \ctrl{2} & \qw & \ctrl{1} & \qw & \ctrl{1} & \gate{T} & \qw\\
        \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \ctrl{1} & \qw & \qw & \gate{T^{\dagger}} & \targ{} & \gate{T^{\dagger}} & \targ{} & \gate{S} & \qw\\
        \qw & \gate{H} & \targ{} & \gate{T^{\dagger}} & \targ{} & \gate{T} & \targ{} & \gate{T^{\dagger}} & \targ{} & \gate{T} & \gate{H} & \qw & \qw & \qw & \qw
    \end{quantikz}} implements the Toffoli gate.
\end{exe}
\begin{pf}
    Inputting the computational basis states to the Toffoli gate, the state changing process is shown in Table \ref{E4.24-state-changing-process-1}.
    \begin{table}[h]
        \centering
        \caption{Truth table of the Toffoli gate.}
        \label{E4.24-state-changing-process-1}
        \begin{tabular}{|c|c|c|}
        \hline
        & Input & Output \\ \hline
        \multirow{8}{*}{States} & $\lvert 000\rangle$ & $\lvert 000\rangle$ \\ \cline{2-3}
        & $\lvert 001\rangle$ & $\lvert 001\rangle$ \\ \cline{2-3}
        & $\lvert 010\rangle$ & $\lvert 010\rangle$ \\ \cline{2-3}
        & $\lvert 011\rangle$ & $\lvert 011\rangle$ \\ \cline{2-3}
        & $\lvert 100\rangle$ & $\lvert 100\rangle$ \\ \cline{2-3}
        & $\lvert 101\rangle$ & $\lvert 101\rangle$ \\ \cline{2-3}
        & $\lvert 110\rangle$ & $\lvert 111\rangle$ \\ \cline{2-3}
        & $\lvert 111\rangle$ & $\lvert 110\rangle$ \\ \hline
        \end{tabular}
    \end{table}\\
    Inputting the computational basis states to the right side of Figure 4.9\footref{Fig-4.9-Implementation-of-Toffoli-gate-using-Hadamard-phase-controlled-NOT-and-pi/8-gates}, the state changing process is shown in Table \ref{E4.24-state-changing-process-2}, which is the same with that of Toffoli gate.
    \begin{table}[h]
        \centering\scriptsize
        \caption{State changing process for the computational basis states inputs into the left side of Figure 4.9\textsuperscript{\ref{Fig-4.9-Implementation-of-Toffoli-gate-using-Hadamard-phase-controlled-NOT-and-pi/8-gates}}.}
        \label{E4.24-state-changing-process-2}
        \begin{tabular}{|c|c|c|c|c|}
            \hline
             & Input & After $1$st $H$ & After $1$st CNOT & After $1$st $T^{\dagger}$ \\ \hline
            \multirow{8}{*}{States} & $\lvert 000\rangle$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+e^{-i\pi/4}\lvert 001\rangle)$ \\ \cline{2-5}
             & $\lvert 001\rangle$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-e^{-i\pi/4}\lvert 001\rangle)$ \\ \cline{2-5}
             & $\lvert 010\rangle$ & $\frac{1}{\sqrt{2}}(\lvert 010\rangle+\lvert 011\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 011\rangle+\lvert 010\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 011\rangle+\lvert 010\rangle)$ \\ \cline{2-5}
             & $\lvert 011\rangle$ & $\frac{1}{\sqrt{2}}(\lvert 010\rangle-\lvert 011\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 011\rangle-\lvert 010\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 011\rangle-\lvert 010\rangle)$ \\ \cline{2-5}
             & $\lvert 100\rangle$ & $\frac{1}{\sqrt{2}}(\lvert 100\rangle+\lvert 101\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 100\rangle+\lvert 101\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 100\rangle+e^{-i\pi/4}\lvert 101\rangle)$ \\ \cline{2-5}
             & $\lvert 101\rangle$ & $\frac{1}{\sqrt{2}}(\lvert 100\rangle-\lvert 101\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 100\rangle-\lvert 101\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 100\rangle-e^{-i\pi/4}\lvert 101\rangle)$ \\ \cline{2-5}
             & $\lvert 110\rangle$ & $\frac{1}{\sqrt{2}}(\lvert 110\rangle+\lvert 111\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 111\rangle+\lvert 110\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 111\rangle+\lvert 110\rangle)$ \\ \cline{2-5}
             & $\lvert 111\rangle$ & $\frac{1}{\sqrt{2}}(\lvert 110\rangle-\lvert 111\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 111\rangle-\lvert 110\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 111\rangle-\lvert 110\rangle)$ \\ \hline\hline
             & After $2$nd CNOT & After $1$st $T$ & After $3$rd CNOT & After $2$nd $T^{\dagger}$ \\ \hline
            \multirow{8}{*}{States} & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+e^{-i\pi/4}\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+e^{-i\pi/4}\lvert 001\rangle)$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-e^{-i\pi/4}\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-e^{-i\pi/4}\lvert 001\rangle)$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 011\rangle+\lvert 010\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 011\rangle+\lvert 010\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 010\rangle+\lvert 011\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 010\rangle+e^{-i\pi/4}\lvert 011\rangle)$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 011\rangle-\lvert 010\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 011\rangle-\lvert 010\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 010\rangle-\lvert 011\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 010\rangle-e^{-i\pi/4}\lvert 011\rangle)$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(\lvert 101\rangle+e^{-i\pi/4}\lvert 100\rangle)$ & $\frac{1}{\sqrt{2}}(e^{i\pi/4}\lvert 101\rangle+e^{-i\pi/4}\lvert 100\rangle)$ & $\frac{1}{\sqrt{2}}(e^{i\pi/4}\lvert 101\rangle+e^{-i\pi/4}\lvert 100\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 101\rangle+e^{-i\pi/4}\lvert 100\rangle)$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(\lvert 101\rangle-e^{-i\pi/4}\lvert 100\rangle)$ & $\frac{1}{\sqrt{2}}(e^{i\pi/4}\lvert 101\rangle-e^{-i\pi/4}\lvert 100\rangle)$ & $\frac{1}{\sqrt{2}}(e^{i\pi/4}\lvert 101\rangle-e^{-i\pi/4}\lvert 100\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 101\rangle-e^{-i\pi/4}\lvert 100\rangle)$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 110\rangle+\lvert 111\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 110\rangle+e^{i\pi/4}\lvert 111\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 111\rangle+e^{i\pi/4}\lvert 110\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/2}\lvert 111\rangle+e^{i\pi/4}\lvert 110\rangle)$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 110\rangle-\lvert 111\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 110\rangle-e^{i\pi/4}\lvert 111\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/4}\lvert 111\rangle-e^{i\pi/4}\lvert 110\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i\pi/2}\lvert 111\rangle-e^{i\pi/4}\lvert 110\rangle)$ \\ \hline\hline
             & After $4$th CNOT & After $3$rd $T^{\dagger}$ and $2$nd $T$ & After $5$th CNOT and $2$nd $H$ & After $4$th $T^{\dagger}$ \\ \hline
            \multirow{8}{*}{States} & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+e^{-i\pi/4}\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle+\lvert 001\rangle)$ & $\lvert 000\rangle$ & $\lvert 000\rangle$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-e^{-i\pi/4}\lvert 001\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 000\rangle-\lvert 001\rangle)$ & $\lvert 001\rangle$ & $\lvert 001\rangle$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(\lvert 010\rangle+e^{-i\pi/4}\lvert 011\rangle)$ & $\frac{e^{-i\pi/4}}{\sqrt{2}}(\lvert 010\rangle+\lvert 011\rangle)$ & $e^{-i\pi/4}\lvert 010\rangle$ & $e^{-i\pi/2}\lvert 010\rangle$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(\lvert 010\rangle-e^{-i\pi/4}\lvert 011\rangle)$ & $\frac{e^{-i\pi/4}}{\sqrt{2}}(\lvert 010\rangle-\lvert 011\rangle)$ & $e^{-i\pi/4}\lvert 011\rangle$ & $e^{-i\pi/2}\lvert 011\rangle$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(\lvert 100\rangle+e^{-i\pi/4}\lvert 101\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 100\rangle+\lvert 101\rangle)$ & $\lvert 110\rangle$ & $e^{-i\pi/4}\lvert 110\rangle$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(\lvert 100\rangle-e^{-i\pi/4}\lvert 101\rangle)$ & $\frac{1}{\sqrt{2}}(\lvert 100\rangle-\lvert 101\rangle)$ & $\lvert 111\rangle$ & $e^{-i\pi/4}\lvert 111\rangle$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(e^{-i\pi/2}\lvert 110\rangle+e^{i\pi/4}\lvert 111\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i3\pi/4}\lvert 110\rangle+e^{i\pi/4}\lvert 111\rangle)$ & $-e^{i\pi/4}\lvert 101\rangle$ & $e^{i\pi/4}\lvert 100\rangle$ \\ \cline{2-5}
             & $\frac{1}{\sqrt{2}}(e^{-i\pi/2}\lvert 110\rangle-e^{i\pi/4}\lvert 111\rangle)$ & $\frac{1}{\sqrt{2}}(e^{-i3\pi/4}\lvert 110\rangle-e^{i\pi/4}\lvert 111\rangle)$ & $-e^{i\pi/4}\lvert 100\rangle$ & $e^{i\pi/4}\lvert 101\rangle$ \\ \hline\hline
             & After $6$th CNOT & Output &  &  \\ \hline
            \multirow{8}{*}{States} & $\lvert 000\rangle$ & $\lvert 000\rangle$ &  &  \\ \cline{2-5}
             & $\lvert 001\rangle$ & $\lvert 001\rangle$ &  &  \\ \cline{2-5}
             & $e^{-i\pi/2}\lvert 010\rangle$ & $\lvert 010\rangle$ &  &  \\ \cline{2-5}
             & $e^{-i\pi/2}\lvert 011\rangle$ & $\lvert 011\rangle$ &  &  \\ \cline{2-5}
             & $e^{-i\pi/4}\lvert 100\rangle$ & $\lvert 100\rangle$ &  &  \\ \cline{2-5}
             & $e^{-i\pi/4}\lvert 101\rangle$ & $\lvert 101\rangle$ &  &  \\ \cline{2-5}
             & $-e^{i\pi/4}\lvert 111\rangle$ & $\lvert 111\rangle$ &  &  \\ \cline{2-5}
             & $-e^{i\pi/4}\lvert 110\rangle$ & $\lvert 110\rangle$ &  &  \\ \hline
        \end{tabular}
    \end{table}

    Therefore, Figure 4.9\footref{Fig-4.9-Implementation-of-Toffoli-gate-using-Hadamard-phase-controlled-NOT-and-pi/8-gates} implements Toffoli gate.
\end{pf}

\begin{exe}[Fredkin gate construction]
    Recall that the Fredkin (controlled-swap) gate performs the transform
    \begin{align}
        \begin{bmatrix}
            1&0&0&0&0&0&0&0\\
            0&1&0&0&0&0&0&0\\
            0&0&1&0&0&0&0&0\\
            0&0&0&1&0&0&0&0\\
            0&0&0&0&1&0&0&0\\
            0&0&0&0&0&0&1&0\\
            0&0&0&0&0&1&0&0\\
            0&0&0&0&0&0&0&1
        \end{bmatrix}.
    \end{align}
    \begin{itemize}
        \item[(1)] Give a quantum circuit which use three Toffoli gates to construct the Fredkin gate (\emph{Hint}: think of the swap gate Construction --- you can control each gate, one at a time).
        \item[(2)] Show that the first and the last Toffoli gates can be replaced by CNOT gates.
        \item[(3)] Now replace the middle Toffoli gate with the circuit in Figure 4.8\footref{Fig-4.8-Circuit-for-the-C2(U)-gate} to obtain a Fredkin gate construction using only six two-qubit gates.
        \item[(4)] Can you come up with an even simpler construction, with only five two-qubit gates?
    \end{itemize}
\end{exe}
\begin{sol}
    \begin{itemize}
        \item[(1)] According to Figure 1.7 in the textbook, the swap gate can be constructed with three CNOT gate,
        \begin{center}
            \begin{quantikz}
                \qw & \swap{1} & \qw\\
                \qw & \targX{} & \qw
            \end{quantikz}$\equiv$
            \begin{quantikz}
                \qw & \ctrl{1} & \targ{} & \ctrl{1} & \qw\\
                \qw & \targ{} & \ctrl{-1} & \targ{} & \qw
            \end{quantikz}
        \end{center}
        Thus, we can use three Toffoli gates to construct the Fredkin gate,
        \begin{center}
            \begin{quantikz}
                \qw & \ctrl{2} & \qw\\
                \qw & \swap{1} & \qw\\
                \qw & \targX{} & \qw
            \end{quantikz}$=$
            \begin{quantikz}
                \qw & \ctrl{1} & \ctrl{1} & \ctrl{1} & \qw\\
                \qw & \ctrl{1} & \targ{} & \ctrl{1} & \qw\\
                \qw & \targ{} & \ctrl{-1} & \targ{} & \qw
            \end{quantikz}
        \end{center}
        \item[(2)] If we replace the first and the last Toffoli with CNOT gates and get
        \begin{center}
            \begin{quantikz}
                \qw & \qw & \ctrl{1} & \qw & \qw\\
                \qw & \ctrl{1} & \targ{} & \ctrl{1} & \qw\\
                \qw & \targ{} & \ctrl{-1} & \targ{} & \qw
            \end{quantikz}
        \end{center}
        the unitary matrix of the quantum circuit is
        \begin{align}
            \notag&\text{CNOT}\times\text{C}^2\text{NOT}\times\text{CNOT}\\
            \notag=&\begin{bmatrix}
                1&0&0&0&0&0&0&0\\
                0&1&0&0&0&0&0&0\\
                0&0&0&1&0&0&0&0\\
                0&0&1&0&0&0&0&0\\
                0&0&0&0&1&0&0&0\\
                0&0&0&0&0&1&0&0\\
                0&0&0&0&0&0&0&1\\
                0&0&0&0&0&0&1&0
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0&0&0&0&0\\
                0&1&0&0&0&0&0&0\\
                0&0&1&0&0&0&0&0\\
                0&0&0&1&0&0&0&0\\
                0&0&0&0&1&0&0&0\\
                0&0&0&0&0&0&0&1\\
                0&0&0&0&0&0&1&0\\
                0&0&0&0&0&1&0&0
            \end{bmatrix}\begin{bmatrix}
                1&0&0&0&0&0&0&0\\
                0&1&0&0&0&0&0&0\\
                0&0&0&1&0&0&0&0\\
                0&0&1&0&0&0&0&0\\
                0&0&0&0&1&0&0&0\\
                0&0&0&0&0&1&0&0\\
                0&0&0&0&0&0&0&1\\
                0&0&0&0&0&0&1&0
            \end{bmatrix}\\
            =&\begin{bmatrix}
                1&0&0&0&0&0&0&0\\
                0&1&0&0&0&0&0&0\\
                0&0&1&0&0&0&0&0\\
                0&0&0&1&0&0&0&0\\
                0&0&0&0&1&0&0&0\\
                0&0&0&0&0&0&1&0\\
                0&0&0&0&0&1&0&0\\
                0&0&0&0&0&0&0&1
            \end{bmatrix},
        \end{align}
        which is the same as that of Fredkin.

        Therefore, the first and the last Toffoli gate can be replaced by CNOT gates.
        \item[(3)] Replacing the middle Toffoli gate with circuit in Figure 4.8\footref{Fig-4.8-Circuit-for-the-C2(U)-gate}, we get
        \begin{center}
            \begin{quantikz}
                \qw & \qw & \qw & \ctrl{2} & \qw & \ctrl{2} & \ctrl{1} & \qw & \qw\\
                \qw & \ctrl{1} & \gate{V} & \qw & \gate{V^{\dagger}} & \qw & \gate{V} & \ctrl{1} & \qw\\
                \qw & \targ{} & \ctrl{-1} & \targ{} & \ctrl{-1} & \targ{} & \qw & \targ{} & \qw
            \end{quantikz}
        \end{center}
        where
        \begin{align}
            V=\frac{1-i}{2}(I+iX)=e^{-i\pi/4}R_x\left(-\frac{\pi}{4}\right)=\frac{1-i}{2}\begin{bmatrix}
                1&i\\
                i&1
            \end{bmatrix}.
        \end{align}
        Combining the first two CNOT gate and the first controlled-$V$ gate into one two-qubit gate, we get
        \begin{center}
            \begin{quantikz}
                \qw & \qw & \ctrl{2} & \qw & \ctrl{2} & \ctrl{1} & \qw & \qw\\
                \qw & \gate[wires=2]{W} & \qw & \gate{V^{\dagger}} & \qw & \gate{V} & \ctrl{1} & \qw\\
                \qw & & \targ{} & \ctrl{-1} & \targ{} & \qw & \targ{} & \qw
            \end{quantikz}
        \end{center}
        where
        \begin{align}
            W=\text{CNOT}\times\text{controlled-}V=\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&0&1\\
                0&0&1&0
            \end{bmatrix}\times\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&\frac{1-i}{2}&\frac{i+1}{2}\\
                0&0&\frac{i+1}{2}&\frac{1-i}{2}
            \end{bmatrix}=\begin{bmatrix}
                1&0&0&0\\
                0&1&0&0\\
                0&0&\frac{i+1}{2}&\frac{1-i}{2}\\
                0&0&\frac{1-i}{2}&\frac{i+1}{2}
            \end{bmatrix}.
        \end{align}
        In this way, we obtain a Fredkin gate construction using only six two-qubit gates.
        \item[(4)] Using Corollary \ref{E4.25-cor-2}, we can move the last controlled-$V$ gate to behind $W$ gate and get
        \begin{center}
            \begin{quantikz}
                \qw & \qw & \ctrl{1} & \ctrl{2} & \qw & \ctrl{2} & \qw & \qw\\
                \qw & \gate[wires=2]{W} & \gate{V} & \qw & \gate{V^{\dagger}} & \qw & \ctrl{1} & \qw\\
                \qw & & \qw & \targ{} & \ctrl{-1} & \targ{} & \targ{} & \qw
            \end{quantikz}
        \end{center}
        Using Corollary , we exchage the last two CNOT gate and combine the last controlled-$V^{\dagger}$ and CNOT gate into one two-qubit gate, we get
        \begin{center}
            \begin{quantikz}
                \qw & \qw & \ctrl{1} & \ctrl{2} & \qw & \ctrl{2} & \qw\\
                \qw & \gate[wires=2]{W} & \gate{V} & \qw & \gate[wires=2]{W^{\dagger}} & \qw & \qw\\
                \qw & & \qw & \targ{} & & \targ{} & \qw
            \end{quantikz}
        \end{center}
        which is a Fredkin gate construction using only five two-qubit gates.
        \begin{cor}
            \label{E4.25-cor-1}
            \begin{quantikz}
                \qw & \ctrl{1} & \qw\\
                \qw & \gate{V} & \qw\\
                \qw & \qw & \qw
            \end{quantikz}
            commutes with
            \begin{quantikz}
                \qw & \qw & \qw\\
                \qw & \gate{V^{\dagger}} & \qw\\
                \qw & \ctrl{-1} & \qw
            \end{quantikz}.
        \end{cor}
        \begin{pf}
            The unitary matrix of the first circuit is
            \begin{align}
                \begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}&0\\
                    0&0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}\\
                    0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}&0\\
                    0&0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}
                \end{bmatrix}.
            \end{align}
            The unitary matrix of the second circuit is
            \begin{align}
                \begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&\frac{1-i}{2}&0&\frac{i+1}{2}&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&\frac{i+1}{2}&0&\frac{1-i}{2}&0&0&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}\\
                    0&0&0&0&0&0&1&0\\
                    0&0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}
                \end{bmatrix}.
            \end{align}
            Since
            \begin{align}
                \notag&\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}&0\\
                    0&0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}\\
                    0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}&0\\
                    0&0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}
                \end{bmatrix}\times\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&\frac{1-i}{2}&0&\frac{i+1}{2}&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&\frac{i+1}{2}&0&\frac{1-i}{2}&0&0&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}\\
                    0&0&0&0&0&0&1&0\\
                    0&0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}
                \end{bmatrix}\\
                \notag=&\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&\frac{1-i}{2}&0&\frac{1+i}{2}&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&\frac{1+i}{2}&0&\frac{1-i}{2}&0&0&0&0\\
                    0&0&0&0&\frac{1-i}{2}&0&\frac{1+i}{2}&0\\
                    0&0&0&0&0&0&0&1\\
                    0&0&0&0&\frac{1+i}{2}&0&\frac{1-i}{2}&0\\
                    0&0&0&0&0&1&0&0
                \end{bmatrix}\\
                =&\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&\frac{1-i}{2}&0&\frac{i+1}{2}&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&\frac{i+1}{2}&0&\frac{1-i}{2}&0&0&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}\\
                    0&0&0&0&0&0&1&0\\
                    0&0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}
                \end{bmatrix}\times\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}&0\\
                    0&0&0&0&0&\frac{1-i}{2}&0&\frac{i+1}{2}\\
                    0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}&0\\
                    0&0&0&0&0&\frac{i+1}{2}&0&\frac{1-i}{2}
                \end{bmatrix},
            \end{align}
            Corollary \ref{E4.25-cor-1} holds.
        \end{pf}
        \begin{cor}
            \label{E4.25-cor-2}
            \begin{quantikz}
                \qw & \ctrl{2} & \qw\\
                \qw & \qw & \qw\\
                \qw & \targ{} & \qw
            \end{quantikz}
            commutes with
            \begin{quantikz}
                \qw & \qw & \qw\\
                \qw & \ctrl{1} & \qw\\
                \qw & \targ{} & \qw
            \end{quantikz}.
        \end{cor}
        \begin{pf}
            The unitary matrix of the first circuit is
            \begin{align}
                \begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&0&0&0&1&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&0&0&1\\
                    0&0&0&0&0&0&1&0
                \end{bmatrix}.
            \end{align}
            The unitary matrix of the second circuit is
            \begin{align}
                \begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&1&0&0\\
                    0&0&0&0&0&0&0&1\\
                    0&0&0&0&0&0&1&0
                \end{bmatrix}.
            \end{align}
            Since
            \begin{align}
                \notag&\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&0&0&0&1&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&0&0&1\\
                    0&0&0&0&0&0&1&0
                \end{bmatrix}\times\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&1&0&0\\
                    0&0&0&0&0&0&0&1\\
                    0&0&0&0&0&0&1&0
                \end{bmatrix}\\
                \notag=&\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&0&0&1&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&0&1&0\\
                    0&0&0&0&0&0&0&1
                \end{bmatrix}\\
                =&\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&1&0&0\\
                    0&0&0&0&0&0&0&1\\
                    0&0&0&0&0&0&1&0
                \end{bmatrix}\times\begin{bmatrix}
                    1&0&0&0&0&0&0&0\\
                    0&1&0&0&0&0&0&0\\
                    0&0&1&0&0&0&0&0\\
                    0&0&0&1&0&0&0&0\\
                    0&0&0&0&0&1&0&0\\
                    0&0&0&0&1&0&0&0\\
                    0&0&0&0&0&0&0&1\\
                    0&0&0&0&0&0&1&0
                \end{bmatrix},
            \end{align}
            Corollary \ref{E4.25-cor-2} holds.
        \end{pf}
    \end{itemize}
\end{sol}

\begin{exe}
    Show that the circuit:
    \begin{center}
        \begin{quantikz}
            \qw & \qw & \qw & \qw & \ctrl{2} & \qw & \qw & \qw & \qw\\
            \qw & \qw & \ctrl{1} & \qw & \qw & \qw & \ctrl{1} & \qw & \qw\\
            \qw & \gate{R_y(\pi/4)} & \targ{} & \gate{R_y(\pi/4)} & \targ{} & \gate{R_y(-\pi/4)} & \targ{} & \gate{R_y(-\pi/4)} & \qw
        \end{quantikz}
    \end{center}
    differs from a Toffoli gate only by relative phases. That is, the circuit takes $\lvert c_1,c_2,t\rangle$ to $e^{i\theta}\lvert c_1,c_2,t\oplus c_1\cdot c_2\rangle$, where $e^{i\theta(c_1,c_2,t)}$ is some relative phase factor. Such gates can sometimes be useful in experimental implementations, where it may be much easier to implement a gate that is the same as the Toffoli up to relative phases that it is to do the Toffoli directly.
\end{exe}
\begin{pf}
    Inputting the computational basis states to the above circuit, the state changing process is shown in Table \ref{E4.26-State-Changing-Process},
    \begin{table}[h]
        \centering
        \caption{State changing process for the computational basis states inputs into the above circuit.}
        \label{E4.26-State-Changing-Process}
        \begin{tabular}{|c|c|c|c|c|}
            \hline
             & Input & After $1$st $R_y(\pi/4)$ & After $1$st CNOT & After $2$nd $R_y(\pi/4)$ \\ \hline
            \multirow{8}{*}{States} & $\lvert 000\rangle$ & $\cos\frac{\pi}{8}\lvert 000\rangle+\sin\frac{\pi}{8}\lvert 001\rangle$ & $\cos\frac{\pi}{8}\lvert 000\rangle+\sin\frac{\pi}{8}\lvert 001\rangle$ & $\cos\frac{\pi}{4}\lvert 000\rangle+\sin\frac{\pi}{4}\lvert 001\rangle$ \\ \cline{2-5}
             & $\lvert 001\rangle$ & $-\sin\frac{\pi}{8}\lvert 000\rangle+\cos\frac{\pi}{8}\lvert 001\rangle$ & $-\sin\frac{\pi}{8}\lvert 000\rangle+\cos\frac{\pi}{8}\lvert 001\rangle$ & $-\sin\frac{\pi}{4}\lvert 000\rangle+\cos\frac{\pi}{4}\lvert 001\rangle$ \\ \cline{2-5}
             & $\lvert 010\rangle$ & $\cos\frac{\pi}{8}\lvert 010\rangle+\sin\frac{\pi}{8}\lvert 011\rangle$ & $\cos\frac{\pi}{8}\lvert 011\rangle+\sin\frac{\pi}{8}\lvert 010\rangle$ & $\lvert 011\rangle$ \\ \cline{2-5}
             & $\lvert 011\rangle$ & $-\sin\frac{\pi}{8}\lvert 010\rangle+\cos\frac{\pi}{8}\lvert 011\rangle$ & $-\sin\frac{\pi}{8}\lvert 011\rangle+\cos\frac{\pi}{8}\lvert 010\rangle$ & $\lvert 010\rangle$ \\ \cline{2-5}
             & $\lvert 100\rangle$ & $\cos\frac{\pi}{8}\lvert 100\rangle+\sin\frac{\pi}{8}\lvert 101\rangle$ & $\cos\frac{\pi}{8}\lvert 100\rangle+\sin\frac{\pi}{8}\lvert 101\rangle$ & $\cos\frac{\pi}{4}\lvert 100\rangle+\sin\frac{\pi}{4}\lvert 101\rangle$ \\ \cline{2-5}
             & $\lvert 101\rangle$ & $-\sin\frac{\pi}{8}\lvert 100\rangle+\cos\frac{\pi}{8}\lvert 101\rangle$ & $-\sin\frac{\pi}{8}\lvert 100\rangle+\cos\frac{\pi}{8}\lvert 101\rangle$ & $-\sin\frac{\pi}{4}\lvert 100\rangle+\cos\frac{\pi}{4}\lvert 101\rangle$ \\ \cline{2-5}
             & $\lvert 110\rangle$ & $\cos\frac{\pi}{8}\lvert 110\rangle+\sin\frac{\pi}{8}\lvert 111\rangle$ & $\cos\frac{\pi}{8}\lvert 111\rangle+\sin\frac{\pi}{8}\lvert 110\rangle$ & $\lvert 111\rangle$ \\ \cline{2-5}
             & $\lvert 111\rangle$ & $-\sin\frac{\pi}{8}\lvert 110\rangle+\cos\frac{\pi}{8}\lvert 111\rangle$ & $-\sin\frac{\pi}{8}\lvert 111\rangle+\cos\frac{\pi}{8}\lvert 110\rangle$ & $\lvert 110\rangle$ \\ \hline\hline
             & After $2$nd CNOT & After $1$st $R_y(-pi/4)$ & After $3$rd CNOT & Output \\ \hline
            \multirow{8}{*}{States} & $\cos\frac{\pi}{4}\lvert 000\rangle+\sin\frac{\pi}{4}\lvert 001\rangle$ & $\cos\frac{\pi}{8}\lvert 000\rangle+\sin\frac{\pi}{8}\lvert 001\rangle$ & $\cos\frac{\pi}{8}\lvert 000\rangle+\sin\frac{\pi}{8}\lvert 001\rangle$ & $\lvert 000\rangle$ \\ \cline{2-5}
             & $-\sin\frac{\pi}{4}\lvert 000\rangle+\cos\frac{\pi}{4}\lvert 001\rangle$ & $-\sin\frac{\pi}{8}\lvert 000\rangle+\cos\frac{\pi}{8}\lvert 001\rangle$ & $-\sin\frac{\pi}{8}\lvert 000\rangle+\cos\frac{\pi}{8}\lvert 001\rangle$ & $\lvert 001\rangle$ \\ \cline{2-5}
             & $\lvert 011\rangle$ & $\sin\frac{\pi}{8}\lvert 010\rangle+\cos\frac{\pi}{8}\lvert 011\rangle$ & $\sin\frac{\pi}{8}\lvert 011\rangle+\cos\frac{\pi}{8}\lvert 010\rangle$ & $\lvert 010\rangle$ \\ \cline{2-5}
             & $\lvert 010\rangle$ & $\cos\frac{\pi}{8}\lvert 010\rangle-\sin\frac{\pi}{8}\lvert 011\rangle$ & $\cos\frac{\pi}{8}\lvert 011\rangle-\sin\frac{\pi}{8}\lvert 010\rangle$ & $\lvert 011\rangle$ \\ \cline{2-5}
             & $\cos\frac{\pi}{4}\lvert 101\rangle+\sin\frac{\pi}{4}\lvert 100\rangle$ & $\sin\frac{3\pi}{8}\lvert 100\rangle+\cos\frac{3\pi}{8}\lvert 101\rangle$ & $\sin\frac{3\pi}{8}\lvert 100\rangle+\cos\frac{3\pi}{8}\lvert 101\rangle$ & $\lvert 100\rangle$ \\ \cline{2-5}
             & $-\sin\frac{\pi}{4}\lvert 101\rangle+\cos\frac{\pi}{4}\lvert 100\rangle$ & $\cos\frac{3\pi}{8}\lvert 100\rangle-\sin\frac{3\pi}{8}\lvert 101\rangle$ & $\cos\frac{3\pi}{8}\lvert 100\rangle-\sin\frac{3\pi}{8}\lvert 101\rangle$ & $-\lvert 101\rangle$ \\ \cline{2-5}
             & $\lvert 110\rangle$ & $\cos\frac{\pi}{8}\lvert 110\rangle-\sin\frac{\pi}{8}\lvert 111\rangle$ & $\cos\frac{\pi}{8}\lvert 111\rangle-\sin\frac{\pi}{8}\lvert 110\rangle$ & $\lvert 111\rangle$ \\ \cline{2-5}
             & $\lvert 111\rangle$ & $\sin\frac{\pi}{8}\lvert 110\rangle+\cos\frac{\pi}{8}\lvert 111\rangle$ & $\sin\frac{\pi}{8}\lvert 111\rangle+\cos\frac{\pi}{8}\lvert 110\rangle$ & $\lvert 110\rangle$ \\ \hline
            \end{tabular}
    \end{table}\\
    which is the same as that of Toffoli gate shown in Table \ref{E4.24-state-changing-process-1}.

    Therefore, the above circuit differs from Toffoli gate only by relative phases.
\end{pf}

\begin{exe}
    Using just CNOTs and Toffoli gats, construct a quantum circuit to perform the transformation
    \begin{align}
        \begin{bmatrix}
            1&0&0&0&0&0&0&0\\
            0&0&0&0&0&0&0&1\\
            0&1&0&0&0&0&0&0\\
            0&0&1&0&0&0&0&0\\
            0&0&0&1&0&0&0&0\\
            0&0&0&0&1&0&0&0\\
            0&0&0&0&0&1&0&0\\
            0&0&0&0&0&0&1&0
        \end{bmatrix}
    \end{align}
    This kind of partial cyclic permutation operation will be useful later, in Chapter 7.
\end{exe}
\begin{sol}
    Quantum circuit
    \begin{center}
        \begin{quantikz}
            \qw & \qw & \qw & \targ{} & \qw\\
            \qw & \ctrl{1} & \targ{} & \ctrl{-1} & \qw\\
            \qw & \targ{} & \ctrl{-1} & \ctrl{-2} & \qw
        \end{quantikz}
    \end{center}
    uses just CNOTs and Toffoli gates and its unitary matrix is
    \begin{align}
        \notag&\begin{bmatrix}
            1&0&0&0&0&0&0&0\\
            0&1&0&0&0&0&0&0\\
            0&0&0&1&0&0&0&0\\
            0&0&1&0&0&0&0&0\\
            0&0&0&0&1&0&0&0\\
            0&0&0&0&0&1&0&0\\
            0&0&0&0&0&0&0&1\\
            0&0&0&0&0&0&1&0
        \end{bmatrix}\begin{bmatrix}
            1&0&0&0&0&0&0&0\\
            0&0&0&1&0&0&0&0\\
            0&0&1&0&0&0&0&0\\
            0&1&0&0&0&0&0&0\\
            0&0&0&0&1&0&0&0\\
            0&0&0&0&0&0&0&1\\
            0&0&0&0&0&0&1&0\\
            0&0&0&0&0&1&0&0
        \end{bmatrix}\begin{bmatrix}
            1&0&0&0&0&0&0&0\\
            0&1&0&0&0&0&0&0\\
            0&0&1&0&0&0&0&0\\
            0&0&0&0&0&0&0&1\\
            0&0&0&0&1&0&0&0\\
            0&0&0&0&0&1&0&0\\
            0&0&0&0&0&0&1&0\\
            0&0&0&1&0&0&0&0
        \end{bmatrix}\\
        =&\begin{bmatrix}
            1&0&0&0&0&0&0&0\\
            0&0&0&0&0&0&0&1\\
            0&1&0&0&0&0&0&0\\
            0&0&1&0&0&0&0&0\\
            0&0&0&1&0&0&0&0\\
            0&0&0&0&1&0&0&0\\
            0&0&0&0&0&1&0&0\\
            0&0&0&0&0&0&1&0
        \end{bmatrix},
    \end{align}
    which is the same as the above transformation matrix.
    Thus, this quantum circuit performs the above transformation.
\end{sol}

\begin{exe}
    For $U=V^2$ with $V$ unitary, construct a $C^5(U)$ gate analogous to that in Figure 4.10\footnote{\label{Fig-4.10-Network-implementing-the-Cn(U)-operation,for-use-n=5}\begin{quantikz}
        \lstick[wires=5]{control qubits} & \lstick{$\lvert c_1\rangle$} & \ctrl{1} & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{1}\\
        & \lstick{$\lvert c_2\rangle$} & \ctrl{4} & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{4}\\
        & \lstick{$\lvert c_3\rangle$} & \qw & \ctrl{3} & \qw & \qw & \qw & \qw & \qw & \ctrl{3} & \qw\\
        & \lstick{$\lvert c_4\rangle$} & \qw & \qw & \ctrl{3} & \qw & \qw & \qw & \ctrl{3} & \qw & \qw\\
        & \lstick{$\lvert c_5\rangle$} & \qw & \qw & \qw & \ctrl{3} & \qw & \ctrl{3} & \qw & \qw & \qw\\
        \lstick[wires=4]{work qubits} & \lstick{$\lvert 0\rangle$} & \targ{} & \ctrl{1} & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \targ{}\\
        & \lstick{$\lvert 0\rangle$} & \qw & \targ{} & \ctrl{1} & \qw & \qw & \qw & \ctrl{1} & \targ{} & \qw\\
        & \lstick{$\lvert 0\rangle$} & \qw & \qw & \targ{} & \ctrl{1} & \qw & \ctrl{1} & \targ{} & \qw & \qw\\
        & \lstick{$\lvert 0\rangle$} & \qw & \qw & \qw & \targ{} & \ctrl{1} & \targ{} & \qw & \qw & \qw\\
        \lstick{target qubit} & & \qw & \qw & \qw & \qw & \gate{U} & \qw & \qw & \qw & \qw\\
    \end{quantikz}}, but using no work qubits. You may use controlled-$V$ and controlled-$V^{\dagger}$ gates.
\end{exe}
\begin{sol}
    Iterating based on Figure 4.8\footref{Fig-4.8-Circuit-for-the-C2(U)-gate}, we get quantum circuit
    \begin{center}
        \begin{quantikz}
            \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \ctrl{5} & \qw\\
            \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \ctrl{4} & \targ{} & \ctrl{4} & \targ{} & \qw & \qw\\
            \qw & \qw & \qw & \qw & \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \ctrl{3} & \targ{} & \ctrl{3} & \targ{} & \qw & \qw & \qw & \qw & \qw & \qw\\
            \qw & \qw & \ctrl{1} & \qw & \ctrl{1} & \ctrl{2} & \targ{} & \ctrl{2} & \targ{} & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw\\
            \qw & \ctrl{1} & \targ{} & \ctrl{1} & \targ{} & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw & \qw\\
            \qw & \gate{V} & \qw & \gate{V^{\dagger}} & \qw & \gate{V} & \qw & \gate{V^{\dagger}} & \qw & \gate{V} & \qw & \gate{V^{\dagger}} & \qw & \gate{V} & \qw & \gate{V^{\dagger}} & \qw & \gate{V} & \qw
        \end{quantikz}
    \end{center}
    where $V^2=U$, which constructs a $C^5(U)$ gate analogous to that in Figure 4.10\footref{Fig-4.10-Network-implementing-the-Cn(U)-operation,for-use-n=5}, using no work qubits but only controlled-$V$ and controlled-$V^{\dagger}$ gates (and CNOT gates).
\end{sol}

\begin{exe}
    Find a circuit containing $O(n^2)$ Toffoli, CNOT and single qubit gates which implements a $C^n(X)$ gate (for $n>3$), using no work qubits.
\end{exe}
\begin{sol}

\end{sol}

\ifx\allfiles\undefined
\end{document}
\fi