Basic Operations on Quantum Objects¶
First things first¶
Warning
Do not run QuTiP from the installation directory.
To load the qutip modules, we must first call the import statement:
In [1]: from qutip import *
that will load all of the user available functions. Often, we also need to import the NumPy and Matplotlib libraries with:
In [2]: import numpy as np
In [3]: import matplotlib.pyplot as plt
Note that, in the rest of the documentation, functions are written using qutip.module.function() notation which links to the corresponding function in the QuTiP API: Functions. However, in calling import *, we have already loaded all of the QuTiP modules. Therefore, we will only need the function name and not the complete path when calling the function from the interpreter prompt, Python script, or Jupyter notebook.
The quantum object class¶
Introduction¶
The key difference between classical and quantum mechanics lies in the use of operators instead of numbers as variables. Moreover, we need to specify state vectors and their properties. Therefore, in computing the dynamics of quantum systems we need a data structure that is capable of encapsulating the properties of a quantum operator and ket/bra vectors. The quantum object class, qutip.Qobj
, accomplishes this using matrix representation.
To begin, let us create a blank Qobj
:
In [4]: Qobj()
Out[4]:
Quantum object: dims = [[1], [1]], shape = (1, 1), type = bra
Qobj data =
[[0.]]
where we see the blank Qobj
object with dimensions, shape, and data. Here the data corresponds to a 1x1dimensional matrix consisting of a single zero entry.
Hint
By convention, Class objects in Python such as Qobj()
differ from functions in the use of a beginning capital letter.
We can create a Qobj
with a user defined data set by passing a list or array of data into the Qobj
:
In [5]: Qobj([[1],[2],[3],[4],[5]])
Out[5]:
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket
Qobj data =
[[1.]
[2.]
[3.]
[4.]
[5.]]
In [6]: x = np.array([[1, 2, 3, 4, 5]])
In [7]: Qobj(x)
Out[7]:
Quantum object: dims = [[1], [5]], shape = (1, 5), type = bra
Qobj data =
[[1. 2. 3. 4. 5.]]
In [8]: r = np.random.rand(4, 4)
In [9]: Qobj(r)
Out[9]:
Quantum object: dims = [[4], [4]], shape = (4, 4), type = oper, isherm = False
Qobj data =
[[0.05466434 0.64087948 0.17482837 0.515324 ]
[0.70784678 0.524319 0.31937872 0.56781693]
[0.93001784 0.30682201 0.83393431 0.11172942]
[0.17233207 0.48796145 0.88912997 0.94194679]]
Notice how both the dims and shape change according to the input data. Although dims and shape appear to have the same function, the difference will become quite clear in the section on tensor products and partial traces.
Note
If you are running QuTiP from a python script you must use the print
function to view the Qobj attributes.
States and operators¶
Manually specifying the data for each quantum object is inefficient. Even more so when most objects correspond to commonly used types such as the ladder operators of a harmonic oscillator, the Pauli spin operators for a twolevel system, or state vectors such as Fock states. Therefore, QuTiP includes predefined objects for a variety of states:
States 
Command (# means optional) 
Inputs 

Fock state ket vector 

N = number of levels in Hilbert space, m = level containing excitation (0 if no m given) 
Fock density matrix (outer product of basis) 

same as basis(N,m) / fock(N,m) 
Coherent state 

alpha = complex number (eigenvalue) for requested coherent state 
Coherent density matrix (outer product) 

same as coherent(N,alpha) 
Thermal density matrix (for n particles) 

n = particle number expectation value 
and operators:
Operators 
Command (# means optional) 
Inputs 

Charge operator 

Diagonal operator with entries from M..0..N. 
Commutator 

Kind = ‘normal’ or ‘anti’. 
Diagonals operator 

Quantum object created from arrays of diagonals at given offsets. 
Displacement operator (Singlemode) 

N=number of levels in Hilbert space, alpha = complex displacement amplitude. 
Higher spin operators 

j = integer or halfinteger representing spin, s = ‘x’, ‘y’, ‘z’, ‘+’, or ‘‘ 
Identity 

N = number of levels in Hilbert space. 
Lowering (destruction) operator 

same as above 
Momentum operator 

same as above 
Number operator 

same as above 
Phase operator (Singlemode) 

Singlemode PeggBarnett phase operator with ref phase phi0. 
Position operator 

same as above 
Raising (creation) operator 

same as above 
Squeezing operator (Singlemode) 

N=number of levels in Hilbert space, sp = squeezing parameter. 
Squeezing operator (Generalized) 

q1,q2 = Quantum operators (Qobj) sp = squeezing parameter. 
SigmaX 


SigmaY 


SigmaZ 


Sigma plus 


Sigma minus 


Tunneling operator 

Tunneling operator with elements of the form \(N><N+m + N+m><N\). 
As an example, we give the output for a few of these functions:
In [10]: basis(5,3)
Out[10]:
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket
Qobj data =
[[0.]
[0.]
[0.]
[1.]
[0.]]
In [11]: coherent(5,0.50.5j)
Out[11]:
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket
Qobj data =
[[ 0.7788017 +0.j ]
[ 0.389391420.38939142j]
[ 0. 0.27545895j]
[0.078986170.07898617j]
[0.04314271+0.j ]]
In [12]: destroy(4)
Out[12]:
Quantum object: dims = [[4], [4]], shape = (4, 4), type = oper, isherm = False
Qobj data =
[[0. 1. 0. 0. ]
[0. 0. 1.41421356 0. ]
[0. 0. 0. 1.73205081]
[0. 0. 0. 0. ]]
In [13]: sigmaz()
Out[13]:
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True
Qobj data =
[[ 1. 0.]
[ 0. 1.]]
In [14]: jmat(5/2.0,'+')
Out[14]:
Quantum object: dims = [[6], [6]], shape = (6, 6), type = oper, isherm = False
Qobj data =
[[0. 2.23606798 0. 0. 0. 0. ]
[0. 0. 2.82842712 0. 0. 0. ]
[0. 0. 0. 3. 0. 0. ]
[0. 0. 0. 0. 2.82842712 0. ]
[0. 0. 0. 0. 0. 2.23606798]
[0. 0. 0. 0. 0. 0. ]]
Qobj attributes¶
We have seen that a quantum object has several internal attributes, such as data, dims, and shape. These can be accessed in the following way:
In [15]: q = destroy(4)
In [16]: q.dims
Out[16]: [[4], [4]]
In [17]: q.shape
Out[17]: (4, 4)
In general, the attributes (properties) of a Qobj
object (or any Python class) can be retrieved using the Q.attribute notation. In addition to the attributes shown with the print
function, the Qobj
class also has the following:
Property 
Attribute 
Description 

Data 

Matrix representing state or operator 
Dimensions 

List keeping track of shapes for individual components of a multipartite system (for tensor products and partial traces). 
Shape 

Dimensions of underlying data matrix. 
is Hermitian? 

Is the operator Hermitian or not? 
Type 

Is object of type ‘ket, ‘bra’, ‘oper’, or ‘super’? 
For the destruction operator above:
In [18]: q.type
Out[18]: 'oper'
In [19]: q.isherm
Out[19]: False
In [20]: q.data
Out[20]:
<4x4 sparse matrix of type '<class 'numpy.complex128'>'
with 3 stored elements in Compressed Sparse Row format>
The data attribute returns a message stating that the data is a sparse matrix. All Qobj
instances store their data as a sparse matrix to save memory. To access the underlying dense matrix one needs to use the qutip.Qobj.full
function as described below.
Qobj Math¶
The rules for mathematical operations on Qobj
instances are similar to standard matrix arithmetic:
In [21]: q = destroy(4)
In [22]: x = sigmax()
In [23]: q + 5
Out[23]:
Quantum object: dims = [[4], [4]], shape = (4, 4), type = oper, isherm = False
Qobj data =
[[5. 1. 0. 0. ]
[0. 5. 1.41421356 0. ]
[0. 0. 5. 1.73205081]
[0. 0. 0. 5. ]]
In [24]: x * x
Out[24]:
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True
Qobj data =
[[1. 0.]
[0. 1.]]
In [25]: q ** 3
Out[25]:
Quantum object: dims = [[4], [4]], shape = (4, 4), type = oper, isherm = False
Qobj data =
[[0. 0. 0. 2.44948974]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]
[0. 0. 0. 0. ]]
In [26]: x / np.sqrt(2)
Out[26]:
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = True
Qobj data =
[[0. 0.70710678]
[0.70710678 0. ]]
Of course, like matrices, multiplying two objects of incompatible shape throws an error:
In [27]: q * x

TypeError Traceback (most recent call last)
<ipythoninput2757f05cd0899f> in <module>
> 1 q * x
~/release/build_doc/lib/python3.7/sitepackages/qutip/qobj.py in __mul__(self, other)
543
544 else:
> 545 raise TypeError("Incompatible Qobj shapes")
546
547 elif isinstance(other, np.ndarray):
TypeError: Incompatible Qobj shapes
In addition, the logic operators is equal == and is not equal != are also supported.
Functions operating on Qobj class¶
Like attributes, the quantum object class has defined functions (methods) that operate on Qobj
class instances. For a general quantum object Q
:
Function 
Command 
Description 

Check Hermicity 

Check if quantum object is Hermitian 
Conjugate 

Conjugate of quantum object. 
Cosine 

Cosine of quantum object. 
Dagger (adjoint) 

Returns adjoint (dagger) of object. 
Diagonal 

Returns the diagonal elements. 
Diamond Norm 

Returns the diamond norm. 
Eigenenergies 

Eigenenergies (values) of operator. 
Eigenstates 

Returns eigenvalues and eigenvectors. 
Eliminate States 

Returns quantum object with states in list inds removed. 
Exponential 

Matrix exponential of operator. 
Extract States 

Qobj with states listed in inds only. 
Full 

Returns full (not sparse) array of Q’s data. 
Groundstate 

Eigenval & eigket of Qobj groundstate. 
Matrix Element 

Matrix element <braQket> 
Norm 

Returns L2 norm for states, trace norm for operators. 
Overlap 

Overlap between current Qobj and a given state. 
Partial Trace 

Partial trace returning components selected using ‘sel’ parameter. 
Permute 

Permutes the tensor structure of a composite object in the given order. 
Projector 

Form projector operator from given ket or bra vector. 
Sine 

Sine of quantum operator. 
Sqrt 

Matrix sqrt of operator. 
Tidyup 

Removes small elements from Qobj. 
Trace 

Returns trace of quantum object. 
Transform 

A basis transformation defined by matrix or list of kets ‘inpt’ . 
Transpose 

Transpose of quantum object. 
Truncate Neg 

Truncates negative eigenvalues 
Unit 

Returns normalized (unit) vector Q/Q.norm(). 
In [28]: basis(5, 3)
Out[28]:
Quantum object: dims = [[5], [1]], shape = (5, 1), type = ket
Qobj data =
[[0.]
[0.]
[0.]
[1.]
[0.]]
In [29]: basis(5, 3).dag()
Out[29]:
Quantum object: dims = [[1], [5]], shape = (1, 5), type = bra
Qobj data =
[[0. 0. 0. 1. 0.]]
In [30]: coherent_dm(5, 1)
Out[30]:
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[0.36791117 0.36774407 0.26105441 0.14620658 0.08826704]
[0.36774407 0.36757705 0.26093584 0.14614018 0.08822695]
[0.26105441 0.26093584 0.18523331 0.10374209 0.06263061]
[0.14620658 0.14614018 0.10374209 0.05810197 0.035077 ]
[0.08826704 0.08822695 0.06263061 0.035077 0.0211765 ]]
In [31]: coherent_dm(5, 1).diag()
Out[31]: array([0.36791117, 0.36757705, 0.18523331, 0.05810197, 0.0211765 ])
In [32]: coherent_dm(5, 1).full()
Out[32]:
array([[0.36791117+0.j, 0.36774407+0.j, 0.26105441+0.j, 0.14620658+0.j,
0.08826704+0.j],
[0.36774407+0.j, 0.36757705+0.j, 0.26093584+0.j, 0.14614018+0.j,
0.08822695+0.j],
[0.26105441+0.j, 0.26093584+0.j, 0.18523331+0.j, 0.10374209+0.j,
0.06263061+0.j],
[0.14620658+0.j, 0.14614018+0.j, 0.10374209+0.j, 0.05810197+0.j,
0.035077 +0.j],
[0.08826704+0.j, 0.08822695+0.j, 0.06263061+0.j, 0.035077 +0.j,
0.0211765 +0.j]])
In [33]: coherent_dm(5, 1).norm()
Out[33]: 1.000000029930193
In [34]: coherent_dm(5, 1).sqrtm()
Out[34]:
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = False
Qobj data =
[[0.36791119+0.00000000e+00j 0.36774406+0.00000000e+00j
0.2610544 +0.00000000e+00j 0.14620658+0.00000000e+00j
0.08826704+0.00000000e+00j]
[0.36774406+0.00000000e+00j 0.36757705+9.33478344e11j
0.26093584+4.30453576e11j 0.146140182.99165502e10j
0.088226952.06796067e11j]
[0.2610544 +0.00000000e+00j 0.26093584+4.30453576e11j
0.18523332+1.98494461e11j 0.103742091.37953774e10j
0.062630619.53595840e12j]
[0.14620658+0.00000000e+00j 0.146140182.99165502e10j
0.103742091.37953774e10j 0.05810197+9.58779581e10j
0.035077 +6.62749700e11j]
[0.08826704+0.00000000e+00j 0.088226952.06796067e11j
0.062630619.53595840e12j 0.035077 +6.62749700e11j
0.0211765 +4.58121108e12j]]
In [35]: coherent_dm(5, 1).tr()
Out[35]: 1.0
In [36]: (basis(4, 2) + basis(4, 1)).unit()
Out[36]:
Quantum object: dims = [[4], [1]], shape = (4, 1), type = ket
Qobj data =
[[0. ]
[0.70710678]
[0.70710678]
[0. ]]