Introduction to quantum computing with Q# – Part 3, Single qubit gates

We left off in the last post after having discussed the Hadamard gate – our first quantum gate – and how it can be used to crate a uniform superposiiton for a single qubit. We are going to continue today by exploring other single qubit gates, discussing the underlying mathematics and, of course, testing it all out with some Q# code.

Pauli spin matrices in quantum mechanics

Let's start our today's journey by looking at the famous Pauli spin matrices, as they are central to quantum computational transformations.

Spin is a property of each elementary particle and represents one of two types of angular momentums that can be found in quantum mechanics – intrinsic angular momentum. The value of the spin is a discrete number and, in general, we can divide particles into two families – those with half-integer spin, called fermions, and those with (round) integer spin, called bosons. Their role in nature is fundamentally different, with fermions making up what we consider ordinary matter (quarks or electrons), and bosons (photons, gluons or for example Higgs boson) acting as force carriers. This is described by the so-called spin-statistics theorem, and is still one of the more puzzling concepts in quantum mechanics.

Electrons have spin equal to $\frac{1}{2}$ (spin up) or $-\frac{1}{2}$ (spin down) which also gives it a very useful designation of being the simplest possible quantum system. If you recall the discussions in part 1 of this series that we had around the concept of a qubit, you should immediately recognize that the nature of spin, with its two orthogonal basis states and two-component vector representation, maps perfectly to what was defined as prerequisites for a qubit.

In 1927, in a breakthrough step in the development of quantum mechanics, Wolfgang Pauli formalized the mathematics behind non-relativistic theory of spin and discovered the Pauli matrices as representations of the spin operators. Overall, Pauli's contributions to modern quantum theory were immense, spearheaded of course by the formulation of the famous Pauli exclusion principle, formulated in 1925, when he was only 25 years old, and for which he ultimately received the Nobel prize in physics in 1945, after the nomination from Albert Einstein. In 1928, Pauli was appointed a Professor of Theoretical Physics at (my nearby) ETH Zürich, where, after a brief time in the USA during the 2nd World War, he went on to have an illustrious scientific career and become a towering figure in the history of this prestigious university that has produced 21 Nobel prize winners – one of them being Albert Einstein himself.

In 1939, Pauli's assistant at ETH, Markus Fierz, in his habilitation degree, formulated the aforementioned spin-statistics theorem, which was later refined further by Pauli himself. It is therefore of no surprise that today, the ETH Institute for Theoretical Physics, which was established upon Pauli's arrival at the university, is located at the Wolfgang-​Pauli-Strasse 27 in Zürich.

In quantum mechanics, the three famous Pauli gates are represented by the greek letter $\sigma$, and they are shown below.

$$
\sigma_x=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}$$

$$
\sigma_y=\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}$$

$$
\sigma_z=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}$$

The presence of $i$, the imaginary number ($i = \sqrt{-1}$), in $\sigma_y$, is dictated by the mathematics of quantum mechanics. The state of a closed quantum system is a vector in a complex vector space and complex numbers are made up of real and imaginary components.

Pauli matrices are of fundamental importance in quantum computing too, as they represent three of the most useful single qubit gates, and will be our main focus in this part 3 of the series.

Circuits

Similarly to classical computer science, where gates can be used to assemble circuits, in quantum computing, a sequence of quantum gates is usually referred to as a “quantum circuit”. The primary role of circuits is that they allow us to visualize the computation models as a sequence of bit (or, in our case, qubit) operations. The analogy between classical and quantum computing works reasonably well in this case, but a work of caution is necessary. In classical computing, the gates typically correspond (sometimes in a slightly idealized form) to physical electronic devices. That is not necessarily the case in quantum computing. Depending on the implementation model of the physical quantum computer, a gate may really have a physical meaning – for example in nuclear magnetic resonance quantum computers – or they might be a purely logical concept, enacted on a stationary particle via series of pulses – for example in trapped ion quantum computer architectures.

In this part 3 of the series, we are going to focus on single qubit gates only – we'll be looking at multi qubit gates next time. In classical computing, single bit gates – at least in boolean circuits – can only exist in two variants: identity gate, which leaves the bit value intact, and a $NOT$ gate, which flips the bit value. In other words, if we input $0$ into an identity gate, $0$ would come out, and if we input $1$, then $1$ would come out. Conversely, for the $NOT$ gate, an input of $0$ produces and output of $1$ and vice versa.

