Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses scipy's Haar measure functions #731

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions strawberryfields/utils/random_numbers_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,10 @@ def random_interferometer(N, real=False):
Returns:
array: random :math:`N\times N` unitary distributed with the Haar measure
"""
if N == 1:
if real:
return np.array([[2 * (np.random.binomial(1, 0.5) - 0.5)]])
return np.array([[np.exp(1j * 2 * np.pi * np.random.rand())]])
if real:
z = np.random.randn(N, N)
else:
z = randnc(N, N) / np.sqrt(2.0)
q, r = sp.linalg.qr(z)
d = np.diagonal(r)
ph = d / np.abs(d)
U = np.multiply(q, ph, q)
return U
return sp.stats.ortho_group.rvs(N)
return sp.stats.unitary_group.rvs(N)
6 changes: 1 addition & 5 deletions tests/frontend/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,10 @@ def test_odd_cat_state(self, a, cutoff, tol):
# ===================================================================================


@pytest.mark.parametrize("modes", [1, 2, 3])
class TestRandomMatrices:
"""Unit tests for random matrices"""

@pytest.fixture
def modes(self):
"""Number of modes to use when creating matrices"""
return 3

@pytest.mark.parametrize("pure_state", [True, False])
@pytest.mark.parametrize("block_diag", [True, False])
def test_random_covariance_square(self, modes, hbar, pure_state, block_diag):
Expand Down