Skip to content

Commit

Permalink
identity mappings are now automatically added if explicit mappings are
Browse files Browse the repository at this point in the history
omitted. also tests.
  • Loading branch information
pbethke committed Jan 1, 2016
1 parent 055bb94 commit 3ca0a1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 8 additions & 1 deletion qctoolkit/pulses/sequence_pulse_template.py
Expand Up @@ -43,7 +43,7 @@ def __len__(self):

class SequencePulseTemplate(PulseTemplate):
"""A sequence of different PulseTemplates.
SequencePulseTemplate allows to group smaller
PulseTemplates (subtemplates) into on larger sequence,
i.e., when instantiating a pulse from a SequencePulseTemplate
Expand All @@ -64,6 +64,13 @@ class SequencePulseTemplate(PulseTemplate):
def __init__(self, subtemplates: List[Subtemplate], external_parameters: List[str], identifier: Optional[str]=None) -> None:
super().__init__(identifier)
self.__parameter_names = frozenset(external_parameters)
# insert identity mappings for entries without explicit mapping
for i, entry in enumerate(subtemplates):
if type(entry) != tuple:
subtemplates[i] = (entry, IdentityMapping(entry))
elif type(entry) == tuple and len(entry) == 1:
subtemplates[i] = (entry[0], IdentityMapping(entry[0]))

# convert all mapping strings to expressions
for i, (template, mappings) in enumerate(subtemplates):
subtemplates[i] = (template, {k: Expression(v) for k, v in mappings.items()})
Expand Down
22 changes: 17 additions & 5 deletions tests/pulses/sequence_pulse_template_tests.py
Expand Up @@ -37,15 +37,15 @@ def __init__(self, *args, **kwargs):
self.sequence = SequencePulseTemplate([(self.square, self.mapping1)], self.outer_parameters)

def test_missing_mapping(self):
mapping = self.mapping1
mapping = copy.deepcopy(self.mapping1)
mapping.pop('v')

subtemplates = [(self.square, mapping)]
with self.assertRaises(MissingMappingException):
SequencePulseTemplate(subtemplates, self.outer_parameters)

def test_unnecessary_mapping(self):
mapping = self.mapping1
mapping = copy.deepcopy(self.mapping1)
mapping['unnecessary'] = 'voltage'

subtemplates = [(self.square, mapping)]
Expand All @@ -57,6 +57,18 @@ def test_identifier(self):
pulse = SequencePulseTemplate([], [], identifier=identifier)
self.assertEqual(identifier, pulse.identifier)

def test_identity_mapping_tuple(self):
mapping = copy.deepcopy(self.mapping1)
subtemplates = [(self.square,)]
pulse = SequencePulseTemplate(subtemplates, self.square.parameter_names)
self.assertEqual(self.square.parameter_names, pulse.parameter_names)

def test_identity_mapping_direct(self):
mapping = copy.deepcopy(self.mapping1)
subtemplates = [self.square]
pulse = SequencePulseTemplate(subtemplates, self.square.parameter_names)
self.assertEqual(self.square.parameter_names, pulse.parameter_names)


class SequencePulseTemplateSerializationTests(unittest.TestCase):

Expand Down Expand Up @@ -147,7 +159,7 @@ def test_str(self):
a = [UnnecessaryMappingException(T,"b"),
MissingMappingException(T,"b"),
MissingParameterDeclarationException(T, "c")]

b = [x.__str__() for x in a]
for s in b:
self.assertIsInstance(s, str)
Expand All @@ -158,11 +170,11 @@ def test_is_interruptable(self):
self.assertTrue(self.sequence.is_interruptable)
self.sequence.is_interruptable = False
self.assertFalse(self.sequence.is_interruptable)

def test_parameter_declarations(self):
decl = self.sequence.parameter_declarations
self.assertEqual(decl, set([ParameterDeclaration(i) for i in self.outer_parameters]))

def test_requires_stop(self):
seq = SequencePulseTemplate([],[])
self.assertFalse(seq.requires_stop({}, {}))
Expand Down

0 comments on commit 3ca0a1d

Please sign in to comment.