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)
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[ 0.00000000+0.j         -0.61443139+0.5565611j   0.00000000+0.j
  -0.07695716+0.33797916j  0.00000000+0.j        ]
 [-0.61443139-0.5565611j   0.00000000+0.j         -0.58658376+0.47851684j
   0.00000000+0.j          0.29087032-0.04403322j]
 [ 0.00000000+0.j         -0.58658376-0.47851684j -1.48253672+0.j
   0.14821531-0.00900551j  0.21133530-0.4525358j ]
 [-0.07695716-0.33797916j  0.00000000+0.j          0.14821531+0.00900551j
   0.00000000+0.j          0.08601466-0.31579196j]
 [ 0.00000000+0.j          0.29087032+0.04403322j  0.21133530+0.4525358j
   0.08601466+0.31579196j  0.00000000+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-aef77a269b21>", 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-fbc42bdd7e86>", 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)
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[ 0.45350481+0.j          0.00000000+0.j         -0.01385138-0.08093636j
  -0.02190011+0.03272436j  0.00000000+0.j        ]
 [ 0.00000000+0.j          0.29181490+0.j          0.00000000+0.j
   0.21426922-0.0356683j   0.00000000+0.j        ]
 [-0.01385138+0.08093636j  0.00000000+0.j          0.04170053+0.j
  -0.01450458-0.01376581j  0.00000000+0.j        ]
 [-0.02190011-0.03272436j  0.21426922+0.0356683j  -0.01450458+0.01376581j
   0.21297977+0.j          0.00000000+0.j        ]
 [ 0.00000000+0.j          0.00000000+0.j          0.00000000+0.j
   0.00000000+0.j          0.00000000+0.j        ]]

In [5]: rand_dm_ginibre(5, rank=2)
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[ 0.14066942 -1.01991394e-18j -0.08005758 +2.58808924e-02j
   0.11820086 +1.53135504e-01j  0.00792885 +1.49036180e-02j
   0.02169210 +2.07400940e-01j]
 [-0.08005758 -2.58808924e-02j  0.08398545 -1.22838721e-18j
  -0.00748508 -7.37014680e-02j  0.05206634 -8.96589039e-03j
   0.06426153 -1.29411293e-01j]
 [ 0.11820086 -1.53135504e-01j -0.00748508 +7.37014680e-02j
   0.33251664 -3.12769705e-18j  0.07446297 -5.14868086e-02j
   0.27239256 +1.03521226e-01j]
 [ 0.00792885 -1.49036180e-02j  0.05206634 +8.96589039e-03j
   0.07446297 +5.14868086e-02j  0.08815831 +2.19682196e-18j
   0.08447533 -3.53205371e-03j]
 [ 0.02169210 -2.07400940e-01j  0.06426153 +1.29411293e-01j
   0.27239256 -1.03521226e-01j  0.08447533 +3.53205371e-03j
   0.35467018 +3.17917623e-18j]]

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


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


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
Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = True
Qobj data =
[[ 1.25000000 +2.77555756e-17j -0.25000000 -2.77555756e-17j
  -1.02918489 +2.56473139e-01j  0.00000000 +0.00000000e+00j
   0.14115753 +4.79660870e-01j]
 [-0.25000000 -2.77555756e-17j  1.25000000 +2.77555756e-17j
   1.02918489 -2.56473139e-01j  0.00000000 +0.00000000e+00j
  -0.14115753 -4.79660870e-01j]
 [-1.02918489 -2.56473139e-01j  1.02918489 +2.56473139e-01j
   1.50000000 +0.00000000e+00j  0.00000000 +0.00000000e+00j
  -0.02967610 -7.06483779e-01j]
 [ 0.00000000 +0.00000000e+00j  0.00000000 +0.00000000e+00j
   0.00000000 +0.00000000e+00j  3.00000000 +0.00000000e+00j
   0.00000000 +0.00000000e+00j]
 [ 0.14115753 -4.79660870e-01j -0.14115753 +4.79660870e-01j
  -0.02967610 +7.06483779e-01j  0.00000000 +0.00000000e+00j
   3.00000000 +0.00000000e+00j]]

In [9]: H.eigenenergies()
array([  2.36825980e-16,   1.00000000e+00,   2.00000000e+00,
         3.00000000e+00,   4.00000000e+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]])
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.67661788+0.j          0.15116885-0.27120685j
 [ 0.00000000+0.j          0.15116885+0.27120685j  0.26754373+0.j
 [ 0.00000000+0.j         -0.07200880-0.09761737j -0.05378799-0.05057372j
   0.05583839+0.j        ]]