In quantum computing, however, as we already mentioned in previous parts, we can come up with infinitely many single qubit gates, as there are infinitely many ways of manipulating a quantum system that the qubit represents. The reason is
quantum transformations are mathematically described by matrices – more specifically, $2^n * 2^n$ sized unitary matrices, where $n$ stands for the number of qubits the gate acts on, and for a single qubit case, we can construct infinite amount of unique 2 x 2 unitary matrices.

Single qubit quantum gates are often referred to as elementary quantum gates. Most general equation we can write here for quantum gates is transforming one quantum state $\psi$ to another quantum state $\varphi$ using a unitary transformation:

$$U\ket{\psi} = \ket{\varphi}$$

Quantum circuits can be visualized in a similar way to how classical circuits are visualized, with simple diagrams representing ordered operations on qubits. My favorite basic tool for quantum circuits is called Bono, and it can be cloned from Github and run locally; however, there are many other useful circuit building tools, one of the most popular – albeit a little overwhelming at first – being Quiirk.

An example is shown below:

In the circuit above, we start with a single qubit in a basis state $\ket{0}$, and apply the Hadamard gate that we discussed last time around – aptly marked by the H symbol. What follows is the act of measurement, which is denoted by its own measurement symbol. The line type is also of significance – a single line represents a qubit, while a double line represents a classical bit – after all, after the measurement we deal with a classical 0 or 1 value only.

Identity gate

The simplest possible quantum gate is the 2 x 2 identity gate and it semantically corresponds to the behavior of the identity gate in classical computing. Given that it is two dimensional, in linear algebra it is often denoted with $I_2$, while in quantum mechanics it is sometimes marked as $1$. In quantum computing, however, we normally just use the letter $I$ to represent the identity gate. Mathematically it can be expressed using the matrix below:

$$
I=\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}$$

We can also say that the linear transformation behind the identity is an identity function – a function that always returns the same value as was used as its argument. We can write it in the following way:

$$I\ket{\psi} = \ket{\psi}$$

Another important property of the identity matrix is that if we multiply a matrix $U$ by itself and get $I$ as a result, we know that $U$ is also its own inverse.

Let's see what would happen when we apply $I$ to a qubit:

$$I\ket{\varphi} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}(\alpha\ket{0} + \beta\ket{1}) = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix} \\ = \alpha\ket{0} + \beta\ket{1}$$

As expected, nothing really happens – when the identity gate is applied, the qubit state is left unchanged.

Pauli matrices in quantum computing

In quantum computing, when representing Pauli matrices, it is common to skip the quantum mechanical $\sigma$ notation and refer to the matrices (and thus, the quantum gates they represent) using the letters $X$, $Y$ and $Z$ only, which gives us:

$$
X=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}$$

$$
Y=\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}$$

$$
Z=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}$$

As already mentioned in part 1 of this series, we are going to try to avoid imaginary and complex numbers wherever we can (it won't always be possible) – and in this case we can simplify $Y$ and the relationship between $Y$ and $\sigma_y$ could be defined as:

$$
Y=\begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}$$
$$\sigma_y = -iY$$

And that's going to be approach we will take here.

Each of the matrices can also be written using Dirac notation, as the outer product of the base vectors with their complex conjugates. This might seem somewhat confusing at first but quickly becomes very intuitive:

$$
X=\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} = \ket{1}\bra{0} + \ket{0}\bra{1}$$

$$
Y=\begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} = -\ket{1}\bra{0} + \ket{0}\bra{1}$$

$$
Z=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} = \ket{0}\bra{0} – \ket{1}\bra{1}$$

We shall now dive deeper into the mathematical consequences of applying each of the three Pauli gates to such a quantum system, starting with $X$.

The Pauli $X$ gate is often referred to as the bit flip gate because it ends up swapping the probability amplitudes $\alpha$ and $\beta$ with each other. If the qubit is already in a known state $\ket{0}$ or $\ket{1}$, in other words, we know that the probability amplitude, and thus the probability itself, of either $\alpha$ or $\beta$ is zero, then the gate acts like the $NOT$ gate in classical computing – flipping the value to the opposite one.

Since we already know that we can write the state of our qubit as the linear compination of the base vectors, with $alpha$ and $beta$ being the probability amplitudes:

$$\ket{\varphi} = \alpha\ket{0} + \beta\ket{1}$$

we can now express the $X$ gate acting upon a qubit using simple algebra:

$$X\ket{\varphi} = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}(\alpha\ket{0} + \beta\ket{1}) = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix} \\ = \beta\ket{0} + \alpha\ket{1}$$

