def test_build_waveform_time_type(self): from qupulse.utils.types import TimeType table = TablePulseTemplate({0: [(0, 0), ('foo', 'v', 'linear'), ('bar', 0, 'jump')]}, parameter_constraints=['foo>1'], measurements=[('M', 'b', 'l'), ('N', 1, 2)]) parameters = {'v': 2.3, 'foo': TimeType.from_float(1.), 'bar': TimeType.from_float(4), 'b': TimeType.from_float(2), 'l': TimeType.from_float(1)} channel_mapping = {0: 'ch'} with self.assertRaises(ParameterConstraintViolation): table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) parameters['foo'] = TimeType.from_float(1.1) waveform = table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) self.assertIsInstance(waveform, TableWaveform) self.assertEqual(waveform._table, ((0, 0, HoldInterpolationStrategy()), (TimeType.from_float(1.1), 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy()))) self.assertEqual(waveform._channel_id, 'ch')
def test_external_constraints(self): table = TablePulseTemplate( { 0: [(1, 'v'), (2, 'w')], 1: [('t', 'x'), ('t+2', 'y')] }, parameter_constraints=['x<h', 'y<w', 't<1']) self.assertEqual(table.parameter_names, {'v', 'w', 't', 'x', 'y', 'h'}) with self.assertRaises(ParameterConstraintViolation): table.build_waveform(parameters=dict(v=1., w=2, t=0.1, x=2.2, y=1, h=1), channel_mapping={ 0: 0, 1: 1 }) table.build_waveform(parameters=dict(v=1., w=2, t=0.1, x=1.2, y=1, h=2), channel_mapping={ 0: 0, 1: 1 })
def test_build_waveform_single_channel(self): table = TablePulseTemplate({0: [(0, 0), ('foo', 'v', 'linear'), ('bar', 0, 'jump')]}, parameter_constraints=['foo>1'], measurements=[('M', 'b', 'l'), ('N', 1, 2)]) parameters = {'v': 2.3, 'foo': 1, 'bar': 4, 'b': 2, 'l': 1} channel_mapping = {0: 'ch'} with self.assertRaises(ParameterConstraintViolation): table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) parameters['foo'] = 1.1 waveform = table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) self.assertIsInstance(waveform, TableWaveform) self.assertEqual(waveform._table, ((0, 0, HoldInterpolationStrategy()), (1.1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy()))) self.assertEqual(waveform._channel_id, 'ch')
def test_build_waveform_multi_channel(self): table = TablePulseTemplate( { 0: [(0, 0), ('foo', 'v', 'linear'), ('bar', 0, 'jump')], 3: [(0, 1), ('bar+foo', 0, 'linear')] }, parameter_constraints=['foo>1'], measurements=[('M', 'b', 'l'), ('N', 1, 2)]) parameters = {'v': 2.3, 'foo': 1, 'bar': 4, 'b': 2, 'l': 1} channel_mapping = {0: 'ch', 3: 'oh'} with self.assertRaises(ParameterConstraintViolation): table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) parameters['foo'] = 1.1 waveform = table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) self.assertIsInstance(waveform, MultiChannelWaveform) expected_waveforms = [ TableWaveform.from_table( 'ch', ((0, 0, HoldInterpolationStrategy()), (1.1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy()), (5.1, 0, HoldInterpolationStrategy()))), TableWaveform.from_table( 'oh', ((0, 1, HoldInterpolationStrategy()), (5.1, 0, LinearInterpolationStrategy()))), ] self.assertEqual(waveform._sub_waveforms, tuple(expected_waveforms))
def test_time_is_0_on_instantiation(self): table = TablePulseTemplate({0: [('a', 1)]}) self.assertEqual(table.duration, Expression('a')) self.assertEqual(table.parameter_names, {'a'}) self.assertIsNone( table.build_waveform(parameters=dict(a=0), channel_mapping={0: 0}))
def test_time_is_0_on_construction(self) -> None: with self.assertWarns(ZeroDurationTablePulseTemplate): warnings.simplefilter('default', ZeroDurationTablePulseTemplate) table = TablePulseTemplate({0: [(0, 1.4)]}) self.assertTrue(table.duration == 0) self.assertTrue(table.duration == 0) self.assertIsNone( table.build_waveform(parameters=dict(), channel_mapping={0: 0}))
def test_build_waveform_multi_channel(self): table = TablePulseTemplate({0: [(0, 0), ('foo', 'v', 'linear'), ('bar', 0, 'jump')], 3: [(0, 1), ('bar+foo', 0, 'linear')]}, parameter_constraints=['foo>1'], measurements=[('M', 'b', 'l'), ('N', 1, 2)]) parameters = {'v': 2.3, 'foo': 1, 'bar': 4, 'b': 2, 'l': 1} channel_mapping = {0: 'ch', 3: 'oh'} with self.assertRaises(ParameterConstraintViolation): table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) parameters['foo'] = 1.1 waveform = table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) self.assertIsInstance(waveform, MultiChannelWaveform) self.assertEqual(len(waveform._sub_waveforms), 2) channels = {'oh', 'ch'} for wf in waveform._sub_waveforms: self.assertIsInstance(wf, TableWaveform) self.assertIn(wf._channel_id, channels) channels.remove(wf._channel_id) if wf.defined_channels == {'ch'}: self.assertEqual(wf._table, ((0, 0, HoldInterpolationStrategy()), (1.1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy()), (5.1, 0, HoldInterpolationStrategy()))) elif wf.defined_channels == {'oh'}: self.assertEqual(wf._table, ((0, 1, HoldInterpolationStrategy()), (5.1, 0, LinearInterpolationStrategy())))
def test_build_waveform_none(self) -> None: table = TablePulseTemplate( { 0: [(0, 0), ('foo', 'v', 'linear'), ('bar', 0, 'jump')], 3: [(0, 1), ('bar+foo', 0, 'linear')] }, parameter_constraints=['foo>1'], measurements=[('M', 'b', 'l'), ('N', 1, 2)]) parameters = {'v': 2.3, 'foo': 1, 'bar': 4, 'b': 2, 'l': 1} channel_mapping = {0: None, 3: None} with self.assertRaises(ParameterConstraintViolation): table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) parameters['foo'] = 1.1 self.assertIsNone( table.build_waveform(parameters=parameters, channel_mapping=channel_mapping)) channel_mapping = {0: 1, 3: None} wf = table.build_waveform(parameters=parameters, channel_mapping=channel_mapping) self.assertEqual(wf.defined_channels, {1})
def test_as_expression(self): pulse = TablePulseTemplate( entries={ 0: [(0, 0), (1, 2), (3, 0, 'linear'), (4, 2, 'jump'), (5, 8, 'hold')], 'other_channel': [(0, 7), (2, 0, 'linear'), (10, 0)], 'symbolic': [(3, 'a'), ('b', 4, 'hold'), ('c', Expression('d'), 'linear')] }) parameters = dict(a=2., b=4, c=9, d=8) wf = pulse.build_waveform(parameters, channel_mapping={ 0: 0, 'other_channel': 'other_channel', 'symbolic': 'symbolic' }) expr = pulse._as_expression() ts = numpy.linspace(0, float(wf.duration), num=33) sampled = {ch: wf.get_sampled(ch, ts) for ch in pulse.defined_channels} from_expr = {} for ch, expected_vs in sampled.items(): ch_expr = expr[ch] ch_from_expr = [] for t, expected in zip(ts, expected_vs): params = { **parameters, TablePulseTemplate._AS_EXPRESSION_TIME: t } result = ch_expr.sympified_expression.subs(params, simultaneous=True) ch_from_expr.append(result) from_expr[ch] = ch_from_expr numpy.testing.assert_almost_equal(expected_vs, ch_from_expr)
def test_build_waveform_empty(self) -> None: table = TablePulseTemplate(dict(a=[('t', 0)])) self.assertIsNone(table.build_waveform(dict(t=0), dict(a='a')))