How To Setup JupyterLab For Quantum Computing — On Windows
By: Frank Zickert
Tags: Quantum Computing, Python, Windows 10, Programming, Jupyter Notebook

How To Setup JupyterLab For Quantum Computing — On Windows

This post is part of the book: Hands-On Quantum Machine Learning With Python.

Image by author, Frank Zickert

If you’re a Python developer, there’s no way around a Unix-based operating system. Python is a language to write software that’s usually supposed to run at a server. And most servers run some kind of Linux.

Consequently, the default configuration in Python caters to the specificities of a Unix-based system. While Python works on Windows, too, it requires a lot more attention to get all the configuration details right. Starting from the path separator that is not a slash but a backslash ( \ ) to the different charset ( windows-1252 ), to different commands (e.g. del /s /q instead of rm ), Windows differs in quite a few aspects.

While Linux is great for developing, you may prefer Windows in other situations. Maybe you don’t even have a choice. Your working computer simply runs Windows. Full stop.

Fortunately, there’s a solution — at least if you’re running Windows 10. Windows 10 contains WSL2, the Windows Subsystem for Linux. It lets you run a full Ubuntu Linux inside Windows. Windows 10 must be updated to version 2004 and Intel’s virtualization technology must be enabled in BIOS settings.

In the first step, we need to activate the Windows Subsystem for Linux optional feature. Open PowerShell as Administrator and run the following command:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

In the next step, we update the subsystem to WSL2. Download the latest kernel update for your system from https://aka.ms/wsl2kernel and install the MSI package.

Now, we enable the Virtual machine platform and set WSL2 as the default version.

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart wsl --set-default-version 2

Finally, we can install a Linux distribution as if it was a normal program. Open the Microsoft store, search for “Ubuntu 20.04 LTS”, and install the program. Once the installation finishes, you can start Ubuntu from your start menu. On the first start, you need to create a new Unix user and specify a password.

You can proceed with the installation of the libraries and packages we need for quantum computing.

First, we update the apt-get package manager.

$ sudo apt-get update

$ sudo apt-get upgrade

The next step downloads and installs Python 3.8.5 (the latest stable release at the time of writing) and the libraries it needs during installation.

$ sudo apt-get install -y build-essential wget python3-dev \

libreadline-gplv2-dev libncursesw5-dev libssl-dev \

libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev \

libffi-dev

$ mkdir /tmp/Python38

$ cd /tmp/Python38

$ wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz

$ tar xvf Python-3.8.5.tar.xz

$ cd /tmp/Python38/Python-3.8.5

$ ./configure

$ sudo make altinstall

If you want to have Python3 as the default Python version, run:

$ sudo ln -s /usr/local/bin/python3.8 /usr/bin/python

Like most programming languages, Python has its own package installer. This is pip . It installs packages from the Python Package Index (PyPI) and other indexes.

Python is ready to work. Let’s now install and update the Python package manager pip:

$ wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py

$ pip install --upgrade pip

You might restart your machine to recognize pip as a command.

By default, pip installs the packages in the same base directory that is shared among all your Python projects. It makes an installed package available to all your projects. This seems to be good because you don’t need to install the same packages over and over again.

However, if any two of your projects require different versions of a package, you’ll be in trouble. Because there is no differentiation between versions. You would need to uninstall one version and install another whenever you switch working on either one of the projects.

This is where virtual environments come into play. Their purpose is to create an isolated environment for each of your Python projects. It’s no surprise, using Python virtual environments is the best practice.

As mentioned, we install all the Python packages in a virtual environment. So, we need to install virtualenv :

$ sudo apt-get install python3-venv

To create a virtual environment, go to your project directory and run venv . The following parameter (here env ) specifies the name of your environment

$ python -m venv env

You’ll need to activate your environment before you can start installing or using packages.

$ source env/bin/activate

When you’re done working on this project, you can leave your virtual environment by running the command deactivate . If you want to reenter, simply call source env/bin/activate again.

We’re now ready to install the packages we need.

Install Jupyter:

$ pip install jupyter notebook jupyterlab --upgrade

Install PyTorch:

$ pip install torch

Install TensorFlow

$ pip install tensorflow

Install Qiskit

$ pip install qiskit

If you don’t install Qiskit in the virtual environment, you should add the --user flag. Otherwise, the installation might fail due to missing permissions.

Install Cirq

$ pip install cirq

Install further dependencies required of Qiskit and Scikit-Learn. If you don’t use a virtual environment, use the --user flag here, too.

$ pip install numpy scipy matplotlib ipython pandas sympy nose seaborn

Install Scikit-Learn, with the --user flag if you're not using a virtual environment.

$ pip install scikit-learn

Install drawing libraries:

$ pip install pylatexenc ipywidgets

You’re now ready to start. Open up JupyterLab with

$ jupyter lab

And open localhost:8888 in a browser tab.

You’re now ready to start your quantum computing journey.

Here’s the code of a small quantum circuit that should run in a Jupyter notebook:

This post is part of the book: Hands-On Quantum Machine Learning With Python.

Get the first three chapters for free here.