# Pure to mixed

 Other toolboxes required pure_to_mixed Converts a state vector or density matrix representation of a state to a density matrix none Helper functions
 This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function.

pure_to_mixed is a function that convert's a state's current representation (either as a pure state vector or as a density matrix) into a density matrix. This function just exists to make the code within some other functions a bit cleaner.

## Syntax

• RHO = pure_to_mixed(PHI)

## Argument descriptions

• PHI: A density matrix or a pure state vector.

## Examples

The following code shows that if v is a pure state vector then pure_to_mixed(v) == v*v', yet if rho is a density matrix then pure_to_mixed(rho) == rho.

```>> v = RandomStateVector(2)

v =

0.6732 - 0.5736i
0.3190 + 0.3407i

>> pure_to_mixed(v)

ans =

0.7822             0.0193 - 0.4123i
0.0193 + 0.4123i   0.2178

>> pure_to_mixed(v*v')

ans =

0.7822             0.0193 - 0.4123i
0.0193 + 0.4123i   0.2178

>> v*v'

ans =

0.7822             0.0193 - 0.4123i
0.0193 + 0.4123i   0.2178```

## Source code

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

1. `%%  pure_to_mixed    Converts a state vector or density matrix representation of a state to a density matrix`
2. `%   This function has one required argument:`
3. `%     PHI: a density matrix or a pure state vector`
4. `%`
5. `%   RHO = pure_to_mixed(PHI) is a density matrix representation of PHI,`
6. `%   regardless of whether PHI is itself already a density matrix, or if it`
7. `%   is a pure state vector.`
8. `%`
9. `%   URL: http://www.qetlab.com/pure_to_mixed`
10. ` `
11. `%   requires: nothing`
12. `%   author: Nathaniel Johnston (nathaniel@njohnston.ca)`
13. `%   package: QETLAB`
14. `%   last updated: January 12, 2016`
15. ` `
16. `function rho = pure_to_mixed(phi)`
17. ` `
18. `% Compute the size of phi. If it's already a mixed state, leave it alone.`
19. `% If it's a vector (pure state), make it into a density matrix.`
20. `[m,n] = size(phi);`
21. ` `
22. `if(min(m,n) == 1) % it's a pure state vector`
23. `    phi = phi(:);`
24. `    rho = phi*phi';`
25. `elseif(m == n) % it's a density matrix`
26. `    rho = phi;`
27. `else % it's neither`
28. `    error('pure_to_mixed:InvalidDimensions','PHI must be either a vector or a square matrix.');`
29. `end`