- In order to install MacPorts, you need to have the Apple XCode Developer Tools installed on your machine. This can be done from inside the Apple App Store.
- Next, run Xcode and go to Preferences->Downloads and install the "Command Line Tools"
- Download the MacPorts package and install the package.
- To install QuTiP, in the terminal type: sudo port install py27-qutip
Thursday, May 9, 2013
QuTiP Now on MacPorts
Wednesday, April 3, 2013
Getting Started with QuTiP on PiCloud
%pylab inline
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
PiCloud let's you compute in the cloud via an easy to use interface. You are charged by the time your jobs actually take and you don't need to worry about managing hardware or the cloud resources yourself. To get started, PiCloud even provides you with 20 compute hours for free every month. In this post, I'll show you how you can get started in no time to use PiCloud together with QuTiP.
(This post has been written as an IPython notebook, you can look at the notebook in nbviewer or download the notebook as a gist to execute the code)
Getting Started
from qutip.ipynbtools import version_table
version_table()
| Software | Version |
|---|---|
| Cython | 0.18 |
| SciPy | 0.11.0 |
| QuTiP | 2.3.0.dev-f19dd9e |
| Python | 2.7.3 |Anaconda 1.4.0 (x86_64)| (default, Feb 25 2013, 18:45:56) [GCC 4.0.1 (Apple Inc. build 5493)] |
| IPython | 0.13.1 |
| OS | posix [darwin] |
| Numpy | 1.7.0 |
| matplotlib | 1.2.0 |
| Tue Apr 02 21:30:16 2013 SGT | |
import cloud
def add(x, y):
return x + y
jid = cloud.call(add, 1, 2)
cloud.result(jid)
3
import numpy as np
jid = cloud.call(add, np.arange(9.0), np.arange(9.0))
cloud.result(jid)
array([ 0., 2., 4., 6., 8., 10., 12., 14., 16.])
But first let's simulate something on the cloud, using the public environment. Let's start by having a look at our QuTiP installation in the cloud.
jid = cloud.call(version_table, _env='/mbaden/precise_with_qutip')
cloud.result(jid)
| Software | Version |
|---|---|
| Cython | 0.17.1 |
| SciPy | 0.11.0 |
| QuTiP | 2.2.0 |
| Python | 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] |
| IPython | 0.13 |
| OS | posix [linux2] |
| Numpy | 1.6.2 |
| matplotlib | 1.1.1 |
| Tue Apr 2 13:31:07 2013 | |
A simple example
from qutip import *
import time
wc = 1.0 * 2 * pi # cavity frequency
wa = 1.0 * 2 * pi # atom frequency
N = 25 # number of cavity fock states
kappa = 0.05 # cavity decay rate
n_th = 0.5
a = tensor(destroy(N), qeye(2))
sm = tensor(qeye(N), destroy(2))
nc = a.dag() * a
na = sm.dag() * sm
c_ops = [sqrt(kappa * (1 + n_th)) * a, sqrt(kappa * n_th) * a.dag()]
H0 = wc * nc + wa * na
H1 = (a.dag() + a) * (sm + sm.dag())
args = {
'H0': H0,
'H1': H1,
'c_ops': c_ops
}
# Create a list of 400 coupling strengths If you have a slow computer
# you want to decrease that number to ~50 first.
g_vec = linspace(0, 2.5, 400) * 2 * pi
def compute_task(g_vec, args):
H0, H1, c_ops = args['H0'], args['H1'], args['c_ops']
n_vec = zeros(g_vec.shape)
for k, g in enumerate(g_vec):
H = H0 + g * H1
rho_ss = steadystate(H, c_ops)
n_vec[k] = expect(nc, rho_ss)
return n_vec
def visualize_results(g_vec, n_vec):
fig, ax = subplots()
ax.plot(g_vec, n_vec, lw=2)
ax.set_xlabel('Coupling strength (g)')
ax.set_ylabel('Photon Number')
ax.set_title('# of photons in the steady state')
t0 = time.time()
n_vec = compute_task(g_vec, args)
t1 = time.time()
print "elapsed =", (t1-t0)
elapsed = 70.7504220009
visualize_results(g_vec / (2 * pi), n_vec)
We will use numpy's array_split function to create a number of chunks from our coupling strength array, create a function that takes only one argument, that is the list of input parameters, from our compute task and submit it to the cloud via cloud.map. In a last step we use numpy's concatenate to create a single output array again.
t0 = time.time()
no_chunks = 10
g_chunks = array_split(g_vec, no_chunks)
single_variable_task = lambda g: compute_task(g, args=args)
jids = cloud.map(single_variable_task, g_chunks,
_env='/mbaden/precise_with_qutip',
_type='c2')
n_chunks = cloud.result(jids)
n_vec = concatenate(n_chunks)
t1 = time.time()
print "elapsed =", (t1-t0)
elapsed = 39.917057991
visualize_results(g_vec / (2 * pi), n_vec)
Happy simulating!
Footnote: Creating a QuTiP Environment
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:jrjohansson/qutip-releases
sudo apt-get update
sudo apt-get install python-qutip
Here, the first step is an additional step as compared to the
installation instructions in the QuTiP documentation, which installs
add-apt-repository, which is not installed in the base environment. By
the way, these steps are exactly what I did to create
/mbaden/precise_with_ubuntu.Now you can customize your new qutip environment to your liking.
Tuesday, July 24, 2012
Quantum process tomography
Quantum process tomography (QPT) is a useful technique for characterizing experimental implementations of quantum gates involving a small number of qubits. Recently, QPT has been used in a number of beautiful experiments with for example superconducting qubits [Bialczak2010][Dewes2012], nuclear spins [Steger2012], NV-centers [Fuchs2012], ion traps [Riebe2006], just to mention a few.
QPT can also be a useful theoretical tool, which can give insight in how a gate transforms states, and be used to study how noise or other imperfections deteriorate a gate. Whereas a fidelity or distance measure can give a single number that indicates how far from ideal a gate is, a quantum process tomography analysis can give detailed information about exactly what kind of errors various imperfections introduce.
In QuTiP, we have recently added support for computing and visualizing quantum process tomography matrices. Here we give a simple example that demonstrates these new functions for a few common one, two and three qubit gates. The gates in this example are all available in QuTiP's gate module.
"""
Plot the process tomography matrices for some 1, 2, and 3-qubit qubit gates.
"""
from qutip import *
gate_list = [['C-NOT', cnot()],
['SWAP', swap()],
['$i$SWAP', iswap()],
['$\sqrt{i\mathrm{SWAP}}$', sqrtiswap()],
['S-NOT', snot()],
['$\pi/2$ phase gate', phasegate(pi/2)],
['Toffoli', toffoli()],
['Fredkin', fredkin()]]
# loop though the gate list and plot the gates
for gate in gate_list:
name = gate[0]
U_psi = gate[1]
N = len(U_psi.dims[0]) # number of qubits
# create a superoperator for the density matrix
# transformation rho = U_psi * rho_0 * U_psi.dag()
U_rho = spre(U_psi) * spost(U_psi.dag())
# operator basis for the process tomography
op_basis = [[qeye(2), sigmax(), sigmay(), sigmaz()] for i in range(N)]
# labels for operator basis
op_label = [["i", "x", "y", "z"] for i in range(N)]
# calculate the chi matrix
chi = qpt(U_rho, op_basis)
# visualize the chi matrix
qpt_plot_combined(chi, op_label, name)
show()
These figures are all examples of ideal quantum gates. To illustrate how the quality of a gate can deteriorate due to interaction between the qubits and their environment, let's consider a simple but experimentally realizable implementation of the two-qubit iSWAP gate. One way of implementing this gate is to produce the interaction Hamiltonian $H = g\left(\sigma_x\otimes\sigma_x + \sigma_y\otimes\sigma_y\right)$, and turn on this interaction for a period of time $T=\pi/4g$ [Schuch2003] [Steffen2006]. In the absence of noise and other imperfections, this procedure results in an ideal iSWAP gate to be applied to the two qubits. With QuTiP, we can easily simulate this process, and additionally study how and to what extent the gate deteriorates due to dissipation, imperfect switching of the interaction, detuning, etc. The following code demonstrates how to do this kind of analysis using QuTiP.
"""
Plot the process tomography matrix for a 2-qubit iSWAP gate.
"""
from qutip import *
g = 1.0 * 2 * pi # coupling strength
g1 = 0.75 # relaxation rate
g2 = 0.25 # dephasing rate
n_th = 1.5 # bath temperature
T = pi/(4*g)
H = g * (tensor(sigmax(), sigmax()) + tensor(sigmay(), sigmay()))
psi0 = tensor(basis(2,1), basis(2,0))
c_ops = []
# qubit 1 collapse operators
sm1 = tensor(sigmam(), qeye(2))
sz1 = tensor(sigmaz(), qeye(2))
c_ops.append(sqrt(g1 * (1+n_th)) * sm1)
c_ops.append(sqrt(g1 * n_th) * sm1.dag())
c_ops.append(sqrt(g2) * sz1)
# qubit 2 collapse operators
sm2 = tensor(qeye(2), sigmam())
sz2 = tensor(qeye(2), sigmaz())
c_ops.append(sqrt(g1 * (1+n_th)) * sm2)
c_ops.append(sqrt(g1 * n_th) * sm2.dag())
c_ops.append(sqrt(g2) * sz2)
# process tomography basis
op_basis = [[qeye(2), sigmax(), sigmay(), sigmaz()]] * 2
op_label = [["i", "x", "y", "z"]] * 2
# dissipative gate
U_diss = propagator(H, T, c_ops)
chi = qpt(U_diss, op_basis)
qpt_plot_combined(chi, op_label)
# ideal gate
U_psi = (-1j * H * T).expm()
U_ideal = spre(U_psi) * spost(U_psi.dag())
chi = qpt(U_ideal, op_basis)
qpt_plot_combined(chi, op_label)
show()
The code produces the following figures (the ideal iSWAP gate to the left, the dissipative one to the right):
Note: the quantum process tomography functions were added after the 2.0 release, so to run these examples you need to get the latest development version of QuTiP from the SVN repository. Alternatively, if you prefer to stay on the official release (recommended for stability), you can download this file and put it in the same directory as the example code and add "from qpt import *" at the top of the example files (just below the line "from qutip import *").
Monday, July 2, 2012
How large quantum systems can QuTiP handle?
The answer to the question posed in the title depends strongly on the type of the problem. For example, whether the Hamiltonian is constant or time-dependent, the system is closed or open, states are pure or mixed. Ultimately it is the RAM memory that limits the size of the systems we can manage, but in some cases you are likely to run out of patience long before the RAM memory is exhausted (in particular for time-dependent systems).
Computationally, the simplest problem that QuTiP will typically be used for is the evolution of a closed quantum system with a constant Hamiltonian. To explore how large quantum systems of this kind we can simulate with QuTiP, let’s consider a chain of spins coupled to their nearest neighbors through a sigma-x interaction. In QuTiP it is easy to program this model in a way that parameterize the number of spins in the chain, allowing us to simulate systems with increasing number of spins until we run out of RAM memory (resulting in a MemoryError exception being raised by python) or until we run out of patience. You can find the program code here:
"""
QuTiP example: Dynamics of a spin-1/2 chain
"""
from qutip import *
from pylab import *
rcParams['text.usetex'] = True
rcParams['font.size'] = 16
rcParams['font.family'] = 'serif'
def system_ops(N, h, J, gamma):
si = qeye(2); sx = sigmax(); sz = sigmaz()
sx_list = []; sz_list = []
for n in range(N):
op_list = [si for m in range(N)]
op_list[n] = sx
sx_list.append(tensor(op_list))
op_list[n] = sz
sz_list.append(tensor(op_list))
# construct the hamiltonian
H = 0
for n in range(N):
H += - 0.5 * h[n] * sz_list[n] # energy splitting terms
for n in range(N-1):
H += - 0.5 * J[n] * sx_list[n] * sx_list[n+1] # interaction terms
# collapse operators for spin dephasing
c_op_list = []
for n in range(N):
if gamma[n] > 0.0:
c_op_list.append(sqrt(gamma[n]) * sz_list[n])
# intital state, first and last spin in state |1>, the rest in state |0>
psi_list = [basis(2,0) for n in range(N-2)]
psi_list.insert(0, basis(2,1)) # first
psi_list.append(basis(2,1)) # last
psi0 = tensor(psi_list)
return H, c_op_list, sz_list, psi0
def evolve(N, h, J, tlist, gamma, solver):
H, c_ops, e_ops, psi0 = system_ops(N, h, J, gamma)
output = solver(H, psi0, tlist, c_ops, e_ops)
return (1 - array(output.expect))/2.0 # <sigma_z> to excitation prob.
# ---
solver = mesolve # select solver mesolve or mcsolve
N = 16 # number of spins
h = 1.0 * 2 * pi * ones(N) # energy splittings
J = 0.05 * 2 * pi * ones(N) # coupling
gamma = 0.0 * ones(N) # dephasing rate
tlist = linspace(0, 150, 150)
sn_expt = evolve(N, h, J, tlist, gamma, solver)
# plot excitation probabilities for each spin
yvals = []
ylabels = []
for n in range(N):
x = 0.1 # spacing between stacked plots
p = real(sn_expt[n]) * (1-2*x)
base = n + zeros(shape(p)) + 0.5 + x
plot(tlist, base, 'k', lw=1)
plot(tlist, base+(1-2*x), 'k', lw=1)
fill_between(tlist, base, base + p, color='b', alpha=0.75)
fill_between(tlist, base + p, base + (1-2*x), color='g', alpha=0.2)
yvals.append(n + 1)
ylabels.append("Qubit %d" % (n+1,))
autoscale(tight=True)
xlabel(r'Time [ns]',fontsize=14)
title(r'Occupation probabilities of spins in a chain', fontsize=16)
yticks(yvals, ylabels, fontsize=12)
savefig('spinchain-%d.pdf' % N)
My patience ran out after 21 spins. This corresponds to a very large quantum state space, 2’097’152 states to be exact, which is a quite impressive number. But admittedly, this simulation took a long time to run: About 7 hours on my laptop, and the memory usage peaked at about 4.3 Gb of RAM.
Open quantum systems are significantly more computationally demanding to evolve. First, we need to use the density matrix rather than a state vector to describe the system, which immediately squares the problem size. Second, the equations of motion (Master equation) are more complicated, including for example superoperators, requiring us to rearrange the equations of motion to obtain an ODE on standard form (of squared system size). Alternatively, we can use a stochastic equation of motion, such as the quantum trajectory Monte-Carlo method, and circumvent the requirement to square the system size, but this also comes at a steep price as we then need to average a large number of trajectories with the original system size. Regardless if we use a master equation or the Monte-Carlo quantum trajectory method, we cannot hope to evolve as large systems as we could for a closed quantum system.
Including dissipation in the example program above (by selecting gamma=0.0025), I could simulate systems up to 12 spins before the RAM memory was exhausted (on a 16Gb desktop). The 12 spin simulation took a weekend to run, and the peak memory usage was about 20 Gb (temporarily swapping 4 Gb). On this computer, further increasing the system size would result in memory error when using the master equation solver. Using the Monto-Carlo solver (by setting solver=mcsolve in the code) I could simulate open systems up to 16 spins in a similar time period (a few days).
If we include a time-dependence in for example the Hamiltonian, the memory foot-print is not significantly altered, but the problem becomes more CPU demanding, increasing the simulation time by sometimes quite a lot. However, at least in principle, we can simulate time-dependent quantum systems of similar size as time-independent problems.











