-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrices.py
More file actions
27 lines (23 loc) · 888 Bytes
/
Copy pathmatrices.py
File metadata and controls
27 lines (23 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np
def get_random_array(n: int) -> np.ndarray:
return np.random.randn(n, n) + 1j * np.random.randn(n, n)
class RandomMatrixBuilder:
@staticmethod
def build(n: int) -> np.ndarray:
return get_random_array(n)
class DiagonalMatrixBuilder:
@staticmethod
def build(n: int, diag_values: np.ndarray | None = None) -> np.ndarray:
if diag_values is None:
diag_values = np.random.randn(n) + 1j * np.random.randn(n)
return np.diag(diag_values)
class UnitaryMatrixBuilder:
@staticmethod
def build(n: int) -> np.ndarray:
# Generate a random complex matrix and perform QR decomposition
z = get_random_array(n)
q, r = np.linalg.qr(z)
# Ensure the diagonal of R has positive real parts to get a unitary matrix
d = np.diag(r)
ph = d / np.abs(d)
return q * ph