QuTiP: The Quantum Toolbox in Python
QuTiP is an open-source Python library for simulating the dynamics of closed and open quantum systems. It provides user-friendly and efficient numerical simulations for a wide range of quantum mechanical problems, including those with arbitrary time-dependence, commonly found in quantum optics, trapped ions, superconducting circuits, and quantum nanomechanical resonators. The library, currently at version 5.2.3, is built upon NumPy, SciPy, and Cython for numerical backends and Matplotlib for graphical output, with regular minor and patch releases, and significant major updates.
Common errors
-
TypeError: 'complex' object is not subscriptable
cause In QuTiP v5.x, the multiplication of a bra `Qobj` with a ket `Qobj` now returns a scalar (a Python `complex` number) instead of a zero-dimensional `Qobj`. Attempting to index this scalar (e.g., `result[0][0]`) will raise this error.fixIf your code expects a `Qobj` and attempts to index it, remove the indexing. The result of `bra * ket` is directly the scalar complex number. If you need the complex value, use it directly (e.g., `value = bra * ket`). -
AttributeError: 'QobjEvo' object has no attribute 'ops' / 'cte'
cause In QuTiP 5.x, direct access to individual Hamiltonian elements through `QobjEvo.ops` and `QobjEvo.cte` attributes has been removed as part of the core redesign.fixInstead of direct attribute access, evaluate the `QobjEvo` object at a specific time step by calling it like a function, e.g., `quobjevo(t)`. Most metadata (dims, shape) can be obtained from its properties directly. -
Object passed to assert_allclose is not an array.
cause QuTiP 5.x no longer implicitly converts `Qobj` instances to NumPy arrays using `numpy.array(qobj)`. This change prevents silent data type transformations.fixWhen comparing `Qobj` instances or their NumPy array representations using functions like `numpy.testing.assert_allclose`, explicitly convert the `Qobj` to a NumPy array using the `.full()` method: `qobj.full()`.
Warnings
- breaking QuTiP 5.x introduced significant breaking changes from 4.x, particularly in the core `Qobj`, `QobjEvo`, and solver implementations.
- breaking The `qutip.qip` and `qutip.control` modules have been moved out of the core `qutip` library into separate packages (`qutip-qip`, `qutip-qtrl`, `qutip-qoc`).
- gotcha Using an insufficient number of states (truncation) in simulations, especially for time-evolution of open quantum systems or harmonic oscillators, can lead to inaccurate or incorrect results at later times.
- gotcha The `steadystate()` solver in older QuTiP versions sometimes produced pivot errors for systems with ill-defined steady states (e.g., dissipationless dark states or multiple steady states). While newer versions might avoid the explicit error, they could silently return a 'nonsense' result.
Install
-
pip install qutip -
pip install qutip[full]
Imports
- qutip
from qutip import *
import qutip as qt
- Qobj
from qutip import Qobj # or qt.Qobj if 'import qutip as qt' is used
Quickstart
import qutip as qt
# Create a basis state |0> for a two-level system
ket0 = qt.basis(2, 0)
print(f"Ket |0>:\n{ket0}")
# Create a Pauli sigma-x operator
sigmax = qt.sigmax()
print(f"Sigma-x operator:\n{sigmax}")
# Apply the operator to the state
ket1 = sigmax * ket0
print(f"Sigma-x applied to |0> (gives |1>):\n{ket1}")
# Calculate expectation value
exp_val = qt.expect(sigmax, ket0)
print(f"Expectation value of sigma-x in |0>: {exp_val}")