Saving QuTiP Objects and Data Sets

With time-consuming calculations it is often necessary to store the results to files on disk, so it can be post-processed and archived. In QuTiP there are two facilities for storing data: Quantum objects can be stored to files and later read back as python pickles, and numerical data (vectors and matrices) can be exported as plain text files in for example CSV (comma-separated values), TSV (tab-separated values), etc. The former method is preferred when further calculations will be performed with the data, and the latter when the calculations are completed and data is to be imported into a post-processing tool (e.g. for generating figures).

Storing and loading QuTiP objects

To store and load arbitrary QuTiP related objects (qutip.Qobj, qutip.solver.Result, etc.) there are two functions: qutip.fileio.qsave and qutip.fileio.qload. The function qutip.fileio.qsave takes an arbitrary object as first parameter and an optional filename as second parameter (default filename is qutip_data.qu). The filename extension is always .qu. The function qutip.fileio.qload takes a mandatory filename as first argument and loads and returns the objects in the file.

To illustrate how these functions can be used, consider a simple calculation of the steadystate of the harmonic oscillator:

In [2]: a = destroy(10); H = a.dag() * a ; c_ops = [sqrt(0.5) * a, sqrt(0.25) * a.dag()]

In [3]: rho_ss = steadystate(H, c_ops)

The steadystate density matrix rho_ss is an instance of qutip.Qobj. It can be stored to a file steadystate.qu using

In [4]: qsave(rho_ss, 'steadystate')

In [5]: ls *.qu
density_matrix_vs_time.qu  steadystate.qu

and it can later be loaded again, and used in further calculations:

In [6]: rho_ss_loaded = qload('steadystate')
Loaded Qobj object:
Quantum object: dims = [[10], [10]], shape = [10, 10], type = oper, isHerm = True


In [7]: a = destroy(10)

In [8]: expect(a.dag() * a, rho_ss_loaded)
 Out[8]: 0.9902248289345018

The nice thing about the qutip.fileio.qsave and qutip.fileio.qload functions is that almost any object can be stored and load again later on. We can for example store a list of density matrices as returned by qutip.mesolve:

In [9]: a = destroy(10); H = a.dag() * a ; c_ops = [sqrt(0.5) * a, sqrt(0.25) * a.dag()]

In [10]: psi0 = rand_ket(10)

In [11]: times = np.linspace(0, 10, 10)

In [12]: dm_list = mesolve(H, psi0, times, c_ops, [])

In [13]: qsave(dm_list, 'density_matrix_vs_time')

And it can then be loaded and used again, for example in an other program:

In [14]: dm_list_loaded = qload('density_matrix_vs_time')
Loaded Result object:
Result object with mesolve data.
--------------------------------
states = True
num_collapse = 0

In [15]: a = destroy(10)

In [16]: expect(a.dag() * a, dm_list_loaded.states)
Out[16]: 
array([ 3.87543797,  3.11893669,  2.5717301 ,  2.17012827,  1.87240607,
        1.65062668,  1.48498272,  1.36107052,  1.26828395,  1.19875921])

Storing and loading datasets

The qutip.fileio.qsave and qutip.fileio.qload are great, but the file format used is only understood by QuTiP (python) programs. When data must be exported to other programs the preferred method is to store the data in the commonly used plain-text file formats. With the QuTiP functions qutip.fileio.file_data_store and qutip.fileio.file_data_read we can store and load numpy arrays and matrices to files on disk using a deliminator-separated value format (for example comma-separated values CSV). Almost any program can handle this file format.

The qutip.fileio.file_data_store takes two mandatory and three optional arguments:

>>> file_data_store(filename, data, numtype="complex", numformat="decimal", sep=",")

where filename is the name of the file, data is the data to be written to the file (must be a numpy array), numtype (optional) is a flag indicating numerical type that can take values complex or real, numformat (optional) specifies the numerical format that can take the values exp for the format 1.0e1 and decimal for the format 10.0, and sep (optional) is an arbitrary single-character field separator (usually a tab, space, comma, semicolon, etc.).

A common use for the qutip.fileio.file_data_store function is to store the expectation values of a set of operators for a sequence of times, e.g., as returned by the qutip.mesolve function, which is what the following example does:

