# Generating Random Quantum States & Operators¶

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 [1]: rand_ket(5)
Out[1]:
Quantum object: dims = [[5], [1]], shape = [5, 1], type = ket
Qobj data =
[[-0.37899439-0.03246954j]
[-0.09389192-0.30281261j]
[-0.41147565-0.20947105j]
[-0.41769426-0.02916778j]
[-0.54640563+0.26024817j]]


or

In [2]: rand_herm(5)
Out[2]:
Quantum object: dims = [[5], [5]], shape = [5, 5], type = oper, isherm = True
Qobj data =
[[-0.29514824+0.j          0.00000000+0.j         -0.27781445-0.15337652j
-0.35652395-0.05592461j  0.00000000+0.j        ]
[ 0.00000000+0.j         -0.55204452+0.j         -0.22293747-0.12925792j
-0.09264731+0.20738712j -0.71881796+0.01202871j]
[-0.27781445+0.15337652j -0.22293747+0.12925792j  0.00000000+0.j
-0.84636559+0.30414702j -0.47088943-0.09313568j]
[-0.35652395+0.05592461j -0.09264731-0.20738712j -0.84636559-0.30414702j
-0.02792858+0.j         -0.39742673-0.09375464j]
[ 0.00000000+0.j         -0.71881796-0.01202871j -0.47088943+0.09313568j
-0.39742673+0.09375464j  0.00000000+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 [3]: rand_dm(5, 0.5)
Out[3]:
Quantum object: dims = [[5], [5]], shape = [5, 5], type = oper, isherm = True
Qobj data =
[[ 0.04892987+0.j          0.00000000+0.j          0.00265679-0.0245355j
0.09885662-0.01638816j  0.00000000+0.j        ]
[ 0.00000000+0.j          0.00000000+0.j          0.00000000+0.j
0.00000000+0.j          0.00000000+0.j        ]
[ 0.00265679+0.0245355j   0.00000000+0.j          0.24585391+0.j
0.01358542+0.04868103j  0.21507082+0.04053822j]
[ 0.09885662+0.01638816j  0.00000000+0.j          0.01358542-0.04868103j
0.43862274+0.j          0.01799108+0.05080967j]
[ 0.00000000+0.j          0.00000000+0.j          0.21507082-0.04053822j
0.01799108-0.05080967j  0.26659348+0.j        ]]


has roughly half nonzero elements, or equivalently a density of 0.5.

Warning

In the case of a density matrix, setting the density too low will result in not enough diagonal elements to satisfy $$Tr(\rho)=1$$.

## Composite random objects¶

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 [4]: rand_dm(4, 0.5, dims=[[2,2], [2,2]])
Out[4]:
Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isherm = True
Qobj data =
[[ 0.30122934  0.          0.          0.        ]
[ 0.          0.          0.          0.        ]
[ 0.          0.          0.34938533  0.        ]
[ 0.          0.          0.          0.34938533]]