Quantum Computing · Chapter 3

Qubits & Quantum Gates

A qubit is the quantum version of a bit: instead of being just 0 or 1, it lives on a whole sphere of possible states. This chapter builds the toolkit of quantum computing from the ground up — how to picture a qubit on the Bloch sphere, the single- and multi-qubit gates that rotate and entangle qubits, and the deep result that a tiny handful of gates is enough to build any quantum computation. Along the way we work many concrete matrix examples so the abstract ideas stay grounded.

≈ 90 min read42 worked examplesOriginal PDF ↗

In this chapter we explore quantum computation in detail. This chapter develops the fundamental principles of quantum computation, and establishes the basic building blocks for quantum circuits — a universal language for describing sophisticated quantum computations. It introduces the fundamental model of quantum computation, the quantum circuit model, and the concept that there exists a small set of gates which are universal: any quantum computation whatsoever can be expressed in those gates, along with some basic results of quantum computation.

Many interesting problems are impossible to solve on a classical computer, not because they are in principle insoluble, but because of the astronomical resources required to solve realistic cases of the problem. The spectacular promise of quantum computers is to enable new algorithms which render feasible problems requiring exorbitant resources for their solution on a classical computer. Two broad classes of quantum algorithms are known which fulfill this promise. The first class of algorithms is based upon Shor's quantum Fourier transform, and includes remarkable algorithms for solving the factoring and discrete logarithm problems, providing an exponential speedup over the best known classical algorithms. The second class of algorithms is based upon Grover's algorithm for performing quantum searching. These provide a less striking but still substantial speedup over classical algorithms.

Why are there so few quantum algorithms known which are better than their classical counterparts? The answer is that coming up with a good quantum algorithm seems to be a difficult problem. There are at least two reasons for this. First, algorithm design, be it classical or quantum, is not easy. The history of algorithms shows us that considerable ingenuity is often required to come up with near-optimal algorithms, even for apparently simple problems like the multiplication of two numbers. Finding a good quantum algorithm is made doubly difficult because of the additional constraint that we want our quantum algorithms to be better than the best known classical algorithm. A second reason is that our intuitions are better adapted to the classical world than they are to the quantum world. If we think about our problems using our native intuition, then the algorithms we come up with are going to be classical algorithms. It takes special insights and special tricks to come up with good quantum algorithms.

To quantify the cost we use the total number of gates required, or the circuit depth. The circuit language also comes with a toolbox of tricks that simplifies algorithm design and provides ready conceptual understanding.

3.1 Phase

‘Phase’ is a commonly used term in quantum mechanics, with several different meanings dependent upon context. At this point it is convenient to review a couple of these meanings. Consider, for example, the state \(e^{\iota \theta}\ket{\psi}\), where \(\ket{\psi}\) is a state vector, and \(\theta\) is a real number. We say that the state \(e^{\iota \theta}\ket{\psi}\) is equal to \(\ket{\psi}\), up to a global phase factor \(e^{\iota \theta}\). It is interesting to note that the statistics of measurement predicted for these two states are the same. To see this, suppose \(M_m\) is a measurement operator associated to some quantum measurement, and note that the respective probabilities for outcome \(m\) occurring are \(\bra{\psi}M_m^{\dagger}M_m\ket{\psi}\) and \(\bra{\psi}e^{-\iota \theta}M_m^{\dagger} M_m e^{\iota \theta} \ket{\psi}=\bra{\psi}M_m^{\dagger}M_m\ket{\psi}\). Therefore, from an observational point of view these two states are identical. For this reason we may ignore global phase factors as being irrelevant to the observed properties of the physical system.

There is another kind of phase known as the relative phase, which has quite a different meaning. Consider the states

\[ \frac{\ket{0}+\ket{1}}{\sqrt{2}}\quad \text{and} \quad \frac{\ket{0}-\ket{1}}{\sqrt{2}} \]

In the first state the amplitude of \(\ket{1}\) is \(\frac{1}{\sqrt{2}}\). For the second state the amplitude is \(-\frac{1}{\sqrt{2}}\). In each case the magnitude of the amplitude is the same, but they differ in sign. More generally, we say that two amplitudes, \(a\) and \(b\), differ by a relative phase if there is a real \(\theta\) such that \(a=e^{\iota \theta}b\). More generally still, two states are said to differ by a relative phase factor in some basis if each of the amplitudes in that basis is related by such a phase factor. For example, the two states displayed above are the same up to a relative phase shift because the \(\ket{0}\) amplitudes are identical (a relative phase factor of \(1\)), and the \(\ket{1}\) amplitudes differ only by a relative phase factor of \(-1\). The difference between relative phase factors and global phase factors is that for relative phase factors the phase factors may vary from amplitude to amplitude. This makes the relative phase a basis-dependent concept, unlike global phase. As a result, states which differ only by relative phases in some basis give rise to physically observable differences in measurement statistics, and it is not possible to regard these states as physically equivalent, as we do with states differing by a global phase factor.

In short: multiplying an entire state by a single phase (a global phase) changes nothing you can ever measure, so we throw it away. But changing the phase of just one amplitude relative to another (a relative phase) is real physics — it is exactly what distinguishes \(\ket{+}\) from \(\ket{-}\).
Worked Example

Express the states \((\ket{0}+\ket{1})/\sqrt{2}\) and \((\ket{0}-\ket{1})/\sqrt{2}\) in a basis in which they are not the same up to a relative phase shift.

Show solution

In the \(\ket{+}, \ket{-}\) basis they will not be the same, even up to a relative phase shift. (Indeed, in that basis the two states are just \(\ket{+}\) and \(\ket{-}\) themselves, which are orthogonal.)

3.2 Bloch–Poincaré Sphere Representation of a Qubit

Consider a qubit in an arbitrary state \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\), where \(\ket{0}\) and \(\ket{1}\) are orthonormal basis states and \(\alpha,\beta\) are complex numbers such that \(|\alpha|^2+|\beta|^2=1\). How many real parameters are required to specify the state of a qubit? Since \(\alpha\) and \(\beta\) are complex numbers, we can write them as \(\alpha=|\alpha|e^{\iota \phi_{\alpha}}\) and \(\beta = |\beta| e^{\iota \phi_{\beta}}\). Here \(|\alpha|\) and \(|\beta|\) are real numbers and \(\phi_{\alpha}\), \(\phi_{\beta}\) are real numbers. Thus we need 4 real numbers to specify the state of a qubit. Now putting the condition that \(|\alpha|^2+|\beta|^2=1\), we can write \(|\beta|^2=1-|\alpha|^2 \implies |\beta|=\sqrt{1-|\alpha|^2}\). Thus we can rewrite the state of a qubit as:

\[ \ket{\psi}=|\alpha|e^{\iota \phi_{\alpha}}\ket{0}+\sqrt{1-|\alpha|^2}e^{\iota \phi_{\beta}}\ket{1} \]

Now the unknowns are \(|\alpha|,\phi_{\alpha},\phi_{\beta}\). Taking \(e^{\iota \phi_{\alpha}}\) common we get:

\[ \ket{\psi}=e^{\iota \phi_{\alpha}}\left(|\alpha|\ket{0}+\sqrt{1-|\alpha|^2}e^{\iota(\phi_{\beta}-\phi_{\alpha})}\ket{1}\right) \]

Now, we know that the state of a qubit is defined up to a global phase. Thus, ignoring the term \(e^{\iota \phi_{\alpha}}\) we can write the state of a qubit as:

\[ \ket{\psi}=|\alpha|\ket{0}+\sqrt{1-|\alpha|^2}e^{\iota \phi}\ket{1} \]

where \(\phi=\phi_{\beta}-\phi_{\alpha}\) is the relative phase between the states \(\ket{0}\) and \(\ket{1}\). Now, on substituting \(|\alpha|=\cos(\theta/2)\), \(\sqrt{1-|\alpha|^2}=\sin(\theta/2)\), we get:

\[ \ket{\psi}=\cos\left(\frac{\theta}{2}\right)\ket{0}+e^{\iota \phi}\sin\left(\frac{\theta}{2}\right)\ket{1} \]

This cannot be further simplified to reduce the number of variables. Thus we can clearly see that we need two real numbers — \(\theta\) and \(\phi\) — to specify the state of a qubit. The state of a qubit can be represented as a point on the surface of a unit sphere in three-dimensional space. This sphere is called the Bloch Sphere. A visual representation of the Bloch Sphere (or Bloch–Poincaré Sphere) is shown in the figure below.

Diagram of the Bloch sphere showing the polar angle theta and azimuthal angle phi locating a qubit state on the unit sphere
Figure — Bloch–Poincaré Sphere.

Thus for different values of \(\theta\) and \(\phi\) we get different points on the Bloch sphere which represent the different states of a qubit. For a unique mapping from the \(\theta\) and \(\phi\) to the points on the Bloch Sphere, we restrict the value of \(\theta \in [0,\pi]\) and \(\phi \in [0,2\pi]\). Thus, the state of a qubit can be represented as:

\[ \ket{\psi}=\cos\left(\frac{\theta}{2}\right)\ket{0}+e^{\iota \phi}\sin\left(\frac{\theta}{2}\right)\ket{1} \]

where \(\theta \in [0,\pi]\) and \(\phi \in [0,2\pi]\) are the polar (representing magnitude or argument) and azimuthal (representing relative phase) angles of the Bloch Sphere respectively. The following code using Qiskit can be used to plot the Bloch Sphere. For running the code you need to install the qiskit library. (Guide on installing qiskit ↗)

import matplotlib.pyplot as plt
from qiskit.visualization import plot_bloch_vector # Import the function to plot the Bloch vector
from qiskit.visualization import plot_bloch_multivector # Import the function to plot the Bloch vector

'''
Function to plot the Bloch Sphere

plot_bloch_vector(bloch, title='', ax=None, figsize=None, coord_type='spherical', font_size=None)

Plots a Bloch Sphere

Parameters
- bloch (list[double]) - array of three elements where [<x>, <y>, <z>] (Cartesian) or
[<r>, <theta>, <phi>] (spherical in radians) <theta> is inclination angle from +z direction
<phi> is azimuth from +x direction
- title (str) - a string that represents the title of the plot
- ax (matplotlib.axes.Axes) - an axes of the current figure to plot the Bloch sphere into
- figsize (tuple) - a tuple (width, height) in inches that represents the size of the figure
- coord_type (str) - the coordinate system to use. 'spherical' or 'cartesian', default is cartesian
- font_size (int) - the font size of the text

Returns
- matplotlib.figure.Figure - a matplotlib figure object

Raises
- ImportError - if matplotlib is not installed
'''

plot_bloch_vector([0,1,0],title='Bloch Sphere')

The output of the above code is shown in the figure below.

Rendered Bloch sphere produced by Qiskit's plot_bloch_vector function
Figure — Bloch Sphere rendered by Qiskit.

Note that the Bloch Sphere is a geometric representation of the pure states of a qubit. The mixed states of a qubit are represented by the interior of the Bloch Sphere.

Worked Example

The state of a qubit for different values of \(\theta\) and \(\phi\) is shown in the table below.

\(\theta\)\(\phi\)State of QubitAt Axis
\(0\)\(0\)\(\ket{0}\)+ve Z
\(\pi\)\(0\)\(\ket{1}\)−ve Z
\(\frac{\pi}{2}\)\(0\)\(\ket{+} = \frac{1}{\sqrt{2}}(\ket{0} + \ket{1})\)+ve X
\(\frac{\pi}{2}\)\(\pi\)\(\ket{-} = \frac{1}{\sqrt{2}}(\ket{0} - \ket{1})\)−ve X
\(\frac{\pi}{2}\)\(\frac{\pi}{2}\)\(\ket{+\iota}=\frac{1}{\sqrt{2}}(\ket{0} + i\ket{1})\)+ve Y
\(\frac{\pi}{2}\)\(-\frac{\pi}{2}\)\(\ket{-\iota}=\frac{1}{\sqrt{2}}(\ket{0} - i\ket{1})\)−ve Y

(Table — State of a qubit for different values of \(\theta\) and \(\phi\).)

Quantum gates acting on qubits can be thought of as rotations on the Bloch Sphere with some global phase. The global phase is irrelevant to observable quantities and thus does not impact the practical interpretation of the rotation as a transformation on the Bloch sphere. In other words, doing matrix–vector multiplication corresponding to a rotation action on qubits is the same up to a global phase. A matrix–vector multiplication by a matrix corresponding to some rotation and the vector corresponding to the state of a qubit will introduce a global phase shift which can be ignored. Thus, it is easier to imagine this as rotations on the Bloch sphere where we ignore the global phases implicitly. For example, \(R_x(\pi)\ket{0}=-\iota \ket{1}\) using matrix–vector multiplication and \(R_x(\pi)\ket{0} = \ket{1}\) using the fact that they are rotations on the Bloch sphere, but both of them are the same states up to a global phase, i.e. \(-\iota = e^{\iota 3\pi/2} \times 1\). This will be explained further in the later sections.

Note that orthonormal states reside at the ends of a diameter of the Bloch Sphere. Let a state \(\ket{\psi}\) be represented by a point on the Bloch Sphere. The state \(\ket{\psi}\) can be written as:

\[ \ket{\psi}=\cos\left(\frac{\theta}{2}\right)\ket{0}+e^{\iota \phi}\sin\left(\frac{\theta}{2}\right)\ket{1} \]

Then the state (say \(\ket{\psi'}\)) exactly opposite on the Bloch sphere will have the state:

\[ \ket{\psi'}=\cos\left(\frac{\pi+\theta}{2}\right)\ket{0}+e^{\iota \phi}\sin\left(\frac{\pi+\theta}{2}\right)\ket{1} \]

Thus, upon simplifying we get:

\[ \ket{\psi'}=-\sin \left(\frac{\theta}{2}\right)\ket{0}+e^{\iota \phi}\cos\left(\frac{\theta}{2}\right)\ket{1} \]

Now taking the inner product of the states \(\ket{\psi}\) and \(\ket{\psi'}\) we get:

\[ \braket{\psi|\psi'}=-\sin\left(\frac{\theta}{2}\right)\cos\left(\frac{\theta}{2}\right)+e^{-\iota \phi}e^{\iota \phi}\sin\left(\frac{\theta}{2}\right)\cos\left(\frac{\theta}{2}\right)=0 \]

Thus, the inner product is zero, which implies that the states on the opposite ends of the Bloch Sphere are orthogonal.

Note: Such a correspondence does not exist for higher dimensions, i.e. no such visualization exists for, say, qudits which are \(d\)-level quantum systems. From the Bloch sphere figure we can see that to convert from polar to Cartesian coordinates we use the following relations:

\[ x=r\sin\theta\cos\phi \] \[ y=r\sin\theta\sin\phi \] \[ z=r\cos\theta \]

Note that here \(r=1\) (for pure states, i.e. points on the surface of the sphere). To convert from Cartesian to polar coordinates we use the following relations:

\[ r=\sqrt{x^2+y^2+z^2} \] \[ \theta=\cos^{-1}(z) \] \[ \phi=\tan^{-1}\left(\frac{y}{x}\right) \]

Let us denote the vector as \(\vec{n}=(\sin\theta \cos \phi, \sin \theta \sin \phi, \cos \theta)\). Points on a Bloch sphere are always a pure state.

3.3 Single Qubit Gates

As said earlier, quantum gates are unitary operators that act on the state of a quantum system. Recall from the Appendix that unitary operators are the ones that preserve the inner product of the vectors and the norm of the vectors, i.e. they only rotate the vectors in the complex vector space (rotation matrices in \(\mathbb{C}^n\)). Thus, a state normalized initially remains in the normalized state and does not change its norm upon the action of unitary gates/operators. The single qubit gates are the quantum gates that act on a single qubit, represented by \(2\times 2\) matrices. Since a quantum state is represented by a column vector (in some basis, generally the standard basis), a gate is a unitary matrix/transformation that acts on the qubit (column vector) and changes its state to some other state. They are rotations on the Bloch Sphere.

Worked Example

Consider a \(2 \times 2\) matrix \(U\) given as:

\[ U=\begin{bmatrix} a & b \\ c & d \end{bmatrix} \]

in the orthonormal input and output basis \(\{\ket{0},\ket{1}\}\). Thus we can write the matrix \(U\) as:

\[ U=a\ket{0}\bra{0}+b\ket{0}\bra{1}+c\ket{1}\bra{0}+d\ket{1}\bra{1} \]

This will be used to express the Pauli matrices in the later sections.

Pauli Matrices

Pauli-I Gate

It is used for the measurement of decoherence (qubits start to lose their quantum abilities after a certain time). It does no rotation of the Bloch vector on the Bloch Sphere. Its truth table is given below.

InputOutput
\(\ket{0}\)\(\ket{0}\)
\(\ket{1}\)\(\ket{1}\)

(Truth Table for Pauli-I Gate.) The code for the Pauli-I Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')

circuit = QuantumCircuit(qreg_q)

circuit.id(qreg_q[0]) #For Pauli-Identity Gate
circuit.draw(output='mpl')#.savefig('../images/pauli-i.png') #Draw the circuit
Circuit symbol for the Pauli-I (identity) gate on a single qubit wire
Figure — Pauli-I Gate.

In operator notation it is:

\[\begin{aligned} I\ket{0}&=\ket{0} \\ I\ket{1}&=\ket{1} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ \sigma_I=I=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \]

Its eigenvalues are 1 and 1. Its eigenvectors are any vector in the 2-D space. Note that it is unitary (\(II^{\dagger}=I^{\dagger}I=I;\ I=I^{-1}\)) and Hermitian (\(I=I^{\dagger}\)), thus a normal matrix (\(II^{\dagger}=I^{\dagger}I\)), hence unitarily diagonalizable. Thus it has a spectral decomposition (outer product representation) which can be written as:

\[ I=\ket{0}\bra{0}+\ket{1}\bra{1} \]

Action of the Pauli-I Gate on a general qubit \(\alpha\ket{0}+\beta\ket{1}\) is:

\[ \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} \alpha \\ \beta \end{bmatrix}= \alpha\ket{0}+\beta\ket{1} \]

Pauli-X Gate

It is also known as the NOT gate. It is the equivalent of the classical NOT gate, and is thus called the Bit Flip Gate. It does an anticlockwise \(\pi\) rotation about the X axis of the Bloch sphere. It flips the state \(\ket{0}\) to \(\ket{1}\) and \(\ket{1}\) to \(\ket{0}\). Its truth table is below.

InputOutput
\(\ket{0}\)\(\ket{1}\)
\(\ket{1}\)\(\ket{0}\)

(Truth Table for Pauli-X Gate.) The code for the Pauli-X Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='Not Gate')

circuit.x(qreg_q[0])

circuit.draw(output='mpl')#.savefig('../images/pauli-x.png') #Draw the circuit
Circuit symbol for the Pauli-X (NOT) gate
Figure — Pauli-X Gate.

In operator notation it is:

\[\begin{aligned} X\ket{0}&=\ket{1} \\ X\ket{1}&=\ket{0} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ \sigma_X=X=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \]

Its eigenvalues are 1 and \(-1\). Its corresponding eigenvectors are \(\ket{+}=\frac{1}{\sqrt{2}}(\ket{0}+\ket{1})\) and \(\ket{-}=\frac{1}{\sqrt{2}}(\ket{0}-\ket{1})\). It is a unitary and Hermitian matrix. It is a normal matrix, hence unitarily diagonalizable. It has a spectral decomposition (outer product representation):

\[ X=\ket{+}\bra{+}-\ket{-}\bra{-} \]

Upon substituting the values of \(\ket{+}\) and \(\ket{-}\) we get:

\[\begin{aligned} X&=\tfrac{1}{\sqrt{2}}(\ket{0}+\ket{1})\tfrac{1}{\sqrt{2}}(\bra{0}+\bra{1})-\tfrac{1}{\sqrt{2}}(\ket{0}-\ket{1})\tfrac{1}{\sqrt{2}}(\bra{0}-\bra{1})\\ &=\tfrac{1}{2}(\ket{0}\bra{0}+\ket{0}\bra{1}+\ket{1}\bra{0}+\ket{1}\bra{1})-\tfrac{1}{2}(\ket{0}\bra{0}-\ket{0}\bra{1}-\ket{1}\bra{0}+\ket{1}\bra{1})\\ &=\ket{0}\bra{1}+\ket{1}\bra{0} \end{aligned}\]

Thus, the Pauli-X Gate can also be written as:

\[ X=\ket{0}\bra{1}+\ket{1}\bra{0} \]

Note that the Pauli-X Gate can be written as \(X=HZH\) or \(\sigma_X=H\sigma_Z H\), where \(H\) and \(Z\) are the Hadamard and Pauli-Z gates respectively. Action of the Pauli-X Gate on a general qubit \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\) is:

\[ \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} \beta \\ \alpha \end{bmatrix}= \beta\ket{0}+\alpha\ket{1} \]

Pauli-Y Gate

It is an anticlockwise \(\pi\) rotation about the Y axis of the Bloch Sphere. It flips the qubit and multiplies by a complex amplitude. Its truth table is below.

InputOutput
\(\ket{0}\)\(i\ket{1}\)
\(\ket{1}\)\(-i\ket{0}\)

(Truth Table for Pauli-Y Gate.) The code for the Pauli-Y Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='Not Gate')

circuit.y(qreg_q[0]) #Apply the Y gate

circuit.draw(output='mpl')#.savefig('../images/pauli-x.png') #Draw the circuit
Circuit symbol for the Pauli-Y gate
Figure — Pauli-Y Gate.

In operator notation it is:

\[\begin{aligned} Y\ket{0}&=i\ket{1} \\ Y\ket{1}&=-i\ket{0} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ \sigma_Y=Y=\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} \]

Its eigenvalues are 1 and \(-1\). Its corresponding eigenvectors are \(\ket{+i}=\frac{1}{\sqrt{2}}(\ket{0}+i\ket{1})\) and \(\ket{-i}=\frac{1}{\sqrt{2}}(\ket{0}-i\ket{1})\). It is a unitary and Hermitian matrix. It is a normal matrix, hence unitarily diagonalizable. It has a spectral decomposition:

\[ Y=\ket{+i}\bra{+i}-\ket{-i}\bra{-i} \]

Upon substituting the values of \(\ket{+i}\) and \(\ket{-i}\) we get:

\[\begin{aligned} Y&=\tfrac{1}{\sqrt{2}}(\ket{0}+i\ket{1})\tfrac{1}{\sqrt{2}}(\bra{0}-i\bra{1})-\tfrac{1}{\sqrt{2}}(\ket{0}-i\ket{1})\tfrac{1}{\sqrt{2}}(\bra{0}+i\bra{1})\\ &=\tfrac{1}{2}(\ket{0}\bra{0}-i\ket{0}\bra{1}+i\ket{1}\bra{0}+\ket{1}\bra{1})-\tfrac{1}{2}(\ket{0}\bra{0}+i\ket{0}\bra{1}-i\ket{1}\bra{0}+\ket{1}\bra{1})\\ &=i\ket{1}\bra{0}-i\ket{0}\bra{1} \end{aligned}\]

Thus, the Pauli-Y Gate can also be written as:

\[ Y=-i\ket{0}\bra{1}+i\ket{1}\bra{0} \]

Note that the Pauli-Y Gate can be written as \(Y=\iota XZ\) or \(\sigma_Y=\iota \sigma_X\sigma_Z\), where \(X\) and \(Z\) are the Pauli-X and Pauli-Z gates respectively. This means that the Pauli-Y Gate is a rotation about the Y-axis of the Bloch Sphere by \(\pi\) in the anticlockwise direction and is equivalent to a rotation about the X-axis by anticlockwise \(\pi\) followed by a rotation about the Z-axis by anticlockwise \(\pi\) (up to a global phase). Action of the Pauli-Y Gate on a general qubit \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\) is:

\[ \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} -i\beta \\ i\alpha \end{bmatrix}=i\alpha\ket{1}-i\beta\ket{0} \]

Pauli-Z Gate

It is an anticlockwise \(\pi\) rotation about the Z axis of the Bloch Sphere. It flips the sign of the \(\ket{1}\) state. It is also called the Phase Flip / Phase Shift / Sign Flip gate. Its truth table is below.

InputOutput
\(\ket{0}\)\(\ket{0}\)
\(\ket{1}\)\(-\ket{1}\)

(Truth Table for Pauli-Z Gate.) The code for the Pauli-Z Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='Not Gate')

circuit.z(qreg_q[0]) #Apply the Z gate

circuit.draw(output='mpl')#.savefig('../images/pauli-z.png') #Draw the circuit
Circuit symbol for the Pauli-Z gate
Figure — Pauli-Z Gate.

In operator notation it is:

\[\begin{aligned} Z\ket{0}&=\ket{0} \\ Z\ket{1}&=-\ket{1} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ \sigma_Z=Z=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \]

Its eigenvalues are 1 and \(-1\). Its corresponding eigenvectors are \(\ket{0}\) and \(\ket{1}\). It is a unitary and Hermitian matrix. It is a normal matrix, hence unitarily diagonalizable. It has a spectral decomposition:

\[ Z=\ket{0}\bra{0}-\ket{1}\bra{1} \]

In compact form, we can write the Pauli-Z Gate as:

\[ Z\ket{j}=(-1)^j\ket{j} \]

where \(j=0,1\). Action of the Pauli-Z Gate on a general qubit \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\) is:

\[ \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} \alpha \\ -\beta \end{bmatrix}=\alpha\ket{0}-\beta\ket{1} \]
The four Pauli gates are the workhorses of single-qubit logic. Picture the Bloch sphere: \(X\), \(Y\), \(Z\) each flip the qubit half a turn (\(\pi\)) about their own axis, and \(I\) leaves it alone. \(X\) is the quantum NOT; \(Z\) just stamps a minus sign on \(\ket{1}\); \(Y\) does both at once (with an \(i\)).
Worked Example

