Skip to content

Commit

Permalink
Merge branch 'hotfix/small_improvements' into feature/generalized_fil…
Browse files Browse the repository at this point in the history
…ter_functions
  • Loading branch information
thangleiter committed May 8, 2020
2 parents da03a1c + 4c1e76a commit ba3b685
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
25 changes: 23 additions & 2 deletions filter_functions/pulse_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,29 @@ def __imatmul__(self, other: 'PulseSequence') -> 'PulseSequence':

def is_cached(self, attr: str) -> bool:
"""Returns True if the attribute is cached"""
if not attr.startswith('_'):
attr = '_' + attr
# Define some aliases so that this method can be used by humans
aliases = {'eigenvalues': '_HD',
'eigenvectors': '_HV',
'propagators': '_Q',
'total propagator': '_total_Q',
'total propagator liouville': '_total_Q_liouville',
'frequencies': '_omega',
'total phases': '_total_phases',
'filter function': '_F',
'fidelity filter function': '_F',
'generalized filter function': '_F_kl',
'pulse correlation filter function': '_F_pc',
'fidelity pulse correlation filter function': '_F_pc',
'generalized pulse correlation filter function': '_F_pc_kl',
'control matrix': '_R',
'pulse correlation control matrix': '_R_pc'}

alias = attr.lower().replace('_', ' ')
if alias in aliases:
attr = aliases[alias]
else:
if not attr.startswith('_'):
attr = '_' + attr

return getattr(self, attr) is not None

Expand Down
40 changes: 39 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,45 @@ def test_pulse_sequence_attributes(self):
with self.assertRaises(AttributeError):
_ = A.is_cached(attr)
else:
self.assertFalse(A.is_cached(attr))
# set mock attribute at random
if testutil.rng.randint(0, 2):
setattr(A, attr, 'foo')
assertion = self.assertTrue
else:
assertion = self.assertFalse

assertion(A.is_cached(attr))
setattr(A, attr, None)

aliases = {'eigenvalues': '_HD',
'eigenvectors': '_HV',
'propagators': '_Q',
'total propagator': '_total_Q',
'total propagator liouville': '_total_Q_liouville',
'frequencies': '_omega',
'total phases': '_total_phases',
'filter function': '_F',
'fidelity filter function': '_F',
'generalized filter function': '_F_kl',
'pulse correlation filter function': '_F_pc',
'fidelity pulse correlation filter function': '_F_pc',
'generalized pulse correlation filter function': '_F_pc_kl',
'control matrix': '_R',
'pulse correlation control matrix': '_R'}

for alias, attr in aliases.items():
# set mock attribute at random
if testutil.rng.randint(0, 2):
setattr(A, attr, 'foo')
assertion = self.assertTrue
else:
assertion = self.assertFalse

assertion(A.is_cached(alias))
assertion(A.is_cached(alias.upper()))
assertion(A.is_cached(alias.replace(' ', '_')))

setattr(A, attr, None)

# Test cleanup
C = ff.concatenate((A, A), calc_pulse_correlation_ff=True,
Expand Down

0 comments on commit ba3b685

Please sign in to comment.