rand.random_unitary¶
Generates a random unitary matrix.
Functions¶
|
Generate a random unitary or orthogonal matrix [1]. |
Module Contents¶
- rand.random_unitary.random_unitary(dim, is_real=False, seed=None)¶
Generate a random unitary or orthogonal matrix [1].
Calculates a random unitary matrix (if
is_real = False
) or a random real orthogonal matrix (ifis_real = True
), uniformly distributed according to the Haar measure.Examples
We may generate a random unitary matrix. Here is an example of how we may be able to generate a random \(2\)-dimensional random unitary matrix with complex entries.
from toqito.rand import random_unitary complex_dm = random_unitary(2) complex_dm
array([[ 0.6608344 +5.63273539e-01j, -0.34638359+3.55020022e-01j], [ 0.49600438+6.79987312e-04j, 0.05951131-8.66278012e-01j]])
We can verify that this is in fact a valid unitary matrix using the
is_unitary
function from|toqito⟩
as followsfrom toqito.matrix_props import is_unitary is_unitary(complex_dm)
True
We can also generate random unitary matrices that are real-valued as follows.
from toqito.rand import random_unitary real_dm = random_unitary(2, True) real_dm
array([[ 0.43486567, -0.90049534], [ 0.90049534, 0.43486567]])
Again, verifying that this is a valid unitary matrix can be done as follows.
from toqito.matrix_props import is_unitary is_unitary(real_dm)
True
We may also generate unitaries such that the dimension argument provided is a
list
as opposed to anint
. Here is an example of a random unitary matrix of dimension \(4\).from toqito.rand import random_unitary mat = random_unitary([4, 4], True) mat
array([[ 0.40957691, -0.7230892 , -0.11411148, 0.5443963 ], [-0.41107949, -0.63668853, -0.27139085, -0.59328608], [ 0.25113465, 0.26468702, -0.93049525, -0.03241539], [ 0.77472046, -0.0413582 , 0.2179545 , -0.5921094 ]])
As before, we can verify that this matrix generated is a valid unitary matrix.
from toqito.matrix_props import is_unitary is_unitary(mat)
True
It is also possible to pass a seed to this function for reproducibility.
from toqito.matrix_props import is_unitary seeded = random_unitary(2, seed=42) seeded
array([[ 0.14398279-0.92188954j, -0.05864249+0.35489392j], [ 0.35459797+0.06040626j, 0.91839541+0.16480666j]])
And once again, we can verify that this matrix generated is a valid unitary matrix.
from toqito.matrix_props import is_unitary is_unitary(seeded)
True
References
- Parameters:
dim (list[int] | int) – The number of rows (and columns) of the unitary matrix.
is_real (bool) – Boolean denoting whether the returned matrix has real entries or not. Default is
False
.seed (int | None) – A seed used to instantiate numpy’s random number generator.
- Returns:
A
dim
-by-dim
random unitary matrix.- Return type:
numpy.ndarray