Quantum Computing, Zero to Hero: Part Three — Maths

Gwilym Newton
10 min readNov 15, 2019

--

A guide to get you from foundation maths and physics to writing a quantum algorithm, and understanding everything in between.

The Series
Part 1
Part 2

Bad news everybody; we are going to have to learn some maths. The good news is that I am going to try to teach you as little as possible, without proving anything (sorry mathematicians). We are just going to grab the maths tools we are going to need for quantum computing and leave everything else behind. So you will see a lot of “for our purposes”, and if you know better please forgive me!

Vectors

Vectors are little stacks of numbers. Formally, a vector is defined as “an element of a vector space”. However, for our purposes it’s a column of numbers, where the order of the numbers is important. You will see vectors written like this:

In the case above we are using the vector to represent the measurements of a box. We could have additional vectors to represent the position, rotation and scale of the box. If we had a 2D rectangle we could have used a vector that was 2 “high”; this is its size. We can represent abstract things as vectors as well. Colour, for example, can be represented as Red/Green/Blue/Alpha, so we could write it as a vector of size four.

It’s also important to note the numbers are not independent; they are representing something. We do have a word for a group of numbers in which the order between them is not important. This is a “set”, but you won’t need to know that for our work.

Matrix

So, we have already introduced you to a column of numbers. What about a square? That’s all a matrix is; a collection of numbers (although again, the position and order are important).

So again, let’s bring it back to something a bit more real. Let’s say we have a picture of a triangle (carefully drawn by my good self):

We could represent that as a matrix by dividing it up as a grid like so (more lines would have been better, but we the authors are lazy):

We can then convert this grid into a grid of numbers showing how much “fill” there in in each square. A grid of numbers is basically a matrix. Below is a 4X4 matrix.

We could — and forgive me for not writing it all out — record all colour “channels” in our matrix as well, showing not only how much to shade each box, but what colors. Below we can create a 4x4x2 Matrix, where we have a blue channel as well.

So, to quickly recap: vectors are columns of numbers, while a matrix is a grid of numbers.

Matrix Multiplication / Dot Product

This is going to be one of the most important functions for us. I know we have not hit a lot of “real” quantum computing yet. It’s worth letting you know that quantum information is represented as Q-bits and we change Q-bits by passing them though Quantum Gates.

When you do this on paper, you are just doing matrix maths where the vector is the Qbit, and the Matrix is a Quantum Gate.

We refer to the type of matrix multiplication we are going to do here as a “dot product”.

The first example is easy: we’re just multiplying a matrix by a single number (a scalar).

However, if we want to multiply a matrix by another matrix we will get a new matrix.

If a Matrix has m number of rows and n number of columns, we would say it’s an (m x n) matrix.

So let’s multiply together the two matrices A(m x n) and B(n x p). When we do this, we would form a new matrix: C(m x p). Note that the new matrix size is equal to the number of rows of the first matrix and the number of columns of the second. Then the rule for each given element of the matrix C is the element at position x,y equals the sum of the individual elements in the xth row of matrix A multiplied by the individual elements in the yth column of matrix B. Let’s see this in action.

Now let’s do this with a vector. It might seem strange that I left this until after the matrix, but hopefully you can guess that a vector can just be treated as a special case of a matrix: a matrix with a single column (i.e. a vector of size v is also a matrix of size (v x 1)). So if we multiply vector V of size v by a matrix A(v x n) we get a net matrix: C(v x n).

Tensors

You might have heard of tensors from things like Tensorflow. “For our purposes” we can consider them as a special case of vectors and matrices. All the vectors you encounter after this article will be tensors, so you don’t need to know the difference really, but here are a couple of definitions anyway.

1) A tensor is anything that obeys the rules of ensors (helpful…)
2) A Tensor is constant under a change of coordinates

Let me explain number two for you a second. Imagine we shot an arrow from a bow, towards an apple, and paused to watch it mid-flight.

We could describe many of its attributes, like its position and which way it is pointing. For these attributes to make sense, we would need a zero — or reference — point to measure from. For our example let’s say the bowman who fired it is our zero point. If something is at his exact location, it’s at zero.

We could then describe the arrow’s position and the direction it’s pointing in terms of where the bowman is standing; let’s say “ten meters away”, and “away from me”.

Now if we described it from the apple’s point of view (a “change of coordinates”) we might say “five meters away” and “towards me”.

When we changed our point of view, we changed how we described it, but the arrow was still in the same position, pointing the same way. Hence “constant under a change of coordinates”.

