Long story made short

1 year ago I started studying the topics of Quantum Computing and Quantum Algorithms and I was fascinated. Is interresting and, like Niels Bohr said, “Anyone who is not shocked by quantum theory has not understood it.”

As doing my research I found quite interesting to post some small articles on these topics, some articles on CodeProject (Java based Quantum Computing Library and Grover’s Search Algorithm Explained) and one on my website about Deutsch’s algorithm. For those articles I used a Java library that I created myself with the help of some of my colleagues. Now, after a year and after playing a little with Haskell, I found quite interesting to review the topic and to start posting a series of small articles about Quantum Computing.


I’ll start by talking a little about the difference between a quantum computer and a traditional one. A lot of information about how a quantum computer works can be found in online courses, research papers and books. It’s not the scope of this post to explain in details how it works, just to present some basic differences.

As someone can expect, a quantum computer use the quantum mechanical effects, such as superposition, to carry out computations. We already know that in a traditional computer, the bit is the basic unit and it can be 0 or 1. The value is defined by a voltage level, in TTL technology, ideally, a 1 value is represented by 5.00 volts while a 0 value is represented by 0.00 volts. For a quantum computer, there are some differences. For example, the basic unit is the qubit (quantum bit) and can, at one time, represent value 0 and 1, by exploiting superposition. A qubit is defined by the equation presented below:

Before diving deeper into the subject, we will firstly review some mathematical aspects of the topic.

Complex Numbers

Since probability amplitudes of a qubit are complex numbers I prefer to start with a very short introduction in the mathematics of complex numbers.

A complex number q is defined as

where a and b and is the imaginary basis unit.

We can see that a is the real component of a complex number and bi is the imaginary one. We can obtain the complex conjugate by simply negating the sign of the imaginary component .

Some basic formulas that we will use during this series of articles will be addition, multiplication, and modulus.

Let’s consider 2 complex numbers x and y defined like:


The addition of these 2 complex numbers is defined by:

while multiplication is defined by:

The third operation, the modulus of the complex number x is defined by:


While Complex Numbers are used to represent the probability amplitudes of a qubit, vectors and linear algebra helps us to represent qubits, so I decided that it’ll be usefull to present some notations and operations before starting to write code.

Basically, the state of a qubit is a unit vector in a 2-dimensional complex vector space .

The vector can be written as



In the above example I used the bra-ket notation, the notation of a column vector is called ket while the notation of a row vector is called bra. Next, I will define 3 basic operations that will be used heavily in the examples from the next articles on this topic, the inner product, outer product and tensor product.

The inner product, is the product between the bra and the ket vectors:

Outer product is ket-bra and is given by:

Lastly, if we consider and we can define the tensor product as:

Qubits and Gates

Quantum mechanics tells that any such system can exist in a superposition of states and as we saw in the second chapter, the state of a qubit is described by

where and are complex number that satisfy the relation

We know that on a classic computer gate operations such as AND, OR,XOR constitute the core of data manipulation. On a quantum computer similar operations are possible on qubits by using quantum gates. The gate operations are exactly all unitary linear operations.

For example, the Hadamard transformation is defined as:

Knowing that and then we can represent the transformation as the matrix:

A Hadamard gate creates a superposition state, often beginning and ending a quantum computation to initiate data processing and to collect data, respectively.

A set of useful 1-qubit gates are the Pauli Gates, the X gate, Y gate and Z gate.

Two more gates that we will use in our examples are the Controlled-Not and Controlled Phase Shift and they are defined by the following matrices:

In the next article we will write some code that implements some basic operations.