# RandomStateVector

 Other toolboxes required RandomStateVector Generates a random pure state vector none RandomDensityMatrixRandomProbabilitiesRandomSuperoperatorRandomUnitary Random things

RandomStateVector is a function that generates a random pure state vector, uniformly distributed on the unit hypersphere (sometimes said to be uniformly distributed according to Haar measure).

## Syntax

• V = RandomStateVector(DIM)
• V = RandomStateVector(DIM,RE)
• V = RandomStateVector(DIM,RE,K)

## Argument descriptions

• DIM: The dimension of the Hilbert space in which V lives. If K > 0 (see optional arguments below) then DIM is the local dimension rather than the total dimension. If different local dimensions are desired, DIM should be a 1-by-2 vector containing the desired local dimensions.
• RE (optional, default 0): A flag (either 0 or 1) indicating that V should only have real entries (RE = 1) or that it is allowed to have complex entries (RE = 0).
• K (optional, default 0): If equal to 0 then V will be generated without considering its Schmidt rank. If K > 0 then a random pure state with Schmidt rank ≤ K will be generated (and with probability 1, its Schmidt rank will equal K). Note that when K = 1 the states on the two subsystems are generated uniformly and independently according to Haar measure on those subsystems. When K = DIM, the usual Haar measure on the total space is used. When 1 < K < DIM, a natural measure that interpolates between these two extremes is used (more specifically, the direct sum of the left (similarly, right) Schmidt vectors is chosen according to Haar measure on $\mathbb{C}^K \otimes \mathbb{C}^{DIM}$).

## Examples

### A random qubit

To generate a random qubit, use the following code:

>> RandomStateVector(2)

ans =

-0.1025 - 0.5498i
-0.5518 + 0.6186i

If you want it to only have real entries, set RE = 1:

>> RandomStateVector(2,1)

ans =

-0.4487
0.8937

### Random states with fixed Schmidt rank

To generate a random product qutrit-qutrit state and verify that it is indeed a product state, use the following code:

>> v = RandomStateVector(3,0,1)

v =

0.0400 - 0.3648i
0.1169 - 0.0666i
0.0465 + 0.0016i
-0.1910 + 0.0524i
-0.0566 - 0.0455i
-0.0084 - 0.0236i
-0.4407 + 0.7079i
-0.3050 + 0.0214i
-0.0936 - 0.0489i

>> SchmidtRank(v)

ans =

1

You could create a random pure state with Schmidt rank 2 in $\mathbb{C}^3 \otimes \mathbb{C}^4$, and verify its Schmidt rank, using the following lines of code:

>> v = RandomStateVector([3,4],0,2)

v =

-0.2374 + 0.1984i
0.1643 + 0.0299i
-0.0499 + 0.0376i
-0.0689 - 0.0005i
0.7740 - 0.0448i
-0.1290 - 0.2224i
-0.0514 - 0.1565i
0.2195 + 0.2478i
-0.1636 + 0.1276i
0.0581 + 0.0608i
0.0482 - 0.0178i
-0.1050 + 0.0014i

>> SchmidtRank(v,[3,4])

ans =

2

## Source code

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

1. %%  RANDOMSTATEVECTOR   Generates a random pure state vector
2. %   This function has one required argument:
3. %     DIM: the dimension of the Hilbert space that the pure state lives in
4. %
5. %   V = RandomStateVector(DIM) generates a DIM-dimensional state vector,
6. %   uniformly distributed on the (DIM-1)-sphere. Equivalently, these pure
7. %   states are uniformly distributed according to Haar measure.
8. %
9. %   This function has two optional input arguments:
10. %     RE (default 0)
11. %     K (default 0)
12. %
13. %   V = RandomStateVector(DIM,RE,K) generates a random pure state vector as
14. %   above. If RE=1 then all coordinates of V will be real. If K=0 then a
15. %   pure state is generated without considering its Schmidt rank at all. If
16. %   K>0 then a random bipartite pure state with Schmidt rank <= K is
17. %   generated (and with probability 1, the Schmidt rank will equal K). If
18. %   K>0 then DIM is no longer the dimension of the space on which V lives,
19. %   but rather is the dimension of the *local* systems on which V lives. If
20. %   these two systems have unequal dimension, you can specify them both by
21. %   making DIM a 1-by-2 vector containing the two dimensions.
22. %
23. %   URL: http://www.qetlab.com/RandomStateVector
24. 
25. %   requires: iden.m MaxEntangled.m, opt_args.m, PermuteSystems.m, Swap.m
26. %   author: Nathaniel Johnston (nathaniel@njohnston.ca)
27. %   package: QETLAB
28. %   last updated: November 12, 2014
29. 
30. function v = RandomStateVector(dim,varargin)
31. 
32. % set optional argument defaults: re=0, k=0
33. [re,k] = opt_args({ 0, 0 },varargin{:});
34. 
35. if(k > 0 && k < min(dim)) % Schmidt rank plays a role
36.     % allow the user to enter a single number for dim
37.     if(length(dim) == 1)
38.         dim = [dim,dim];
39.     end
40. 
41.     % if you start with a separable state on a larger space and multiply
42.     % the extra k dimensions by a maximally entangled state, you get a
43.     % Schmidt rank <= k state
44.     psi = MaxEntangled(k,1,0);
45.     a = randn(dim(1)*k,1);
46.     b = randn(dim(2)*k,1);
47.     if(~re)
48.         a = a + 1i*randn(dim(1)*k,1);
49.         b = b + 1i*randn(dim(2)*k,1);
50.     end
51.     v = kron(psi',speye(prod(dim)))*Swap(kron(a,b),[2,3],[k,dim(1),k,dim(2)]);
52.     v = v/norm(v);
53. else % Schmidt rank is full, so ignore it
54.     v = randn(dim,1);
55.     if(~re)
56.         v = v + 1i*randn(dim,1);
57.     end
58.     v = v/norm(v);
59. end