Having seen some of the typical maths you need in part I and part II we’ll turn now to some Python showing how to simulate quantum systems.

I’ll use below the QuTip Python library but there are other very nice libs out there. Also, if Python is not your cup of tea you can also take a look at this list of QC simulators here, the Liquid F# lib in particular.


Creating qubits, bras and kets goes typically likes this:

which returns the $|0\rangle$ state in a 4-dimensional space:

$\begin{pmatrix}1.0 \\ 0.0\\0.0\\0.0 \end{pmatrix}$

The tangled EPR state is hence

$\begin{pmatrix}0.707 \\ 0.0\\0.0\\0.707 \end{pmatrix}$

We saw earlier that qubits can be represented on a Bloch sphere and the library gives you easy access to visualizations:


The sigmax, sigmay, sigmaz above are the Pauli spin matrices. Turning the states into a density matrix can be done with the ket2dm command:

$\begin{pmatrix}0.5 & 0.0 & 0.0 & 0.5\\ 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0 & 0.0\\0.5 & 0.0 & 0.0 & 0.5 \end{pmatrix}$

Remember that this is nothing but turning an arbitrary ket $|\psi\rangle$ into a matrix $|\psi\rangle\langle\psi|$.

Tensor products are obtained using the tensor method. For example, creating the tensor product of $\sigma_x\otimes\sigma_y$:

$\begin{pmatrix}0.0 & 0.0 & 0.0 & -1.0j\\ 0.0 & 0.0 & 1.0j & 0.0\\0.0 & -1.0j & 0.0 & 0.0\\1.0j & 0.0 & 0.0 & 0.0 \end{pmatrix}$

so, $(\sigma_x\otimes\sigma_y)(|1\rangle\otimes|0\rangle)$ is hence

$\begin{pmatrix}0.0 \\ 1.0j\\0.0\\0.0 \end{pmatrix}$.


Operators are created via the Qobj

and applied via multiplication on a state

$\begin{pmatrix}5.0j \\ -5.0j \end{pmatrix}$.

Eigenstates and eigenvalues are fetched via the eigenstates method on the operator

$\begin{pmatrix}-0.707j \\ -0.707j \end{pmatrix}$.

Fock space mechanics works as should be and you can check e.g. that $a^\dagger|0\rangle = |1\rangle

Similarly, $a^\dagger a^\dagger|0\rangle = 0$ since ${a^\dagger, a^\dagger}=0$:

$\begin{pmatrix}0.0 \\ 0.0 \end{pmatrix}$.


The framework allows you to simulate the evolution of kets or observables using the Lindblad or master equation. For example, the following simple Hamiltonian

$\hat{H} = \sigma_x\sigma_z$

evolution on the $|0\rangle$ ket gives: