Quantum Computing in Python
By: Amelie Schreiber
Tags: Quantum Computing, Python, Linear Algebra, Programming, The Singularity Research

Quantum Computing in Python

In this article I will introduce the basic linear algebra you will need to understand quantum computing. We will only use NumPy in this article, and you’ll get an intro at the end to some interactive Jupyter notebooks, so you don’t need to download anything or learn terminal to get started. All you need is a web browser. If you want you can download the notebooks and run them locally.

Photo by Shahadat Rahman on Unsplash

If you want to run these commands locally in Jupyter notebooks, to get started you’ll need to download the latest (free) distribution of Anaconda, which can be downloaded here. Once you have downloaded Anaconda, open the Anaconda navigator. The icon will be in your applications after download:

Anaconda Navigator Icon

Once you open the Anaconda Navigator, you’ll have the option to open a “Jupyter Notebook” or “Jupyter Lab”. Either will work, but personally I prefer Jupyter Lab.

Anaconda Navigator

If you prefer not to download anything, you can try Jupyter Lab in your browser by following this link. Once you’ve opened Jupyter Lab, click on the little “+” button to open a new Python 3 kernel. If can be found just under “File” and “Edit” near the Jupyter icon.

Clicking the “+” button results in the following window. Click on the Python 3 icon just under the orange notebook icon.

This will result in a blank cell (which will likely be white if you haven’t switched to “dark mode” under settings, which is what my settings are on and results in the sleek black aesthetic you see here.

In the empty cell in this window, type the following command hold down “shift” then press “enter” to run the cell. This will load the NumPy library which allows us to do linear algebra.

Importing NumPy

Creating Complex Numbers and Arithmetic Operations

Creating complex numbers is very easy in Python using NumPy. We can create two complex numbers 𝑧=3+4𝑗, and 𝑤=1−1𝑗 using the NumPy library. Here, remember “j” is the imaginary unit which in most math course is represented by “i” and complex numbers are usually written as “𝑎+𝑏𝑖” as apposed to “𝑎+𝑏𝑗”. We will use both throughout, so it is important to get comfortable with both and be fluent in converting between the two notations.

Defining Complex Numbers in Python

We can add the two complex numbers 𝑧 and 𝑤:

Adding two complex numbers in Python

We can also subtract the two complex numbers:

Subtracting complex numbers

Multiplying complex numbers in python is done as follows:

multiplying complex numbers

We can print the real and imaginary parts of complex numbers as follows for 𝑧=3+4𝑖:

printing real and imaginary parts

Vectors

Vectors will be fundamental in our study of quantum computing. The most basic unit of computation in a quantum computer is a qubit, which can be represented as a 2-dimensional complex vector of length one. So understanding vectors will be foundational and necessary for most of what we will be doing in this book. Vectors can be thought of in many ways, one of the most basic is simply as an array of numbers, which we will often represent as a column of numbers called column vectors, but in some cases we will also need row vectors:

row and column vectors

We can create a column vector and a row vector in Python:

row and column vector in Python

This should get you started with the linear algebra you need, but you’ll definitely need a more in depth look at how this is all used in quantum computing. To keep going, we have an interactive notebook up which you can access in your browser you can follow this link. For the entire linear algebra series of notebooks check out this page.

If you’re interested in learning quantum computing in-depth, consider supporting the project or contributing to it! The Singularity Research is constantly producing quantum computing notebook which translates textbooks and research papers into interactive notebooks to learn and use quantum computing and quantum machine learning. Reach out to

thesingularity.research@gmail.com

and include the subject “Hacking the Universe” if you have questions, want to contribute, or have an interest in using quantum computing and quantum machine learning.