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 [2]: rand_ket(5)
 Out[2]: 
Quantum object: dims = [[5], [1]], shape = [5, 1], type = ket
Qobj data =
[[-0.62994785+0.1389789j ]
 [-0.43288801+0.01284981j]
 [-0.22400352-0.20321684j]
 [-0.47922421-0.22806793j]
 [-0.11912553-0.09464168j]]

or

In [3]: rand_herm(5)
 Out[3]: 
Quantum object: dims = [[5], [5]], shape = [5, 5], type = oper, isherm = True
Qobj data =
[[-0.58420310+0.j         -0.43073690+0.2344248j   0.00000000+0.j
  -0.27393962+0.12764388j -0.48896580+0.09691766j]
 [-0.43073690-0.2344248j  -0.52347888+0.j         -0.13899371-0.06069369j
  -0.35374472-0.22620005j  0.00000000+0.j        ]
 [ 0.00000000+0.j         -0.13899371+0.06069369j -0.16089832+0.j
  -0.65111568+0.25401902j -0.30001540-0.14135167j]
 [-0.27393962-0.12764388j -0.35374472+0.22620005j -0.65111568-0.25401902j
  -0.56096362+0.j         -0.48167388+0.23237517j]
 [-0.48896580-0.09691766j  0.00000000+0.j         -0.30001540+0.14135167j
  -0.48167388-0.23237517j  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 [4]: rand_dm(5, 0.5)
 Out[4]: 
Quantum object: dims = [[5], [5]], shape = [5, 5], 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.00000000+0.j          0.32141161+0.j          0.00000000+0.j
   0.19802585-0.11734087j  0.00000000+0.j        ]
 [ 0.00000000+0.j          0.00000000+0.j          0.01374332+0.j
   0.00000000+0.j          0.01897778+0.04365046j]
 [ 0.00000000+0.j          0.19802585+0.11734087j  0.00000000+0.j
   0.17858839+0.j          0.00000000+0.j        ]
 [ 0.00000000+0.j          0.00000000+0.j          0.01897778-0.04365046j
   0.00000000+0.j          0.48625668+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\).

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 [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.39065157  0.          0.          0.        ]
 [ 0.          0.39065157  0.          0.        ]
 [ 0.          0.          0.10934843  0.        ]
 [ 0.          0.          0.          0.10934843]]