|
2 | 2 |
|
3 | 3 | from numbers import Complex, Real
|
4 | 4 | from typing import TYPE_CHECKING
|
| 5 | +from qiskit import quantum_info |
5 | 6 |
|
| 7 | +from scipy.linalg import sqrtm, inv |
| 8 | +import math |
6 | 9 | import numpy as np
|
7 | 10 | import numpy.typing as npt
|
8 | 11 | import sympy as sp
|
@@ -182,3 +185,63 @@ def exp(angle: Expr | Complex) -> sp.Expr | complex:
|
182 | 185 | res = sp.exp(angle)
|
183 | 186 | assert isinstance(res, Expr)
|
184 | 187 | return res
|
| 188 | + |
| 189 | + |
| 190 | +def rand_orthogonal_matrix_seed(size: int, seed: int) -> Matrix: |
| 191 | + """Generate a random orthogonal matrix with a given seed. |
| 192 | +
|
| 193 | + Args: |
| 194 | + size: Size (number of columns, or rows) of the squared matrix to generate. |
| 195 | + seed: Seed used to control the random generation of the matrix. |
| 196 | +
|
| 197 | + Returns: |
| 198 | + A random orthogonal Matrix. |
| 199 | + """ |
| 200 | + # TODO: to example |
| 201 | + np.random.seed(seed) |
| 202 | + m = np.random.rand(size, size) |
| 203 | + return m.dot(inv(sqrtm(m.T.dot(m)))) |
| 204 | + |
| 205 | + |
| 206 | +def rand_orthogonal_matrix(size) -> Matrix: |
| 207 | + """Generate a random orthogonal matrix without a given seed""" |
| 208 | + # TODO: to comment + examples |
| 209 | + m = np.random.rand(size, size) |
| 210 | + return m.dot(inv(sqrtm(m.T.dot(m)))) |
| 211 | + |
| 212 | + |
| 213 | +def rand_clifford_matrix(nb_qubits) -> Matrix: |
| 214 | + """Generate a random Clifford matrix""" |
| 215 | + # TODO: to comment + examples |
| 216 | + return quantum_info.random_clifford(nb_qubits).to_matrix() |
| 217 | + |
| 218 | + |
| 219 | +def rand_unitary_2x2_matrix() -> Matrix: |
| 220 | + """Generate a random one-qubit unitary matrix""" |
| 221 | + # TODO: to comment + examples |
| 222 | + angles = np.random.rand(3)*2*math.pi |
| 223 | + m = np.array([[np.cos(angles[0]/2), -np.exp(angles[2]*1j)*np.sin(angles[0]/2)], |
| 224 | + [np.exp(angles[1]*1j)*np.sin(angles[0]/2), np.exp((angles[1]+angles[2])*1j)*math.cos(angles[0]/2)]]) |
| 225 | + return m |
| 226 | + |
| 227 | + |
| 228 | +def rand_product_local_unitaries(nb_qubits) -> Matrix: |
| 229 | + """Generate a random tensor product of unitary matrices""" |
| 230 | + # TODO: to comment + examples |
| 231 | + matrix = rand_unitary_2x2_matrix() |
| 232 | + for _ in range(nb_qubits-1): |
| 233 | + matrix = np.kron(matrix, rand_unitary_2x2_matrix()) |
| 234 | + return matrix |
| 235 | + |
| 236 | + |
| 237 | +def rand_hermitian_matrix(size) -> Matrix: |
| 238 | + """Generate a random Hermitian matrix. |
| 239 | +
|
| 240 | + Args: |
| 241 | + size: Size (number of columns, or rows) of the squared matrix to generate. |
| 242 | +
|
| 243 | + Returns: |
| 244 | + A random orthogonal Matrix.""" |
| 245 | + # TODO: to comment + examples |
| 246 | + m = np.random.rand(size, size) |
| 247 | + return m + m.conjugate().transpose() |
0 commit comments