Generating Random Quantum States & Operators

QuTiP includes a collection of random state, unitary and channel generators for simulations, Monte Carlo evaluation, theorem evaluation, and code testing. Each of these objects can be sampled from one of several different distributions including the default distributions used by QuTiP versions prior to 3.2.0.

For example, a random Hermitian operator can be sampled by calling rand_herm function:

In [1]: rand_herm(5)
Out[1]: 
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[ 0.        +0.j          0.        +0.j         -0.07903194+0.04842048j
  -0.08160606+0.47629143j  0.        +0.j        ]
 [ 0.        +0.j          0.        +0.j          0.        +0.j
  -0.1919832 -0.22178051j  0.28831061-0.27313483j]
 [-0.07903194-0.04842048j  0.        +0.j          0.        +0.j
   0.42183113+0.00672673j  0.81327946-0.46667892j]
 [-0.08160606-0.47629143j -0.1919832 +0.22178051j  0.42183113-0.00672673j
   0.        +0.j          0.32721563-0.41352091j]
 [ 0.        +0.j          0.28831061+0.27313483j  0.81327946+0.46667892j
   0.32721563+0.41352091j  0.8523009 +0.j        ]]
Random Variable Type Sampling Functions Dimensions
State vector (ket) rand_ket, rand_ket_haar \(N \times 1\)
Hermitian operator (oper) rand_herm \(N \times 1\)
Density operator (oper) rand_dm, rand_dm_hs, rand_dm_ginibre \(N \times N\)
Unitary operator (oper) rand_unitary, rand_unitary_haar \(N \times N\)
CPTP channel (super) rand_super, rand_super_bcsz \((N \times N) \times (N \times N)\)

In all cases, these functions can be called with a single parameter \(N\) that dimension of the relevant Hilbert space. The optional dims keyword argument allows for the dimensions of a random state, unitary or channel to be broken down into subsystems.

In [2]: print rand_super_bcsz(7).dims
  File "<ipython-input-2-ab0324dc4f28>", line 1
    print rand_super_bcsz(7).dims
                        ^
SyntaxError: invalid syntax


In [3]: print rand_super_bcsz(6, dims=[[[2, 3], [2, 3]], [[2, 3], [2, 3]]]).dims
  File "<ipython-input-3-8f96edcf3e41>", line 1
    print rand_super_bcsz(6, dims=[[[2, 3], [2, 3]], [[2, 3], [2, 3]]]).dims
                        ^
SyntaxError: invalid syntax

Several of the distributions supported by QuTiP support additional parameters as well, namely density and rank. In particular, the rand_herm and rand_dm functions return quantum objects such that a fraction of the elements are identically equal to zero. The ratio of nonzero elements is passed as the density keyword argument. By contrast, the rand_dm_ginibre and rand_super_bcsz take as an argument the rank of the generated object, such that passing rank=1 returns a random pure state or unitary channel, respectively. Passing rank=None specifies that the generated object should be full-rank for the given dimension.

For example,

In [4]: rand_dm(5, density=0.5)
Out[4]: 
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[ 0.33237233+0.00000000e+00j -0.09742443+1.08525997e-01j
  -0.03553965-2.49193515e-03j  0.        +0.00000000e+00j
   0.00082278-4.71484356e-05j]
 [-0.09742443-1.08525997e-01j  0.17294646+0.00000000e+00j
   0.        +0.00000000e+00j  0.        +0.00000000e+00j
  -0.00722601+2.40657451e-02j]
 [-0.03553965+2.49193515e-03j  0.        +0.00000000e+00j
   0.12194729+0.00000000e+00j  0.        +0.00000000e+00j
   0.0538316 -9.93161907e-02j]
 [ 0.        +0.00000000e+00j  0.        +0.00000000e+00j
   0.        +0.00000000e+00j  0.08777622+0.00000000e+00j
   0.        +0.00000000e+00j]
 [ 0.00082278+4.71484356e-05j -0.00722601-2.40657451e-02j
   0.0538316 +9.93161907e-02j  0.        +0.00000000e+00j
   0.2849577 +0.00000000e+00j]]

