Skip to content

Commit

Permalink
Continue to increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thangleiter committed May 29, 2020
1 parent fab20f7 commit 4e6897f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
4 changes: 2 additions & 2 deletions filter_functions/pulse_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ def _parse_args(H_c: Hamiltonian, H_n: Hamiltonian, dt: Coefficients,
object.
"""

if not hasattr(dt, '__getitem__'):
if not hasattr(dt, '__len__'):
raise TypeError('Expected a sequence of time steps, not {}'.format(
type(dt)))

Expand Down Expand Up @@ -1016,7 +1016,7 @@ def _parse_Hamiltonian(H: Hamiltonian, n_dt: int,
raise TypeError('Expected operators in '.format(H_str) +
'to be NumPy arrays or QuTiP Qobjs!')

if not all(hasattr(coeff, '__getitem__') for coeff in coeffs):
if not all(hasattr(coeff, '__len__') for coeff in coeffs):
raise TypeError('Expected coefficients in '.format(H_str) +
'to be a sequence')

Expand Down
89 changes: 87 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ def test_pulse_sequence_constructor(self):

for hn in H_n:
hn[0] = hn[0].reshape(2, 2)
with self.assertRaises(ValueError):
# Control and noise operators not same dimension
for hn in H_n:
hn[0] = np.block([[hn[0], hn[0]], [hn[0], hn[0]]])
ff.PulseSequence(H_c, H_n, dt)

for hn in H_n:
hn[0] = hn[0][:2, :2]
with self.assertRaises(ValueError):
# Control identifiers not unique
identifier = H_c[idx][2]
Expand Down Expand Up @@ -334,6 +342,17 @@ def test_pulse_sequence_attributes(self):

assertion(A.is_cached(attr))

# Diagonalization attributes
A.diagonalize()
self.assertIsNotNone(A.HD)
self.assertIsNotNone(A.HV)
self.assertIsNotNone(A.Q)

A.cleanup('conservative')
self.assertIsNotNone(A.HD)
self.assertIsNotNone(A.HV)
self.assertIsNotNone(A.Q)

aliases = {'eigenvalues': '_HD',
'eigenvectors': '_HV',
'propagators': '_Q',
Expand Down Expand Up @@ -565,6 +584,40 @@ def test_filter_function(self):
np.isreal(F[np.eye(len(n_opers), dtype=bool)]).all()
)

# Check switch between fidelity and generalized filter function
F_generalized = total_pulse.get_filter_function(
omega, which='generalized')

F_fidelity = total_pulse.get_filter_function(
omega, which='fidelity')

# Check that F_fidelity is correctly reduced from F_generalized
self.assertArrayAlmostEqual(F_fidelity,
F_generalized.trace(axis1=2, axis2=3))

# Hit getters again to check caching functionality
F_generalized = total_pulse.get_filter_function(
omega, which='generalized')

F_fidelity = total_pulse.get_filter_function(
omega, which='fidelity')

# Check that F_fidelity is correctly reduced from F_generalized
self.assertArrayAlmostEqual(F_fidelity,
F_generalized.trace(axis1=2, axis2=3))

# Different set of frequencies than cached
F_generalized = total_pulse.get_filter_function(
omega + 1, which='generalized')

F_fidelity = total_pulse.get_filter_function(
omega + 1, which='fidelity')

# Check that F_fidelity is correctly reduced from F_generalized
self.assertArrayAlmostEqual(F_fidelity,
F_generalized.trace(axis1=2, axis2=3))


def test_pulse_correlation_filter_function(self):
"""
Test calculation of pulse correlation filter function and control
Expand Down Expand Up @@ -602,11 +655,17 @@ def test_pulse_correlation_filter_function(self):

pulse_1 = pulses['X'] @ pulses['Y']
pulse_2 = ff.concatenate([pulses['X'], pulses['Y']],
calc_pulse_correlation_ff=True)
calc_pulse_correlation_ff=True,
which='fidelity')
pulse_3 = ff.concatenate([pulses['X'], pulses['Y']],
calc_pulse_correlation_ff=True,
which='generalized')

self.assertTrue(pulse_2.is_cached('R_pc'))
self.assertTrue(pulse_2.is_cached('F_pc'))
self.assertTrue(pulse_3.is_cached('R_pc'))
self.assertTrue(pulse_3.is_cached('F_pc_kl'))

# Check if the filter functions on the diagonals are real
F = pulse_2.get_pulse_correlation_filter_function()
diag_1 = np.eye(2, dtype=bool)
Expand Down Expand Up @@ -639,14 +698,40 @@ def test_pulse_correlation_filter_function(self):
)
)

F = pulse_3.get_pulse_correlation_filter_function()
R_pc = pulse_3.get_pulse_correlation_control_matrix()
F = pulse_3.get_pulse_correlation_filter_function(which='fidelity')
self.assertArrayEqual(
F, numeric.calculate_pulse_correlation_filter_function(
R_pc, 'fidelity'
)
)

F = pulse_3.get_pulse_correlation_filter_function(which='generalized')
self.assertArrayEqual(
F, numeric.calculate_pulse_correlation_filter_function(
R_pc, 'generalized'
)
)

# If for some reason F_pc_xy is removed, check if recovered from R_pc
pulse_2._F_pc = None
pulse_3._F_pc_kl = None

R_pc = pulse_3.get_pulse_correlation_control_matrix()
F = pulse_3.get_pulse_correlation_filter_function(which='fidelity')
self.assertArrayEqual(
F, numeric.calculate_pulse_correlation_filter_function(
R_pc, 'fidelity'
)
)

F = pulse_3.get_pulse_correlation_filter_function(which='generalized')
self.assertArrayEqual(
F, numeric.calculate_pulse_correlation_filter_function(
R_pc, 'generalized'
)
)

S = omega**0*1e-2
with self.assertRaises(util.CalculationError):
infid_1 = ff.infidelity(pulse_1, S, omega, which='correlations')
Expand Down

0 comments on commit 4e6897f

Please sign in to comment.