Skip to content

Commit

Permalink
Switch tests to new numpy.random API
Browse files Browse the repository at this point in the history
  • Loading branch information
thangleiter committed May 3, 2020
1 parent 6433fed commit 26949c5
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 155 deletions.
45 changes: 23 additions & 22 deletions tests/test_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_basis_constructor(self):

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

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

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

ggm_basis = ff.Basis.ggm(d)
pauli_basis = ff.Basis.pauli(n)
Expand All @@ -87,11 +87,12 @@ def test_basis_properties(self):
if not btype == 'Pauli':
self.assertEqual(d, base.d)
# Check if __contains__ works as expected
self.assertTrue(base[np.random.randint(0, d**2)] in base)
self.assertTrue(base[testutil.rng.randint(0, d**2)] in base)
else:
self.assertEqual(2**n, base.d)
# Check if __contains__ works as expected
self.assertTrue(base[np.random.randint(0, (2**n)**2)] in base)
self.assertTrue(base[testutil.rng.randint(0, (2**n)**2)]
in base)
# Check if all elements of each basis are orthonormal and hermitian
self.assertArrayEqual(base.T,
base.view(np.ndarray).swapaxes(-1, -2))
Expand Down Expand Up @@ -129,13 +130,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 = np.random.randint(2, 16)
d = testutil.rng.randint(2, 16)
ggm_basis = ff.Basis.ggm(d)
basis = ff.Basis(
np.einsum('i,ijk->ijk', np.random.randn(d**2), ggm_basis),
np.einsum('i,ijk->ijk', testutil.rng.randn(d**2), ggm_basis),
skip_check=True
)
M = np.random.randn(d, d) + 1j*np.random.randn(d, d)
M = testutil.rng.randn(d, d) + 1j*testutil.rng.randn(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 @@ -146,8 +147,8 @@ def test_basis_expansion_and_normalization(self):
ff.basis.ggm_expand(M, traceless=True),
atol=1e-14)

n = np.random.randint(1, 50)
M = np.random.randn(n, d, d) + 1j*np.random.randn(n, d, d)
n = testutil.rng.randint(1, 50)
M = testutil.rng.randn(n, d, d) + 1j*testutil.rng.randn(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 @@ -180,11 +181,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 = np.random.randint(2, 9)
d = testutil.rng.randint(2, 9)
b = ff.Basis.ggm(d)
inds = [i for i in range(d**2)]
tup = tuple(inds.pop(np.random.randint(0, len(inds)))
for _ in range(np.random.randint(1, d**2)))
tup = tuple(inds.pop(testutil.rng.randint(0, len(inds)))
for _ in range(testutil.rng.randint(1, d**2)))
elems = b[tup, ...]
basis = ff.Basis(elems)
self.assertTrue(basis.isorthonorm)
Expand All @@ -198,12 +199,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 = np.random.randint(1, 4)
n = testutil.rng.randint(1, 4)
d = 2**n
b = ff.Basis.pauli(n)
inds = [i for i in range(d**2)]
tup = tuple(inds.pop(np.random.randint(0, len(inds)))
for _ in range(np.random.randint(1, d**2)))
tup = tuple(inds.pop(testutil.rng.randint(0, len(inds)))
for _ in range(testutil.rng.randint(1, d**2)))
elems = b[tup, ...]
basis = ff.Basis(elems)
self.assertTrue(basis.isorthonorm)
Expand All @@ -225,7 +226,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 = np.random.randint(2, 7)
d = testutil.rng.randint(2, 7)
# Get a random traceless hermitian operator
oper = testutil.rand_herm_traceless(d)
# ... and build a basis from it
Expand All @@ -237,8 +238,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(np.random.randint(0, len(inds)))
for _ in range(np.random.randint(1, d**2)))
tup = tuple(inds.pop(testutil.rng.randint(0, len(inds)))
for _ in range(testutil.rng.randint(1, d**2)))
elems = b[tup, ...]
basis = ff.Basis(elems)
self.assertTrue(basis.isorthonorm)
Expand All @@ -249,7 +250,7 @@ def test_basis_generation_from_partial_random(self):

# Test runs with non-traceless opers
for _ in range(25):
d = np.random.randint(2, 7)
d = testutil.rng.randint(2, 7)
# Get a random hermitian operator
oper = testutil.rand_herm(d)
# ... and build a basis from it
Expand All @@ -261,8 +262,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(np.random.randint(0, len(inds)))
for _ in range(np.random.randint(1, d**2)))
tup = tuple(inds.pop(testutil.rng.randint(0, len(inds)))
for _ in range(testutil.rng.randint(1, d**2)))
elems = b[tup, ...]
basis = ff.Basis(elems)
self.assertTrue(basis.isorthonorm)
Expand Down
72 changes: 37 additions & 35 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from random import sample

import numpy as np
from numpy.random import choice, randint, randn

import filter_functions as ff
from filter_functions.numeric import (
Expand Down Expand Up @@ -61,7 +60,7 @@ def test_pulse_sequence_constructor(self):
# dt not a sequence
ff.PulseSequence(H_c, H_n, dt[0])

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

idx = randint(0, 3)
idx = testutil.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 @@ -231,27 +230,27 @@ def test_pulse_sequence_constructor(self):
def test_pulse_sequence_attributes(self):
"""Test attributes of single instance"""
X, Y, Z = ff.util.P_np[1:]
n_dt = randint(1, 10)
n_dt = testutil.rng.randint(1, 10)

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

# different number of time steps
B = ff.PulseSequence([[X, randn(n_dt+1), 'X']],
[[Z, randn(n_dt+1), 'Z']],
np.abs(randn(n_dt+1)))
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)))
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(randn(n_dt))
np.abs(testutil.rng.randn(n_dt))
)
self.assertFalse(A == B)
self.assertTrue(A != B)
Expand All @@ -267,7 +266,8 @@ def test_pulse_sequence_attributes(self):

