An Introduction to Building Quantum Computing Applications with Python


So we want to make a quantum application with Python, but since we do not own any quantum computer we need to have a simulator first. Simulation will not have the same performance as an actual quantum computer but we will be able to run applications. We have a choice from three simulators:  PyQu , QuTip and Qitensor.  We decided to pick QuTip as it has a very large code base and as it has the most recent changes.  PyQu hasn’t been updated since 2010 and Qitensor since a year or so.

Installing
We use a Unix machine in this tutorial, but you should be fine with any other operating system. Install using:

sudo add-apt-repository ppa:jrjohansson/qutip-releases
sudo apt-get update
sudo apt-get install python-qutip

We then start Python from the command line and type the commands listed below ( >>> ).

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from qutip import *
>>> about()
 
QuTiP: Quantum Toolbox in Python
Copyright (c) 2011 and later.
Paul D. Nation & Robert J. Johansson
 
QuTiP Version:      3.1.0
Numpy Version:      1.8.2
Scipy Version:      0.13.3
Cython Version:     0.20.1post0
Matplotlib Version: 1.3.1
Fortran mcsolver:   True
scikits.umfpack:    False
Python Version:     2.7.6
Platform Info:      Linux (i686)
Installation path:  /usr/lib/python2.7/dist-packages/qutip

This indicates that Qutip has been correctly installed.

The Quantum data structure
In quantum systems we need a data structure that is capable of encapsulating the properties of a quantum operator and ket/bra vectors, we use the Qobj data structure for that. In other words, to effectively simulate a quantum application we need to use the appropriate data structure.  Consider the example below:

#!/usr/bin/env python
from qutip import *
from scipy import *
 
r = rand(4, 4)
print Qobj(r)

And execute with:

python quantum.py

This will output the quantum object:

Quantum object: dims = [[4], [4]], shape = [4, 4], type = oper, isherm = False
Qobj data =
[[ 0.25529374  0.75548592  0.85680266  0.1438253 
 [ 0.75070138  0.68628867  0.97435624  0.77396516]
 [ 0.69819458  0.81714756  0.2604015   0.69051901]
 [ 0.0898242   0.05292657  0.49134431  0.4433644 ]]

If you want to specify user input yourself you could use:

#!/usr/bin/env python
from qutip import *
from scipy import *
 
x = array([[1],[2],[3],[4],[5]])
q = Qobj(x)
print q

This quantum object will simply hold your user given data:

Quantum object: dims = [[5], [1]], shape = [5, 1], type = ket
Qobj data =
[[ 1.]
 [ 2.]
 [ 3.]
 [ 4.]
 [ 5.]]

 Quantum states and operators
A quantum system is not a simple two-level system, it has multiple states.  QuTip includes some predefined states and quantum operators which are listed here.

Qubits and operators
We create a Qubit to hold data. Th Qubit is the quantum analogue of the classical bit. Unlike traditional bits, the qubit can be in a superposition of both states at the same time, a property which is fundamental to quantum computing. The code below will create a qubit:

#!/usr/bin/env python
from qutip import *
from scipy import *
 
spin = basis(2, 0)
print spin

You can now apply quantum system operators on the qubit:

#!/usr/bin/env python
from qutip import *
from scipy import *
 
spin = basis(2, 0)
print sigmam() * spin
print sigmap() * spin

Combining qubits
To describe the states of two coupled qubits we need to take the tensor product of the state vectors for each of the system components. Let us try that:

#!/usr/bin/env python
from qutip import *
from scipy import *
 
q1 = basis(2, 0)
q2 = basis(2,0)
 
print q1
print q2
print tensor(q1,q2)

The output we will get is:

Quantum object: dims = [[2], [1]], shape = [2, 1], type = ket
Qobj data =
[[ 1.]
 [ 0.]]
Quantum object: dims = [[2], [1]], shape = [2, 1], type = ket
Qobj data =
[[ 1.]
 [ 0.]]
Quantum object: dims = [[2, 2], [1, 1]], shape = [4, 1], type = ket
Qobj data =
[[ 1.]
 [ 0.]
 [ 0.]
 [ 0.]]

Whats next?
We have built some very simply quantum applications using this simple introduction. Perhaps you want to create an actually useful application, if so you could study more about quantum computing and complete the tutorial at http://qutip.org/docs/2.2.0/index.html