(Pauli Matrices: Hermitian and Unitary) Show that the Pauli matrices are Hermitian and unitary.

Show solution

We check \(I,X,Y,Z\) in turn.

\[ I^{\dagger}=\left(\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}^T \right)^* = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}^* = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} =I \] \[ I^{\dagger}I=II=I \] \[ X^{\dagger}=\left(\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}^T \right)^* = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}^* = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} =X \] \[ X^{\dagger}X=XX=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} = I \] \[ Y^{\dagger}=\left(\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}^T \right)^* = \begin{bmatrix} 0 & \iota \\ -\iota & 0 \end{bmatrix}^* = \begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix} =Y \] \[ Y^{\dagger}Y=YY=\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix} \begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \] \[ Z^{\dagger}=\left(\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}^T \right)^* = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}^* = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} =Z \] \[ Z^{\dagger}Z=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} = I \]
Worked Example

Calculate the matrix representation of the tensor products of the Pauli operators: (a) \(X\) and \(Z\); (b) \(I\) and \(X\); (c) \(X\) and \(I\). Is the tensor product commutative?

Show solution

Using the Kronecker product we have:

  1. \[ X\otimes Z=\begin{bmatrix} 0Z & 1Z \\ 1Z & 0Z \end{bmatrix}= \begin{bmatrix} 0\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} & 1 \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \\ 1 \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} & 0 \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \end{bmatrix}=\begin{bmatrix} 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \\ 1 & 0 & 0 & 0 \\ 0 & -1 & 0 & 0 \end{bmatrix} \]
  2. \[ I\otimes X=\begin{bmatrix} 1X & 0X \\ 0X & 1X \end{bmatrix}= \begin{bmatrix} 1\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} & 0 \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \\ 0 \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} & 1 \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \end{bmatrix}=\begin{bmatrix} 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix} \]
  3. \[ X\otimes I=\begin{bmatrix} 0I & 1I \\ 1I & 0I \end{bmatrix}= \begin{bmatrix} 0\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} & 1\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \\ 1\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} & 0\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \end{bmatrix}=\begin{bmatrix} 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \end{bmatrix} \]

Comparing (b) and (c), we conclude that the tensor product is not commutative.

Worked Example

(Commutation relations for the Pauli Matrices) Verify the commutation relations

\[ [X,Y]=2\iota Z;\quad [Y,Z]=2\iota X;\quad [Z,X]=2\iota Y \]
Show solution

There is an elegant way of writing this using \(\epsilon_{jkl}\), the antisymmetric tensor on three indices, for which \(\epsilon_{jkl}=0\) except for \(\epsilon_{123}=\epsilon_{231}=\epsilon_{312}=1\), and \(\epsilon_{321}=\epsilon_{213}=\epsilon_{132}=-1\):

\[ [\sigma_j,\sigma_k]=2\iota\sum_{l=1}^3 \epsilon_{jkl}\sigma_l \]

We verify the proposed relations via computation in the computational basis:

\[ [X,Y]=XY-YX=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix} - \begin{bmatrix}0 & -\iota \\ \iota & 0 \end{bmatrix} \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} = \begin{bmatrix} \iota & 0 \\ 0 & -\iota \end{bmatrix} - \begin{bmatrix} -\iota & 0 \\ 0 & \iota \end{bmatrix} =2\iota Z \] \[ [Y,Z]=YZ-ZY=\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} - \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix} = \begin{bmatrix} 0 & \iota \\ \iota & 0 \end{bmatrix} - \begin{bmatrix} 0 & -\iota \\ -\iota & 0 \end{bmatrix} =2\iota X \] \[ [Z,X]=ZX-XZ=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} - \begin{bmatrix}0 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} = \begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix} - \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} =2\iota Y \]
Worked Example

(Anti-commutation relations for the Pauli matrices) Verify the anti-commutation relations \(\{\sigma_i,\sigma_j\}=0\) where \(i\neq j\) are both chosen from the set \(1,2,3\). Also verify that (for \(i=0,1,2,3\)) \(\sigma_i^2=I\).

Show solution

We again verify the proposed relations via computation in the computational basis:

\[ \{X,Y\}=XY+YX=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}+\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}=\begin{bmatrix} \iota & 0 \\ 0 & -\iota \end{bmatrix}+\begin{bmatrix} -\iota & 0 \\ 0 & \iota \end{bmatrix} = \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix} \] \[ \{Y,Z\}=YZ+ZY=\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}+\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}=\begin{bmatrix} 0 & \iota \\ \iota & 0 \end{bmatrix}+\begin{bmatrix} 0 & -\iota \\ -\iota & 0 \end{bmatrix} = \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix} \] \[ \{Z,X\}=ZX+XZ=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}+\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}=\begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix}+\begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} = \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix} \]

This proves the first claim, as \(\{A,B\}=AB+BA=BA+AB=\{B,A\}\) and the other 3 relations are equivalent to the ones already proven. Verifying the second claim, we have:

\[ I^2=\begin{bmatrix} 1 & 0 \\ 0 & 1\end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & 1\end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1\end{bmatrix}=I \] \[ X^2=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=I \] \[ Y^2=\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=I \] \[ Z^2=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=I \]
Remark

Note that we can write this result concisely as \(\{\sigma_j,\sigma_k\}=2\delta_{jk}I\).

Worked Example

Show that for \(j,k=1,2,3\)

\[ \sigma_j\sigma_k=\delta_{jk} I + \iota \sum_{l=1}^3 \epsilon_{jkl}\sigma_l \]
Show solution

Applying the previous results we get:

\[\begin{aligned} \sigma_j\sigma_k&=\frac{[\sigma_j,\sigma_k]+\{\sigma_j,\sigma_k\}}{2}\\ &=\frac{2\iota \sum_{l=1}^3 \epsilon_{jkl}\sigma_l +2\delta_{jk}I}{2}\\ &=\delta_{jk}I+\iota\sum_{l=1}^3 \epsilon_{jkl}\sigma_l \end{aligned}\]
Important

Note that all the Pauli gates are unitary and Hermitian. Since all unitary gates (\(U^{\dagger}=U^{-1}\)) are reversible, from Postulate 2: Evolution, we can apply Pauli gates. All the Pauli gates are reversible and thus can be applied in quantum circuits. The four Pauli gates can also be denoted as \(X=\sigma_X\), \(Y=\sigma_Y\), \(Z=\sigma_Z\) and \(I=\sigma_I\). All the Pauli gates, as said earlier, are anticlockwise rotations on the Bloch Sphere. The Pauli-X Gate is a \(\pi\) rotation about the X-axis, Pauli-Y Gate is a \(\pi\) rotation about the Y-axis, and Pauli-Z Gate is a \(\pi\) rotation about the Z-axis. The Pauli-I Gate does not do any rotation on the Bloch Sphere. Also note that applying the same gate twice is equivalent to applying the Identity Gate. Thus, the Pauli gates are self-inverse. Since they are Hermitian, \(U^{\dagger}=U\), and they are unitary, so \(U^{\dagger}U=UU^{\dagger}=I\). Combining the two we get \(U^2=I\). This can also be verified as shown below:

\[ X^2=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=I \] \[ Y^2=\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=I \] \[ Z^2=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=I \] \[ I^2=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=I \]

Thus, we can see that all the Pauli gates are self-inverse. The Pauli gates also form a basis for the single qubit gates. Any single qubit gate can be written as a linear combination of the Pauli gates. In other words, any \(2 \times 2\) matrix can be written as a linear combination of the Pauli matrices:

\[ A=\alpha I+\beta X+\gamma Y+\delta Z \]

where \(\alpha,\beta,\gamma,\delta\) are complex numbers. The Pauli matrices, except the Identity, are also traceless (trace \(=0\)) as can be clearly inferred from the matrices, and all the Pauli matrices are Hermitian. Note that here it should be presumed that the rotations are always in the anticlockwise direction.

Worked Example

(Pauli operators and the outer product) The Pauli matrices can be considered as operators with respect to an orthonormal basis \(\ket{0},\ket{1}\) for a two-dimensional Hilbert space. Express each of the Pauli operators in outer product notation.

Show solution

Recall that if \(A\) has matrix representation

\[ A=\begin{bmatrix} a_{00} & a_{01} \\ a_{10} & a_{11} \end{bmatrix} \]

with respect to \(\ket{0},\ket{1}\) as the input/output bases, then we can express \(A\) in outer product notation as:

\[ A=a_{00}\ket{0}\bra{0} + a_{01}\ket{0}\bra{1} + a_{10}\ket{1}\bra{0} + a_{11}\ket{1}\bra{1} \]

Furthermore, recall the representation of the Pauli matrices with respect to the orthonormal basis \(\ket{0},\ket{1}\):

\[ I=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\quad X=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \quad Y=\begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix} \quad Z=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \]

We immediately see that:

\[\begin{aligned} I&=\ket{0}\bra{0}+\ket{1}\bra{1}\\ X&=\ket{0}\bra{1}+\ket{1}\bra{0}\\ Y&=-\iota \ket{0}\bra{1} + \iota \ket{1}\bra{0}\\ Z&=\ket{0}\bra{0}-\ket{1}\bra{1} \end{aligned}\]
Worked Example

(Eigendecomposition of the Pauli matrices) Find the eigenvectors, eigenvalues and diagonal representation of the Pauli matrices \(I,X,Y\) and \(Z\).

Show solution

The eigenvectors and eigenvalues of \(I\) are the entire space \(\mathbb{C}^2\) and \(\lambda=1,1\) respectively, and its diagonal representation is

\[ I=\ket{0}\bra{0}+\ket{1}\bra{1} \]

Recall that the eigenvectors and eigenvalues of \(X\) are \(\ket{+},\ket{-}\) and \(\lambda=1,-1\) respectively. The diagonal representation is

\[ X=\ket{+}\bra{+}-\ket{-}\bra{-} \]

Similarly, the eigenvalues of \(Y\) are \(\lambda=1,-1\) with eigenvectors \(\ket{+\iota},\ket{-\iota}\) respectively, and its diagonal representation is

\[ Y=\ket{+\iota}\bra{+\iota}-\ket{-\iota}\bra{-\iota} \]

Similarly, the eigenvalues of \(Z\) are \(\lambda=1,-1\) with eigenvectors \(\ket{0},\ket{1}\) respectively, and its diagonal representation is

\[ Z=\ket{0}\bra{0}-\ket{1}\bra{1} \]

Hadamard Gate

It creates a superposition of qubits. It is a \(\pi\) rotation about the X+Z axis of the Bloch Sphere, or a \(\pi/2\) rotation about the Y axis followed by a \(\pi\) rotation about the X axis. Its truth table is below.

InputOutput
\(\ket{0}\)\(\ket{+}=\frac{1}{\sqrt{2}}(\ket{0}+\ket{1})\)
\(\ket{1}\)\(\ket{-}=\frac{1}{\sqrt{2}}(\ket{0}-\ket{1})\)

(Truth Table for Hadamard Gate.) The code for the Hadamard Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='Hadamard Gate')

circuit.h(qreg_q[0]) #Apply the H gate

circuit.draw(output='mpl')#.savefig('../images/hadamard.png') #Draw the circuit
Circuit symbol for the Hadamard gate
Figure — Hadamard Gate.

In operator notation it is:

\[\begin{aligned} H\ket{0}&=\tfrac{1}{\sqrt{2}}(\ket{0}+\ket{1})=\ket{+} \\ H\ket{1}&=\tfrac{1}{\sqrt{2}}(\ket{0}-\ket{1})=\ket{-} \end{aligned}\]

Generally in a quantum circuit we initialise a qubit in state \(\ket{0}\) and thus the Hadamard gate can be used to create a superposition. It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ H=\tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \]

In compact form we can write the action of the Hadamard Gate on one qubit as:

\[ H\ket{j}=\tfrac{1}{\sqrt{2}}\sum_{k=0}^{1}(-1)^{jk}\ket{k} \]

where \(j=0,1\). It is a unitary and Hermitian matrix. It is a normal matrix, hence unitarily diagonalizable. It has eigenvalues \(\lambda_1=1,\lambda_2=-1\), and the corresponding normalized eigenvectors are \(\frac{1}{\sqrt{2+\sqrt{2}}}\begin{bmatrix} 1+\frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}}\end{bmatrix}\) and \(\frac{1}{\sqrt{2-\sqrt{2}}}\begin{bmatrix} -1 + \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}}\end{bmatrix}\) respectively. It has a spectral decomposition (outer product representation):

\[ H=\ket{+}\bra{0}+\ket{-}\bra{1} \]

Action of the Hadamard Gate on a general qubit \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\) is:

\[ \tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} \tfrac{\alpha+\beta}{\sqrt{2}} \\ \tfrac{\alpha-\beta}{\sqrt{2}} \end{bmatrix}=\tfrac{\alpha+\beta}{\sqrt{2}}\ket{0}+\tfrac{\alpha-\beta}{\sqrt{2}}\ket{1} \]

Note that we can also write the Hadamard gate as

\[ H=\frac{X+Z}{\sqrt{2}}=\frac{1}{\sqrt{2}}\left(\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}+\begin{bmatrix} 1 & 0 \\ 0& -1 \end{bmatrix} \right)=\frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \]

Note that the Hadamard gate is also a self-inverse gate. Since it is Hermitian as well as unitary, \(H^{\dagger}=H\) and \(H^{\dagger}H=HH^{\dagger}=I;\ H=H^{-1}\). Thus \(H^2=I\). Intuitively this means that applying the Hadamard gate twice will bring the quantum state back to the same quantum state. This can also be imagined as doing rotations on the Bloch sphere and then arriving back at the same state.

\[ H^2=\tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}\tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}=\tfrac{1}{2}\begin{bmatrix} 1+1 & 1-1 \\ 1-1 & 1+1 \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}=I \]

Thus, we can see that the Hadamard gate is self-inverse. This means that if we apply the Hadamard gate twice on \(\ket{0}\) or on \(\ket{1}\), we will arrive back at the same state.

\[ H^2\ket{0}=H\ket{+}=\ket{0} \] \[ H^2\ket{1}=H\ket{-}=\ket{1} \]

Hadamard Gates on Multiple Qubits
The Hadamard gate can also be applied on multiple qubits. The Hadamard gate on multiple qubits (say \(n\) qubits) using tensor products is given as:

\[ H^{\otimes n}=\underbrace{H\otimes H\otimes \ldots \otimes H}_{n \text{ times}} \]

One way to find the Hadamard matrix is to perform the tensor product \(n\) times to get the Hadamard matrix acting on the \(n\) qubits. For example, we can find the Hadamard gate acting on 2 qubits in matrix form using the tensor product as:

\[ H^{\otimes 2}=\tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}\otimes \tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}=\tfrac{1}{2}\begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 \end{bmatrix} \]

Clearly this does not scale. Another method is that we can find the action of the Hadamard gate on multiple qubits by applying the Hadamard gate on each qubit separately.

\[ H^{\otimes n}(\ket{0}\otimes \ket{0}\otimes \ldots \otimes \ket{0})=H\ket{0}\otimes H\ket{0}\otimes \ldots \otimes H\ket{0}=\ket{+}\otimes \ket{+}\otimes \ldots \otimes \ket{+} \]