# different control coeffs
B = ff.PulseSequence(
list(zip(A.c_opers, [randn(n_dt)], A.c_oper_identifiers)),
list(zip(A.c_opers, [testutil.rng.randn(n_dt)],
A.c_oper_identifiers)),
list(zip(A.n_opers, A.n_coeffs, A.n_oper_identifiers)),
A.dt
)
Expand All @@ -286,7 +286,8 @@ 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, [randn(n_dt)], A.n_oper_identifiers)),
list(zip(A.n_opers, [testutil.rng.randn(n_dt)],
A.n_oper_identifiers)),
A.dt
)
self.assertFalse(A == B)
Expand Down Expand Up @@ -364,30 +365,30 @@ def test_pulse_sequence_attributes(self):
def test_pulse_sequence_attributes_concat(self):
"""Test attributes of concatenated sequence."""
X, Y, Z = ff.util.P_np[1:]
n_dt_1 = randint(5, 11)
x_coeff_1 = randn(n_dt_1)
z_coeff_1 = randn(n_dt_1)
dt_1 = np.abs(randn(n_dt_1))
n_dt_2 = randint(5, 11)
y_coeff_2 = randn(n_dt_2)
z_coeff_2 = randn(n_dt_2)
dt_2 = np.abs(randn(n_dt_2))
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))
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, randn(2)],
[X, randn(2)]],
[[Z, np.abs(randn(2))]],
pulse_3 = ff.PulseSequence([[Y, testutil.rng.randn(2)],
[X, testutil.rng.randn(2)]],
[[Z, np.abs(testutil.rng.randn(2))]],
[1, 1])

pulse_12 = pulse_1 @ pulse_2
pulse_21 = pulse_2 @ pulse_1

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

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

# Test custom identifiers
letters = np.random.choice(list(string.ascii_letters), size=(6, 5),
replace=False)
letters = testutil.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], np.random.randn(3, 2), ids, labels)),
list(zip([X, Y, Z], np.random.randn(3, 2), ids, labels)),
list(zip([X, Y, Z], testutil.rng.randn(3, 2), ids, labels)),
list(zip([X, Y, Z], testutil.rng.randn(3, 2), ids, labels)),
[1, 1]
)

Expand All @@ -452,7 +453,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(randint(2, 10, (3,)), randint(10, 200, (3,))):
for d, n_dt in zip(testutil.rng.randint(2, 10, (3,)),
testutil.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 @@ -574,7 +576,7 @@ def test_pulse_correlation_filter_function(self):
infid_1 = ff.infidelity(pulse_1, S, omega, which='foobar')

for _ in range(10):
n_nops = randint(1, 4)
n_nops = testutil.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 @@ -605,9 +607,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 = randn(43)
omega = testutil.rng.randn(43)
# single spectrum
S = randn(78)
S = testutil.rng.randn(78)
for i in range(4):
with self.assertRaises(ValueError):
calculate_error_vector_correlation_functions(
Expand Down Expand Up @@ -652,8 +654,8 @@ def S(omega):
test_convergence=True)

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

n, infids, (fig, ax) = ff.infidelity(complicated_pulse,
S, omega, test_convergence=True,
Expand Down
15 changes: 9 additions & 6 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import numpy as np
import pytest
import qutip as qt
from numpy.random import randint

import filter_functions as ff
from filter_functions.plotting import (get_bloch_vector, get_states_from_prop,
Expand Down Expand Up @@ -102,7 +101,8 @@ def test_plot_pulse_train(self):

# Call with custom args
c_oper_identifiers = sample(
complicated_pulse.c_oper_identifiers.tolist(), randint(2, 4)
complicated_pulse.c_oper_identifiers.tolist(),
testutil.rng.randint(2, 4)
)

fig, ax = plt.subplots()
Expand Down Expand Up @@ -142,7 +142,8 @@ def test_plot_filter_function(self):

# Non-default args
n_oper_identifiers = sample(
complicated_pulse.n_oper_identifiers.tolist(), randint(2, 4)
complicated_pulse.n_oper_identifiers.tolist(),
testutil.rng.randint(2, 4)
)

fig, ax = plt.subplots()
Expand Down Expand Up @@ -205,7 +206,8 @@ def test_plot_pulse_correlation_filter_function(self):

# Non-default args
n_oper_identifiers = sample(
complicated_pulse.n_oper_identifiers.tolist(), randint(2, 4)
complicated_pulse.n_oper_identifiers.tolist(),
testutil.rng.randint(2, 4)
)

fig, ax = plt.subplots()
Expand Down Expand Up @@ -274,12 +276,13 @@ def test_plot_error_transfer_matrix(self):

# Non-default args
n_oper_inds = sample(range(len(complicated_pulse.n_opers)),
randint(2, 4))
testutil.rng.randint(2, 4))
n_oper_identifiers = complicated_pulse.n_oper_identifiers[n_oper_inds]

basis_labels = []
for i in range(4):
basis_labels.append(string.ascii_uppercase[randint(0, 26)])
basis_labels.append(
string.ascii_uppercase[testutil.rng.randint(0, 26)])

omega = ff.util.get_sample_frequencies(complicated_pulse, n_samples=50,
spacing='log')
Expand Down
Loading

0 comments on commit 26949c5

Please sign in to comment.