All of this, especially the theoretical symmetry to the classical $NOT$ gate, makes the Pauli $X$ gate one of the most important and easy to understand quantum gates.

On the other hand, the Pauli $Z$ gate cannot be really conceptually mapped to a classical computing gate, making it a bit more difficult to apprehend. The mathematics of it is not very complicated though, as shown below:

$$Z\ket{\varphi} = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}(\alpha\ket{0} + \beta\ket{1}) = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix} \\ = \alpha\ket{0} – \beta\ket{1}$$

In other words, we started with a state of $\alpha\ket{0} + \beta\ket{1}$ and ended with a state of $\alpha\ket{0} – \beta\ket{1}$ – the only difference being the sign. The result here may at first glance be a little confusing, especially as we already extensively discussed that the probability amplitudes can be used to obtain the actual classical probabilities of receiving a state $\ket{0}$ or $\ket{1}$ using the Born rule – by squaring the modulus of the amplitude $|\alpha|^2$ or $|\beta|^2$. And naturally, the change of the sign we encountered when applying the Pauli $Z$ gate has no impact on the classical probabilities. The $Z$ gate is thus referred to as the phase flip gate, as it flips the phase of the qubit, leaving the actual classical probabilities intact. A simple way of remembering the effect of the $Z$ gate is that it leaves $\ket{0}$ intact and multiplies $\ket{1}$ by $-1$. While this has no impact on probabilities of reading a classical 0 or 1 out of the qubit, it has some creative application scenarios in quantum algorithms.

Finally, the $Y$ gate is both a bit flip gate and phase flip gate at the same time – so it swaps the amplitudes and at the same time changes the sign of the amplitude for both $\ket{0}$ and $\ket{1}$ (same as above, they get multiplied by $-1$).

$$Y\ket{\varphi} = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}(\alpha\ket{0} + \beta\ket{1}) = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} \alpha \\ \beta \end{bmatrix} \\ = -\beta\ket{0} + \alpha\ket{1}$$

The three Pauli gates are all its own inverses, meaning the following holds:

$$X^2 = Y^2 = Z^2 = I$$

In practice, it means that applying the gate the second time in a row would undo the effects of the first transformation. Additionally, Pauli gates are closely related to the Hadamard gate that we already discussed last time. We will not expand the calculations (although I would encourage you do to so to practice some linear algebra) – but the following relations are true:

$$X = HZH$$
$$Y = -1HYH$$
$$Z = HXH$$

Pauli gates and the identity gate have fundamental meaning in quantum information theory in a sense that they can be used as building blocks to make up any other transformation. We could algebraically express any quantum computational linear transformation in the two dimensional complex vector space as a product of a complex unit and a linear combination of the three Pauli matrices and the identity matrix. It means that we can always find $\alpha$, $\beta$, $\gamma$ and $\delta$, as well as $\theta$ to represent any unitary transformation $U$:

$$U = e^{\theta{i}}(\alpha{I} + \beta{X} + \gamma{Y} + \delta{Z})$$

where $\alpha$ is a real number, $\beta$, $\gamma$ and $\delta$ are complex numbers and $\theta$ is larger or equal to $0$ and smaller than $2\pi$. $e$ stands for the the Euler's number.

Pauli gates and the identity gate in Q

We've spent quite some time looking at the theory, but now we are ready to go back to Q# programming. All three Pauli gates are available as functions in the $Microsoft.Quantum.Intrinsic$ namespace. These are:

  • $X (qubit : Qubit)$
  • $Y (qubit : Qubit)$
  • $Z (qubit : Qubit)$

Identity gate is also available – mainly for completeness, but also because it is sometimes useful when an algorithm requires a no-effect action to be performed on a qubit. It is probably of no surprise to anyone that the function signature for identity is $I (qubit : Qubit)$.

A sample Q# identity operation is shown below.

The operation, similarly to the previous examples in earlier parts of this series, takes in an integer input indicating the amount of time the operation should be repeated. A single qubit is allocated within the loop, then the $I$ gate is applied and a regular measurement along the Z axis (in the computational basis) is performed. The qubit is then reset to the original state as mandated by Q# ($MResetZ$ operation guarantees the reset). We keep track of the amount of ones received by keeping a running total of the measurements. Naturally, the amount of iterations minus the amount of ones gives us the amount of zeros obtained in the measurement.

Below is the following C# driver to orchestrate this Q# operation. We will use this driver for all the snippets that come, so I will not repeat it anymore (only the invoked operation name would differ). The quantum operation would be run 1000 times to give us a better statistical result set.