This is a better method than writing the entire matrix for \(n\) qubits. Thus, we can see that the Hadamard gate on multiple qubits creates a superposition of all possible states of \(n\) qubits. So, for writing the Hadamard gate on multiple qubits in a compact form, first we consider the compact form of \(H^{\otimes 2}\) as:

\[ H^{\otimes 2}\ket{x}=H\ket{x_1}\otimes H\ket{x_2} \]

where \(x=x_1x_2\). Now using the fact that \(H\ket{x_1}=\tfrac{1}{\sqrt{2}}\sum_{y_1 \in \{0,1\}} (-1)^{x_1y_1}\ket{y_1}\) and \(H\ket{x_2}=\tfrac{1}{\sqrt{2}}\sum_{y_2 \in \{0,1\}} (-1)^{x_2y_2}\ket{y_2}\), we get:

\[ H^{\otimes 2}\ket{x}=\tfrac{1}{2}\sum_{y_1,y_2 \in \{0,1\}} (-1)^{x_1y_1}(-1)^{x_2y_2}\ket{y_1}\otimes \ket{y_2}=\tfrac{1}{2}\sum_{y \in \{0,1\}^2}(-1)^{x\cdot y}\ket{y} \]

where \(x=x_1x_2\) and \(y=y_1y_2\). Thus, it can be generalized and we can write the Hadamard gate on \(n\) qubits as:

\[ H^{\otimes n}\ket{x}=\tfrac{1}{\sqrt{2^n}}\sum_{y \in \{0,1\}^n}(-1)^{x\cdot y}\ket{y} \]

where \(x=x_1x_2\ldots x_n\) and \(y=y_1y_2\ldots y_n\), and \(x\cdot y\) is the dot product between \(x\) and \(y\). Thus, the Hadamard gate on \(n\) qubits creates a superposition of all possible states of \(n\) qubits. In general, we start with state \(\ket{0}\) for all the qubits initially, thus the action of the Hadamard gate on \(n\) qubits all in state \(\ket{0}\) is:

\[ H^{\otimes n}\ket{0}=\tfrac{1}{\sqrt{2^n}}\sum_{y \in \{0,1\}^n}\ket{y} \]

For example, explicitly writing \(H^{\otimes 2}\), we get:

\[ H^{\otimes 2}=\frac{1}{\sqrt{2^2}}\sum_{x,y}(-1)^{x \cdot y} \ket{x}\bra{y} \] \[ =\frac{1}{2} \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 \end{bmatrix} \]

Note that here \(x,y\) are binary length-2 strings. The sum goes through all pairwise combinations of \(x,y \in \{00,01,10,11\}\).

The Hadamard gate is the single most important trick for getting quantum parallelism started: hit a register of \(n\) qubits (all \(\ket{0}\)) with one \(H\) each, and you instantly have an equal blend of all \(2^n\) possible bit strings at once. Everything downstream then acts on all those strings simultaneously.
Remark

Sylvester's construction gives an interesting recursive construction of Hadamard matrices (see the Hadamard matrix article ↗). A discussion of the related open problem concerning the maximal determinant of matrices consisting of entries of \(1\) and \(-1\) can be found in the Hadamard's maximal determinant problem article ↗.

Worked Example

Fast Hadamard Gate using the tabular method. Consider a state

\[ \ket{\psi}=\tfrac{\ket{000}+\ket{110}-\ket{100}-\ket{111}}{\sqrt{4}} \]

We are supposed to find \(H^{\otimes 3}\ket{\psi}\).

Show solution

Solving using the equation for the Hadamard gate acting on multiple qubits, \(H^{\otimes n}\ket{x}=\tfrac{1}{\sqrt{2^n}}\sum_{y \in \{0,1\}^n}(-1)^{x\cdot y}\ket{y}\), we get:

\[ \frac{1}{\sqrt{8}}\left(\ket{001}-\ket{011}+\ket{100}+\ket{110}+\ket{111}\right) \]

\(S\) and \(S^{\dagger}\) Gate

These are some specialized rotations.

\(S\) Gate

It is a \(\pi/2\) rotation around the Z axis of the Bloch Sphere. It is also called the Phase Gate. Its truth table is below.

InputOutput
\(\ket{0}\)\(\ket{0}\)
\(\ket{1}\)\(i\ket{1}\)

(Truth Table for S Gate.) The code for the S Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='S Gate')

circuit.s(qreg_q[0]) #Apply the S gate

circuit.draw(output='mpl')#.savefig('../images/s-gate.png') #Draw the circuit
Circuit symbol for the S (phase) gate
Figure — S Gate.

In operator notation it is:

\[\begin{aligned} S\ket{0}&=\ket{0} \\ S\ket{1}&=i\ket{1} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ S=\begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}=e^{i\pi/4}\begin{bmatrix} e^{-i\pi/4} & 0 \\ 0 & e^{i\pi/4} \end{bmatrix} \]

Thus, it is called the \(\pi/4\) gate. Its eigenvalues are 1 and \(i\). Its corresponding eigenvectors are \(\ket{0}\) and \(\ket{1}\). It is a unitary and normal matrix, hence unitarily diagonalizable. It has a spectral decomposition:

\[ S=\ket{0}\bra{0}+i\ket{1}\bra{1} \]

Action of the S Gate on a general qubit \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\) is:

\[ \begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} \alpha \\ i\beta \end{bmatrix}=\alpha\ket{0}+i\beta\ket{1} \]

\(S^{\dagger}\) Gate

It is a \(\pi/2\) rotation around the Z axis of the Bloch Sphere in the opposite direction. It is also called the Conjugate Phase Gate. Its truth table is below.

InputOutput
\(\ket{0}\)\(\ket{0}\)
\(\ket{1}\)\(-i\ket{1}\)

(Truth Table for \(S^{\dagger}\) Gate.) The code for the \(S^{\dagger}\) Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='T Gate')

circuit.sdg(qreg_q[0]) #Apply the sdag gate

circuit.draw(output='mpl')#.savefig('../images/sdag-gate.png') #Draw the circuit
Circuit symbol for the S-dagger gate
Figure — \(S^{\dagger}\) Gate.

In operator notation it is:

\[\begin{aligned} S^{\dagger}\ket{0}&=\ket{0} \\ S^{\dagger}\ket{1}&=-i\ket{1} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ S^{\dagger}=\begin{bmatrix} 1 & 0 \\ 0 & -i \end{bmatrix}=e^{-i\pi/4}\begin{bmatrix} e^{i\pi/4} & 0 \\ 0 & e^{-i\pi/4} \end{bmatrix} \]

Its eigenvalues are 1 and \(-i\). Its corresponding eigenvectors are \(\ket{0}\) and \(\ket{1}\). It is a unitary and normal matrix, hence unitarily diagonalizable. It has a spectral decomposition:

\[ S^{\dagger}=\ket{0}\bra{0}-i\ket{1}\bra{1} \]

Action of the \(S^{\dagger}\) Gate on a general qubit \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\) is:

\[ \begin{bmatrix} 1 & 0 \\ 0 & -i \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} \alpha \\ -i\beta \end{bmatrix}=\alpha\ket{0}-i\beta\ket{1} \]

Note that both \(S\) and \(S^{\dagger}\) gates are unitary but not Hermitian, thus they are not self-inverse. Recall that the only criterion for a gate is to be unitary (from Postulate 2: unitary evolution) and not necessarily Hermitian.

\(T\) and \(T^{\dagger}\) Gate

These are some specialised rotations.

\(T\) Gate

It is a \(\pi/4\) rotation around the Z axis of the Bloch Sphere. It is also called the \(\pi/8\) Gate. Its truth table is below.

InputOutput
\(\ket{0}\)\(\ket{0}\)
\(\ket{1}\)\(e^{i\pi/4}\ket{1}\)

(Truth Table for T Gate.) The code for the T Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='T Gate')

circuit.sdg(qreg_q[0]) #Apply the T gate

circuit.draw(output='mpl')#.savefig('../images/sdag-gate.png') #Draw the circuit
Circuit symbol for the T gate
Figure — T Gate.

In operator notation it is:

\[\begin{aligned} T\ket{0}&=\ket{0} \\ T\ket{1}&=e^{i\pi/4}\ket{1} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ T=\begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix}=e^{i\pi/8}\begin{bmatrix} e^{-i\pi/8} & 0 \\ 0 & e^{i\pi/8} \end{bmatrix} \]

Thus, it is called the \(\pi/8\) gate. This is because, up to an unimportant global phase, \(T\) is equal to a gate which has \(e^{\pm \iota \pi/8}\) appearing on its diagonals. Its eigenvalues are 1 and \(e^{i\pi/4}\). Its corresponding eigenvectors are \(\ket{0}\) and \(\ket{1}\). It is a unitary and normal matrix, hence unitarily diagonalizable. It has a spectral decomposition:

\[ T=\ket{0}\bra{0}+e^{i\pi/4}\ket{1}\bra{1} \]

Action of the T Gate on a general qubit \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\) is:

\[ \begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} \alpha \\ e^{i\pi/4}\beta \end{bmatrix}=\alpha\ket{0}+e^{i\pi/4}\beta\ket{1} \]

\(T^{\dagger}\) Gate

It is a \(\pi/4\) rotation around the Z axis of the Bloch Sphere in the opposite direction. It is also called the \(\pi/8\) Gate. Its truth table is below.

InputOutput
\(\ket{0}\)\(\ket{0}\)
\(\ket{1}\)\(e^{-i\pi/4}\ket{1}\)

(Truth Table for \(T^{\dagger}\) Gate.) The code for the \(T^{\dagger}\) Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='T Gate')

circuit.tdg(qreg_q[0]) #Apply the T gate

circuit.draw(output='mpl')#.savefig('../images/tdag-gate.png') #Draw the circuit
Circuit symbol for the T-dagger gate
Figure — \(T^{\dagger}\) Gate.

In operator notation it is:

\[\begin{aligned} T^{\dagger}\ket{0}&=\ket{0} \\ T^{\dagger}\ket{1}&=e^{-i\pi/4}\ket{1} \end{aligned}\]

One might wonder why the T gate is called the \(\pi/8\) gate when it is \(\pi/4\) that appears in the definition. The reason is that the gate has historically often been referred to as the \(\pi/8\) gate simply because, up to an unimportant global phase, \(T\) is equal to a gate which has \(e^{\pm \iota \pi/8}\) appearing on its diagonals. It is represented by the matrix in the computational basis (\(\ket{0},\ket{1}\)) as:

\[ T^{\dagger}=\begin{bmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{bmatrix}=e^{-i\pi/8}\begin{bmatrix} e^{i\pi/8} & 0 \\ 0 & e^{-i\pi/8} \end{bmatrix} \]

Its eigenvalues are 1 and \(e^{-i\pi/4}\). Its corresponding eigenvectors are \(\ket{0}\) and \(\ket{1}\). It is a unitary and normal matrix, hence unitarily diagonalizable. It has a spectral decomposition:

\[ T^{\dagger}=\ket{0}\bra{0}+e^{-i\pi/4}\ket{1}\bra{1} \]

Action of the \(T^{\dagger}\) Gate on a general qubit \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\) is:

\[ \begin{bmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix}=\begin{bmatrix} \alpha \\ e^{-i\pi/4}\beta \end{bmatrix}=\alpha\ket{0}+e^{-i\pi/4}\beta\ket{1} \]

Some relations between the S and T Gates:

\[\begin{aligned} S&=T^2 \\ S^{\dagger}&=T^{\dagger}T^{\dagger} \end{aligned}\]

This can be verified as follows. Intuitively, one can think that twice a \(\pi/4\) rotation around the Z axis is a \(\pi/2\) rotation around the Z axis. Thus, the relations:

\[\begin{aligned} T^2&=\begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/2} \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}=S \\ T^{\dagger}T^{\dagger}&=\begin{bmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{bmatrix}\begin{bmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & e^{-i\pi/2} \end{bmatrix}=\begin{bmatrix} 1 & 0 \\ 0 & -i \end{bmatrix}=S^{\dagger} \end{aligned}\]
Worked Example

Let \(x\) be a real number and \(A\) a matrix such that \(A^2=I\). Show that

\[ e^{\iota Ax}=\cos (x)I + \iota \sin(x) A \]
Show solution

Let \(\ket{v}\) be an eigenvector of \(A\) with eigenvalue \(\lambda\). It then follows that \(A^2\ket{v}=\lambda^2\ket{v}\), and furthermore \(A^2 \ket{v}=I\ket{v}=\ket{v}\) by assumption. We obtain \(\lambda^2= 1\) and therefore the only possible eigenvalues of \(A\) are \(\lambda=\pm 1\). Let \(\ket{v_1},\ldots,\ket{v_k}\) be the eigenvectors with eigenvalue \(1\) and \(\ket{v_{k+1}},\ldots,\ket{v_n}\) be the eigenvectors with eigenvalue \(-1\). By the spectral decomposition, we can write:

\[ A=\sum_{i=1}^k \ket{v_i}\bra{v_i} - \sum_{i=k+1}^n \ket{v_i}\bra{v_i} \]

so by the definition of operator functions we have:

\[ e^{\iota Ax}=\sum_{i=1}^k e^{\iota x}\ket{v_i}\bra{v_i} + \sum_{i=k+1}^n e^{-\iota x} \ket{v_i}\bra{v_i} \]

By Euler's identity we have:

\[ e^{\iota Ax}=\sum_{i=1}^k (\cos(x)+\iota\sin(x))\ket{v_i}\bra{v_i} + \sum_{i=k+1}^n (\cos(x)-\iota \sin(x))\ket{v_i}\bra{v_i} \]

Grouping terms, we obtain:

\[ e^{\iota Ax}=\cos(x) \sum_{i=1}^n \ket{v_i}\bra{v_i} + \iota \sin(x)\left(\sum_{i=1}^k \ket{v_i}\bra{v_i}-\sum_{i=k+1}^n \ket{v_i}\bra{v_i}\right) \]

Using the spectral decomposition and the definition of \(I\), we therefore obtain the desired relation:

\[ e^{\iota Ax}=\cos(x)I+\iota\sin(x)A \]

Since all the Pauli matrices satisfy \(A^2=I\), i.e. they are self-inverse, for \(\theta\in\mathbb{R}\) we can apply this obtained relation. This will be useful for application in Hamiltonian simulation.

Worked Example

Functions of the Pauli matrices. Let \(\vec{n}\) be a normalized (real, three-dimensional unit) vector and \(\theta\) a real number. Let \(f(\cdot)\) be any function from complex numbers to complex numbers. Show that

\[ f(\theta \vec{n} \cdot \vec{\sigma})=\frac{f(\theta)+f(-\theta)}{2}I+\frac{f(\theta)-f(-\theta)}{2}\vec{n}\cdot \vec{\sigma} \]

where \(\vec{n}\cdot\vec{\sigma}=\sum_{i=1}^3 n_i\sigma_i\), which implies \(\theta\vec{n}\cdot\vec{\sigma}=\sum_{i=1}^3 \theta n_i\sigma_i\).

Show solution
\[\begin{aligned} \sum_{i=1}^3 \theta n_i\sigma_i &= \theta n_1 \ket{+}\bra{+}-\theta n_1\ket{-}\bra{-}+\theta n_2\ket{+\iota}\bra{+\iota}-\theta n_2\ket{-\iota}\bra{-\iota} + \theta n_3\ket{0}\bra{0}-\theta n_3\ket{1}\bra{1}\\ &=\theta \begin{bmatrix} n_3 & n_1-\iota n_2 \\ n_1 + \iota n_2 & -n_3 \end{bmatrix} \end{aligned}\]

where \(\vec{n}\) is a real, three-dimensional unit vector and \(\theta \in \mathbb{R}\), \(n_1,n_2,n_3 \in \mathbb{R}\). Note that the above matrix is clearly Hermitian, thus has a unitary decomposition (a complete set of eigenvectors spanning \(\mathbb{C}^2\)). Solving for the eigenvalues we get:

\[ \begin{vmatrix} n_3 - \lambda & n_1-\iota n_2 \\ n_1+\iota n_2 & -n_3-\lambda \end{vmatrix} = 0 \]

This results in the eigenvalues (using the property of \(\vec{n}\) being a unit vector):

\[ \lambda=\pm\sqrt{n_1^2+n_2^2+n_3^2}=\pm 1 \]

Say its eigenvectors are \(\ket{v_+}\) corresponding to \(\lambda=1\) and \(\ket{v_-}\) corresponding to \(\lambda=-1\). Thus:

\[ \vec{n}\cdot\vec{\sigma}=\ket{v_+}\bra{v_+}-\ket{v_-}\bra{v_-} \] \[ \sum_{i=1}^3 \theta n_i\sigma_i=\theta (\ket{v_+}\bra{v_+} - \ket{v_-}\bra{v_-}) \]

Now,

\[ f(\theta \vec{n}\cdot\vec{\sigma})=f(\theta)\ket{v_+}\bra{v_+}+f(-\theta)\ket{v_-}\bra{v_-} \]

From the completeness relation we can write the identity as \(I=\ket{v_+}\bra{v_+}+\ket{v_-}\bra{v_-}\), hence for an observable on a 2-dimensional Hilbert space with eigenvalues \(\lambda=\pm 1\) the projectors are

\[\begin{aligned} \frac{I+\vec{n}\cdot\vec{\sigma}}{2}&=\ket{v_+}\bra{v_+}\\ \frac{I-\vec{n}\cdot\vec{\sigma}}{2}&=\ket{v_-}\bra{v_-} \end{aligned}\]

Substituting this in the equation we get:

\[ f(\theta\vec{n}\cdot\vec{\sigma})=f(\theta)\frac{I+\vec{n}\cdot\vec{\sigma}}{2}+f(-\theta)\frac{I-\vec{n}\cdot\vec{\sigma}}{2} \]

On simplifying:

\[ f(\theta\vec{n}\cdot\vec{\sigma})=\frac{f(\theta)+f(-\theta)}{2}I+\frac{f(\theta)-f(-\theta)}{2}\vec{n}\cdot\vec{\sigma} \]

In case we write the function as \(e^{\iota \theta\vec{n}\cdot\vec{\sigma}}\) we get:

\[ e^{\iota\theta\vec{n}\cdot\vec{\sigma}}=\cos(\theta)I+\iota\sin(\theta) \vec{n}\cdot\vec{\sigma} \]

Quantum Rotation Gates

These are generalized rotations on the Bloch Sphere denoted as \(R_X,R_Y,R_Z\) about the X, Y and Z axis of the Bloch Sphere respectively. The Pauli matrices give rise to three useful classes of unitary matrices when they are exponentiated: the rotation operators about the \(x,y,z\) axes.

\(R_X\) Gate

It is a rotation about the X axis of the Bloch Sphere by an angle \(\theta\). Putting \(\vec{n}=(1,0,0)\) in the previous equation. This is denoted as \(R^{X}_{\theta}\). The matrix representation of \(R^{X}_{\theta}\) is:

\[ R^{X}_{\theta}=\begin{bmatrix} \cos(\theta/2) & -i\sin(\theta/2) \\ -i\sin(\theta/2) & \cos(\theta/2) \end{bmatrix}=\cos \frac{\theta}{2}I-\iota \sin\frac{\theta}{2}\sigma_X=e^{-\iota\frac{\theta}{2}\sigma_X} \]

The code for the \(R_X\) Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='U1 Gate')

circuit.rx(pi/2,qreg_q[0]) #Apply the RX gate

circuit.draw(output='mpl')#.savefig('../images/rx-gate.png') #Draw the circuit
Circuit symbol for the RX rotation gate with angle pi/2
Figure — \(R_X\) Gate.

Note that the value \(\pi/2\) given in the figure denotes the angle by which we rotate around the X axis of the Bloch Sphere. Note that \(R_{\pi}^{X} = -\iota X\).

\(R_Y\) Gate

It is a rotation about the Y axis of the Bloch Sphere by an angle \(\theta\). This is denoted as \(R^{Y}_{\theta}\). The matrix representation of \(R^{Y}_{\theta}\) is:

\[ R^{Y}_{\theta}=\begin{bmatrix} \cos(\theta/2) & -\sin(\theta/2) \\ \sin(\theta/2) & \cos(\theta/2) \end{bmatrix}= \cos \frac{\theta}{2}I-\iota \sin \frac{\theta}{2}\sigma_Y=e^{-\iota \frac{\theta}{2}\sigma_Y} \]

The code for the \(R_Y\) Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='RY Gate')

