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_sequence_empty(self) -> None: table = TablePulseTemplate() sequencer = DummySequencer() instruction_block = DummyInstructionBlock() table.build_sequence(sequencer, {}, {}, instruction_block) self.assertFalse(instruction_block.instructions) self.assertIsNone(table.build_waveform({}))
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_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} measurement_mapping = {'M': 'P', 'N': 'N'} 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_sequence_multi_one_channel_empty(self) -> None: table = TablePulseTemplate(channels=2) table.add_entry('foo', 4) parameters = {'foo': 3} instantiated_entries = table.get_entries_instantiated(parameters) sequencer = DummySequencer() instruction_block = DummyInstructionBlock() table.build_sequence(sequencer, parameters, {}, instruction_block) expected_waveform = TableWaveform(instantiated_entries) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] self.assertIsInstance(instruction, EXECInstruction) self.assertEqual(expected_waveform, instruction.waveform) waveform = table.build_waveform(parameters) self.assertEqual(expected_waveform, waveform)
def test_build_sequence(self) -> None: table = TablePulseTemplate() foo_decl = ParameterDeclaration('foo', min=1) bar_decl = ParameterDeclaration('bar') table.add_entry(foo_decl, 'v', 'linear') table.add_entry(bar_decl, 0, 'jump') parameters = {'v': 2.3, 'foo': 1, 'bar': 4} instantiated_entries = tuple(table.get_entries_instantiated(parameters)) waveform = table.build_waveform(parameters) sequencer = DummySequencer() instruction_block = DummyInstructionBlock() table.build_sequence(sequencer, parameters, {}, instruction_block) expected_waveform = TableWaveform(instantiated_entries) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] self.assertIsInstance(instruction, EXECInstruction) self.assertEqual(expected_waveform, instruction.waveform) self.assertEqual(expected_waveform, waveform)
def test_build_waveform_empty(self) -> None: table = TablePulseTemplate(dict(a=[('t', 0)])) self.assertIsNone(table.build_waveform(dict(t=0), dict(a='a')))