def make_kwargs(self): return { 'entries': dict(A=[('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')], B=[(0, 5, 'hold'), (1, 7, 'jump'), ('k', 't', 'hold')]), 'measurements': [('m', 1, 1), ('foo', 'z', 'o')], 'parameter_constraints': [str(ParameterConstraint('ilse>2')), str(ParameterConstraint('k>foo'))] }
def test_get_serialization_data(self) -> None: # test for deprecated version during transition period, remove after final switch with self.assertWarnsRegex(DeprecationWarning, "deprecated", msg="SequencePT does not issue warning for old serialization routines."): dummy_pt = DummyPulseTemplate(defined_channels={'foo'}, measurement_names={'meas'}, parameter_names={'hugo', 'herbert', 'ilse'}) mpt = MappingPulseTemplate( template=dummy_pt, parameter_mapping={'hugo': Expression('2*k+c'), 'herbert': Expression('c-1.5'), 'ilse': Expression('ilse')}, measurement_mapping={'meas': 'seam'}, channel_mapping={'foo': 'default_channel'}, parameter_constraints=[str(ParameterConstraint('c > 0'))] ) serializer = DummySerializer() expected_data = { 'template': serializer.dictify(dummy_pt), 'parameter_mapping': {'hugo': str(Expression('2*k+c')), 'herbert': str(Expression('c-1.5')), 'ilse': str(Expression('ilse'))}, 'measurement_mapping': {'meas': 'seam'}, 'channel_mapping': {'foo': 'default_channel'}, 'parameter_constraints': [str(ParameterConstraint('c > 0'))] } data = mpt.get_serialization_data(serializer=serializer) self.assertEqual(expected_data, data)
def make_kwargs(self): return { 'time_point_tuple_list': [('foo', 2, 'hold'), ('hugo', 'A + B', 'linear'), ('sudo', [1, 'a'], 'jump')], 'channel_names': (0, 'A'), 'measurements': [('m', 1, 1), ('foo', 'z', 'o')], 'parameter_constraints': [str(ParameterConstraint('ilse>2')), str(ParameterConstraint('k>foo'))] }
def test_str_and_serialization(self): self.assertEqual(str(ParameterConstraint('a < b')), 'a < b') self.assertEqual( ParameterConstraint('a < b').get_serialization_data(), 'a < b') self.assertEqual(str(ParameterConstraint('a==b')), 'a==b') self.assertEqual( ParameterConstraint('a==b').get_serialization_data(), 'a==b')
def test_parameter_constraints(self): to_test = self.to_test_constructor() self.assertEqual(to_test.parameter_constraints, []) to_test = self.to_test_constructor(['a < b']) self.assertEqual(to_test.parameter_constraints, [ParameterConstraint('a < b')]) to_test = self.to_test_constructor(['a < b', 'c < 1']) self.assertEqual(to_test.parameter_constraints, [ParameterConstraint('a < b'), ParameterConstraint('c < 1')])
def make_kwargs(self): return { 'expression': Expression('a + b * t'), 'duration_expression': Expression('c'), 'channel': 'A', 'measurements': [('mw', 1, 1), ('mw', 'x', 'z'), ('drup', 'j', 'u')], 'parameter_constraints': [str(ParameterConstraint('a < b')), str(ParameterConstraint('c > 1')), str(ParameterConstraint('d > c'))] }
def make_kwargs(self): return { 'subtemplates': [ DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}), DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'}) ], 'parameter_constraints': [ str(ParameterConstraint('ilse>2')), str(ParameterConstraint('k>foo')) ] }
def test_deserialize_all_features_old(self) -> None: # test for deprecated version during transition period, remove after final switch with self.assertWarnsRegex( DeprecationWarning, "deprecated", msg= "RepetitionPT does not issue warning for old serialization routines." ): serializer = DummySerializer( deserialize_callback=lambda x: x['name']) body = DummyPulseTemplate() data = dict(repetition_count='foo', body=dict(name=str(id(body))), identifier='foo', parameter_constraints=['foo < 3'], measurements=[('a', 0, 1), ('b', 1, 1)]) # prepare dependencies for deserialization serializer.subelements[str(id(body))] = body # deserialize template = RepetitionPulseTemplate.deserialize(serializer, **data) # compare! self.assertIs(body, template.body) self.assertEqual('foo', template.repetition_count) self.assertEqual(template.parameter_constraints, [ParameterConstraint('foo < 3')]) self.assertEqual(template.measurement_declarations, data['measurements'])
def make_kwargs(self): return { 'subtemplates': [DummyPulseTemplate(), DummyPulseTemplate()], 'parameter_constraints': [str(ParameterConstraint('a<b'))], 'measurements': [('m', 0, 1)] }
def test_deserialize_old(self) -> None: # test for deprecated version during transition period, remove after final switch with self.assertWarnsRegex(DeprecationWarning, "deprecated", msg="AtomicMultiChannelPT does not issue warning for old serialization routines."): sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}), DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'})] def deserialization_callback(ident: str): self.assertIn(ident, ('0', '1')) if ident == '0': return 0 else: return 1 serializer = DummySerializer(deserialize_callback=deserialization_callback) serializer.subelements = sts data = dict(subtemplates=['0', '1'], parameter_constraints=['a < d']) template = AtomicMultiChannelPulseTemplate.deserialize(serializer, **data) self.assertIs(template.subtemplates[0], sts[0]) self.assertIs(template.subtemplates[1], sts[1]) self.assertEqual(template.parameter_constraints, [ParameterConstraint('a < d')])
def test_deserialize_old(self) -> None: # test for deprecated version during transition period, remove after final switch with self.assertWarnsRegex( DeprecationWarning, "deprecated", msg= "SequencePT does not issue warning for old serialization routines." ): dummy1 = DummyPulseTemplate() dummy2 = DummyPulseTemplate() serializer = DummySerializer( serialize_callback=lambda x: str(id(x))) data = dict(subtemplates=[ serializer.dictify(dummy1), serializer.dictify(dummy2) ], identifier='foo', parameter_constraints=['a < b'], measurements=[('m', 0, 1)]) template = SequencePulseTemplate.deserialize(serializer, **data) self.assertEqual(template.subtemplates, [dummy1, dummy2]) self.assertEqual(template.parameter_constraints, [ParameterConstraint('a<b')]) self.assertEqual(template.measurement_declarations, [('m', 0, 1)])
def make_kwargs(self): return { 'body': DummyPulseTemplate(), 'repetition_count': 3, 'parameter_constraints': [str(ParameterConstraint('a<b'))], 'measurements': [('m', 0, 1)] }
def test_ordering(self): constraint = ParameterConstraint('a <= b') self.assertEqual(constraint.affected_parameters, {'a', 'b'}) self.assertTrue(constraint.is_fulfilled(dict(a=1, b=2))) self.assertTrue(constraint.is_fulfilled(dict(a=2, b=2))) self.assertFalse(constraint.is_fulfilled(dict(a=2, b=1)))
def make_kwargs(self): return { 'body': DummyPulseTemplate(parameter_names={'i'}), 'loop_index': 'i', 'loop_range': ('A', 'B', 1), 'parameter_constraints': [str(ParameterConstraint('foo < 3'))], 'measurements': [('a', 0, 1), ('b', 1, 1)] }
def make_kwargs(self): return { 'template': DummyPulseTemplate(defined_channels={'foo'}, measurement_names={'meas'}, parameter_names={'hugo', 'herbert', 'ilse'}), 'parameter_mapping': {'hugo': Expression('2*k+c'), 'herbert': Expression('c-1.5'), 'ilse': Expression('ilse')}, 'measurement_mapping': {'meas': 'seam'}, 'channel_mapping': {'foo': 'default_channel'}, 'parameter_constraints': [str(ParameterConstraint('c > 0'))] }
def test_deserialize(self) -> None: # test for deprecated version during transition period, remove after final switch with self.assertWarnsRegex( DeprecationWarning, "deprecated", msg= "SequencePT does not issue warning for old serialization routines." ): dummy_pt = DummyPulseTemplate( defined_channels={'foo'}, measurement_names={'meas'}, parameter_names={'hugo', 'herbert', 'ilse'}) serializer = DummySerializer() data = { 'template': serializer.dictify(dummy_pt), 'parameter_mapping': { 'hugo': str(Expression('2*k+c')), 'herbert': str(Expression('c-1.5')), 'ilse': str(Expression('ilse')) }, 'measurement_mapping': { 'meas': 'seam' }, 'channel_mapping': { 'foo': 'default_channel' }, 'parameter_constraints': [str(ParameterConstraint('c > 0'))] } deserialized = MappingPulseTemplate.deserialize( serializer=serializer, **data) self.assertIsInstance(deserialized, MappingPulseTemplate) self.assertEqual(data['parameter_mapping'], deserialized.parameter_mapping) self.assertEqual(data['channel_mapping'], deserialized.channel_mapping) self.assertEqual(data['measurement_mapping'], deserialized.measurement_mapping) self.assertEqual( data['parameter_constraints'], [str(pc) for pc in deserialized.parameter_constraints]) self.assertIs(deserialized.template, dummy_pt)
def test_repr(self): pc = ParameterConstraint('a < b') self.assertEqual("ParameterConstraint('a < b')", repr(pc))
def test_no_relation(self): with self.assertRaises(ValueError): ParameterConstraint('a*b') ParameterConstraint('1 < 2')
def test_expressions(self): constraint = ParameterConstraint('Max(a, b) < a*c') self.assertEqual(constraint.affected_parameters, {'a', 'b', 'c'}) self.assertTrue(constraint.is_fulfilled(dict(a=2, b=2, c=3))) self.assertFalse(constraint.is_fulfilled(dict(a=3, b=5, c=1)))
def test_str(self): self.assertEqual(str(ParameterConstraint('a < b')), 'a < b') self.assertEqual(str(ParameterConstraint('a==b')), 'a==b')