Skip to content

Commit b7ec7ca

Browse files
Adding random matrix generators in math
1 parent f1b7d2e commit b7ec7ca

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

mpqp/tools/maths.py

+63
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
from numbers import Complex, Real
44
from typing import TYPE_CHECKING
5+
from qiskit import quantum_info
56

7+
from scipy.linalg import sqrtm, inv
8+
import math
69
import numpy as np
710
import numpy.typing as npt
811
import sympy as sp
@@ -182,3 +185,63 @@ def exp(angle: Expr | Complex) -> sp.Expr | complex:
182185
res = sp.exp(angle)
183186
assert isinstance(res, Expr)
184187
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

Comments
 (0)