Skip to content

Commit

Permalink
Raise better exceptions and test them
Browse files Browse the repository at this point in the history
  • Loading branch information
terrorfisch committed Jul 6, 2018
1 parent 89a3bc6 commit 87da872
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion qctoolkit/pulses/table_pulse_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,14 @@ def concatenate(*table_pulse_templates: TablePulseTemplate, **kwargs) -> TablePu
duration = ExpressionScalar(0)

for i, template in enumerate(table_pulse_templates):
if not isinstance(template, TablePulseTemplate):
raise TypeError('Template number %d is not a TablePulseTemplate' % i)

new_duration = duration + template.duration

if template.defined_channels != first_template.defined_channels:
raise ValueError()
raise ValueError('Template number %d has differing defined channels' % i,
first_template.defined_channels, template.defined_channels)

for channel, channel_entries in template.entries.items():
first_t, first_v, _ = channel_entries[0]
Expand Down
21 changes: 20 additions & 1 deletion tests/pulses/table_pulse_template_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from qctoolkit.pulses.interpolation import HoldInterpolationStrategy, LinearInterpolationStrategy, JumpInterpolationStrategy
from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform

from tests.pulses.sequencing_dummies import DummyInterpolationStrategy, DummyParameter, DummyCondition
from tests.pulses.sequencing_dummies import DummyInterpolationStrategy, DummyParameter, DummyCondition,\
DummyPulseTemplate
from tests.serialization_dummies import DummySerializer, DummyStorageBackend
from tests.pulses.measurement_tests import ParameterConstrainerTest, MeasurementDefinerTest

Expand Down Expand Up @@ -776,6 +777,24 @@ def test_duplication(self):

self.assertEqual(expected.entries, concatenated.entries)

def test_wrong_channels(self):
tpt_1 = TablePulseTemplate({'A': [(0, 1), ('a', 5, 'linear')],
'B': [(0, 2), ('b', 7)]})

tpt_2 = TablePulseTemplate({'A': [('c', 9), ('a', 10, 'jump')],
'C': [(0, 6), ('b', 8)]})

with self.assertRaisesRegex(ValueError, 'differing defined channels'):
concatenate(tpt_1, tpt_2)

def test_wrong_type(self):
dummy = DummyPulseTemplate()
tpt = TablePulseTemplate({'A': [(0, 1), ('a', 5, 'linear')],
'B': [(0, 2), ('b', 7)]})

with self.assertRaisesRegex(TypeError, 'not a TablePulseTemplate'):
concatenate(dummy, tpt)


if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 87da872

Please sign in to comment.