Skip to content

Commit 2cd31e3

Browse files
committed
random_unitary, random_regular
1 parent 0c1e4f6 commit 2cd31e3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/generate.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::ops::*;
66

77
use super::convert::*;
88
use super::error::*;
9+
use super::qr::*;
910
use super::types::*;
1011

1112
/// Hermite conjugate matrix
@@ -34,6 +35,29 @@ where
3435
ArrayBase::from_shape_fn(sh, |_| A::randn(&mut rng))
3536
}
3637

38+
/// Generate random unitary matrix using QR decomposition
39+
pub fn random_unitary<A>(n: usize) -> Array2<A>
40+
where
41+
A: Scalar + RandNormal,
42+
{
43+
let a: Array2<A> = random((n, n));
44+
let (q, _r) = a.qr_into().unwrap();
45+
q
46+
}
47+
48+
/// Generate random regular matrix
49+
pub fn random_regular<A>(n: usize) -> Array2<A>
50+
where
51+
A: Scalar + RandNormal,
52+
{
53+
let a: Array2<A> = random((n, n));
54+
let (q, mut r) = a.qr_into().unwrap();
55+
for i in 0..n {
56+
r[(i, i)] = A::from_f64(1.0) + AssociatedReal::inject(r[(i, i)].abs());
57+
}
58+
q.dot(&r)
59+
}
60+
3761
/// Random Hermite matrix
3862
pub fn random_hermite<A, S>(n: usize) -> ArrayBase<S, Ix2>
3963
where

0 commit comments

Comments
 (0)