circuit.ry(pi/2,qreg_q[0]) #Apply the RY gate

circuit.draw(output='mpl')#.savefig('../images/ry-gate.png') #Draw the circuit
Circuit symbol for the RY rotation gate with angle pi/2
Figure — \(R_Y\) Gate.

Note here the value \(\pi/2\) denotes the angle by which we rotate around the Y axis of the Bloch Sphere. Note that \(R_{\pi}^Y=-\iota Y\).

\(R_Z\) Gate

It is a rotation about the Z axis of the Bloch Sphere by an angle \(\theta\). This is denoted as \(R^{Z}_{\theta}\). The matrix representation of \(R^{Z}_{\theta}\) is:

\[ R^{Z}_{\theta}=\begin{bmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{bmatrix}=\cos \frac{\theta}{2}I-\iota \sin \frac{\theta}{2}\sigma_Z=e^{-\iota \frac{\theta}{2}\sigma_Z} \]

The code for the \(R_Z\) Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='RZ Gate')

circuit.rz(pi/2,qreg_q[0]) #Apply the RZ gate

circuit.draw(output='mpl')#.savefig('../images/rz-gate.png') #Draw the circuit
Circuit symbol for the RZ rotation gate with angle pi/2
Figure — \(R_Z\) Gate.

Note here the value \(\pi/2\) denotes the angle by which we rotate around the Z axis of the Bloch Sphere. Note that \(R_{\pi}^Z = -\iota Z\). These quantum rotation gates can be used in making variational circuits, as the angles can vary.

\(R_n\) Gate

If \(\hat{n}=(n_x,n_y,n_z)\) is a real unit vector in three dimensions then we generalize the previous definitions by defining a rotation by \(\theta\) about the \(\hat{n}\) axis by the equation

\[ R_{\hat{n}}(\theta)=e^{-\iota \theta \hat{n}\cdot \vec{\sigma}/2}=\cos \left(\frac{\theta}{2}\right)I- \iota \sin \left(\frac{\theta}{2}\right) (n_xX+n_yY+n_zZ) \]

where \(\vec{\sigma}\) denotes the three-component vector \((X,Y,Z)\) of Pauli matrices. Thus, in matrix form, we can write \(R_n(\theta)\) as:

\[ \begin{bmatrix} \cos \frac{\theta}{2} - \iota \sin \frac{\theta}{2} n_z & -\sin \frac{\theta}{2} (n_y+\iota n_x)\\ \sin \frac{\theta}{2} (n_y-\iota n_x) & \cos\frac{\theta}{2} + \iota \sin \frac{\theta}{2} n_z \end{bmatrix} \]

Universal Quantum Gates

To construct any single qubit quantum gate:

\[ U_3(\theta,\phi,\lambda)=\begin{bmatrix} \cos(\theta/2) & -e^{i\lambda}\sin(\theta/2) \\ e^{i\phi}\sin(\theta/2) & e^{i(\phi+\lambda)}\cos(\theta/2) \end{bmatrix} \]

where \(\theta,\phi,\lambda\in\mathbb{R}\) are rotations around the x, y and z axis respectively. Here, \(0 \leq \theta \leq \pi\), \(0\leq \phi \leq 2\pi\) and \(0\leq \lambda \leq 2\pi\). Similarly, we can fix \(\theta\) and write the \(U_2\) gate as:

\[ U_2(\phi,\lambda)=\frac{1}{\sqrt{2}}\begin{bmatrix} 1 & -e^{i\lambda} \\ e^{i\phi} & e^{i(\phi+\lambda)} \end{bmatrix} \]

Here \(\theta=\pi/2\) by default. Similarly, we can fix \(\theta\) and \(\phi\) and write the \(U_1\) gate as:

\[ U_1(\lambda)=\begin{bmatrix} 1 & 0 \\ 0 & e^{i\lambda} \end{bmatrix} \]

Here \(\theta=\phi=0\) by default. The code for the \(U_3\) Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(1, 'q')
circuit = QuantumCircuit(qreg_q,name='U3 Gate')

circuit.u(pi/2,pi/2,pi/2,qreg_q[0]) #Apply the U3 gate

circuit.draw(output='mpl')#.savefig('../images/u3-gate.png') #Draw the circuit
Circuit symbol for the U3 universal single-qubit gate
Figure — \(U_3\) Gate.

Note here the values \(\pi/2,\pi/2,\pi/2\) denote the angles \(\theta,\phi,\lambda\) by which we rotate around the Bloch Sphere respectively.

Every single-qubit gate is secretly just a rotation of the Bloch sphere about some axis. The \(R_X, R_Y, R_Z\) gates let you dial in any angle about the three coordinate axes; \(R_n\) does it about any axis you like; and \(U_3\) is the fully general three-angle knob that can reproduce any one-qubit gate at all.
Worked Example

Show that, up to a global phase, the \(\pi/8\) gate satisfies \(T=R_z(\pi/4)\).

Show solution

Recall that the \(T\) gate is defined as \(T=\begin{bmatrix} 1 & 0 \\ 0 & e^{\iota \pi/4} \end{bmatrix}\). We observe that:

\[ R_z(\pi/4)=\begin{bmatrix} e^{-\iota \pi/8} & 0 \\ 0 & e^{\iota \pi/8} \end{bmatrix}=e^{-\iota \pi/8}\begin{bmatrix} 1 & 0 \\ 0 & e^{\iota \pi/4} \end{bmatrix} = e^{-\iota \pi/8}T \]
Worked Example

Express the Hadamard gate \(H\) as a product of \(R_x\) and \(R_z\) rotations and \(e^{\iota \varphi}\) for some \(\varphi\).

Show solution

We claim that \(H=R_z(\pi/2)R_x(\pi/2)R_z(\pi/2)\) up to a global phase of \(e^{-\iota \pi/2}\). Verifying this claim:

\[\begin{aligned} R_z(\pi/2)R_x(\pi/2)R_z(\pi/2)&=\begin{bmatrix} e^{-\iota \pi/4} & 0 \\ 0 & e^{\iota \pi/4} \end{bmatrix} \begin{bmatrix} \cos\frac{\pi}{4} & -\iota \sin\frac{\pi}{4} \\ - \iota \sin \frac{\pi}{4} & \cos\frac{\pi}{4} \end{bmatrix}\begin{bmatrix} e^{-\iota \pi/4} & 0 \\ 0 &e^{\iota \pi/4} \end{bmatrix}\\ &=\begin{bmatrix} e^{-\iota \pi/4} & 0 \\ 0 & e^{\iota \pi/4} \end{bmatrix} \begin{bmatrix}\frac{1}{\sqrt{2}} & -\frac{\iota}{\sqrt{2}} \\ -\frac{\iota}{\sqrt{2}} & \frac{1}{\sqrt{2}} \end{bmatrix}\begin{bmatrix} e^{-\iota \pi/4} & 0 \\ 0 &e^{\iota \pi/4} \end{bmatrix}\\ &=\frac{1}{\sqrt{2}} \begin{bmatrix} e^{-\iota \pi/4} & 0 \\ 0 & e^{\iota \pi/4} \end{bmatrix}\begin{bmatrix} e^{-\iota \pi/4} & -\iota e^{\iota\pi/4} \\ -\iota e^{-\iota \pi/4} & e^{\iota \pi/4} \end{bmatrix}\\ &=\frac{1}{\sqrt{2}}\begin{bmatrix} e^{-\iota \pi/2} & -e^{\iota \pi/2} \\ - e^{-\iota \pi/2} & e^{\iota \pi/2} \end{bmatrix}\\ &=\frac{e^{-\iota \pi/2}}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}\\ &=e^{-\iota \pi/2} H \end{aligned}\]

Thus, we get \(e^{\iota \pi/2} R_z(\pi/2)R_x(\pi/2)R_z(\pi/2) =H\).

Worked Example

Prove that \((\hat{\mathbf{n}}\cdot \boldsymbol{\sigma})^2 = I\).

Show solution
\[\begin{aligned} (\hat{\mathbf{n}}\cdot \boldsymbol{\sigma})^2 &= (n_xX+n_yY+n_zZ)^2\\ &=n_x^2X^2+n_y^2 Y^2+n_z^2 Z^2 +n_xn_y(XY+YX)+n_xn_z(XZ+ZX)+n_yn_z(YZ+ZY) \end{aligned}\]

Recall that \(\{\sigma_i,\sigma_j\}=2\delta_{ij}I\); using this, we have:

\[ (\hat{\mathbf{n}} \cdot \boldsymbol{\sigma})^2 =(n_x^2 + n_y^2 + n_z^2)I=I \]

where we use the fact that \(\hat{\mathbf{n}}\) is a vector of unit length.

Worked Example

