Taking Python to the Next Level

on February 22, 2018

A brief intro to simulating quantum systems with QuTiP.

With the reincarnation ofLinux Journal, I thought I'd take thisarticle through a quantum leap (pun intended) and look at quantumcomputing. As was true with the beginning of parallel programming, the nexthurdle in quantum computing is developing algorithms that can do usefulwork while harnessing the full potential of this new hardware.

Unfortunately though, most people don't have a handy quantum computer lying aroundon which they can develop code. The vast majority will need todevelop ideas and algorithms on simulated systems, and that's fine for suchfundamental algorithm design.

So, let's take look at one of the Pythonmodules available to simulate quantum systems—specifically,QuTiP. For this short article, I'm focusing on the mechanics of how to usethe code rather than the theory of quantum computing.

The first step is installing the QuTiP module. On most machines, you caninstall it with:

sudo pip install qutip

This should work fine for most people. If you need somelatest-and-greatest feature, you always can installQuTiP from source by going to thehome page.

Once it's installed, verify that everything worked by starting upa Python instance and entering the following Python commands:

>> from qutip import *>> about()

You should see details about the version numbers and installation paths.

The first step is to create a qubit. This is the simplest unit of data tobe used for quantum calculations. The following code generates a qubitfor two-level quantum systems:

>> q1 = basis(2,0)>> q1   Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket   Qobj data =   [[ 1.]    [ 0.]]

By itself, this object doesn't give you much. The simulation kicks in whenyou start applying operators to such an object. For example, you can applythe sigma plus operator (which is equivalent to the raising operator forquantum states). You can do this with one of the operator functions:

>> q2 = sigmap * q1>> q2   Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket   Qobj data =   [[ 0.]    [ 0.]]

As you can see, you get the zero vector as a result from the application ofthis operator.

You can combine multiple qubits into a tensor object. The following codeshows how that can work:

>> from qutip import *>> from scipy import *>> q1 = basis(2, 0)>> q2 = basis(2,0)>> print q1   Quantum object: dims = [[2], [1]], shape = [2, 1], type = ket   Qobj data =   [[ 1.]    [ 0.]]>> print q2   Quantum object: dims = [[2], [1]], shape = [2, 1], type = ket   Qobj data =   [[ 1.]    [ 0.]]>> print tensor(q1,q2)   Quantum object: dims = [[2, 2], [1, 1]], shape = [4, 1], type = ket   Qobj data =   [[ 1.]    [ 0.]    [ 0.]    [ 0.]]

This will couple them together, and they'll be treated as a single objectby operators. This lets you start to build up systems of multiple qubitsand more complicated algorithms.

More general objects and operators are available when you start toget to even more complicated algorithms. You can create the basic quantumobject with the following constructor:

>> q = Qobj([[1], [0]])>> q   Quantum object: dims = [[2], [1]], shape = [2, 1], type = ket   Qobj data =   [[1.0]    [0.0]]

These objects have several visible properties, such as the shape and numberof dimensions, along with the actual data stored in the object. You can usethese quantum objects in regular arithmetic operations, just like any otherPython objects. For example, if you have two Pauli operators, sz and sy,you could create a Hamiltonian, like this:

>> H = 1.0 * sz + 0.1 * sy

You can then apply operations to this compound object. You can get thetrace with the following:

>> H.tr()

In this particular case, you can find the eigen energies for the givenHamiltonian with the methodeigenenergies():

>> H.eigenenergies()

Several helper objects also are available to create these quantum objects foryou. The basis constructor used earlier is one of those helpers. There arealso other helpers, such asfock() andcoherent().

Because you'll be dealing with states that are so far outside yourusual day-to-day experiences, it may be difficult to reason what ishappening within any particular algorithm. Because of this, QuTiP includesa very complete visualization library to help see, literally, what ishappening within your code. In order to initialize the graphics libraries,you'll likely want to stick the following code at the top of yourprogram:

>> import matplotlib.pyplot as plt>> import numpy as np>> from qutip import *

From here, you can use thesphereplot() function to generatethree-dimensional spherical plots of orbitals. Theplot_energy_levels()function takes a given quantum object and calculates the associatedenergies for the object. Along with the energies, you can plot theexpectation values for a given system with the functionplot_expectation_values().

I've covered only the barest tip of the proverbial icebergwhen it comes to using QuTiP. There is functionality that allows you tomodel entire quantum systems and see them evolving over time. Hopefully,this short article has been able to highlight one tool available to you ifyou decide to embark on research in quantum systems and computation.

Load Disqus comments