We should get the following output when running this program:

This is of course hardly suprising. The default state of the qubit is $\ket{0}$, which when measured guarantees to produce a classic bit 0. On top of that, us applying the $I$ gate doesn't really have any effects, other than the fact that the qubit was indeed acted upon – so the 100% rate of receiving 0s is quite obvious.

We will now turn our attention to the bit flip. The Q# code is identical to the one above, except we will use $X$ operation.

Running this, using the same type of C# driver, would produce the following result:

This is also very much in line with our expectations. The default state of the qubit is $\ket{0}$, which the $X$ gate flipped to $\ket{1}$ – and that, when measured, produces a bit value 1 100% percent of times.

The next thing worth experimenting with is to try to chain several gates together. For example, we already made a claim – not supported by any algebraic calculation though – that the following relation holds:

$$X = HZH$$

We could verify this easily using Q#. Our operation would look as follows:

This code is yet again similar to the previous snippets, except this time we apply a chain of gates on the same qubit – $H – Z – H$. Running this code, with the same type of C# driver as before, produces the following result:

The result is identical to running the bit flip $X$ gate, so we have really managed to experimentally verify that $X = HZH$.

Rotation gates

As we already mentioned, there are infinitely many possibilities for constructing single qubit gates. For the purpose of this series, the 5 we already discussed – Hadamard gate $H$, Pauli gates $X$, $Y$, $Z$ and identity gate $I$ – are most important, and we will be using them repeatedly in the next parts. However, there are several other common and interesting gates, most importantly the so-called rotation gates.

The three rotation gates $R_x$ , $R_y$ and $R_z$ are all generalizations of the the Pauli $X$, $Y$ and $Z$ gates. The names of these gates come form the fact that all single qubit transformations can be visualized as rotations around the x, y and z axes of the so-called Bloch sphere. Bloch sphere, named so after another physicist connected to ETH in Zürich, Felix Bloch is commonly used to provide a way to represent graphically (geometrically) the state of a two-dimensional quantum system, such as a qubit.

$R_x$ , $R_y$ and $R_z$ are often used as part of quantum algorithms and can be utilized, for example, to prove Bell's theorem. They are represented by the matrices below:

$$
R_x=\begin{bmatrix} cos(\frac{\varphi}{2}) & -sin(\frac{\varphi}{2})i \\ -sin(\frac{\varphi}{2})i & cos(\frac{\varphi}{2}) \end{bmatrix}$$

$$
R_y=\begin{bmatrix} cos(\frac{\varphi}{2}) & -sin(\frac{\varphi}{2}) \\ sin(\frac{\varphi}{2})i & cos(\frac{\varphi}{2}) \end{bmatrix}$$

$$
R_z=\begin{bmatrix} e^{i\frac{\varphi}{2}} & 0 \\ 0 & e^{i\frac{\varphi}{2}} \end{bmatrix}$$

We mentioned that they are generializations of the Pauli gates, and looking at the matrices closely, the relation between the rotation gates and Pauli gates should become a little bit more apparent. For example, the $R_z$ gate will actually become $I$ gate when $\varphi=0$, and it will become $Z$ gate when $\varphi=\pi$.

Similarly to Pauli gates, all three of these rotation gates are available in Q# using the $Rx (theta : Double, qubit : Qubit)$, $Ry (theta : Double, qubit : Qubit)$ and $Rz (theta : Double, qubit : Qubit)$ functions in the $Microsoft.Quantum.Intrinsic$ namespace, where $theta$ represents the desired rotation angle.

Let's try invoking $R_x$ with a 45 degree rotation and see the effects:

Executing this code produces the following result:

So a rotation by 45 degrees along the X axis, distributes the probabilities for obtaining one or zero 0.25-0.75 – when measuring in the computational basis along the Z axis.

Summary

In this post, we discussed in depth several important quantum computing gates – Pauli gates $X$, $Y$, $Z$, as well as the identity gate $I$. In addition to that, we looked at the rotation gates too, as generalizations of the Pauli gates. In the previous post in this series, we already had a look at the Hadamard gate $H$.

All of this is still quite basic in terms of what we can do at the Q# code level, but we are slowly building up the necessary knowledge and amassing building blocks that will be extremely helpful when putting together quantum algorithms.

Equipped in this knowledge we should be are ready to have a look at multi qubit gates and the algebraic foundations behind them – which we will do in the next part. We are also going to look at one of the more bizarre quantum phenomena – entanglement. After that, we will be ready to start putting it all to good use by exploring some quantum algorithms.