One reason the \(\hat{R}_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 the Bloch vector \(\boldsymbol{\lambda}\). Then the effect of the rotation \(\hat{R}_n(\theta)\) on the state is to rotate by an angle \(\theta\) about the \(\hat{n}\) axis of the Bloch sphere. This fact explains the rather mysterious-looking factors of two in the definition of the rotation matrices.

Show solution

Let \(\boldsymbol{\lambda}\) be an arbitrary Bloch vector. Without loss of generality, we can express \(\boldsymbol{\lambda}\) in a coordinate system such that \(\hat{n}\) is aligned with the \(z\) axis, so it suffices to consider how the state behaves under the application of \(R_z(\theta)\). The density operator corresponding to this Bloch vector is:

\[ \rho=\frac{I+\boldsymbol{\lambda} \cdot \boldsymbol{\sigma}}{2} \]

We now observe how \(\rho\) transforms under conjugation by \(R_z(\theta)\).

\[\begin{aligned} R_z(\theta)\rho R_z(\theta)^{\dagger} &=R_z(\theta)\rho R_z(-\theta)\\ &=R_z(\theta)\left(\frac{I+\lambda_xX+\lambda_yY+\lambda_zZ}{2}\right)R_z(-\theta) \end{aligned}\]

Using that \(XZ=-ZX\), we observe that:

\[\begin{aligned} R_z(\theta)X&=\left(\cos \frac{\theta}{2}I-\iota \sin\frac{\theta}{2}Z \right) X\\ &=X\left(\cos \frac{\theta}{2}I+\iota \sin\frac{\theta}{2}Z\right)\\ &=X\left(\cos\frac{-\theta}{2}I-\iota \sin\frac{-\theta}{2}Z\right)\\ &=XR_z(-\theta) \end{aligned}\]

Similarly, \(R_z(\theta)Y=YR_z(-\theta)\) (same anticommutation) and \(R_z(\theta)Z=ZR_z(\theta)\) (all terms commute). With this, the expression \(R_z(\theta)\rho R_z(\theta)^{\dagger}\) simplifies to:

\[\begin{aligned} R_z(\theta)\rho R_z(\theta)^{\dagger}&=R_z(\theta)\left(\frac{I+\lambda_xX+\lambda_yY+\lambda_zZ}{2}\right) R_z(-\theta)\\ &= \frac{I+\lambda_xXR_z(-2\theta)+\lambda_yYR_z(-2\theta)+\lambda_zZ}{2} \end{aligned}\]

Calculating each of the terms:

\[\begin{aligned} XR_z(-2\theta)&=X\left(\cos\frac{-2\theta}{2}-\iota \sin\frac{-2\theta}{2} Z\right)\\ &=X(\cos\theta+\iota \sin\theta\,Z)\\ &=\cos\theta\,X+\iota \sin\theta\,XZ\\ &=\cos\theta\,X+\iota \sin\theta\,(-\iota Y)\\ &=\cos\theta\,X+\sin\theta\,Y\\ YR_z(-2\theta)&=Y(\cos\theta+\iota \sin\theta\,Z)\\ &=\cos\theta\,Y+\iota \sin\theta\,YZ\\ &=\cos\theta\,Y+\iota \sin\theta\,(\iota X)\\ &=\cos\theta\,Y-\sin\theta\,X \end{aligned}\]

Plugging these back and collecting like terms:

\[ R_z(\theta)\rho R_z(\theta)^{\dagger}=\frac{I+(\lambda_x\cos\theta-\lambda_y\sin\theta)X+(\lambda_x\sin\theta+\lambda_y\cos\theta)Y+\lambda_zZ}{2} \]

From this we can read off the new Bloch vector \(\boldsymbol{\lambda'}\) after conjugation by \(R_z(\theta)\):

\[ \boldsymbol{\lambda'}=(\lambda_x\cos\theta-\lambda_y \sin\theta,\ \lambda_x\sin\theta+\lambda_y\cos\theta,\ \lambda_z) \]

Alternatively, suppose we apply the 3-dimensional rotation matrix \(A_z(\theta)\) to the original Bloch vector \(\boldsymbol{\lambda}\):

\[ A_z(\theta)\boldsymbol{\lambda}=\begin{bmatrix}\cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} \lambda_x \\ \lambda_y \\ \lambda_z \end{bmatrix}=\begin{bmatrix} \lambda_x \cos\theta-\lambda_y \sin\theta \\ \lambda_x\sin\theta+\lambda_y \cos\theta \\ \lambda_z \end{bmatrix} \]

We end up with the same resulting vector \(\boldsymbol{\lambda'}\). We conclude that conjugation of \(\rho\) under \(R_z(\theta)\) has the equivalent effect of rotating the Bloch vector by \(\theta\) about the z-axis, and hence the effect of \(R_n(\theta)\) on a one-qubit state is to rotate it by an angle \(\theta\) about \(\hat{\mathbf{n}}\).

Worked Example

Show that \(XYX=-Y\) and use this to prove that \(XR_y(\theta)X=R_y(-\theta)\).

Show solution

For the first claim we use that \(XY=-YX\) and \(X^2=I\) to obtain:

\[ XYX=-YXX=-YI=-Y \]

Using this:

\[\begin{aligned} XR_y(\theta)X&=X\left(\cos\frac{\theta}{2}I-\iota\sin\frac{\theta}{2}Y\right)X\\ &=\cos\frac{\theta}{2}XIX-\iota \sin\frac{\theta}{2}XYX\\ &=\cos\frac{\theta}{2} I+\iota \sin\frac{\theta}{2}Y\\ &=\cos\frac{-\theta}{2}I-\iota \sin\frac{-\theta}{2}Y\\ &=R_y(-\theta) \end{aligned}\]
Worked Example

An arbitrary single qubit unitary operator can be written in the form \(U=e^{\iota \alpha}R_n(\theta)\) for some real numbers \(\alpha\) and \(\theta\), and a real three-dimensional unit vector \(\hat{n}\).

  1. Prove this fact.
  2. Find values for \(\alpha,\theta,\hat{n}\) giving the Hadamard gate \(H\).
  3. Find values for \(\alpha,\theta,\hat{n}\) giving the phase gate \(S=\begin{bmatrix} 1 & 0 \\ 0 & \iota \end{bmatrix}\).
Show solution
  1. By definition, for any unitary operator \(U\) we have \(U^{\dagger}U=I\), so for any state vector \(\braket{\psi|\psi}=\bra{\psi}U^{\dagger}U\ket{\psi}\). Therefore all unitary \(U\)s are norm-preserving, and hence for a single qubit correspond to some reflection/rotation in 3-dimensional space (up to a global phase factor). Hence we can write \(U=e^{\iota \alpha}R_n(\theta)\) for some \(\hat{n}\) (rotation axis), \(\theta\) (rotation angle) and \(\alpha\) (global phase). Any \(2\times 2\) unitary matrix for \(a^2+b^2+c^2+d^2=1\) can be written as

    \[ U=e^{\iota \alpha} \begin{bmatrix} a +\iota b & c+\iota d \\ -c+\iota d & a-\iota b \end{bmatrix} \]

    Consider the given form for \(U\). Note that we can write \(R_n(\theta)=e^{-\iota \frac{\theta}{2} (\hat{n}\cdot \sigma)}\) by substituting the values of the \(X, Y, Z\) Pauli matrices, arriving at:

    \[ U=e^{\iota \alpha}\begin{bmatrix} \cos \frac{\theta}{2} - \iota \sin \frac{\theta}{2} n_z & -\sin \frac{\theta}{2} (n_y+\iota n_x)\\ \sin \frac{\theta}{2} (n_y-\iota n_x) & \cos\frac{\theta}{2} + \iota \sin \frac{\theta}{2} n_z \end{bmatrix} \]

    As \(n_x^2+n_y^2+n_z^2=1\), this has the same form as the general \(U\); there are four independent variables — any two of \(n_x,n_y,n_z\) (constrained by \(n_x^2+n_y^2+n_z^2=1\)) and \(\theta, \alpha\). Hence any arbitrary \(2\times 2\) unitary matrix can be written as \(U=e^{\iota \alpha}R_n(\theta)\).

  2. Using \(H=\frac{X+Z}{\sqrt{2}}\), and that modulo a factor of \(\iota\), \(X\) and \(Z\) correspond to rotations \(R_x(\pi)\) and \(R_z(\pi)\):

    \[\begin{aligned} H=\frac{\iota R_x(\pi)+\iota R_z(\pi)}{\sqrt{2}}&=\iota\left(\frac{2\cos\frac{\pi}{2}I-\iota \sin\frac{\pi}{2}X-\iota \sin\frac{\pi}{2} Z}{\sqrt{2}}\right)\\ &=e^{\iota \frac{\pi}{2}} \left(\cos\frac{\pi}{2}I-\iota \sin\frac{\pi}{2}\left(\frac{1}{\sqrt{2}}X+0Y+\frac{1}{\sqrt{2}}Z\right)\right) \end{aligned}\]

    In the second equality we use that \(\cos\frac{\pi}{2}=0\) and hence \(\frac{2}{\sqrt{2}}\cos\frac{\pi}{2}=\cos\frac{\pi}{2}\). From the last expression we read off \(n=\left(\frac{1}{\sqrt{2}},0,\frac{1}{\sqrt{2}}\right)\), \(\theta=\pi\), and \(\alpha=\frac{\pi}{2}\).

  3. We observe that

    \[ R_z\left(\frac{\pi}{2}\right)=\begin{bmatrix} e^{-\iota \pi/4} & 0 \\ 0 & e^{\iota \pi/4} \end{bmatrix}=e^{-\iota \pi/4}\begin{bmatrix} 1 & 0 \\ 0 & \iota \end{bmatrix} \]

    Hence \(S=e^{\iota \pi/4}R_z\left(\frac{\pi}{2}\right)\), from which \(\hat{n}=\hat{z}=(0,0,1),\ \theta=\frac{\pi}{2}\), and \(\alpha=\frac{\pi}{4}\).

Note that one can just use the definition \(R_n(\theta)=e^{-\iota \theta \hat{n}\cdot \sigma/2}=\cos\frac{\theta}{2}I-\iota \sin\frac{\theta}{2}(n_xX+n_yY+n_zZ)\) and the fact that \(H=(X+Z)/\sqrt{2}\), to arrive at \(\cos\frac{\theta}{2}=0,\ n_x=n_z=\frac{1}{\sqrt{2}},\ n_y=0\).

An arbitrary unitary operator on a single qubit can be written in many ways as a combination of rotations, together with global phase shifts on the qubit. The following theorem provides a means of expressing an arbitrary single qubit rotation that will be particularly useful in later applications to controlled operations.

Theorem

(Z–Y decomposition for a 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^{\iota \alpha} R_z(\beta)R_y(\gamma)R_z(\delta) \]
Proof

Since \(U\) is unitary, the rows and columns of \(U\) are orthonormal, from which it follows that there exist real numbers \(\alpha,\beta,\gamma,\delta\) such that

\[ U=\begin{bmatrix} e^{\iota (\alpha - \beta/2 -\delta/2)} \cos \frac{\gamma}{2} & -e^{\iota (\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\ e^{\iota (\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2} & e^{\iota(\alpha+\beta/2+\delta/2)}\cos \frac{\gamma}{2} \end{bmatrix} \]

Thus, the equation now follows immediately from the definition of the rotation matrices and matrix multiplication. ∎

Worked Example

Explain why any single qubit unitary operator may be written in the previous form.

Show solution

Let \(U\) be a single qubit unitary operator. We have \(U^{\dagger}U=I\), so identifying

\[ U=\begin{bmatrix} a & b \\ c & d \end{bmatrix} = \begin{bmatrix} \mathbf{v_1} & \mathbf{v_2} \end{bmatrix} \]

we obtain

\[ \begin{bmatrix} |a|^2 + |c|^2 & a^*b+c^*d \\ ab^*+cd^* & |b|^2+|d|^2 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \]

From the diagonal entries we obtain \(|a|^2+|c|^2=1\) and \(|b|^2+|d|^2=1\), and from the off-diagonal entries \(\braket{v_1|v_2}=0\), hence the columns of \(U\) are orthonormal. From the fact that \(v_1\) is normalized, we can parametrize the magnitudes with \(\gamma \in \mathbb{R}\) such that \(|a|=\cos \frac{\gamma}{2}, |c|=\sin \frac{\gamma}{2}\). From orthogonality \(U^{\dagger}=U^{-1}\) we get

\[ U^{-1}=\begin{bmatrix} d & -b \\ -c & a \end{bmatrix} =U^{\dagger} = \begin{bmatrix} a^* & c^* \\ b^* & d^* \end{bmatrix} \]

We further obtain \(b=-c^*\) and \(d=a^*\), from which \(|b|=|c|\) and \(|d|=|a|\). Thus:

\[ U=\begin{bmatrix} a & -c^* \\c & a^* \end{bmatrix} \]

Furthermore (also from orthogonality) we can parametrize such that \(\arg(a)=-\frac{\beta}{2}-\frac{\delta}{2}\) and \(\arg(b)=\frac{\beta}{2} - \frac{\delta}{2}\) for \(\beta,\delta \in \mathbb{R}\). Finally, multiplying \(U\) by a complex phase \(e^{\iota \alpha}\) preserves the unitarity of \(U\) and the orthonormality of the columns. Combining these facts gives the desired form:

\[ U=\begin{bmatrix} e^{\iota (\alpha - \beta/2 -\delta/2)} \cos \frac{\gamma}{2} & -e^{\iota (\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\ e^{\iota (\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2} & e^{\iota(\alpha+\beta/2+\delta/2)}\cos \frac{\gamma}{2} \end{bmatrix}=e^{\iota \alpha}\begin{bmatrix} a & -c^* \\ c & a^* \end{bmatrix} \]

which is the general form of a \(2\times 2\) unitary matrix, as \(\det(U)=\cos^2 \frac{\gamma}{2} + \sin^2 \frac{\gamma}{2}=1\).

Worked Example

(X–Y decomposition of rotations) Give a decomposition analogous to the previous theorem but using \(R_x\) instead of \(R_z\).

Show solution

We use the fact that we can do a change of reference to interchange the \(x\) and \(z\) axes. The Hadamard \(H\), being unitary and self-inverse, implements the appropriate swap between the \(x\) and \(z\) axes, while sending \(y\) to \(-y\). So instead of working with \(U\) directly, consider

\[ U'=HUH^{-1}= HUH \]

which is the frame-rotated \(U\). Now we use the ZY decomposition of \(U'\): \(U'=e^{\iota \alpha}R_z(\beta)R_y(\gamma)R_z(\delta)\). What does this imply for \(U\)?

\[\begin{aligned} U=HU'H&=e^{\iota \alpha}(HR_z(\beta)H)(HR_y(\gamma)H)(HR_z(\delta)H)\\ &=e^{\iota \alpha} R_x(\beta)R_y(-\gamma)R_x(\delta) \end{aligned}\]
Worked Example

Suppose \(\hat{m}\) and \(\hat{n}\) are non-parallel real unit vectors in three dimensions. Use the theorem to show that an arbitrary single qubit unitary \(U\) may be written \(U=e^{\iota \alpha} R_{\hat{n}}(\beta)R_{\hat{m}}(\gamma)R_{\hat{n}}(\delta)\) for appropriate choices of \(\alpha,\beta,\gamma,\delta\).

Show solution

The same arguments as in the above example generalize. We could choose any two orthogonal axes \(\hat{n},\hat{m}\) for rotations in our decomposition, using the fact that we can always frame-rotate to the \(z\) and \(x\) axes.

Consider the following corollary, which is the key to the construction of controlled multi-qubit unitary operations.

Corollary

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^{\iota \alpha}AXBXC\), where \(\alpha\) is some overall phase factor.

Proof

Set \(A=R_z(\beta)R_y(\gamma/2)\), \(B=R_y(-\gamma/2)R_z(-(\delta+\beta)/2)\) and \(C=R_z((\delta-\beta)/2)\). Note that

\[ ABC=R_z(\beta)R_y\left(\frac{\gamma}{2}\right)R_y\left(-\frac{\gamma}{2}\right)R_z\left(-\frac{\delta+\beta}{2}\right)R_z\left(\frac{\delta-\beta}{2}\right)=I \]

Since \(X^2=I\) and \(XR_y(\theta)X=R_y(-\theta)\), we see that

\[ XBX=XR_y\left(-\frac{\gamma}{2}\right)XXR_z\left(-\frac{\delta+\beta}{2}\right)X=R_y\left(\frac{\gamma}{2}\right)R_z\left(\frac{\delta+\beta}{2}\right) \]

Thus

\[\begin{aligned} AXBXC&=R_z(\beta)R_y\left(\frac{\gamma}{2}\right)R_y\left(\frac{\gamma}{2}\right)R_z\left(\frac{\delta+\beta}{2}\right)R_z\left(\frac{\delta-\beta}{2}\right)\\ &=R_z(\beta)R_y(\gamma)R_z(\delta) \end{aligned}\]

Thus \(U=e^{\iota\alpha}AXBXC\) and \(ABC=I\), as required. ∎

Worked Example

Give \(A,B,C\) and \(\alpha\) for the Hadamard Gate.

Show solution

Recall that \(U\) is unitary such that

\[ U=\begin{bmatrix} e^{\iota (\alpha - \beta/2 -\delta/2)} \cos \frac{\gamma}{2} & -e^{\iota (\alpha-\beta/2+\delta/2)}\sin\frac{\gamma}{2}\\ e^{\iota (\alpha+\beta/2-\delta/2)}\sin\frac{\gamma}{2} & e^{\iota(\alpha+\beta/2+\delta/2)}\cos \frac{\gamma}{2} \end{bmatrix} \]

This matrix becomes \(H\) when \(\alpha=\gamma=\frac{\pi}{2},\ \delta=\pi,\ \beta=0\). Thus, from the previous corollary, \(A=R_z(\beta)R_y(\gamma/2)=R_y\left(\frac{\pi}{4}\right)\), \(B=R_y(-\gamma/2)R_z(-(\delta+\beta)/2)=R_y\left(-\frac{\pi}{4}\right)R_z\left(-\frac{\pi}{2}\right)\), \(C=R_z((\delta-\beta)/2)=R_z\left(\frac{\pi}{2}\right)\) and \(\alpha=0\).

Worked Example

(Circuit identities) It is useful to be able to simplify circuits by inspection using well-known identities. Prove the following three identities:

\[ HXH=Z;\quad HYH=-Y;\quad HZH=X \]
Show solution
\[ HXH=\frac{1}{2}\begin{bmatrix}1 & 1 \\ 1 & -1 \end{bmatrix}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} =\frac{1}{2}\begin{bmatrix}2 & 0 \\ 0 & -2 \end{bmatrix} = Z \] \[ HYH=\frac{1}{2}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix} 0 & -\iota \\ \iota & 0 \end{bmatrix} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} =\frac{1}{2} \begin{bmatrix} 0 & 2\iota \\ -2\iota & 0 \end{bmatrix} = -Y \] \[ HZH=HHXHH=X \]
Worked Example

Use the previous example to show that \(HTH=R_x(\pi/4)\), up to a global phase.

Show solution

Recall that \(T=R_z(\pi/4)\) up to a global phase. Thus \(HTH=HR_z(\pi/4)H\), so:

\[ HTH=HR_z\left(\frac{\pi}{4}\right)H=H\left(\cos\frac{\pi}{8} I - \iota\sin \frac{\pi}{8}Z\right)H=\cos\frac{\pi}{8} I- \iota\sin\frac{\pi}{8}X=R_x\left(\frac{\pi}{4}\right) \]
Worked Example

(Composition of single qubit operations) The Bloch representation gives a nice way to visualize the effect of composing two rotations:

  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{aligned} 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{aligned}\]
  2. Show that if \(\beta_1=\beta_2\) and \(\hat{n}_1=\hat{z}\) these equations simplify to \[\begin{aligned} c_{12}&=c^2-s^2\hat{z}\cdot\hat{n}_2\\ s_{12}\hat{n}_{12}&=sc(\hat{z}+\hat{n}_2)-s^2\hat{n}_2\times \hat{z} \end{aligned}\] where \(c=c_1\) and \(s=s_1\).
Show solution

We are required to compute \(R_{\hat{n}_2}(\beta_2)R_{\hat{n}_1}(\beta_1)\):

\[\begin{aligned} R_{\hat{n}_2}(\beta_2)R_{\hat{n}_1}(\beta_1)&=\left(\cos \frac{\beta_2}{2} I -\iota \sin \frac{\beta_2}{2} \hat{n}_2 \cdot \vec{\sigma}\right) \left(\cos\frac{\beta_1}{2} I -\iota \sin \frac{\beta_1}{2} \hat{n}_1\cdot \vec{\sigma} \right)\\ &=\cos \frac{\beta_2}{2} \cos \frac{\beta_1}{2} - \iota \cos \frac{\beta_2}{2}\sin\frac{\beta_1}{2} \hat{n}_1\cdot \vec{\sigma} - \iota \sin \frac{\beta_2}{2} \cos \frac{\beta_1}{2} \hat{n}_2\cdot \vec{\sigma} \\ &\quad -\sin\frac{\beta_2}{2}\sin\frac{\beta_1}{2} (\hat{n}_2\cdot\vec{\sigma})(\hat{n}_1 \cdot\vec{\sigma})\\ &=[c_2c_1-s_2s_1(\hat{n}_2\cdot\hat{n}_1)]I-\iota [c_1s_2\hat{n}_2+c_2s_1\hat{n}_1+s_2s_1(\hat{n}_2 \times \hat{n}_1)]\cdot\vec{\sigma} \end{aligned}\]

The proof of the identity \((\hat{n}_2 \cdot\vec{\sigma})(\hat{n}_1 \cdot\vec{\sigma})=(\hat{n}_2 \cdot\hat{n}_1)I+\iota (\hat{n}_2\times \hat{n}_1)\cdot \vec{\sigma}\) is as follows. Recall the familiar Pauli matrix relation already proved:

\[ \sigma_i\sigma_j =\delta_{ij} I +\iota \sum_{k=1}^3\epsilon_{ijk}\sigma_k \]

Thus we arrive at

\[ a_i\sigma_ib_j\sigma_j = a_i b_j\delta_{ij} + \iota \sum_{k=1}^3 (a_ib_j\epsilon_{ijk})\sigma_k \]

for \(\epsilon_{ijk}=0\) except \(\epsilon_{123}=\epsilon_{231}=\epsilon_{312}=1\), and \(\epsilon_{321}=\epsilon_{213}=\epsilon_{132}=-1\). Summing over \(i,j=1,2,3\):

\[\begin{aligned} \sum_{i=1}^3\sum_{j=1}^3 a_i\sigma_ib_j\sigma_j &= \sum_{i=1}^3\sum_{j=1}^3a_ib_j \delta_{ij} +\iota \sum_{i=1}^3\sum_{j=1}^3\sum_{k=1}^3(a_ib_j\epsilon_{ijk})\sigma_k\\ (\hat{a}\cdot\vec{\sigma})(\hat{b}\cdot\vec{\sigma})&=(\hat{a}\cdot\hat{b})I+\iota \sum_{k=1}^3 (\hat{a}\times \hat{b})_k\sigma_k \end{aligned}\]

where \(\hat{a}=(a_1,a_2,a_3)\), \(\hat{b}=(b_1,b_2,b_3)\) and \(\vec{\sigma} = (X,Y,Z)\). Thus, in matrix form:

\[ (\hat{a}\cdot\vec{\sigma})(\hat{b}\cdot\vec{\sigma})=(\hat{a}\cdot\hat{b})I+\iota (\hat{a}\times \hat{b})\cdot\vec{\sigma} \]

Identifying this operation with a single rotation \(R_{\hat{n}_{12}}=c_{12}I-\iota s_{12}\hat{n}_{12}\cdot\vec{\sigma}\), we arrive at the required relations

\[\begin{aligned} 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{aligned}\]

Setting \(\beta_1=\beta_2\) and \(\hat{n}_1 = \hat{z}\), combined with \(c=c_1=\cos(\beta_1/2)=\cos(\beta_2/2)=c_2\) (and similarly \(s_1=s_2=s\)) we have:

\[\begin{aligned} c_{12}&=c^2-s^2 \hat{z}\cdot\hat{n}_2\\ s_{12}\hat{n}_{12} &=sc\hat{z} + cs\hat{n}_2 -s^2 \hat{n}_2 \times \hat{z} = sc(\hat{z}+\hat{n}_2)-s^2\hat{n}_2 \times \hat{z} \end{aligned}\]

3.4 Multi-Qubit Gates

If \(A\) is true, then do \(B\). This type of controlled operation is one of the most useful in computing, both classical and quantum. We will now see how complex controlled operations are implemented using quantum circuits built from elementary operations. Quantum Pauli gates and the Hadamard gate are unitary and Hermitian, and all the other single qubit gates are unitary, hence reversible. In general, we can generalize any single qubit gate to a multi-qubit gate. Note that since all the gates can in general be represented as unitary matrices, the number of qubits in the input equals the number of qubits in the output, always. Multi-qubit gates act on multiple qubits at once, thus the matrices are of size \(2^n \times 2^n\) where \(n\) is the number of qubits.

Two-qubit Gates

CNOT / CX Gate

It is a two-qubit gate, also called the Controlled-NOT gate. It has two input qubits, known as the control qubit and the target qubit. Here the 1st qubit is control and the 2nd qubit is target (it is also possible the other way, with the 2nd qubit as control and the 1st as target). This gate is used to create entanglement. Its truth table is below.

InputOutput
\(\ket{00}\)\(\ket{00}\)
\(\ket{01}\)\(\ket{01}\)
\(\ket{10}\)\(\ket{11}\)
\(\ket{11}\)\(\ket{10}\)

(Truth Table for CNOT Gate.) For this table the 1st qubit is control and the 2nd qubit is target. Thus, the CNOT gate flips the target qubit if the control qubit is \(\ket{1}\). The code for the CNOT Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

# Add 2 qubits for CNOT gate demo
qreg_q = QuantumRegister(2, 'q')
circuit = QuantumCircuit(qreg_q,name='CNOT Gate')

circuit.cx(qreg_q[0],qreg_q[1]) #Apply the CNOT gate

circuit.draw(output='mpl')#.savefig('../images/cnot-gate.png') #Draw the circuit
Circuit symbol for the CNOT gate showing a control dot on the first wire and a target plus on the second
Figure — CNOT Gate.
q0: ─────●───────
q1: ─────X───────

ASCII circuit: the control qubit q0 (●) flips the target qubit q1 (X) whenever q0 is \(\ket{1}\). In the figure, the dot denotes the control qubit and the cross denotes the target qubit. In operator notation it is:

\[\begin{aligned} CX\ket{00}&=\ket{00} \\ CX\ket{01}&=\ket{01} \\ CX\ket{10}&=\ket{11} \\ CX\ket{11}&=\ket{10} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{00},\ket{01},\ket{10},\ket{11}\)) as:

\[ CX=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix} \]

In outer product representation:

\[ CX=\ket{00}\bra{00}+\ket{01}\bra{01}+\ket{10}\bra{11}+\ket{11}\bra{10} \]

In compact form we can write the CNOT gate as:

\[ CX\ket{x}\ket{y}=\ket{x}\ket{x\oplus y} \]

where \(\oplus\) denotes the XOR operation. If the control qubit is set to \(\ket{1}\) then the target qubit is flipped, otherwise the target qubit is left alone. It is used in quantum error correction codes. This is a very important gate which is used in the teleportation protocol, superdense coding, quantum error correction codes, etc. Action of CX/CNOT on a general two-qubit state \(\ket{\psi} = \alpha\ket{00}+\beta\ket{01}+\gamma\ket{10}+\delta\ket{11}\) is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \\ \gamma \\ \delta \end{bmatrix}=\begin{bmatrix} \alpha \\ \beta \\ \delta \\ \gamma \end{bmatrix}=\alpha\ket{00}+\beta\ket{01}+\delta\ket{10}+\gamma\ket{11} \]
A controlled gate is an "if–then": look at the control qubit, and only if it is \(\ket{1}\) do you apply the gate to the target. The CNOT is the two-qubit workhorse — it is what lets two qubits become entangled, and it shows up in nearly every quantum protocol.

CY Gate

It is a two-qubit gate, also called the Controlled-Y gate. Here the 1st qubit is control and the 2nd qubit is target (Pauli-Y gate). Its truth table is below.

InputOutput
\(\ket{00}\)\(\ket{00}\)
\(\ket{01}\)\(\ket{01}\)
\(\ket{10}\)\(i\ket{10}\)
\(\ket{11}\)\(-i\ket{11}\)

(Truth Table for CY Gate.) For this table the 1st qubit is control and the 2nd qubit is target. Thus, the CY gate applies Y to the target qubit if the control qubit is \(\ket{1}\). The code for the CY Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

# Add 2 qubits for CNOT gate demo
qreg_q = QuantumRegister(2, 'q')
circuit = QuantumCircuit(qreg_q,name='CY Gate')

circuit.cy(qreg_q[0],qreg_q[1]) #Apply the CY gate

circuit.draw(output='mpl')#.savefig('../images/cy-gate.png') #Draw the circuit
Circuit symbol for the controlled-Y gate
Figure — CY Gate.

In this figure, the first qubit is control and the second qubit is target. If the first qubit is \(\ket{1}\), then the Y gate is applied on the second qubit. The Y gate rotates the qubit by \(\pi\) around the Y axis of the Bloch Sphere; thus if the input is \(\ket{0}\) the output is \(\ket{0}\), and if the input is \(\ket{1}\) the output is \(-i\ket{1}\). In operator notation it is:

\[\begin{aligned} CY\ket{00}&=\ket{00} \\ CY\ket{01}&=\ket{01} \\ CY\ket{10}&=i\ket{11} \\ CY\ket{11}&=-i\ket{10} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{00},\ket{01},\ket{10},\ket{11}\)) as:

\[ CY=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & -\iota \\ 0 & 0 & \iota & 0 \end{bmatrix} \]

In outer product representation:

\[ CY=\ket{00}\bra{00}+\ket{01}\bra{01}+\iota\ket{11}\bra{10}-\iota\ket{10}\bra{11} \]

In compact form we can write the CY gate as:

\[ CY\ket{xy}=(-1)^y\iota^x\ket{x(x\oplus y)} \]

Flipping the amplitude of the 1 state. Marking of this state can be utilised for marking elements in a database. It is useful in Grover's algorithm. Action of CY on a general two-qubit state \(\ket{\psi} = \alpha\ket{00}+\beta\ket{01}+\gamma\ket{10}+\delta\ket{11}\) is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & -\iota \\ 0 & 0 & \iota & 0 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \\ \gamma \\ \delta \end{bmatrix}=\begin{bmatrix} \alpha \\ \beta \\ -\iota\delta \\ \iota\gamma \end{bmatrix}=\alpha\ket{00}+\beta\ket{01}-\iota\delta\ket{10}+\iota\gamma\ket{11} \]

CZ / CPHASE Gate

It is a two-qubit gate, also called the Controlled-Z gate. Here the 1st qubit is control and the 2nd qubit is target (Pauli-Z gate). Its truth table is below.

InputOutput
\(\ket{00}\)\(\ket{00}\)
\(\ket{01}\)\(\ket{01}\)
\(\ket{10}\)\(\ket{10}\)
\(\ket{11}\)\(-\ket{11}\)

(Truth Table for CZ Gate.) For this table the 1st qubit is control and the 2nd qubit is target. Thus, the CZ gate applies Z to the target qubit if the control qubit is \(\ket{1}\). The code for the CZ Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(2, 'q')
circuit = QuantumCircuit(qreg_q,name='CZ Gate')

circuit.cz(qreg_q[0],qreg_q[1]) #Apply the CZ gate

circuit.draw(output='mpl')#.savefig('../images/CZ-gate.png') #Draw the circuit
Circuit symbol for the controlled-Z gate
Figure — CZ Gate.

In this figure, the first qubit is control and the second qubit is target. If the first qubit is \(\ket{1}\), then the Z gate is applied on the second qubit. The Z gate rotates the qubit by \(\pi\) around the Z axis of the Bloch Sphere; thus if the input is \(\ket{0}\) the output is \(\ket{0}\), and if the input is \(\ket{1}\) the output is \(-\ket{1}\). In operator notation it is:

\[\begin{aligned} CZ\ket{00}&=\ket{00} \\ CZ\ket{01}&=\ket{01} \\ CZ\ket{10}&=\ket{10} \\ CZ\ket{11}&=-\ket{11} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{00},\ket{01},\ket{10},\ket{11}\)) as:

\[ CZ=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix} \]

In outer product representation:

\[ CZ=\ket{00}\bra{00}+\ket{01}\bra{01}+\ket{10}\bra{10}-\ket{11}\bra{11} \]

In compact form we can write the CZ gate as:

\[ CZ\ket{xy}=(-1)^{xy}\ket{xy} \]

Action of CZ on a general two-qubit state \(\ket{\psi} = \alpha\ket{00}+\beta\ket{01}+\gamma\ket{10}+\delta\ket{11}\) is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \\ \gamma \\ \delta \end{bmatrix}=\begin{bmatrix} \alpha \\ \beta \\ \gamma \\ -\delta \end{bmatrix}=\alpha\ket{00}+\beta\ket{01}+\gamma\ket{10}-\delta\ket{11} \]

CH / Controlled Hadamard Gate

It is a two-qubit gate, also called the Controlled-Hadamard gate. Here the 1st qubit is control and the 2nd qubit is target. Its truth table is below.

InputOutput
\(\ket{00}\)\(\ket{00}\)
\(\ket{01}\)\(\ket{01}\)
\(\ket{10}\)\(\frac{1}{\sqrt{2}}(\ket{10}+\ket{11})\)
\(\ket{11}\)\(\frac{1}{\sqrt{2}}(\ket{10}-\ket{11})\)

(Truth Table for CH Gate.) For this table the 1st qubit is control and the 2nd qubit is target. Thus, the CH gate applies the Hadamard gate on the target qubit if the control qubit is \(\ket{1}\). The code for the CH Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

# Add 2 qubits for CNOT gate demo
qreg_q = QuantumRegister(2, 'q')
circuit = QuantumCircuit(qreg_q,name='CY Gate')

circuit.ch(qreg_q[0],qreg_q[1]) #Apply the CH gate

circuit.draw(output='mpl')#.savefig('../images/ch-gate.png') #Draw the circuit
Circuit symbol for the controlled-Hadamard gate
Figure — CH Gate.

In this figure, the first qubit is control and the second qubit is target. If the first qubit is \(\ket{1}\), then the Hadamard gate is applied on the second qubit; thus if the input is \(\ket{0}\) the output is \(\ket{0}\), and if the input is \(\ket{1}\) the output is \(\frac{1}{\sqrt{2}}(\ket{0}+\ket{1})\). In operator notation it is:

\[\begin{aligned} CH\ket{00}&=\ket{00} \\ CH\ket{01}&=\ket{01} \\ CH\ket{10}&=\tfrac{1}{\sqrt{2}}(\ket{10}+\ket{11}) \\ CH\ket{11}&=\tfrac{1}{\sqrt{2}}(\ket{10}-\ket{11}) \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{00},\ket{01},\ket{10},\ket{11}\)) as:

\[ CH=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ 0 & 0 & \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix} \]