In [5]: rand_dm_ginibre(5, rank=2)
Out[5]: 
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[ 0.34309675+2.44144807e-18j  0.07346975-2.32328854e-01j
  -0.04735553+3.72039905e-02j -0.05386331+2.00854083e-01j
  -0.16468943+2.12369480e-01j]
 [ 0.07346975+2.32328854e-01j  0.21841733-1.95734735e-18j
  -0.07951442-6.62245543e-02j -0.13828712+1.07511750e-02j
  -0.19571393-7.22139583e-02j]
 [-0.04735553-3.72039905e-02j -0.07951442+6.62245543e-02j
   0.09271802+8.16276342e-19j  0.01628573-1.73915538e-02j
   0.06769714-2.08975658e-02j]
 [-0.05386331-2.00854083e-01j -0.13828712-1.07511750e-02j
   0.01628573+1.73915538e-02j  0.12831929+6.51010658e-19j
   0.14621042+6.33585921e-02j]
 [-0.16468943-2.12369480e-01j -0.19571393+7.22139583e-02j
   0.06769714+2.08975658e-02j  0.14621042-6.33585921e-02j
   0.21744861-1.95138772e-18j]]

See the API documentation: Random Operators and States for details.

Warning

When using the density keyword argument, setting the density too low may result in not enough diagonal elements to satisfy trace constraints.

Random objects with a given eigen spectrum

Note

New in QuTiP 3.2

It is also possible to generate random Hamiltonian (rand_herm) and densitiy matrices (rand_dm) with a given eigen spectrum. This is done by passing an array of eigenvalues as the first argument to either function. For example,

In [6]: eigs = np.arange(5)

In [7]: H = rand_herm(eigs, density=0.5)

In [8]: H
Out[8]: 
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[ 2.06577169-1.38777878e-17j  0.        +0.00000000e+00j
   0.1134664 -6.97943676e-01j -1.06577169-1.38777878e-17j
  -0.23590054+3.83509247e-02j]
 [ 0.        +0.00000000e+00j  1.        +0.00000000e+00j
   0.        +0.00000000e+00j  0.        +0.00000000e+00j
   0.        +0.00000000e+00j]
 [ 0.1134664 +6.97943676e-01j  0.        +0.00000000e+00j
   1.        +0.00000000e+00j  0.1134664 +6.97943676e-01j
   0.        +0.00000000e+00j]
 [-1.06577169-1.38777878e-17j  0.        +0.00000000e+00j
   0.1134664 -6.97943676e-01j  2.06577169-1.38777878e-17j
   0.23590054-3.83509247e-02j]
 [-0.23590054-3.83509247e-02j  0.        +0.00000000e+00j
   0.        +0.00000000e+00j  0.23590054+3.83509247e-02j
   3.86845663+0.00000000e+00j]]

In [9]: H.eigenenergies()
Out[9]: 
array([-3.1918912e-16,  1.0000000e+00,  2.0000000e+00,  3.0000000e+00,
        4.0000000e+00])

In order to generate a random object with a given spectrum QuTiP applies a series of random complex Jacobi rotations. This technique requires many steps to build the desired quantum object, and is thus suitable only for objects with Hilbert dimensionality \(\lesssim 1000\).

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 [10]: rand_dm(4, 0.5, dims=[[2,2], [2,2]])
Out[10]: 
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = True
Qobj data =
[[0.5      +0.j         0.       +0.j         0.       +0.j
  0.       +0.j        ]
 [0.       +0.j         0.       +0.j         0.       +0.j
  0.       +0.j        ]
 [0.       +0.j         0.       +0.j         0.1804389+0.j
  0.074003 -0.22843995j]
 [0.       +0.j         0.       +0.j         0.074003 +0.22843995j
  0.3195611+0.j        ]]