If you have the time you can learn all the ins and outs of them here
[Vectors for Beginners — https://www.youtube.com/playlist?list=PLJHszsWbB6hrkmmq57lX8BV-o-YIOFsiG]

Covectors

Remember vectors? When we had a column of numbers? Well now we have covectors, which are a row of numbers. Like this:

(Note the lack of a T, this is not a vector that’s been laid on its side (or “Transposed”), it’s a row vector).

Covectors act as functions or operations on vectors. You can think of a function as a black box that takes some input and transforms it to some output. You feed something in one side and get something out the other. When you multiply a covector by a vector you get a scalar output (scalar being the fancy word for just one number).

So if we have a covector, C, and a vector, V, we can generate the scalar output like this:

Let’s try that with real numbers:

Further Reading

Tensor Product

Do you remember when we said “a tensor is constant under a change of coordinates” and discussed how the tensor is always relative to something? The more formal way to say this is that it’s part of a vector space. Sometimes you might need to get the product of two tensors from different vector spaces. For this we have the tensor product, denoted by:

There is not really a good way to give you an intuitive understanding of what happens, or an easy, real-world example to help you relate. So simply “for our purposes”, just think of it as working like this:

The second matrix is applied at every position in the first matrix as if each of the elements of the first matrix were scalars. Note that the size of the resulting matrix is the multiplication of the number of elements in the row of each matrix by the number of elements in the columns of each matrix. If we were to take the tensor product of another 1x2 matrix and a 1x4 matrix, the resulting matrix size would be 1x8. The fact that tensor products scale so quickly is important in understanding the power of quantum computing, as the addition of a single qubit doubles the vector space of the quantum computer.

Further Reading

Bra-ket notation

You will also come across this described as Dirac notation. Now that you know what vectors and covectors are, we have some useful shorthand to write them out: bra-ket.

Bras are row vectors (or covectors) and written like this: <B|

Kets are vectors, and are written like this: |K>

The most common ones you will see in this series are |1> and |0> This is the shorthand we use for a quantum one, and a quantum zero.

You see, we store the state of a quantum bit in what we call a one-hot register. This means we have a bunch of labels to represent the state of a thing (in this case a Qbit), and we put a 1 next to the state it’s in.

So our Qbit state can be stored like this:

And when it’s a one, we can write

And zero

But wait, that’s just a column of numbers! That’s a vector. So yes, we can store a Quantum one as [1,0] and a Quantum zero as [0,1]. This leads us back to the shorthand, we can just write “the one is |state> position” as |state>, and that leads us all the way back to |1> and |0>

You will see more complex versions of this later, like |8>, which means we have a vector full of zeroes and the 8th number in the list is a 1. Easier to write than [0,0,0,0,0,0,0,1]. Or you might see even more complex versions like |+>. This is a case where “+” is the name of the state, and we are just saying have a special case and we can’t be bothered to write all the numbers for it, which are:

Just like we write 𝜋 instead of 3.14159… It’s just easier sometimes.

Complex numbers

Complex numbers are going to require a little suspension of disbelief, like when we looked at the physics of quantum computing. We just had to agree to believe some things.

First, imaginary numbers. You see, there is a tricky little equation:

f(x) = x² +1 = 0

The problem is, we have this bucket of numbers we call the “real numbers”, and none of them work as x. The function is unsolvable, which is an even bigger problem because we have something called “the fundamental theorem of algebra” which says that every equation of this type can be solved. So this leaves us feeling a bit awkward and silly. Either something as important as “the fundamental theorem of algebra” is wrong, or we better go looking for some new kinds of numbers.

And thus we found the imaginary numbers. There is a good argument that these should be called lateral numbers, but as 90% of the world still calls them imaginary numbers so shall we.

They are a set of numbers that you can think of as going off to 90°, perpendicular from the number line you learned at school.

Just as when you see a “-” before a number you know its negative, if you see the “i” then you know its Imaginary. You can add, subtract, multiply with them just like many other numbers.

You might notice the title of this section is “complex” numbers not imaginary; that’s because what happens if something is part real numbers, and part imaginary numbers? Well you guessed it. Its “complex”, and you just write it as the sum of its complex and imaginary parts like so:

3+4i

Further Reading

Well done for reading all of that. You don’t need to memorise it; a lot of the reason we wrote it was to give you something to refer back to when you don’t understand something we are doing in the next part, where we will finally get to meet a Quantum bit.

--

--

Gwilym Newton

Experienced Technology Specialist, Skilled in Emerging Technology such as Brain-computer Interfaces, Virtual and Augmented Reality, Blockchain and ML