In outer product representation:

\[ CH=\ket{00}\bra{00}+\ket{01}\bra{01}+\tfrac{1}{\sqrt{2}}\ket{10}\bra{10}+\tfrac{1}{\sqrt{2}}\ket{10}\bra{11} \]

In compact form we can write the CH gate as:

\[ CH\ket{xy}=\left(\frac{1}{\sqrt{2}}\right)^x\left(\ket{x(x\oplus y)}+(-1)^y\ket{xy}\right) \]

Action of CH on a general two-qubit state \(\ket{\psi} = \alpha\ket{00}+\beta\ket{01}+\gamma\ket{10}+\delta\ket{11}\) is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ 0 & 0 & \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \\ \gamma \\ \delta \end{bmatrix}=\begin{bmatrix} \alpha \\ \beta \\ \frac{\gamma+\delta}{\sqrt{2}} \\ \frac{\gamma-\delta}{\sqrt{2}} \end{bmatrix}=\alpha\ket{00}+\beta\ket{01}+\frac{\gamma+\delta}{\sqrt{2}}\ket{10}+\frac{\gamma-\delta}{\sqrt{2}}\ket{11} \]
Important

The CX/CNOT, CY, CZ and CH gates are unitary as well as Hermitian. In general we can write any of the controlled gates, where the first qubit acts as control and the second acts as target, in matrix form as:

\[ \begin{bmatrix} I & 0 \\ 0 & U \end{bmatrix} \]

where \(I\) is the identity matrix of size \(2(2^{n-1}-1)\times 2(2^{n-1}-1)\) and \(U\) is the unitary matrix of size \(2\times 2\). To form any of the \(n\)-qubit controlled gates where there are \(n-1\) control bits and 1 target bit, we replace \(U\) with the corresponding gate. For example, for the 2-qubit controlled Hadamard gate we replace \(U\) with the Hadamard matrix:

\[ \begin{bmatrix} I & 0 \\ 0 & H \end{bmatrix} \]

Similarly, for any \(n\)-qubit controlled gate we can form the matrix by replacing \(U\) with the corresponding gate matrix. For example, for the 3-qubit controlled-Z gate we replace \(U\) with the Z matrix:

\[ \begin{bmatrix} I & 0 \\ 0 & Z \end{bmatrix} \]

where \(I\) is the identity matrix of size \(6\times 6\) and \(Z\) is the Pauli-Z gate matrix of size \(2 \times 2\). We will later use this concept to create the CCNOT/Toffoli gate. More generally, suppose \(U\) is an arbitrary single qubit unitary operation. A controlled-\(U\) operation is a two-qubit operation, again with a control and a target qubit. If the control qubit is set then \(U\) is applied to the target qubit, otherwise the target qubit is left alone; that is, \(\ket{c}\ket{t}\rightarrow \ket{c}U^c\ket{t}\).

Circuit for a controlled-U operation with control on the top wire and U applied to the target on the bottom wire
Figure — Controlled-\(U\) operation. The top line is the control qubit, and the bottom line is the target qubit. If the control qubit is set then \(U\) is applied to the target, otherwise it is left alone.
Worked Example

(Matrix representation of multi-qubit gates) What is the \(4\times 4\) unitary matrix for the first circuit below in the computational basis? What is the unitary matrix for the second circuit?

Two-qubit circuit with a Hadamard gate on the second qubit only
Figure — First circuit.
Two-qubit circuit with a Hadamard gate on the first qubit only
Figure — Second circuit.
Show solution

Depending upon how the input bits are written (as \(x_1x_2\) or \(x_2x_1\)), we can write the matrix in different ways. Writing \(x_1\) first and then \(x_2\), the unitary matrix for the first circuit is:

\[ I_1 \otimes H_2 = \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} \]

The unitary matrix for the second circuit is:

\[ H_1 \otimes I_2 = \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} \]
Worked Example

(Building a CNOT from controlled-\(Z\) gates) Construct a CNOT gate from one controlled-\(Z\) gate — 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.

Show solution

Recall that \(HZH=X\). Hence, to obtain a CNOT gate from a single controlled-\(Z\) gate, we conjugate the target qubit with Hadamard gates:

Circuit building a CNOT by sandwiching a controlled-Z between two Hadamards on the target
Figure — CNOT from a controlled-Z sandwiched by Hadamards.

We can verify this via matrix multiplication, using the result from the previous example:

\[\begin{aligned} (I_1 \otimes H_2)(CZ_{12})(I_1\otimes H_2)&=\frac{1}{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}\begin{bmatrix} 1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & -1 \end{bmatrix}\\ &=\frac{1}{2}\begin{bmatrix} 2 & 0 & 0 & 0 \\ 0 & 2 & 0 & 0 \\ 0 & 0& 0 & 2\\ 0 & 0 & 2 & 0 \end{bmatrix}\\ &=CX_{12} \end{aligned}\]
Worked Example

Show that \(CZ_{12}=CZ_{21}\), i.e. the controlled-Z gate is symmetric in its two qubits.

Circuit identity showing controlled-Z with control and target roles swapped are equal
Figure — CZ is symmetric under exchange of control and target.
Show solution

Recall that we can write \(CZ\ket{b_1}\ket{b_2} = \ket{b_1}\otimes Z^{b_1}\ket{b_2} = (-1)^{b_1 \cdot b_2} \ket{b_1}\ket{b_2}\) for computational basis states \(b_1,b_2\in \{0,1\}\). Using this form:

\[\begin{aligned} CZ_{12}\ket{b_1}\ket{b_2} &= (-1)^{b_1\cdot b_2} \ket{b_1}\ket{b_2}\\ &=\ket{b_1}\otimes Z^{b_2}\ket{b_2}\quad\text{(reading the phase as controlled by }b_2)\\ &=CZ_{21}\ket{b_1}\ket{b_2} \end{aligned}\]

We can also verify this in other ways, such as by checking that the action of the controlled gate on all possible input qubits is the same.

Worked Example

(CNOT action on density matrices) The CNOT gate is a simple permutation whose action on a density matrix \(\rho\) is to rearrange the elements in the matrix. Write out this action explicitly in the computational basis.

Show solution

Let \(\rho\) be an arbitrary density matrix for a 2-qubit state. In the computational basis:

\[ \rho = \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{31} & a_{32} & a_{33} & a_{34}\\a_{41} & a_{42} & a_{43} & a_{44} \end{bmatrix} \]

Studying the action of the CNOT gate on this density matrix:

\[\begin{aligned} CX_{12}\rho CX_{12} &=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix} \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{31} & a_{32} & a_{33} & a_{34} \\ a_{41} & a_{42} & a_{43} & a_{44} \end{bmatrix}\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\\ &=\begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{41} & a_{42} & a_{43} & a_{44}\\ a_{31} & a_{32} & a_{33} & a_{34} \end{bmatrix}\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\\ &=\begin{bmatrix} a_{11} & a_{12} & a_{14} & a_{13} \\ a_{21} & a_{22} & a_{24} & a_{23} \\ a_{41} & a_{42} & a_{44} & a_{43}\\ a_{31} & a_{32} & a_{34} & a_{33} \end{bmatrix} \end{aligned}\]

Thus, the action of CNOT on density matrices is to rearrange the elements in the matrix, as seen from the result.

Worked Example

(CNOT basis transformations) Unlike ideal classical gates, ideal quantum gates do not have (as electrical engineers say) ‘high impedance’ inputs. In fact, the roles of ‘control’ and ‘target’ are arbitrary — they depend on what basis you think of a device as operating in. We have described how CNOT behaves with respect to the computational basis, in which the state of the control qubit is not changed. However, if we work in a different basis then the control qubit does change: its phase is flipped depending on the state of the target qubit. Show this.

Circuit identity showing a CNOT conjugated by Hadamards on both qubits equals a CNOT with roles reversed
Figure — CNOT conjugated by Hadamards on both qubits.
Show solution

Using the identity from the previous example that \(CX=(I_1\otimes H_2)(CZ_{12})(I_1\otimes H_2)\), and \(CZ_{12}=CZ_{21}\), we get:

\[\begin{aligned} (H\otimes H)(CX_{12})(H\otimes H)&=(H\otimes H)(I\otimes H)(CZ_{12})(I\otimes H)(H\otimes H)\\ &=(H\otimes I)(CZ_{12})(H\otimes I)\\ &=(H\otimes I)CZ_{21}(H\otimes I)\\ &=CX_{21} \end{aligned}\]

which proves the circuit identity. We know already that \(CX\ket{c}\ket{t}=\ket{c}\ket{c\oplus t}\). Using the proven identity and the fact that \(H\ket{0}=\ket{+},H\ket{1}=\ket{-}\), we obtain the required map. Introducing basis states \(\ket{\pm} \equiv (\ket{0}\pm\ket{1})/\sqrt{2}\), the effect of a CNOT with the first qubit as control and the second as target is:

\[\begin{aligned} \ket{+}\ket{+}&\rightarrow \ket{+}\ket{+}\\ \ket{-}\ket{+}&\rightarrow \ket{-}\ket{+}\\ \ket{+}\ket{-}&\rightarrow \ket{-}\ket{-}\\ \ket{-}\ket{-}&\rightarrow \ket{+}\ket{-} \end{aligned}\]

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 \(\ket{-}\), otherwise it is left alone. That is, in this basis the target and control have essentially interchanged roles.

SWAP Gate

It is a two-qubit gate which swaps the states of the two qubits. Its truth table is below.

InputOutput
\(\ket{00}\)\(\ket{00}\)
\(\ket{01}\)\(\ket{10}\)
\(\ket{10}\)\(\ket{01}\)
\(\ket{11}\)\(\ket{11}\)

(Truth Table for SWAP Gate.) The code for the SWAP Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

# Add 2 qubits for CNOT gate demo
qreg_q = QuantumRegister(2, 'q')
circuit = QuantumCircuit(qreg_q,name='SWAP Gate')

circuit.swap(qreg_q[0],qreg_q[1]) #Apply the SWAP gate

circuit.draw(output='mpl')#.savefig('../images/swap-gate.png') #Draw the circuit
Circuit symbol for the SWAP gate
Figure — SWAP Gate.

In operator notation it is:

\[\begin{aligned} SWAP\ket{00}&=\ket{00} \\ SWAP\ket{01}&=\ket{10} \\ SWAP\ket{10}&=\ket{01} \\ SWAP\ket{11}&=\ket{11} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{00},\ket{01},\ket{10},\ket{11}\)) as:

\[ SWAP=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

In outer product representation:

\[ SWAP=\ket{00}\bra{00}+\ket{01}\bra{10}+\ket{10}\bra{01}+\ket{11}\bra{11} \]

Action of SWAP on a general two-qubit state \(\ket{\psi} = \alpha\ket{00}+\beta\ket{01}+\gamma\ket{10}+\delta\ket{11}\) is:

\[ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \\ \gamma \\ \delta \end{bmatrix}=\begin{bmatrix} \alpha \\ \gamma \\ \beta \\ \delta \end{bmatrix}=\alpha\ket{00}+\gamma\ket{01}+\beta\ket{10}+\delta\ket{11} \]

The SWAP Gate can also be formed using CNOT gates as follows:

SWAP gate implemented as three alternating CNOT gates
Figure — SWAP Gate using CNOT Gates.

Or using CNOT gates as follows:

Alternative SWAP gate implementation using three CNOT gates
Figure — SWAP Gate using CNOT Gates (alternative).
Important

The global phase does not matter provided we are doing a measurement at the end (or in between). If we are not doing any measurement, then the global phase does matter in the computation.

Three-qubit Gates

CCNOT / Toffoli Gate

It is a three-qubit gate, also called the CCNOT gate. Here the 1st and 2nd qubits are control and the 3rd qubit is target. Its truth table is below.

InputOutput
\(\ket{000}\)\(\ket{000}\)
\(\ket{001}\)\(\ket{001}\)
\(\ket{010}\)\(\ket{010}\)
\(\ket{011}\)\(\ket{011}\)
\(\ket{100}\)\(\ket{100}\)
\(\ket{101}\)\(\ket{101}\)
\(\ket{110}\)\(\ket{111}\)
\(\ket{111}\)\(\ket{110}\)

(Truth Table for Toffoli Gate.) For this table the 1st and 2nd qubits are control and the 3rd qubit is target. Thus, the Toffoli gate flips the target qubit if both control qubits are \(\ket{1}\). The code for the Toffoli Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(3, 'q')
circuit = QuantumCircuit(qreg_q)

circuit.ccx(qreg_q[0], qreg_q[1], qreg_q[2])

circuit.draw(output='mpl')#.savefig('../images/ccx-gate.png')
Circuit symbol for the Toffoli (CCNOT) gate with two control dots and one target
Figure — Toffoli Gate.
q0: ─────●───────
q1: ─────●───────
q2: ─────X───────

ASCII circuit: q2 (X) is flipped only when both controls q0 and q1 (●) are \(\ket{1}\). In operator notation it is:

\[\begin{aligned} CCX\ket{000}&=\ket{000} & CCX\ket{100}&=\ket{100} \\ CCX\ket{001}&=\ket{001} & CCX\ket{101}&=\ket{101} \\ CCX\ket{010}&=\ket{010} & CCX\ket{110}&=\ket{111} \\ CCX\ket{011}&=\ket{011} & CCX\ket{111}&=\ket{110} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{000},\ket{001},\ldots,\ket{110},\ket{111}\)) as:

\[ CCX=\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 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \end{bmatrix} \]

In outer product representation:

\[\begin{aligned} CCX&=\ket{000}\bra{000}+\ket{001}\bra{001}+\ket{010}\bra{010}+\ket{011}\bra{011} \\ &+\ket{100}\bra{100}+\ket{101}\bra{101}+\ket{110}\bra{111}+\ket{111}\bra{110} \end{aligned}\]

Action of CCX on a general three-qubit state \(\ket{\psi} = \alpha\ket{000}+\beta\ket{001}+\gamma\ket{010}+\delta\ket{011}+\epsilon\ket{100}+\zeta\ket{101}+\eta\ket{110}+\theta\ket{111}\) is:

\[ CCX\ket{\psi}=\alpha\ket{000}+\beta\ket{001}+\gamma\ket{010}+\delta\ket{011}+\epsilon\ket{100}+\zeta\ket{101}+\theta\ket{110}+\eta\ket{111} \]

(the last two amplitudes are swapped, as \(\ket{110}\) and \(\ket{111}\) are interchanged).

The Toffoli gate is the quantum version of a classical AND-with-a-flip: flip the target only when both controls are on. It is powerful enough to build any classical logic circuit reversibly — which is why quantum computers can do everything classical computers can.

CSWAP / Fredkin Gate

It is a three-qubit gate, also called the CSWAP gate. Here the 1st qubit is control and the 2nd and 3rd qubits are target. Its truth table is below.

InputOutput
\(\ket{000}\)\(\ket{000}\)
\(\ket{001}\)\(\ket{001}\)
\(\ket{010}\)\(\ket{010}\)
\(\ket{011}\)\(\ket{011}\)
\(\ket{100}\)\(\ket{100}\)
\(\ket{101}\)\(\ket{110}\)
\(\ket{110}\)\(\ket{101}\)
\(\ket{111}\)\(\ket{111}\)

(Truth Table for Fredkin Gate.) For this table the 1st qubit is control and the 2nd and 3rd qubits are target. Thus, the Fredkin gate swaps the states of the 2nd and 3rd qubits if the control qubit is \(\ket{1}\). The code for the Fredkin Gate is as follows:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(3, 'q')
circuit = QuantumCircuit(qreg_q)

circuit.cswap(qreg_q[0], qreg_q[2], qreg_q[1])

circuit.draw(output='mpl')#.savefig('../images/cswap-gate.png')
Circuit symbol for the Fredkin (CSWAP) gate with a control dot and two swap crosses
Figure — Fredkin Gate.

In operator notation it is:

\[\begin{aligned} CSWAP\ket{000}&=\ket{000} & CSWAP\ket{100}&=\ket{100} \\ CSWAP\ket{001}&=\ket{001} & CSWAP\ket{101}&=\ket{110} \\ CSWAP\ket{010}&=\ket{010} & CSWAP\ket{110}&=\ket{101} \\ CSWAP\ket{011}&=\ket{011} & CSWAP\ket{111}&=\ket{111} \end{aligned}\]

It is represented by the matrix in the computational basis (\(\ket{000},\ket{001},\ldots,\ket{110},\ket{111}\)) as:

\[ CSWAP=\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} \]

In outer product representation:

\[\begin{aligned} CSWAP&=\ket{000}\bra{000}+\ket{001}\bra{001}+\ket{010}\bra{010}+\ket{011}\bra{011} \\ &+\ket{100}\bra{100}+\ket{101}\bra{110}+\ket{110}\bra{101}+\ket{111}\bra{111} \end{aligned}\]

Action of CSWAP on a general three-qubit state \(\ket{\psi} = \alpha\ket{000}+\beta\ket{001}+\gamma\ket{010}+\delta\ket{011}+\epsilon\ket{100}+\zeta\ket{101}+\eta\ket{110}+\theta\ket{111}\) is:

\[ CSWAP\ket{\psi}=\alpha\ket{000}+\beta\ket{001}+\gamma\ket{010}+\delta\ket{011}+\epsilon\ket{100}+\eta\ket{101}+\zeta\ket{110}+\theta\ket{111} \]

(the amplitudes of \(\ket{101}\) and \(\ket{110}\) are swapped).

We can construct the Fredkin gate using CNOT and Toffoli gates as shown below.

