Skip to content

Commit d62400a

Browse files
committed
Merge branch 'master' of https://github.com/vprusso/toqito
2 parents 886222c + 5812b50 commit d62400a

18 files changed

+510
-204
lines changed

poetry.lock

Lines changed: 1 addition & 173 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ numpy = "2.1.2"
2020
scipy = "1.14.1"
2121
scs = "3.2.7"
2222
picos = "2.4.17"
23-
qiskit = "1.2.4"
2423

2524

2625
[tool.poetry.group.dev.dependencies]

toqito/rand/random_circulant_gram_matrix.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44

55

6-
def random_circulant_gram_matrix(dim: int) -> np.ndarray:
6+
def random_circulant_gram_matrix(dim: int, seed: int | None = None) -> np.ndarray:
77
r"""Generate a random circulant Gram matrix of specified dimension.
88
99
A circulant matrix is a square matrix where the elements of each row are identical to the elements of the
@@ -38,6 +38,16 @@ def random_circulant_gram_matrix(dim: int) -> np.ndarray:
3838
[0.04257471, 0.21058986, 0.42351891, 0.21058986],
3939
[0.21058986, 0.04257471, 0.21058986, 0.42351891]])
4040
41+
It is also possible to pass a seed to this function for reproducibility.
42+
43+
>>> from toqito.rand import random_circulant_gram_matrix
44+
>>> circulant_matrix = random_circulant_gram_matrix(4, seed=42)
45+
>>> circulant_matrix
46+
array([[ 0.69220011, -0.02116047, 0.12407687, -0.02116047],
47+
[-0.02116047, 0.69220011, -0.02116047, 0.12407687],
48+
[ 0.12407687, -0.02116047, 0.69220011, -0.02116047],
49+
[-0.02116047, 0.12407687, -0.02116047, 0.69220011]])
50+
4151
4252
References
4353
==========
@@ -46,13 +56,16 @@ def random_circulant_gram_matrix(dim: int) -> np.ndarray:
4656
4757
:param dim: int
4858
The dimension of the circulant matrix to generate.
59+
:param seed: int | None
60+
A seed used to instantiate numpy's random number generator.
4961
5062
:return: numpy.ndarray
5163
A `dim` x `dim` real, symmetric, circulant matrix.
5264
5365
"""
66+
gen = np.random.default_rng(seed=seed)
5467
# Step 1: Generate a random diagonal matrix with non-negative entries
55-
diag_mat = np.diag(np.random.rand(dim))
68+
diag_mat = np.diag(gen.random(dim))
5669

5770
# Step 2: Construct the normalized DFT matrix
5871
dft_mat = np.fft.fft(np.eye(dim)) / np.sqrt(dim)

toqito/rand/random_density_matrix.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def random_density_matrix(
1010
is_real: bool = False,
1111
k_param: list[int] | int = None,
1212
distance_metric: str = "haar",
13+
seed: int | None = None,
1314
) -> np.ndarray:
1415
r"""Generate a random density matrix.
1516
@@ -74,6 +75,21 @@ def random_density_matrix(
7475
>>> is_density(bures_mat)
7576
np.True_
7677
78+
It is also possible to pass a seed to this function for reproducibility.
79+
80+
>>> from toqito.rand import random_density_matrix
81+
>>> seeded = random_density_matrix(2, seed=42)
82+
>>> seeded
83+
array([[0.82448019+0.j , 0.14841568-0.33318114j],
84+
[0.14841568+0.33318114j, 0.17551981+0.j ]])
85+
86+
We can once again verify that this is in fact a valid density matrix using the
87+
:code:`is_density` function from :code:`toqito` as follows
88+
89+
>>> from toqito.matrix_props import is_density
90+
>>> is_density(seeded)
91+
np.True_
92+
7793
7894
:param dim: The number of rows (and columns) of the density matrix.
7995
:param is_real: Boolean denoting whether the returned matrix will have all
@@ -83,20 +99,22 @@ def random_density_matrix(
8399
density matrix. This metric is either the Haar
84100
measure or the Bures measure. Default value is to
85101
use the Haar measure.
102+
:param seed: A seed used to instantiate numpy's random number generator.
86103
:return: A :code:`dim`-by-:code:`dim` random density matrix.
87104
88105
"""
106+
gen = np.random.default_rng(seed=seed)
89107
if k_param is None:
90108
k_param = dim
91109

92110
# Haar / Hilbert-Schmidt measure.
93-
gin = np.random.rand(dim, k_param)
111+
gin = gen.random((dim, k_param))
94112

95113
if not is_real:
96-
gin = gin + 1j * np.random.randn(dim, k_param)
114+
gin = gin + 1j * gen.standard_normal((dim, k_param))
97115

98116
if distance_metric == "bures":
99-
gin = random_unitary(dim, is_real) + np.identity(dim) @ gin
117+
gin = random_unitary(dim, is_real, seed=seed) + np.identity(dim) @ gin
100118

101119
rho = gin @ np.array(gin).conj().T
102120

toqito/rand/random_ginibre.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44

55

6-
def random_ginibre(dim_n: int, dim_m: int) -> np.ndarray:
6+
def random_ginibre(dim_n: int, dim_m: int, seed: int | None = None) -> np.ndarray:
77
r"""Generate a Ginibre random matrix :cite:`WikiCircLaw`.
88
99
Generates a random :code:`dim_n`-by-:code:`dim_m` Ginibre matrix.
@@ -23,6 +23,12 @@ def random_ginibre(dim_n: int, dim_m: int) -> np.ndarray:
2323
array([[0.39166472-1.54657971j, 0.36538245+0.23324642j],
2424
[0.50103695-0.25857737j, 0.8357054 +0.31404353j]])
2525
26+
It is also possible to pass a seed to this function for reproducibility.
27+
28+
>>> from toqito.rand import random_ginibre
29+
>>> random_ginibre(2, 2, seed=42)
30+
array([[ 0.21546751-1.37959021j, -0.73537981-0.92077996j],
31+
[ 0.53064913+0.09039682j, 0.66507969-0.22361728j]])
2632
2733
2834
References
@@ -33,7 +39,9 @@ def random_ginibre(dim_n: int, dim_m: int) -> np.ndarray:
3339
3440
:param dim_n: The number of rows of the Ginibre random matrix.
3541
:param dim_m: The number of columns of the Ginibre random matrix.
42+
:param seed: A seed used to instantiate numpy's random number generator.
3643
:return: A :code:`dim_n`-by-:code:`dim_m` Ginibre random density matrix.
3744
3845
"""
39-
return (np.random.randn(dim_n, dim_m) + 1j * np.random.randn(dim_n, dim_m)) / np.sqrt(2)
46+
gen = np.random.default_rng(seed=seed)
47+
return (gen.standard_normal((dim_n, dim_m)) + 1j * gen.standard_normal((dim_n, dim_m))) / np.sqrt(2)

0 commit comments

Comments
 (0)