In [17]: a = destroy(10); H = a.dag() * a ; c_ops = [sqrt(0.5) * a, sqrt(0.25) * a.dag()]

In [18]: psi0 = rand_ket(10)

In [19]: times = np.linspace(0, 100, 100)

In [20]: medata = mesolve(H, psi0, times, c_ops, [a.dag() * a, a + a.dag(), -1j * (a - a.dag())])

In [21]: shape(medata.expect)
Out[21]: (3, 100)

In [22]: shape(times)
Out[22]: (100,)

In [23]: output_data = np.vstack((times, medata.expect))   # join time and expt data

In [24]: file_data_store('expect.dat', output_data.T) # Note the .T for transpose!

In [25]: ls *.dat
expect.dat

In [26]: !head expect.dat
# Generated by QuTiP: 100x4 complex matrix in decimal format [',' separated values].
0.0000000000+0.0000000000j,4.6345969256+0.0000000000j,2.2937984593+0.0000000000j,-0.0834019751+0.0000000000j
1.0101010101+0.0000000000j,3.6929064190+0.0000000000j,0.9743342177+0.0000000000j,-1.6588904559+0.0000000000j
2.0202020202+0.0000000000j,3.0435621859+0.0000000000j,-0.7571772825+0.0000000000j,-1.4733799069+0.0000000000j
3.0303030303+0.0000000000j,2.5588500376+0.0000000000j,-1.4298597237+0.0000000000j,-0.1269254187+0.0000000000j
4.0404040404+0.0000000000j,2.1919778643+0.0000000000j,-0.7563634928+0.0000000000j,0.9933039417+0.0000000000j
5.0505050505+0.0000000000j,1.9124889790+0.0000000000j,0.3816502268+0.0000000000j,1.0192022454+0.0000000000j
6.0606060606+0.0000000000j,1.6987829608+0.0000000000j,0.9305516565+0.0000000000j,0.1916095033+0.0000000000j
7.0707070707+0.0000000000j,1.5350006737+0.0000000000j,0.5744885468+0.0000000000j,-0.5993416663+0.0000000000j
8.0808080808+0.0000000000j,1.4092899918+0.0000000000j,-0.1764731916+0.0000000000j,-0.7041849176+0.0000000000j

In this case we didn’t really need to store both the real and imaginary parts, so instead we could use the numtype=”real” option:

In [27]: file_data_store('expect.dat', output_data.T, numtype="real")

In [28]: !head -n5 expect.dat
# Generated by QuTiP: 100x4 real matrix in decimal format [',' separated values].
0.0000000000,4.6345969256,2.2937984593,-0.0834019751
1.0101010101,3.6929064190,0.9743342177,-1.6588904559
2.0202020202,3.0435621859,-0.7571772825,-1.4733799069
3.0303030303,2.5588500376,-1.4298597237,-0.1269254187

and if we prefer scientific notation we can request that using the numformat=”exp” option

In [29]: file_data_store('expect.dat', output_data.T, numtype="real", numformat="exp")

In [30]: !head -n 5 expect.dat
# Generated by QuTiP: 100x4 real matrix in exp format [',' separated values].
0.0000000000e+00,4.6345969256e+00,2.2937984593e+00,-8.3401975147e-02
1.0101010101e+00,3.6929064190e+00,9.7433421768e-01,-1.6588904559e+00
2.0202020202e+00,3.0435621859e+00,-7.5717728249e-01,-1.4733799069e+00
3.0303030303e+00,2.5588500376e+00,-1.4298597237e+00,-1.2692541867e-01

Loading data previously stored using qutip.fileio.file_data_store (or some other software) is a even easier. Regardless of which deliminator was used, if data was stored as complex or real numbers, if it is in decimal or exponential form, the data can be loaded using the qutip.fileio.file_data_read, which only takes the filename as mandatory argument.

In [31]: input_data = file_data_read('expect.dat')

In [32]: shape(input_data)
Out[32]: (100, 4)

In [33]: from pylab import *

In [34]: plot(input_data[:,0], input_data[:,1]);  # plot the data
Out[34]: [<matplotlib.lines.Line2D at 0x2ac6d0b3cfd0>]
../images/saving_ex.png

(If a particularly obscure choice of deliminator was used it might be necessary to use the optional second argument, for example sep=”_” if _ is the deliminator).