def test_get_entries_instantiated_two_equal_entries(self) -> None: table = TablePulseTemplate({0: [(0, 0), (1, 5), (3, 5), (5, 1)]}) entries = table.get_entries_instantiated(dict()) expected = [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 5, HoldInterpolationStrategy()), TableEntry(3, 5, HoldInterpolationStrategy()), TableEntry(5, 1, HoldInterpolationStrategy()) ] self.assertEqual({0: expected}, entries)
def test_get_instantiated_entries_multi_same_time_param(self) -> None: table = TablePulseTemplate(channels=2) table.add_entry(1, 3) table.add_entry('foo', 'bar') table.add_entry(7, 3) table.add_entry(0, -5, channel=1) table.add_entry(0.5, -2, channel=1) table.add_entry('foo', 0, channel=1) table.add_entry(5, 'bar', channel=1) parameters = {'foo': 2.7, 'bar': -3.3} entries = table.get_entries_instantiated(parameters) expected = [ [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 3, HoldInterpolationStrategy()), TableEntry(2.7, -3.3, HoldInterpolationStrategy()), TableEntry(7, 3, HoldInterpolationStrategy()), ], [ TableEntry(0, -5, HoldInterpolationStrategy()), TableEntry(0.5, -2, HoldInterpolationStrategy()), TableEntry(2.7, 0, HoldInterpolationStrategy()), TableEntry(5, -3.3, HoldInterpolationStrategy()), TableEntry(7, -3.3, HoldInterpolationStrategy()) ] ] self.assertEqual(expected, entries)
def test_get_instantiated_entries_multi_same_time_param(self) -> None: table = TablePulseTemplate({ 0: [(1, 3), ('foo', 'bar'), (7, 3)], 1: [(0, -5), (0.5, -2), ('foo', 0), (5, 'bar')] }) parameters = {'foo': 2.7, 'bar': -3.3} entries = table.get_entries_instantiated(parameters) expected = { 0: [ TableEntry(0, 3, HoldInterpolationStrategy()), TableEntry(1, 3, HoldInterpolationStrategy()), TableEntry(2.7, -3.3, HoldInterpolationStrategy()), TableEntry(7, 3, HoldInterpolationStrategy()), ], 1: [ TableEntry(0, -5, HoldInterpolationStrategy()), TableEntry(0.5, -2, HoldInterpolationStrategy()), TableEntry(2.7, 0, HoldInterpolationStrategy()), TableEntry(5, -3.3, HoldInterpolationStrategy()), TableEntry(7, -3.3, HoldInterpolationStrategy()) ] } self.assertEqual(expected, entries)
def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries(self) -> None: table = TablePulseTemplate() table.add_entry(1, 5) table.add_entry(1.5, 5) table.add_entry(2, 5) table.add_entry(3, 0) entries = table.get_entries_instantiated({}) expected = [[ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 5, HoldInterpolationStrategy()), TableEntry(2, 5, HoldInterpolationStrategy()), TableEntry(3, 0, HoldInterpolationStrategy()) ]] self.assertEqual(expected, entries)
def test_get_entries_instantiated_two_equal_entries(self) -> None: table = TablePulseTemplate() table.add_entry(0, 0) table.add_entry(1, 5) table.add_entry(3, 5) table.add_entry(5, 1) entries = table.get_entries_instantiated({}) expected = [[ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 5, HoldInterpolationStrategy()), TableEntry(3, 5, HoldInterpolationStrategy()), TableEntry(5, 1, HoldInterpolationStrategy()) ]] self.assertEqual(expected, entries)
def test_single_channel_no_parameters(self): raw_entries = [(0., 1.1), (1.1, 2.), (2.2, 2.4)] table = TablePulseTemplate({0: raw_entries}) expected = [TableEntry(*entry) for entry in raw_entries] self.assertEqual(table.entries, dict([(0, expected)])) self.assertEqual(table.duration, 2.2) self.assertEqual(table.parameter_names, set())
def test_add_entry_time_and_voltage_same_declaration(self) -> None: table = TablePulseTemplate() with self.assertRaises(ValueError): table.add_entry('foo', 'foo') self.assertEquals([[TableEntry(0, 0, HoldInterpolationStrategy())]], table.entries) self.assertFalse(table.parameter_names) self.assertFalse(table.parameter_declarations)
def test_add_entry_empty_time_is_negative(self) -> None: table = TablePulseTemplate() with self.assertRaises(ValueError): table.add_entry(-2, 0) self.assertEqual([[TableEntry(0, 0, HoldInterpolationStrategy())]], table.entries) #self.assertFalse(table.entries[0]) self.assertFalse(table.parameter_declarations) self.assertFalse(table.parameter_names)
def test_from_array_1D(self) -> None: times = numpy.array([0, 1, 3]) voltages = numpy.array([5, 0, 5]) pulse = TablePulseTemplate.from_array(times, voltages) entries = [[]] for (time, voltage) in zip(times, voltages): entries[0].append(TableEntry(time, voltage, HoldInterpolationStrategy())) self.assertEqual(entries, pulse.entries)
def test_from_array_multi(self) -> None: times = numpy.array([0, 1, 3]) voltages = numpy.array([[1,2,3], [2,3,4]]).T # todo: why transposed?? pulse = TablePulseTemplate.from_array(times, voltages) entries = [[],[]] for i, channel in enumerate(voltages.T): for (time, voltage) in zip(times, channel): entries[i].append(TableEntry(time, voltage, HoldInterpolationStrategy())) self.assertEqual(entries, pulse.entries)
def test_get_entries_instantiated_two_channels_one_empty(self) -> None: table = TablePulseTemplate(channels=2) table.add_entry('foo', 4) parameters = {'foo': 10} entries = table.get_entries_instantiated(parameters) expected = [ [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(10, 4, HoldInterpolationStrategy()), ], [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(10, 0, HoldInterpolationStrategy()) ] ] self.assertEqual(expected, entries)
def test_known_interpolation_strategies(self): strategies = [("linear", LinearInterpolationStrategy()), ("hold", HoldInterpolationStrategy()), ("jump", JumpInterpolationStrategy())] for strat_name, strat_val in strategies: entry = TableEntry('a', Expression('b'), strat_name) self.assertEqual(entry.t, Expression('a')) self.assertEqual(entry.v, Expression('b')) self.assertEqual(entry.interp, strat_val)
def test_from_array_multi_one_voltage(self) -> None: times = numpy.array([[0, 1, 3], [2, 3, 4]]) voltages = numpy.array([1, 2, 3]) pulse = TablePulseTemplate.from_array(times, voltages, [0, 1]) entries = { i: [ TableEntry(time, voltage, HoldInterpolationStrategy()) for (time, voltage) in zip(times[i, :], voltages) ] for i in range(2) } self.assertEqual(entries, pulse.entries)
def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries_does_not_destroy_linear_interpolation(self) -> None: table = TablePulseTemplate() table.add_entry(0, 5) table.add_entry(2, 5, 'linear') table.add_entry(5, 5) table.add_entry(10, 0, 'linear') entries = table.get_entries_instantiated({}) expected = [[ TableEntry(0, 5, HoldInterpolationStrategy()), TableEntry(5, 5, HoldInterpolationStrategy()), TableEntry(10, 0, LinearInterpolationStrategy()) ]] self.assertEqual(expected, entries) result_sampled = TableWaveform(entries).sample(numpy.linspace(0, 10, 11), 0) numbers = [5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0] expected = [[float(x) for x in numbers]] self.assertEqual(expected, result_sampled.tolist())
def test_add_entry_time_declaration_invalid_bounds(self) -> None: table = TablePulseTemplate() bar_decl = ParameterDeclaration('bar') foo_decl = ParameterDeclaration('foo') foo_decl.min_value = bar_decl with self.assertRaises(ValueError): table.add_entry(foo_decl, 23857.23) with self.assertRaises(ValueError): table.add_entry(bar_decl, -4967.1) self.assertEquals([[TableEntry(0, 0, HoldInterpolationStrategy())]], table.entries) self.assertFalse(table.parameter_names) self.assertFalse(table.parameter_declarations)
def test_get_instaniated_entries_multi_one_empty_channel(self) -> None: table = TablePulseTemplate(channels=2) table.add_entry(1, 3, channel=1) table.add_entry('foo', 'bar', 'linear', channel=1) parameters = {'foo': 5.2, 'bar': -83.8} entries = table.get_entries_instantiated(parameters) expected = [ [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(5.2, 0, HoldInterpolationStrategy()) ], [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 3, HoldInterpolationStrategy()), TableEntry(5.2, -83.8, LinearInterpolationStrategy()) ] ] self.assertEqual(expected, entries)
def test_add_entry_multi_same_time_param(self) -> None: pulse = TablePulseTemplate(channels=2) pulse.add_entry(1, 3) pulse.add_entry('foo', 'bar') pulse.add_entry(7, 3) pulse.add_entry(0, -5, channel=1) pulse.add_entry(0.5, -2, channel=1) pulse.add_entry('foo', 0, channel=1) pulse.add_entry(5, 'bar', channel=1) expected_foo = ParameterDeclaration('foo', min=1, max=5) expected_bar = ParameterDeclaration('bar') entries = [[TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 3, HoldInterpolationStrategy()), TableEntry(expected_foo, expected_bar, HoldInterpolationStrategy()), TableEntry(7, 3, HoldInterpolationStrategy())], [TableEntry(0, -5, HoldInterpolationStrategy()), TableEntry(0.5, -2, HoldInterpolationStrategy()), TableEntry(expected_foo, 0, HoldInterpolationStrategy()), TableEntry(5, expected_bar, HoldInterpolationStrategy())]] self.assertEqual(entries, pulse.entries) self.assertEqual({'foo', 'bar'}, pulse.parameter_names) self.assertEqual({expected_bar, expected_foo}, pulse.parameter_declarations)
def test_slots(self): entry = TableEntry('a', Expression.make('b'), 'hold') self.assertFalse(hasattr(entry, '__dict__'))
def test_unknown_interpolation_strategy(self): with self.assertRaises(KeyError): TableEntry(0, 0, 'foo')