QuTiP includes a collection of random state generators for simulations, theorem evaluation, and code testing:
Function | Description |
---|---|
rand_ket | Random ket-vector |
rand_dm | Random density matrix |
rand_herm | Random Hermitian matrix |
rand_unitary | Random Unitary matrix |
See the API documentation: Random Operators and States for details.
In all cases, these functions can be called with a single parameter \(N\) that indicates a \(NxN\) matrix (rand_dm, rand_herm, rand_unitary), or a \(Nx1\) vector (rand_ket), should be generated. For example:
In [2]: rand_ket(5)
Out[2]:
Quantum object: dims = [[5], [1]], shape = [5, 1], type = ket
Qobj data =
[[-0.39285336+0.27024047j]
[-0.51803588+0.10328369j]
[-0.58850701-0.16960316j]
[-0.20872011+0.08251858j]
[-0.23355115+0.11654335j]]
or
In [3]: rand_herm(5)
Out[3]:
Quantum object: dims = [[5], [5]], shape = [5, 5], type = oper, isherm = True
Qobj data =
[[-0.78906375+0.j -0.49042711+0.01532022j -0.20366873+0.17308185j
-0.33542127-0.1744431j 0.00000000+0.j ]
[-0.49042711-0.01532022j 0.00000000+0.j -0.48262948+0.34613995j
0.00000000+0.j -0.32747896+0.11494093j]
[-0.20366873-0.17308185j -0.48262948-0.34613995j -0.85621763+0.j
0.00000000+0.j -0.29460117+0.01075528j]
[-0.33542127+0.1744431j 0.00000000+0.j 0.00000000+0.j
0.00000000+0.j -0.49346057+0.11200746j]
[ 0.00000000+0.j -0.32747896-0.11494093j -0.29460117-0.01075528j
-0.49346057-0.11200746j -0.74274000+0.j ]]
In this previous example, we see that the generated Hermitian operator contains a fraction of elements that are identically equal to zero. The number of nonzero elements is called the density and can be controlled by calling any of the random state/operator generators with a second argument between 0 and 1. By default, the density for the operators is 0.75 where as ket vectors are completely dense (1). For example:
In [4]: rand_dm(5, 0.5)
Out[4]:
Quantum object: dims = [[5], [5]], shape = [5, 5], type = oper, isherm = True
Qobj data =
[[ 0.16777653+0.j 0.00000000+0.j 0.00000000+0.j
0.00000000+0.j 0.00000000+0.j ]
[ 0.00000000+0.j 0.48109101+0.j 0.07516889-0.09787062j
0.15916938-0.11843809j 0.00000000+0.j ]
[ 0.00000000+0.j 0.07516889+0.09787062j 0.05114957+0.j
0.07911797+0.02241975j 0.00000000+0.j ]
[ 0.00000000+0.j 0.15916938+0.11843809j 0.07911797-0.02241975j
0.13220636+0.j 0.00000000+0.j ]
[ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j
0.00000000+0.j 0.16777653+0.j ]]
has rougly half nonzero elements, or equivalently a density of 0.5.
Important
In the case of a density matrix, setting the density too low will result in not enough diagonal elements to satisfy \(Tr(\rho)=1\).
In many cases, one is interested in generating random quantum objects that correspond to composite systems generated using the qutip.tensor.tensor function. Specifying the tensor structure of a quantum object is done using the dims keyword argument in the same fashion as one would do for a qutip.Qobj object:
In [5]: rand_dm(4, 0.5, dims=[[2,2], [2,2]])
Out[5]:
Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = True
Qobj data =
[[ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j
0.00000000+0.j ]
[ 0.00000000+0.j 0.10052169+0.j 0.00000000+0.j
0.28287538-0.01714619j]
[ 0.00000000+0.j 0.00000000+0.j 0.00000000+0.j
0.00000000+0.j ]
[ 0.00000000+0.j 0.28287538+0.01714619j 0.00000000+0.j
0.89947831+0.j ]]