# RandomDensityMatrix

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 Other toolboxes required RandomDensityMatrix Generates a random density matrix none RandomStateVectorRandomSuperoperatorRandomUnitary Random things

RandomDensityMatrix is a function that generates a random density matrix, uniformly according to the Hilbert-Schmidt measure (equivalently, by generating a pure state according to Haar measure on a larger system and then tracing out the ancillary space) or the Bures measure.

## Syntax

• RHO = RandomDensityMatrix(DIM)
• RHO = RandomDensityMatrix(DIM,RE)
• RHO = RandomDensityMatrix(DIM,RE,K)
• RHO = RandomDensityMatrix(DIM,RE,K,DIST)

## Argument descriptions

• DIM: The number of rows (or equivalently, columns) that RHO will have.
• RE (optional, default 0): A flag (either 0 or 1) indicating that RHO should only have real entries (RE = 1) or that it is allowed to have complex entries (RE = 0).
• K (optional, default DIM): The maximal rank of the density matrix to be produced. With probability 1, rank(RHO) = K (if K ≤ DIM).
• DIST (optional, default 'haar'): A string indicating the desired distribution that RHO should be chosen from. It can take on one of three values:
• 'haar' or 'hs': The density matrix is generated by generating a Haar-uniform pure state in $\mathbb{C}^K \otimes \mathbb{C}^{DIM}$ and then tracing out the first subsystem. In the special case when K = DIM, this is sometimes called the Hilbert-Schmidt measure.
• 'bures': The Bures measure.

## Examples

### Random mixed qubits

The following code generates a random mixed state on a 2-level system:

>> rho = RandomDensityMatrix(2)

rho =

0.1187            -0.0728 + 0.0409i
-0.0728 - 0.0409i   0.8813

We can verify that this is indeed a valid density matrix as follows:

>> trace(rho)

ans =

1

>> IsPSD(rho)

ans =

1

The following code generates a density matrix with all real entries, chosen according to the Bures measure:

>> RandomDensityMatrix(2,1,2,'bures')

ans =

0.1578    0.2259
0.2259    0.8422

### A larger example of specified rank

To generate a 6-by-6 density matrix with rank at most 4, you could use the following code:

>> rho = RandomDensityMatrix(6,0,4)

rho =

0.1750            -0.0299 - 0.0103i  -0.0304 - 0.0668i   0.0108 - 0.0176i  -0.0294 - 0.0796i  -0.0026 + 0.0705i
-0.0299 + 0.0103i   0.1461            -0.0483 + 0.0490i   0.0406 + 0.0422i  -0.0064 + 0.1005i   0.0461 + 0.0225i
-0.0304 + 0.0668i  -0.0483 - 0.0490i   0.1896            -0.0010 + 0.0652i   0.0156 + 0.0388i  -0.0610 - 0.0002i
0.0108 + 0.0176i   0.0406 - 0.0422i  -0.0010 - 0.0652i   0.1332             0.1221 + 0.0212i  -0.0023 + 0.0264i
-0.0294 + 0.0796i  -0.0064 - 0.1005i   0.0156 - 0.0388i   0.1221 - 0.0212i   0.2355            -0.0381 - 0.0789i
-0.0026 - 0.0705i   0.0461 - 0.0225i  -0.0610 + 0.0002i  -0.0023 - 0.0264i  -0.0381 + 0.0789i   0.1206

>> rank(rho)

ans =

4

### Purity of random density matrices

It is known that the expected purity of a random $n \times n$ density matrix, generated according to Haar measure with an ancillary space of dimension $m$, is $(n+m)/(nm+1)$. We can verify this numerically as follows:

>> n = 3; m = 7;
>> ct = 0; s = 100000;
>> for j = 1:s
ct = ct + Purity(RandomDensityMatrix(n,0,m));
end
ct/s % this is the numerically-generated average purity

ans =

0.4546

>> (n+m)/(n*m+1)

ans =

0.4545

## Source code

Click on "expand" to the right to view the MATLAB source code for this function.

1. %%  RANDOMDENSITYMATRIX    Generates a random density matrix
2. %   This function has one required argument:
3. %     DIM: the number of rows (and columns) of the density matrix
4. %
5. %   RHO = RandomDensityMatrix(DIM) generates a random DIM-by-DIM density
6. %   matrix, distributed according to the Hilbert-Schmidt measure.
7. %
8. %   This function has three optional arguments:
9. %     RE (default 0)
10. %     K (default DIM)
11. %     DIST (default 'haar')
12. %
13. %   RHO = RandomDensityMatrix(DIM,RE,K,DIST) generates a random density
14. %   matrix of rank <= K, distributed according to the distribution DIST. If
15. %   RE = 1 then all of its entries will be real. DIST must be one of:
16. %     'haar' or 'hs' (default) - Generate a larger  pure state according to
17. %                      Haar measure and trace out the extra dimensions.
18. %                      Sometimes called the Hilbert-Schmidt measure when
19. %                      K = DIM.
20. %     'bures'        - the Bures measure
21. %
22. %   URL: http://www.qetlab.com/RandomDensityMatrix
23. 
24. %   requires: opt_args.m, RandomUnitary.m
25. %
26. %   author: Nathaniel Johnston (nathaniel@njohnston.ca)
27. %   package: QETLAB
28. %   last updated: October 10, 2014
29. 
30. function rho = RandomDensityMatrix(dim,varargin)
31. 
32. % set optional argument defaults: re=0, k=dim, dist='hs'
33. [re,k,dist] = opt_args({ 0, dim, 'haar' },varargin{:});
34. 
35. % Haar/Hilbert-Schmidt measure
36. gin = randn(dim,k);
37. if(~re)
38.     gin = gin + 1i*randn(dim,k);
39. end
40. if(strcmpi(dist,'bures')) % Bures measure
41.     gin = (RandomUnitary(dim,re) + eye(dim))*gin;
42. end
43. 
44. rho = gin*gin';
45. rho = rho/trace(rho);