Skip to content

Commit

Permalink
Add random_state argument to linalg testing functions for seeded rand…
Browse files Browse the repository at this point in the history
…omness (#2459)
  • Loading branch information
kevinsung authored and CirqBot committed Oct 30, 2019
1 parent d3cfe3f commit c266da8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
25 changes: 18 additions & 7 deletions cirq/testing/lin_alg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
from cirq import linalg, value


def random_superposition(dim: int) -> np.ndarray:
def random_superposition(dim: int,
*,
random_state: value.RANDOM_STATE_LIKE = None
) -> np.ndarray:
"""Returns a random unit-length vector from the uniform distribution.
Args:
Expand All @@ -30,8 +33,10 @@ def random_superposition(dim: int) -> np.ndarray:
Returns:
The sampled unit-length vector.
"""
state_vector = np.random.randn(dim).astype(complex)
state_vector += 1j * np.random.randn(dim)
random_state = value.parse_random_state(random_state)

state_vector = random_state.randn(dim).astype(complex)
state_vector += 1j * random_state.randn(dim)
state_vector /= np.linalg.norm(state_vector)
return state_vector

Expand Down Expand Up @@ -59,7 +64,8 @@ def random_unitary(dim: int, *,
return q * (d / abs(d))


def random_orthogonal(dim: int) -> np.ndarray:
def random_orthogonal(dim: int, *, random_state: value.RANDOM_STATE_LIKE = None
) -> np.ndarray:
"""Returns a random orthogonal matrix distributed with Haar measure.
Args:
Expand All @@ -72,7 +78,9 @@ def random_orthogonal(dim: int) -> np.ndarray:
'How to generate random matrices from the classical compact groups'
http://arxiv.org/abs/math-ph/0609050
"""
m = np.random.randn(dim, dim)
random_state = value.parse_random_state(random_state)

m = random_state.randn(dim, dim)
q, r = np.linalg.qr(m)
d = np.diag(r)
return q * (d / abs(d))
Expand All @@ -96,7 +104,10 @@ def random_special_unitary(dim: int,
return r


def random_special_orthogonal(dim: int) -> np.ndarray:
def random_special_orthogonal(dim: int,
*,
random_state: value.RANDOM_STATE_LIKE = None
) -> np.ndarray:
"""Returns a random special orthogonal matrix distributed with Haar measure.
Args:
Expand All @@ -105,7 +116,7 @@ def random_special_orthogonal(dim: int) -> np.ndarray:
Returns:
The sampled special orthogonal matrix.
"""
m = random_orthogonal(dim)
m = random_orthogonal(dim, random_state=random_state)
if np.linalg.det(m) < 0:
m[0, :] *= -1
return m
Expand Down
23 changes: 23 additions & 0 deletions cirq/testing/lin_alg_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def test_random_superposition(dim):
assert np.isclose(np.linalg.norm(state), 1.0)


def test_random_superposition_deterministic_given_seed():
state1 = random_superposition(10, random_state=1234)
state2 = random_superposition(10, random_state=1234)

np.testing.assert_equal(state1, state2)


def test_random_unitary():
u1 = random_unitary(2)
u2 = random_unitary(2)
Expand All @@ -49,6 +56,14 @@ def test_random_orthogonal():
assert is_orthogonal(o2)
assert not np.allclose(o1, o2)


def test_random_orthogonal_deterministic_given_seed():
o1 = random_orthogonal(2, random_state=1234)
o2 = random_orthogonal(2, random_state=1234)

np.testing.assert_equal(o1, o2)


def test_random_special_unitary():
u1 = random_special_unitary(2)
u2 = random_special_unitary(2)
Expand All @@ -72,6 +87,14 @@ def test_random_special_orthgonal():
assert is_special_orthogonal(o2)
assert not np.allclose(o1, o2)


def test_random_special_orthogonal_deterministic_given_seed():
o1 = random_special_orthogonal(2, random_state=1234)
o2 = random_special_orthogonal(2, random_state=1234)

np.testing.assert_equal(o1, o2)


def test_assert_allclose_up_to_global_phase():
assert_allclose_up_to_global_phase(
np.array([[1]]),
Expand Down

0 comments on commit c266da8

Please sign in to comment.