Fredkin gate built from a Toffoli gate flanked by two CNOT gates
Figure — Fredkin Gate using CNOT and Toffoli Gates.
Important

Applying a gate on a superposition state is the same as applying the gate on each of the basis states and then superposing the results. This is because the gate is a linear operator and the superposition is a linear combination of the basis states. For example, consider a superposition state \(\ket{\psi}=\alpha\ket{0}+\beta\ket{1}\). If we apply a gate \(U\) on this state, the output is \(U\ket{\psi}=U(\alpha\ket{0}+\beta\ket{1})=\alpha U\ket{0}+\beta U\ket{1}\). Since \(U\) is a \(2 \times 2\) matrix and \(\ket{0},\ket{1}\) are \(2 \times 1\) column vectors, computing \(U(\alpha\ket{0}+\beta\ket{1})\) is the same as \(\alpha U\ket{0}+\beta U\ket{1}\), since matrix multiplication is distributive.

For example, say a CNOT gate needs to be applied on \(\ket{\psi}=\frac{\ket{00}+\ket{11}}{\sqrt{2}}\). Then the output is \(CNOT\ket{\psi}=\frac{CNOT\ket{00}+CNOT\ket{11}}{\sqrt{2}}=\frac{\ket{00}+\ket{10}}{\sqrt{2}}\). In matrix form:

\[ CNOT\ket{\psi}=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\frac{1}{\sqrt{2}}\left(\begin{bmatrix} 1 \\ 0 \\ 0 \\ 0 \end{bmatrix}+\begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}\right) \] \[ =\frac{1}{\sqrt{2}}\left(CNOT\ket{00}+CNOT\ket{11}\right)=\frac{1}{\sqrt{2}}\left(\begin{bmatrix} 1 \\ 0 \\ 0 \\ 0 \end{bmatrix}+\begin{bmatrix} 0 \\ 0 \\ 1 \\ 0 \end{bmatrix}\right)=\frac{1}{\sqrt{2}}\begin{bmatrix} 1 \\ 0 \\ 1 \\ 0 \end{bmatrix} =\frac{1}{\sqrt{2}}\left(\ket{00}+\ket{10}\right) \]

Thus, applying a gate on a superposition is the same as applying the gate on the basis states of the superposition, and superposing the results in the same linear combination as that in which \(\ket{\psi}\) was superposed.

3.5 Implementation of Controlled-U

Now we need to understand how to implement the controlled-\(U\) operation for an arbitrary single qubit \(U\), using only single qubit operations and the CNOT gate. Our strategy is a two-part procedure based upon the decomposition \(e^{\iota\alpha}AXBXC\).

The first step is to apply the phase shift \(e^{\iota\alpha}\) on the target qubit, controlled by the control qubit. That is, if the control qubit is \(\ket{0}\), the target qubit is left alone; if the control qubit is \(\ket{1}\), a phase shift \(e^{\iota\alpha}\) is applied to the target. A circuit implementing this operation using just a single qubit unitary gate is depicted below. To verify this circuit works correctly, note that the effect of the circuit on the right-hand side is

\[ \ket{00}\rightarrow \ket{00},\quad \ket{01}\rightarrow \ket{01},\quad \ket{10}\rightarrow e^{\iota\alpha}\ket{10},\quad \ket{11}\rightarrow e^{\iota\alpha}\ket{11} \]
Controlled phase shift circuit and its equivalent implementation for two qubits
Figure — Controlled phase shift gate and an equivalent circuit for two qubits.

We can now complete the construction of the controlled-\(U\) operation, as shown below. Recall that \(U\) may be written as \(U=e^{\iota\alpha}AXBXC\), where \(A,B,C\) are single qubit operations such that \(ABC=I\). If the control qubit is set, the operation \(e^{\iota\alpha}AXBXC=U\) is applied to the second qubit. If the control qubit is not set, the operation \(ABC=I\) is applied to the second qubit — no change is made. That is, the circuit implements the controlled-\(U\) operation.

Circuit implementing controlled-U for a single qubit using single-qubit gates A, B, C and two CNOTs
Figure — Circuit implementing the controlled-\(U\) operation for single qubit \(U\). Here \(\alpha,A,B,C\) satisfy \(U=e^{\iota\alpha} AXBXC,\ ABC=I\).

Now that we know how to condition on a single qubit, what about conditioning on multiple qubits? We have already met one example, the Toffoli gate, which flips the target qubit conditioned on the first two qubits being set to one. More generally, suppose we have \(n+k\) qubits, and \(U\) is a \(k\)-qubit unitary operator. Then we define the controlled operation \(C^n(U)\) by the equation

\[ C^n(U)\ket{x_1x_2\ldots x_n} \ket{\psi} = \ket{x_1x_2\ldots x_n}U^{x_1x_2\ldots x_n}\ket{\psi} \]

where \(x_1x_2\ldots x_n\) in the exponent of \(U\) means the product of the bits \(x_1,x_2,\ldots,x_n\). That is, the operator \(U\) is applied to the last \(k\) qubits if the first \(n\) qubits are all equal to one, and otherwise nothing is done. Such conditional operations are so useful that we introduce a special circuit notation for them, illustrated below. For the following we assume that \(k=1\) for simplicity; larger \(k\) can be dealt with using essentially the same methods, however for \(k\geq 2\) there is the added complication that we don't know how to perform arbitrary operations on \(k\) qubits.

Sample circuit representation for the C-to-the-n of U operation with four controls and a three-qubit target
Figure — Sample circuit representation for the \(C^n(U)\) operation, where \(U\) is a unitary operator on \(k\) qubits, for \(n=4\) and \(k=3\).

Suppose \(U\) is a single qubit unitary operator, and \(V\) is a unitary operator chosen so that \(V^2=U\). Then the operation \(C^2(U)\) may be implemented using the circuit shown below.

Circuit for the C-squared of U gate built from controlled-V, controlled-V-dagger and a CNOT between the controls
Figure — Circuit for the \(C^2(U)\) gate. \(V\) is any unitary operator satisfying \(V^2=U\). The special case \(V\equiv (1-\iota)(I+\iota X)/2\) corresponds to the Toffoli gate.
Worked Example

Verify that the circuit above implements the \(C^2(U)\) operation.

Show solution

Considering all the possible inputs (in the computational basis), let us track the outcome:

\[\begin{aligned} \ket{00\psi} &\rightarrow \ket{00\psi}\rightarrow \ket{00\psi}\rightarrow \ket{00\psi}\rightarrow \ket{00\psi}\\ \ket{01\psi}&\rightarrow \ket{01(V\psi)} \rightarrow \ket{01(V\psi)} \rightarrow \ket{01(V^{\dagger}V\psi)}\rightarrow \ket{01\psi}\\ \ket{10\psi}&\rightarrow \ket{10\psi}\rightarrow \ket{11\psi} \rightarrow \ket{11(V^{\dagger}\psi)} \rightarrow \ket{10(VV^{\dagger}\psi)} = \ket{10\psi}\\ \ket{11\psi} &\rightarrow \ket{11(V\psi)} \rightarrow \ket{10(V\psi)} \rightarrow \ket{10(V\psi)} \rightarrow \ket{11(V^2\psi)} = \ket{11(U\psi)} \end{aligned}\]

Hence, the circuit does perform the \(C^2(U)\) operation.

Worked Example

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.

Show solution

Firstly, we apply the multi-control construction to the \(C^2(U)\) circuit, which gives:

Expanded C-squared of U circuit before simplification
Figure — Expanded \(C^2(U)\) circuit.

We move the 6th CNOT left from the 4th one, which involves adding CNOTs after the 4th and 5th CNOTs from the first to the third qubit, due to:

Circuit identity used to move a CNOT past others
Figure — CNOT commutation identity.

which can be checked by considering all possible inputs. We also see that \(AA^{\dagger}=CC^{\dagger}=I\). Hence, we get the following circuit:

Partially simplified C-squared of U circuit
Figure — Partially simplified circuit.

The dashed part is diagonal, hence commutes with the rest of the components and can be moved to the end for convenience:

Further simplified C-squared of U circuit with diagonal part moved to the end
Figure — Further simplified circuit.

CNOTs 2 and 3, and 6 and 7, cancel each other, hence we are left with the final circuit. This has 8 single qubit gates and 6 CNOTs as desired. The circuit performs the operation \(AXBXB^{\dagger}XBXC=(VC^{\dagger})B^{\dagger}(A^{\dagger}V)=V^2 = U\). Therefore, the circuit performs the \(C^2(U)\) operation.

Worked Example

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?

The familiar Toffoli gate is an especially important special case of the \(C^2(U)\) operation — the case \(C^2(X)\). Defining \(V=(1-\iota)(I+\iota X)/2\) and noting that \(V^2=X\), we get an implementation of the Toffoli gate in terms of one- and two-qubit operations, shown below. From a classical viewpoint this is a remarkable result. Recall that one- and two-bit classical reversible gates are not sufficient to implement the Toffoli gate, or more generally universal computation. By contrast, in the quantum case one- and two-qubit reversible gates are sufficient to implement the Toffoli gate, and eventually suffice for universal computation.

Circuit for the Toffoli gate as a special case of the C-squared of U construction
Figure — Circuit for the \(C^2(U)\) gate. \(V\) is any unitary operator satisfying \(V^2=U\). The special case \(V=(1-\iota)(I+\iota X)/2\) corresponds to the Toffoli gate.

Ultimately we will show that any unitary operation can be composed to an arbitrarily good approximation from just the Hadamard, phase, controlled-NOT and \(\pi/8\) gates. Because of the great usefulness of the Toffoli gate, it is interesting to see how it can be built from just this gate set. The figure below illustrates a simple circuit for the Toffoli gate made up of just Hadamard, phase, controlled-NOT and \(\pi/8\) gates.

Implementation of the Toffoli gate using Hadamard, phase, CNOT and T gates
Figure — Implementation of the Toffoli gate using Hadamard, phase, controlled-NOT and \(\pi/8\) gates.

How may we implement \(C^n(U)\) gates using our existing repertoire of gates, where \(U\) is an arbitrary single qubit unitary operation? A particularly simple circuit for achieving this is illustrated below. The circuit divides into three stages and makes use of a small number \((n-1)\) of working qubits, which all start and end in the state \(\ket{0}\). Suppose the control qubits are in the computational basis state \(\ket{c_1,c_2,\ldots,c_n}\). The first stage is to reversibly AND all the control bits \(c_1,\ldots,c_n\) together to produce the product \(c_1\cdot c_2 \cdots c_n\). To do this, the first gate ANDs \(c_1\) and \(c_2\) together using a Toffoli gate, changing the state of the first work qubit to \(\ket{c_1\cdot c_2}\). The next Toffoli gate ANDs \(c_3\) with \(c_1 \cdot c_2\), changing the second work qubit to \(\ket{c_1\cdot c_2 \cdot c_3}\). We continue in this fashion until the final work qubit is in the state \(\ket{c_1 \cdot c_2\cdots c_n}\). Next, a \(U\) operation on the target qubit is performed, conditional on the final work qubit being set to one — that is, \(U\) is applied if and only if all of \(c_1\) through \(c_n\) are set. Finally, the last part of the circuit just reverses the first stage, returning all work qubits to \(\ket{0}\). The combined result is to apply \(U\) to the target qubit if and only if all control bits are set, as desired.

Network implementing the C-to-the-n of U operation using Toffoli gates and ancilla work qubits, for n equals five
Figure — Network implementing the \(C^n(U)\) operation for the case \(n=5\).

In the controlled gates we have been considering, conditional dynamics on the target qubit occurs if the control bits are set to one. Of course, there is nothing special about one, and it is often useful to consider dynamics conditional on the control bits being set to zero. For instance, suppose we wish to implement a two-qubit gate in which the target qubit is flipped conditional on the control qubit being set to zero. Below we introduce a circuit notation for this gate, together with an equivalent circuit in terms of gates we have already introduced. Generically we use the open circle notation to indicate conditioning on the qubit being set to zero, while a closed circle indicates conditioning on the qubit being set to one.

Controlled-NOT conditioned on the control being zero, drawn with an open circle, and its equivalent using X gates
Figure — Controlled operation with a NOT gate performed on the second qubit, conditional on the first qubit being set to zero.

A more elaborate example, involving three control qubits, is illustrated below. The operation \(U\) is applied to the target qubit if the first and third qubits are set to zero and the second qubit is set to one. It is easy to verify by inspection that the right-hand circuit implements the desired operation. More generally, it is easy to move between circuits which condition on qubits being set to one and circuits which condition on qubits being set to zero, by insertion of \(X\) gates in appropriate locations.

Controlled-U with mixed open and closed controls and its equivalent built from X gates and a standard controlled-U
Figure — Controlled-\(U\) operation and its equivalent in terms of circuit elements we already know how to implement. The fourth qubit has \(U\) applied if the first and third qubits are set to zero and the second qubit is set to one.

Another convention which is sometimes useful is to allow the controlled-NOT gates to have multiple targets, as shown below. This means that when the control qubit is \(1\), all the qubits marked with a \(\oplus\) are flipped, and otherwise nothing happens. It is convenient in constructing classical functions such as permutations, or in encoders and decoders for quantum error-correction circuits.

Controlled-NOT gate with a single control and multiple targets
Figure — Controlled-NOT gate with multiple targets.

3.6 Universal Quantum Gates

A small set of gates (e.g. AND, OR, NOT) can be used to compute arbitrary classical functions. We say that such a set of gates is universal for classical computation. In fact, since the Toffoli gate is universal for classical computation, quantum circuits subsume classical circuits. A similar universality is true for quantum computation: a set of gates is said to be universal for quantum computation if any unitary operation may be approximated to arbitrary accuracy by a quantum circuit involving only those gates. We now describe three universality constructions for quantum computation. These build upon each other, and culminate in a proof that any unitary operation can be approximated to arbitrary accuracy using Hadamard, phase, CNOT, and \(\pi/8\) gates. You may wonder why the phase gate appears in this list, since it can be constructed from two \(\pi/8\) gates; it is included because of its natural role in the fault-tolerant constructions described later.

The first construction shows that any arbitrary unitary operator may be expressed exactly as a product of unitary operators that each act non-trivially only on a subspace spanned by two computational basis states. The second construction combines the first with the results of the previous section to show that an arbitrary unitary operator may be expressed exactly using single qubit and CNOT gates. The third combines the second with a proof that single qubit operations may be approximated to arbitrary accuracy using the Hadamard, phase, and \(\pi/8\) gates. This implies that any unitary operation can be approximated to arbitrary accuracy using Hadamard, phase, CNOT and \(\pi/8\) gates.

Our constructions say little about efficiency — how many (polynomially or exponentially many) gates must be composed to create a given unitary transform. Later we show that there exist unitary transforms which require exponentially many gates to approximate. Of course, the goal of quantum computation is to find interesting families of unitary transformations that can be performed efficiently.

Worked Example

Construct a quantum circuit to add two two-bit numbers \(x\) and \(y\) modulo 4. That is, the circuit should perform the transformation \(\ket{x,y} \rightarrow \ket{x,x+y \bmod 4}\).

Two-level unitary gates are universal

Consider a unitary matrix which acts on a \(d\)-dimensional Hilbert space. In this section we explain how \(U\) may be decomposed into a product of two-level unitary matrices — unitary matrices which act non-trivially only on two or fewer vector components. The essential idea may be understood by considering the case when \(U\) is \(3\times 3\), so suppose \(U\) has the form

\[ U=\begin{bmatrix} a & d & g \\ b & c & h \\ c & f & j \end{bmatrix} \]

We will find two-level unitary matrices \(U_1,\ldots,U_3\) such that \(U_3U_2U_1U=I\). It follows that \(U=U_1^{\dagger}U_2^{\dagger}U_3^{\dagger}\). \(U_1,U_2,U_3\) are all two-level unitary matrices, and their inverses \(U_1^{\dagger},U_2^{\dagger},U_3^{\dagger}\) are also two-level unitary matrices. Thus, if we can demonstrate the above equations, we will have shown how to break \(U\) into a product of two-level unitary matrices. Use the following procedure to construct \(U_1\): if \(b=0\) then set

\[ U_1=\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \]

If \(b\neq 0\) then set

\[ U_1 = \begin{bmatrix} \frac{a^*}{\sqrt{|a|^2+|b|^2}} & \frac{b^*}{\sqrt{|a|^2+|b|^2}}& 0 \\ \frac{b}{\sqrt{|a|^2+|b|^2}} & \frac{-a}{\sqrt{|a|^2 + |b|^2}} & 0 \\ 0 & 0 & 1 \end{bmatrix} \]

Note that in either case \(U_1\) is a two-level unitary matrix, and when we multiply the matrices out we get

\[ U_1U= \begin{bmatrix} a' & d' & g' \\ 0 & e' & h' \\ c' & f' & j' \end{bmatrix} \]

