Skip to content

Commit

Permalink
Switch to recommended numpy.random methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thangleiter committed Jul 3, 2020
1 parent b12c7c2 commit 1213e6d
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 169 deletions.
46 changes: 24 additions & 22 deletions tests/test_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import filter_functions as ff
from tests import testutil
from tests.testutil import rng

from . import qutip

Expand All @@ -51,7 +52,7 @@ def test_basis_constructor(self):

# Too many elements
with self.assertRaises(ValueError):
_ = ff.Basis(testutil.rng.randn(5, 2, 2))
_ = ff.Basis(rng.standard_normal((5, 2, 2)))

# Properly normalized
self.assertEqual(ff.Basis.pauli(1), ff.Basis(ff.util.paulis))
Expand All @@ -70,8 +71,8 @@ def test_basis_constructor(self):

def test_basis_properties(self):
"""Basis orthonormal and of correct dimensions"""
d = testutil.rng.randint(2, 17)
n = testutil.rng.randint(1, 5)
d = rng.randint(2, 17)
n = rng.randint(1, 5)

ggm_basis = ff.Basis.ggm(d)
pauli_basis = ff.Basis.pauli(n)
Expand All @@ -87,11 +88,11 @@ def test_basis_properties(self):
if not btype == 'Pauli':
self.assertEqual(d, base.d)
# Check if __contains__ works as expected
self.assertTrue(base[testutil.rng.randint(0, d**2)] in base)
self.assertTrue(base[rng.randint(0, d**2)] in base)
else:
self.assertEqual(2**n, base.d)
# Check if __contains__ works as expected
self.assertTrue(base[testutil.rng.randint(0, (2**n)**2)]
self.assertTrue(base[rng.randint(0, (2**n)**2)]
in base)
# Check if all elements of each basis are orthonormal and hermitian
self.assertArrayEqual(base.T,
Expand Down Expand Up @@ -130,13 +131,13 @@ def test_basis_properties(self):
def test_basis_expansion_and_normalization(self):
"""Correct expansion of operators and normalization of bases"""
for _ in range(10):
d = testutil.rng.randint(2, 16)
d = rng.randint(2, 16)
ggm_basis = ff.Basis.ggm(d)
basis = ff.Basis(
np.einsum('i,ijk->ijk', testutil.rng.randn(d**2), ggm_basis),
np.einsum('i,ijk->ijk', rng.standard_normal(d**2), ggm_basis),
skip_check=True
)
M = testutil.rng.randn(d, d) + 1j*testutil.rng.randn(d, d)
M = rng.standard_normal((d, d)) + 1j*rng.standard_normal((d, d))
M -= np.trace(M)/d
coeffs = ff.basis.expand(M, basis, normalized=False)
self.assertArrayAlmostEqual(M, np.einsum('i,ijk', coeffs, basis))
Expand All @@ -147,8 +148,9 @@ def test_basis_expansion_and_normalization(self):
ff.basis.ggm_expand(M, traceless=True),
atol=1e-14)

n = testutil.rng.randint(1, 50)
M = testutil.rng.randn(n, d, d) + 1j*testutil.rng.randn(n, d, d)
n = rng.randint(1, 50)
M = (rng.standard_normal((n, d, d)) +
1j*rng.standard_normal((n, d, d)))
coeffs = ff.basis.expand(M, basis, normalized=False)
self.assertArrayAlmostEqual(M, np.einsum('li,ijk->ljk', coeffs,
basis))
Expand Down Expand Up @@ -181,11 +183,11 @@ def test_basis_generation_from_partial_ggm(self):
# Do 100 test runs with random elements from a GGM basis in (2 ... 8)
# dimensions
for _ in range(50):
d = testutil.rng.randint(2, 9)
d = rng.randint(2, 9)
b = ff.Basis.ggm(d)
inds = [i for i in range(d**2)]
tup = tuple(inds.pop(testutil.rng.randint(0, len(inds)))
for _ in range(testutil.rng.randint(1, d**2)))
tup = tuple(inds.pop(rng.randint(0, len(inds)))
for _ in range(rng.randint(1, d**2)))
elems = b[tup, ...]
basis = ff.Basis(elems)
self.assertTrue(basis.isorthonorm)
Expand All @@ -199,12 +201,12 @@ def test_basis_generation_from_partial_pauli(self):
# Do 100 test runs with random elements from a Pauli basis in (2 ... 8)
# dimensions
for _ in range(50):
n = testutil.rng.randint(1, 4)
n = rng.randint(1, 4)
d = 2**n
b = ff.Basis.pauli(n)
inds = [i for i in range(d**2)]
tup = tuple(inds.pop(testutil.rng.randint(0, len(inds)))
for _ in range(testutil.rng.randint(1, d**2)))
tup = tuple(inds.pop(rng.randint(0, len(inds)))
for _ in range(rng.randint(1, d**2)))
elems = b[tup, ...]
basis = ff.Basis(elems)
self.assertTrue(basis.isorthonorm)
Expand All @@ -226,7 +228,7 @@ def test_basis_generation_from_partial_random(self):
# Do 25 test runs with random elements from a random basis in
# (2 ... 8) dimensions
for _ in range(25):
d = testutil.rng.randint(2, 7)
d = rng.randint(2, 7)
# Get a random traceless hermitian operator
oper = testutil.rand_herm_traceless(d)
# ... and build a basis from it
Expand All @@ -238,8 +240,8 @@ def test_basis_generation_from_partial_random(self):
# Choose random elements from that basis and generate a new basis
# from it
inds = [i for i in range(d**2)]
tup = tuple(inds.pop(testutil.rng.randint(0, len(inds)))
for _ in range(testutil.rng.randint(1, d**2)))
tup = tuple(inds.pop(rng.randint(0, len(inds)))
for _ in range(rng.randint(1, d**2)))
elems = b[tup, ...]
basis = ff.Basis(elems)
self.assertTrue(basis.isorthonorm)
Expand All @@ -250,7 +252,7 @@ def test_basis_generation_from_partial_random(self):

# Test runs with non-traceless opers
for _ in range(25):
d = testutil.rng.randint(2, 7)
d = rng.randint(2, 7)
# Get a random hermitian operator
oper = testutil.rand_herm(d)
# ... and build a basis from it
Expand All @@ -262,8 +264,8 @@ def test_basis_generation_from_partial_random(self):
# Choose random elements from that basis and generate a new basis
# from it
inds = [i for i in range(d**2)]
tup = tuple(inds.pop(testutil.rng.randint(0, len(inds)))
for _ in range(testutil.rng.randint(1, d**2)))
tup = tuple(inds.pop(rng.randint(0, len(inds)))
for _ in range(rng.randint(1, d**2)))
elems = b[tup, ...]
basis = ff.Basis(elems)
self.assertTrue(basis.isorthonorm)
Expand Down
75 changes: 38 additions & 37 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import filter_functions as ff
from filter_functions import numeric, util
from tests import testutil
from tests.testutil import rng


class CoreTest(testutil.TestCase):
Expand All @@ -55,7 +56,7 @@ def test_pulse_sequence_constructor(self):
# dt not a sequence
ff.PulseSequence(H_c, H_n, dt[0])

idx = testutil.rng.randint(0, 5)
idx = rng.randint(0, 5)
with self.assertRaises(ValueError):
# negative dt
dt[idx] *= -1
Expand Down Expand Up @@ -94,7 +95,7 @@ def test_pulse_sequence_constructor(self):
# Element of noise Hamiltonian not list or tuple
ff.PulseSequence(H_c, [np.array(H_n[0])], dt)

idx = testutil.rng.randint(0, 3)
idx = rng.randint(0, 3)
with self.assertRaises(TypeError):
# Control Hamiltonian element not list or tuple
H_c[idx] = dict(H_c[idx])
Expand Down Expand Up @@ -241,27 +242,27 @@ def test_pulse_sequence_constructor(self):
def test_pulse_sequence_attributes(self):
"""Test attributes of single instance"""
X, Y, Z = util.paulis[1:]
n_dt = testutil.rng.randint(1, 10)
n_dt = rng.randint(1, 10)

# trivial case
A = ff.PulseSequence([[X, testutil.rng.randn(n_dt), 'X']],
[[Z, testutil.rng.randn(n_dt), 'Z']],
np.abs(testutil.rng.randn(n_dt)))
A = ff.PulseSequence([[X, rng.standard_normal(n_dt), 'X']],
[[Z, rng.standard_normal(n_dt), 'Z']],
np.abs(rng.standard_normal(n_dt)))
self.assertFalse(A == 1)
self.assertTrue(A != 1)

# different number of time steps
B = ff.PulseSequence([[X, testutil.rng.randn(n_dt+1), 'X']],
[[Z, testutil.rng.randn(n_dt+1), 'Z']],
np.abs(testutil.rng.randn(n_dt+1)))
B = ff.PulseSequence([[X, rng.standard_normal(n_dt+1), 'X']],
[[Z, rng.standard_normal(n_dt+1), 'Z']],
np.abs(rng.standard_normal(n_dt+1)))
self.assertFalse(A == B)
self.assertTrue(A != B)

# different time steps
B = ff.PulseSequence(
list(zip(A.c_opers, A.c_coeffs, A.c_oper_identifiers)),
list(zip(A.n_opers, A.n_coeffs, A.n_oper_identifiers)),
np.abs(testutil.rng.randn(n_dt))
np.abs(rng.standard_normal(n_dt))
)
self.assertFalse(A == B)
self.assertTrue(A != B)
Expand All @@ -277,7 +278,7 @@ def test_pulse_sequence_attributes(self):

# different control coeffs
B = ff.PulseSequence(
list(zip(A.c_opers, [testutil.rng.randn(n_dt)],
list(zip(A.c_opers, [rng.standard_normal(n_dt)],
A.c_oper_identifiers)),
list(zip(A.n_opers, A.n_coeffs, A.n_oper_identifiers)),
A.dt
Expand All @@ -297,7 +298,7 @@ def test_pulse_sequence_attributes(self):
# different noise coeffs
B = ff.PulseSequence(
list(zip(A.c_opers, A.c_coeffs, A.c_oper_identifiers)),
list(zip(A.n_opers, [testutil.rng.randn(n_dt)],
list(zip(A.n_opers, [rng.standard_normal(n_dt)],
A.n_oper_identifiers)),
A.dt
)
Expand Down Expand Up @@ -341,7 +342,7 @@ def test_pulse_sequence_attributes(self):
_ = A.is_cached(attr)
else:
# set mock attribute at random
if testutil.rng.randint(0, 2):
if rng.randint(0, 2):
setattr(A, attr, 'foo')
assertion = self.assertTrue
else:
Expand Down Expand Up @@ -381,7 +382,7 @@ def test_pulse_sequence_attributes(self):

for alias, attr in aliases.items():
# set mock attribute at random
if testutil.rng.randint(0, 2):
if rng.randint(0, 2):
setattr(A, attr, 'foo')
assertion = self.assertTrue
else:
Expand Down Expand Up @@ -446,23 +447,23 @@ def test_pulse_sequence_attributes(self):
def test_pulse_sequence_attributes_concat(self):
"""Test attributes of concatenated sequence."""
X, Y, Z = util.paulis[1:]
n_dt_1 = testutil.rng.randint(5, 11)
x_coeff_1 = testutil.rng.randn(n_dt_1)
z_coeff_1 = testutil.rng.randn(n_dt_1)
dt_1 = np.abs(testutil.rng.randn(n_dt_1))
n_dt_2 = testutil.rng.randint(5, 11)
y_coeff_2 = testutil.rng.randn(n_dt_2)
z_coeff_2 = testutil.rng.randn(n_dt_2)
dt_2 = np.abs(testutil.rng.randn(n_dt_2))
n_dt_1 = rng.randint(5, 11)
x_coeff_1 = rng.standard_normal(n_dt_1)
z_coeff_1 = rng.standard_normal(n_dt_1)
dt_1 = np.abs(rng.standard_normal(n_dt_1))
n_dt_2 = rng.randint(5, 11)
y_coeff_2 = rng.standard_normal(n_dt_2)
z_coeff_2 = rng.standard_normal(n_dt_2)
dt_2 = np.abs(rng.standard_normal(n_dt_2))
pulse_1 = ff.PulseSequence([[X, x_coeff_1]],
[[Z, z_coeff_1]],
dt_1)
pulse_2 = ff.PulseSequence([[Y, y_coeff_2]],
[[Z, z_coeff_2]],
dt_2)
pulse_3 = ff.PulseSequence([[Y, testutil.rng.randn(2)],
[X, testutil.rng.randn(2)]],
[[Z, np.abs(testutil.rng.randn(2))]],
pulse_3 = ff.PulseSequence([[Y, rng.standard_normal(2)],
[X, rng.standard_normal(2)]],
[[Z, np.abs(rng.standard_normal(2))]],
[1, 1])

# Concatenate with different noise opers
Expand All @@ -476,7 +477,7 @@ def test_pulse_sequence_attributes_concat(self):
pulse_21 = pulse_2 @ pulse_1

with self.assertRaises(TypeError):
_ = pulse_1 @ testutil.rng.randn(2, 2)
_ = pulse_1 @ rng.standard_normal((2, 2))

# Concatenate pulses with same operators but different labels
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -526,13 +527,13 @@ def test_pulse_sequence_attributes_concat(self):
self.assertArrayEqual(total_Q_liouville, pulse._total_Q_liouville)

# Test custom identifiers
letters = testutil.rng.choice(list(string.ascii_letters), size=(6, 5),
replace=False)
letters = rng.choice(list(string.ascii_letters), size=(6, 5),
replace=False)
ids = [''.join(l) for l in letters[:3]]
labels = [''.join(l) for l in letters[3:]]
pulse = ff.PulseSequence(
list(zip([X, Y, Z], testutil.rng.randn(3, 2), ids, labels)),
list(zip([X, Y, Z], testutil.rng.randn(3, 2), ids, labels)),
list(zip([X, Y, Z], rng.standard_normal((3, 2)), ids, labels)),
list(zip([X, Y, Z], rng.standard_normal((3, 2)), ids, labels)),
[1, 1]
)

Expand All @@ -541,8 +542,8 @@ def test_pulse_sequence_attributes_concat(self):

def test_filter_function(self):
"""Test the filter function calculation and related methods"""
for d, n_dt in zip(testutil.rng.randint(2, 10, (3,)),
testutil.rng.randint(10, 200, (3,))):
for d, n_dt in zip(rng.randint(2, 10, (3,)),
rng.randint(10, 200, (3,))):
total_pulse = testutil.rand_pulse_sequence(d, n_dt, 4, 6)
c_opers, c_coeffs = total_pulse.c_opers, total_pulse.c_coeffs
n_opers, n_coeffs = total_pulse.n_opers, total_pulse.n_coeffs
Expand Down Expand Up @@ -756,7 +757,7 @@ def test_pulse_correlation_filter_function(self):
infid_1 = ff.infidelity(pulse_1, S, omega, which='foobar')

for _ in range(10):
n_nops = testutil.rng.randint(1, 4)
n_nops = rng.randint(1, 4)
identifiers = sample(['B_0', 'B_1', 'B_2'], n_nops)

infid_X = ff.infidelity(pulses['X'], S, omega, which='total',
Expand Down Expand Up @@ -787,9 +788,9 @@ def test_calculate_error_vector_correlation_functions(self):
"""Test raises of numeric.error_transfer_matrix"""
pulse = testutil.rand_pulse_sequence(2, 1, 1, 1)

omega = testutil.rng.randn(43)
omega = rng.standard_normal(43)
# single spectrum
S = testutil.rng.randn(78)
S = rng.standard_normal(78)
for i in range(4):
with self.assertRaises(ValueError):
numeric.calculate_error_vector_correlation_functions(
Expand Down Expand Up @@ -829,8 +830,8 @@ def S(omega):
n, infids = ff.infidelity(simple_pulse, S, {}, test_convergence=True)

# Test with non-default args
identifiers = testutil.rng.choice(complicated_pulse.n_oper_identifiers,
testutil.rng.randint(1, 4))
identifiers = rng.choice(complicated_pulse.n_oper_identifiers,
rng.randint(1, 4))

n, infids = ff.infidelity(complicated_pulse, S, omega,
test_convergence=True,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_bloch_sphere_visualization_not_available(self):
from filter_functions import plotting

with self.assertRaises(RuntimeError):
plotting.get_bloch_vector(testutil.rng.randn(10, 2))
plotting.get_bloch_vector(testutil.rng.standard_normal((10, 2)))

with self.assertRaises(RuntimeError):
plotting.init_bloch_sphere()
Expand Down
Loading

0 comments on commit 1213e6d

Please sign in to comment.