The key point is that the middle entry in the left-hand column is zero. We denote the other entries with a generic prime; their actual values do not matter. Now apply a similar procedure to find a two-level matrix \(U_2\) such that \(U_2U_1U\) has no entry in the bottom-left corner. That is, if \(c'=0\) we set

\[ U_2 = \begin{bmatrix} a'^* & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} \]

while if \(c'\neq 0\) then we set

\[ U_2=\begin{bmatrix} \frac{a'^*}{\sqrt{|a'|^2 + |c'|^2}} & 0 & \frac{c'^*}{\sqrt{|a'|^2 + |c'|^2}}\\ 0 & 1 & 0 \\ \frac{c'}{\sqrt{|a'|^2 +|c'|^2}} & 0 & \frac{-a'}{\sqrt{|a'|^2 + |c'|^2}} \end{bmatrix} \]

In either case, when we carry out the matrix multiplication we find that

\[ U_2U_1U=\begin{bmatrix} 1 & d''' & g''' \\ 0 & e''' & h''' \\ 0 & f''' & j''' \end{bmatrix} \]

Since \(U,U_1,U_2\) are unitary, it follows that \(U_2U_1U\) is unitary, and thus \(d'''=g'''=0\), since the first row of \(U_2U_1U\) must have norm 1. Finally, set

\[ U_3=\begin{bmatrix} 1& 0 & 0 \\ 0 & e'''^* & f'''^*\\ 0 & h'''^* & j'''^* \end{bmatrix} \]

It is now easy to verify that \(U_3U_2U_1U=I\), and thus \(U=U_1^{\dagger}U_2^{\dagger}U_3^{\dagger}\), which is a decomposition of \(U\) into two-level unitaries.

More generally, suppose \(U\) acts on a \(d\)-dimensional space. Then, in a similar fashion to the \(3\times 3\) case, we can find two-level unitary matrices \(U_1,\ldots,U_{d-1}\) such that the matrix \(U_{d-1}U_{d-2}\ldots U_1U\) has a one in the top-left corner and zeroes elsewhere in the first row and column. We then repeat this procedure for the \((d-1)\times(d-1)\) unitary submatrix in the lower-right corner, and so on, with the end result that an arbitrary \(d\times d\) unitary matrix may be written

\[ U=V_1\ldots V_k \]

where the \(V_i\) are two-level unitary matrices, and \(k\leq (d-1)+(d-2)+\ldots +1=d(d-1)/2\).

The big idea here: any quantum operation, no matter how complicated, can be chopped into a sequence of simple moves, each of which only touches two of the many basis states at a time. It is like sorting a huge list by only ever swapping two items at a time — clumsy, but always possible.
Worked Example

Provide a decomposition of the transform

\[ \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & \iota & -1 & -\iota \\ 1 & -1 & 1 & -1 \\ 1 & -\iota & -1 & \iota \end{bmatrix} \]

into a product of two-level unitaries. (This is a special case of the quantum Fourier transform.)

A corollary of the above result is that an arbitrary unitary matrix on an \(n\)-qubit system may be written as a product of at most \(2^{n-1}(2^n-1)\) two-level unitary matrices. For specific unitary matrices it may be possible to find much more efficient decompositions, but as the following shows there exist matrices which cannot be decomposed as a product of fewer than \(d-1\) two-level unitary matrices.

Worked Example

Prove that there exists a \(d\times d\) unitary \(U\) which cannot be decomposed as a product of fewer than \(d-1\) two-level unitary matrices.

Single qubit and CNOT gates are universal

We have just shown that an arbitrary unitary matrix on a \(d\)-dimensional Hilbert space may be written as a product of two-level unitary matrices. Now we show that single qubit and CNOT gates together can be used to implement an arbitrary two-level unitary operation on the state space of \(n\) qubits. Combining these results, single qubit and CNOT gates can be used to implement an arbitrary unitary operation on \(n\) qubits, and therefore are universal for quantum computation.

Suppose \(U\) is a two-level unitary matrix on an \(n\)-qubit quantum computer, acting non-trivially on the space spanned by the computational basis states \(\ket{s}\) and \(\ket{t}\), where \(s=s_1\ldots s_n\) and \(t=t_1\ldots t_n\) are the binary expansions for \(s\) and \(t\). Let \(\tilde{U}\) be the non-trivial submatrix of \(U\); \(\tilde{U}\) can be thought of as a unitary operator on a single qubit.

Our immediate goal is to construct a circuit implementing \(U\), built from single qubit and CNOT gates. To do this, we make use of Gray codes. Suppose we have distinct binary numbers \(s\) and \(t\). A Gray code connecting \(s\) and \(t\) is a sequence of binary numbers, starting with \(s\) and concluding with \(t\), such that adjacent members differ in exactly one bit. For instance, with \(s=101001\) and \(t=110011\) we have the Gray code

\[\begin{aligned} &101001\\ &101011\\ &100011\\ &110011 \end{aligned}\]

Let \(g_1\) through \(g_m\) be the elements of a Gray code connecting \(s\) and \(t\), with \(g_1=s\) and \(g_m=t\). Note we can always find a Gray code such that \(m\leq n+1\), since \(s\) and \(t\) can differ in at most \(n\) locations.

The basic idea of the quantum circuit implementing \(U\) is to perform a sequence of gates effecting the state changes \(\ket{g_1}\rightarrow \ket{g_2} \rightarrow \ldots \rightarrow \ket{g_{m-1}}\), then to perform a controlled-\(\tilde{U}\) operation with the target qubit located at the single bit where \(g_{m-1}\) and \(g_m\) differ, and then to undo the first stage, transforming \(\ket{g_{m-1}}\rightarrow\ket{g_{m-2}}\rightarrow \ldots \rightarrow \ket{g_1}\). Each of these steps can be easily implemented using operations developed earlier, and the final result is an implementation of \(U\).

A more precise description of the implementation is as follows. The first step is to swap the states \(\ket{g_1}\) and \(\ket{g_2}\). Suppose \(g_1\) and \(g_2\) differ at the \(i\)th digit. We accomplish the swap by performing a controlled bit flip on the \(i\)th qubit, conditional on the values of the other qubits being identical to those in both \(g_1\) and \(g_2\). Next we use a controlled operation to swap \(\ket{g_2}\) and \(\ket{g_3}\). We continue until we swap \(\ket{g_{m-2}}\) with \(\ket{g_{m-1}}\). The effect of this sequence of \(m-2\) operations is:

\[\begin{aligned} \ket{g_1} &\rightarrow \ket{g_{m-1}}\\ \ket{g_2} &\rightarrow \ket{g_1}\\ \ket{g_3} &\rightarrow \ket{g_2}\\ &\ \ \vdots \\ \ket{g_{m-1}} &\rightarrow \ket{g_{m-2}} \end{aligned}\]

All other computational basis states are left unchanged. Next, suppose \(g_{m-1}\) and \(g_m\) differ in the \(j\)th bit. We apply a controlled-\(\tilde{U}\) operation with the \(j\)th qubit as target, conditional on the other qubits having the same values as appear in both \(g_m\) and \(g_{m-1}\). Finally, we complete the \(U\) operation by undoing the swap operations: swap \(\ket{g_{m-1}}\) with \(\ket{g_{m-2}}\), then \(\ket{g_{m-2}}\) with \(\ket{g_{m-3}}\), and so on, until we swap \(\ket{g_2}\) with \(\ket{g_1}\).

A simple example illustrates the procedure further. Suppose we wish to implement the two-level unitary transformation

\[ U=\begin{bmatrix} a & 0 & 0 & 0 & 0 & 0 & 0 & c \\ 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 \\ b & 0 & 0 & 0 & 0 & 0 & 0 & d \end{bmatrix} \]

Here \(a,b,c,d\) are any complex numbers such that \(\tilde{U}=\begin{bmatrix}a & c \\ b & d \end{bmatrix}\) is a unitary matrix. Notice that \(U\) acts non-trivially only on the states \(\ket{000}\) and \(\ket{111}\). We write a Gray code connecting \(000\) and \(111\):

ABC
000
001
011
111

From this we read off the required circuit, shown below. The first two gates shuffle the states so that \(\ket{000}\) gets swapped with \(\ket{011}\). Next, the operation \(\tilde{U}\) is applied to the first qubit of the states \(\ket{011}\) and \(\ket{111}\), conditional on the second and third qubits being in the state \(\ket{11}\). Finally, we unshuffle the states, ensuring that \(\ket{011}\) gets swapped back with \(\ket{000}\).

Circuit implementing a two-level unitary using Gray-code swaps and a controlled version of the two-by-two submatrix
Figure — Circuit implementing the two-level unitary operation defined above.

Returning to the general case, implementing the two-level unitary operation \(U\) requires at most \(2(n-1)\) controlled operations to swap \(\ket{g_1}\) with \(\ket{g_{m-1}}\) and back again. Each controlled operation can be realized using \(\mathcal{O}(n)\) single qubit and CNOT gates; the controlled-\(\tilde{U}\) operation also requires \(\mathcal{O}(n)\) gates. Thus, implementing \(U\) requires \(\mathcal{O}(n^2)\) single qubit and CNOT gates. We saw in the previous section that an arbitrary unitary matrix on the \(2^n\)-dimensional state space of \(n\) qubits may be written as a product of \(\mathcal{O}(2^{2n})=\mathcal{O}(4^n)\) two-level unitary operations. Combining these results, an arbitrary unitary operation on \(n\) qubits can be implemented using a circuit containing \(\mathcal{O}(n^2 4^n)\) single qubit and CNOT gates. Obviously, this construction does not provide terribly efficient quantum circuits! However, we will later see that this construction is close to optimal, in the sense that there are unitary operations that require an exponential number of gates to implement. Thus, to find fast quantum algorithms we will clearly need a different approach.

Worked Example

Find a quantum circuit using single qubit operations and CNOTs to implement the transformation

\[ \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & a & 0 & 0 & 0 & 0 & 0 & c \\ 0 & 0 & 0 & 1 & 0 & 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 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & b & 0 & 0 & 0 & 0 & 0 & d \end{bmatrix} \]

where \(\tilde{U}=\begin{bmatrix}a & c \\ b & d \end{bmatrix}\) is an arbitrary \(2\times 2\) unitary matrix.

Approximating arbitrary unitary gates

Any unitary transformation on any number of qubits can be decomposed as a product of 1- and 2-qubit gates. However, if you just run the decomposition blindly, it will produce a quantum circuit with something like \(4^n\) gates — just as, if you use the method of truth tables to build a circuit computing an arbitrary Boolean function \(f:\{0,1\}^n \rightarrow \{0,1\}\), you get something with about \(2^n\) AND, OR, and NOT gates. Just as in the classical world our real interest is in which Boolean functions can be efficiently computed (say, using a polynomial number of gates), so too in the quantum world our real interest is in which \(n\)-qubit unitary transformations can be realized using only polynomially many 1- and 2-qubit gates.

That being so, is it possible that all \(n\)-qubit unitaries can be realized by polynomial-size circuits? The answer turns out to be no. In fact, just as ‘almost all’ Boolean functions require exponentially large circuits to compute, so too ‘almost all’ unitaries require exponentially large quantum circuits. As in the classical case, the way to prove this is using a counting argument.

Counting arguments in circuit complexity go back to Claude Shannon in 1949. Shannon observed that almost every \(n\)-bit Boolean function requires a circuit of at least \(\sim \frac{2^n}{n}\) AND, OR and NOT (or equivalently NAND) gates to compute it. The reason, in one sentence, is that there are too many Boolean functions and not enough small circuits. In more detail, there are \(2^{2^n}\) different Boolean functions \(f:\{0,1\}^n\rightarrow \{0,1\}\). For a circuit with \(T\) gates there are \(\sim (n+T)^{\mathcal{O}(T)}\) different circuits that can be constructed, as follows. Without loss of generality, suppose we construct our circuit out of NAND gates with a bounded fan-in of 2. For each NAND gate we have

\[ \binom{N+T-1}{2}\leq (N+T)^2 \]

ways to select the inputs. Since there are \(T\) gates, the number of different circuits is upper bounded by

\[ (N+T)^{2T}=(N+T)^{\mathcal{O}(T)} \]

Since each circuit can compute only one function, we set the two expressions equal and solve for \(T\):

\[ 2^{2^n} =(n+T)^{T}\approx T^T \]

Taking the log of both sides gives

\[ 2^n = T\log T \]

This is satisfied for \(T\approx \frac{2^n}{n}\). Moreover, if \(T\) is smaller then \((N+T)^{\mathcal{O}(T)}\) is minuscule compared to \(2^{2^n}\), so almost every function requires a circuit of size \(\mathcal{O}(\frac{2^n}{n})\).

Strikingly, and famously, this argument doesn't give us a single example of a hard-to-compute Boolean function. It merely tells us that such functions must exist, and indeed are ubiquitous.

We can use a similar argument in the quantum case — although since \(2^n\times 2^n\) unitary matrices form a continuous manifold, it's easier to talk in terms of dimensionality rather than cardinality. For simplicity, restrict attention to those \(2^n\times 2^n\) unitary matrices that are diagonal. Even then, specifying such a matrix requires \(2^n\) independent complex numbers of norm 1 (or \(2^n-1\), if we ignore the global phase). By contrast, a quantum circuit with \(T\) gates, each acting on at most 2 qubits, can be specified using only \(\mathcal{O}(T)\) continuous parameters (plus some discrete choices about where to apply the gates, which won't affect the argument). So we have a \(2^n\)-dimensional manifold in one case, and a union of \(\mathcal{O}(T)\)-dimensional manifolds in the other. Clearly, for the one to cover the other, we need \(T\) to grow at least like \(\sim 2^n\). Hence, there exist \(n\)-qubit unitary transformations that require exponentially many gates to implement. In fact, ‘almost all’ (now in the technical, measure-theory sense of 100% of them) have this property.

You might complain that we've only showed that exponentially many gates are needed to implement most \(n\)-qubit unitary transformations exactly. What about approximately implementing them, up to small error \(\epsilon\) (say, in each entry)? In fact, a little algebraic geometry is enough to show that exponentially many gates are needed to approximate most \(n\)-qubit unitaries as well, or even most \(n\)-qubit diagonal unitary transformations.

Once again, these arguments don't give us a single example of a hard-to-implement unitary transformation. The easy-to-implement unitaries are the rare exceptions! Yet, rare though they might be, the subset of unitaries that are easy (i.e. can be implemented by polynomial-size quantum circuits) are the main ones that will interest us in quantum computing.

We've seen that any unitary transformation on \(n\) qubits can be built up out of a small set of elementary gates. Is it always possible to do this efficiently? That is, given a unitary transformation \(U\) on \(n\) qubits, does there always exist a circuit of size polynomial in \(n\) approximating \(U\)? The answer is a resounding no: in fact, most unitary transformations can only be implemented very inefficiently. One way to consider the question: how many gates does it take to generate an arbitrary state on \(n\) qubits? A simple counting argument shows this requires exponentially many operations in general; it immediately follows that there are unitary operations requiring exponentially many operations. Suppose we have \(g\) different types of gates available, each working on at most \(f\) qubits. These numbers \(f\) and \(g\) are fixed by the computing hardware and may be considered constants. Suppose we have a quantum circuit containing \(m\) gates, starting from the computational basis state \(\ket{0}^{\otimes n}\). For any particular gate there are at most \(\left(\binom{n}{f}\right)^g=\mathcal{O}(n^{fg})\) possible choices. It follows that at most \(\mathcal{O}(n^{fgm})\) different states may be computed using \(m\) gates.

Visualization of covering the set of possible quantum states with epsilon-radius patches
Figure — Visualization of covering the set of possible states with patches of constant radius.

Suppose we wish to approximate a particular state \(\ket{\psi}\) to within a distance \(\epsilon\). The idea is to cover the set of all possible states with a collection of ‘patches’, each of radius \(\epsilon\), and then to show that the number of patches required rises doubly exponentially in \(n\); comparing with the exponential number of different states computable using \(m\) gates will imply the result. First, the space of state vectors of \(n\) qubits can be regarded as just the unit \((2^{n+1}-1)\)-sphere. To see this, suppose the \(n\)-qubit state has amplitudes \(\psi_j = X_j + \iota Y_j\), where \(X_j\) and \(Y_j\) are the real and imaginary parts of the \(j\)th amplitude. The normalization condition can be written \(\sum_j (X_j^2 +Y_j^2)=1\), which is just the condition for a point on the unit sphere in \(2^{n+1}\) real dimensions — the unit \((2^{n+1}-1)\)-sphere. Similarly, the surface area of radius \(\epsilon\) near \(\ket{\psi}\) is the same as the volume of a \((2^{n+1}-2)\)-sphere of radius \(\epsilon\). Using \(S_k(r)=2\pi^{(k+1)/2}r^k/\Gamma((k+1)/2)\) for the surface area of a \(k\)-sphere of radius \(r\), and \(V_k(r)=2\pi^{(k+1)/2}r^{k+1}/[(k+1)\Gamma((k+1)/2)]\) for the volume, the number of patches needed to cover the space goes like

\[ S_{2^{n+1}-1}(\epsilon)=\frac{\sqrt{\pi}\,\Gamma(2^n-\frac{1}{2})(2^{n+1}-1)}{\Gamma(2^n)\epsilon^{2^{n+1}-1}} \]

where \(\Gamma\) is the usual generalization of the factorial function. But \(\Gamma(2^n-\tfrac{1}{2})\geq \Gamma(2^n)/2^n\), so the number of patches required to cover the space is at least

\[ \Omega\left(\frac{1}{\epsilon^{2^{n+1}-1}}\right) \]

Recall that the number of patches reachable in \(m\) gates is \(\mathcal{O}(n^{fgm})\), so in order to reach all the \(\epsilon\)-patches we must have

\[ \mathcal{O}(n^{fgm}) \geq \Omega\left(\frac{1}{\epsilon^{2^{n+1}-1}}\right) \]

which gives

\[ m=\Omega\left(\frac{2^n\log (1/\epsilon)}{\log(n)}\right) \]

That is, there are states of \(n\) qubits which take \(\Omega(2^n\log (1/\epsilon)/\log(n))\) operations to approximate to within a distance \(\epsilon\). This is exponential in \(n\), and thus is ‘difficult’ in the sense of computational complexity. Furthermore, this immediately implies there are unitary transformations on \(n\) qubits which take \(\Omega(2^n\log(1/\epsilon)/\log(n))\) operations to approximate by a quantum circuit implementing an operation \(V\) such that \(E(U,V)\leq \epsilon\). By contrast, using our universality constructions and the Solovay–Kitaev theorem, an arbitrary unitary operation \(U\) on \(n\) qubits may be approximated to within a distance \(\epsilon\) using \(\mathcal{O}(n^2 4^n \log^c(n^2 4^n/\epsilon))\) gates. Thus, to within a polynomial factor, the construction for universality we have given is optimal; unfortunately, it does not address the problem of determining which families of unitary operations can be computed efficiently in the quantum circuit model.

3.7 Measurements

A final element used in quantum circuits, almost implicitly sometimes, is measurement. In our circuits, we denote a projective measurement in the computational basis using a ‘meter’ symbol, as illustrated below. In the theory of quantum circuits it is conventional not to use any special symbols to denote more general measurements, because they can always be represented by unitary transformations with ancilla bits followed by projective measurements.

Meter symbol denoting a projective measurement on a single qubit wire
Figure — Symbol for projective measurement on a single qubit. In this circuit nothing further is done with the measurement result, but in more general quantum circuits it is possible to change later parts of the circuit conditional on measurement outcomes in earlier parts. Such usage of classical information is depicted using wires drawn with double lines (not shown here).

There are two important principles worth bearing in mind about quantum circuits. Both are rather obvious; however, they are of such great utility that they are worth emphasizing early. The first is that classically conditioned operations can be replaced by quantum conditioned operations.

Principle of deferred measurement

Definition — Principle of deferred measurement

Measurement can always be moved from an intermediate stage of a quantum circuit to the end of the circuit; if the measurement results are used at any stage of the circuit, then the classically controlled operations can be replaced by conditional quantum operations.

Often, quantum measurements are performed as an intermediate step in a quantum circuit, and the measurement results are used to conditionally control subsequent quantum gates. That is the case, for example, in the teleportation circuit. However, such measurements can always be moved to the end of the circuit. The figure below illustrates how this may be done by replacing all the classical conditional operations by corresponding quantum conditional operations. (Of course, some of the interpretation of this circuit as performing ‘teleportation’ is lost, because no classical information is transmitted from Alice to Bob, but the overall action of the two quantum circuits is the same, which is the key point.)

Quantum teleportation circuit with measurements deferred to the end and classical corrections replaced by controlled gates
Figure — Quantum teleportation circuit in which measurements are done at the end, instead of in the middle of the circuit. The top two qubits belong to Alice, and the bottom one to Bob.

The second principle is even more obvious and surprisingly useful.

Principle of implicit measurement

Definition — Principle of implicit measurement

Without loss of generality, any unterminated quantum wires (qubits which are not measured) at the end of a quantum circuit may be assumed to be measured.

To understand why this is true, imagine a quantum circuit containing just two qubits, where only the first qubit is measured at the end. Then the measurement statistics observed are completely determined by the reduced density matrix of the first qubit. However, if a measurement had also been performed on the second qubit, it would be highly surprising if that measurement could change the statistics of the measurement on the first qubit. This is because the reduced density matrix of the first qubit is not affected by performing a measurement on the second.

As you consider the role of measurements in quantum circuits, keep in mind that in its role as an interface between the quantum and classical worlds, measurement is generally considered an irreversible operation, destroying quantum information and replacing it with classical information. In certain carefully designed cases, however, this need not be true, as vividly illustrated by teleportation and quantum error-correction. What these have in common is that in neither instance does the measurement reveal any information about the identity of the quantum state being measured. Indeed, we will see in a later chapter that this is a more general feature of measurement: in order for a measurement to be reversible, it must reveal no information about the quantum state being measured.

Worked Example

Suppose \(\rho\) is the density matrix describing a two-qubit system. Suppose we perform a projective measurement in the computational basis of the second qubit. Let \(P_0=\ket{0}\bra{0}\) and \(P_1=\ket{1}\bra{1}\) be the projectors onto the \(\ket{0}\) and \(\ket{1}\) states of the second qubit, respectively. Let \(\rho'\) be the density matrix which would be assigned to the system after the measurement by an observer who did not learn the measurement result. Show that

\[ \rho'=P_0\rho P_0 + P_1 \rho P_1 \]

Also show that the reduced density matrix for the first qubit is not affected by the measurement, that is, \(\operatorname{tr}_2(\rho)=\operatorname{tr}_2(\rho')\).

Worked Example

(Measurement in the Bell basis) The measurement model we have specified for the quantum circuit model is that measurements are performed only in the computational basis. However, often we want to perform a measurement in some other basis, defined by a complete set of orthonormal states. To perform this measurement, simply unitarily transform from the basis we wish to measure in to the computational basis, then measure. Show that the appropriate circuit performs a measurement in the basis of Bell states. More precisely, show that the circuit results in a measurement being performed with corresponding POVM elements the four projectors onto the Bell states. What are the corresponding measurement operators?

Worked Example

(Measuring an operator) Suppose we have a single qubit operator \(U\) with eigenvalues \(\pm1\), so that \(U\) is both Hermitian and unitary, and can be regarded both as an observable and a quantum gate. Suppose we wish to measure the observable \(U\) — that is, we desire a measurement result indicating one of the two eigenvalues, and leaving a post-measurement state which is the corresponding eigenvector. How can this be implemented by a quantum circuit? Show that the appropriate circuit implements a measurement of \(U\).

Worked Example

(Measurement commuted with controls) A consequence of the principle of deferred measurement is that measurements commute with quantum gates when the qubit being measured is a control qubit. (Recall that double lines represent classical bits in this diagram.) Prove the first equality. The rightmost circuit is simply a convenient notation to depict the use of a measurement result to classically control a quantum gate.

The two measurement principles are enormous conveniences for reasoning about circuits: you can always pretend every measurement happens at the very end (deferred measurement), and you can pretend every leftover qubit gets measured too (implicit measurement). Neither changes the observable outcome, so they let you slide measurements around to wherever they are easiest to analyse.

Key Takeaways