def test_requires_stop(self): parameters = dict(A=DummyParameter(requires_stop=False), B=DummyParameter(requires_stop=False)) dt = DummyPulseTemplate(parameter_names={'i'}) flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('A', 'B')) self.assertFalse(flt.requires_stop(parameters, dict())) parameters['A'] = DummyParameter(requires_stop=True) self.assertTrue(flt.requires_stop(parameters, dict()))
def test_deserialize_all_features(self): body_str = 'dt' dt = DummyPulseTemplate(parameter_names={'i'}) measurements = [('a', 0, 1), ('b', 1, 1)] parameter_constraints = ['foo < 3'] def make_dt(ident: str): self.assertEqual(body_str, ident) return ident data = dict(body=body_str, loop_range=('A', 'B', 1), loop_index='i', identifier='meh', measurements=measurements, parameter_constraints=parameter_constraints) serializer = DummySerializer(deserialize_callback=make_dt) serializer.subelements['dt'] = dt flt = ForLoopPulseTemplate.deserialize(serializer, **data) self.assertEqual(flt.identifier, 'meh') self.assertIs(flt.body, dt) self.assertEqual(flt.loop_index, 'i') self.assertEqual(flt.loop_range.to_tuple(), ('A', 'B', 1)) self.assertEqual(flt.measurement_declarations, measurements) self.assertEqual([str(c) for c in flt.parameter_constraints], parameter_constraints)
def test_get_serialization_data_minimal(self): dt = DummyPulseTemplate(parameter_names={'i'}) flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('A', 'B')) def check_dt(to_dictify) -> str: self.assertIs(to_dictify, dt) return 'dt' serializer = DummySerializer(serialize_callback=check_dt) data = flt.get_serialization_data(serializer) expected_data = dict(body='dt', loop_range=('A', 'B', 1), loop_index='i') self.assertEqual(data, expected_data)
def test_duration(self): dt = DummyPulseTemplate(parameter_names={'idx', 'd'}, duration=Expression('d+idx*2')) flt = ForLoopPulseTemplate(body=dt, loop_index='idx', loop_range='n') self.assertEqual(flt.duration.evaluate_numeric(n=4, d=100), 4 * 100 + 2 * (1 + 2 + 3)) flt = ForLoopPulseTemplate(body=dt, loop_index='idx', loop_range=(3, 'n', 2)) self.assertEqual(flt.duration.evaluate_numeric(n=9, d=100), 3*100 + 2*(3 + 5 + 7)) self.assertEqual(flt.duration.evaluate_numeric(n=8, d=100), 3 * 100 + 2 * (3 + 5 + 7)) flt = ForLoopPulseTemplate(body=dt, loop_index='idx', loop_range=('m', 'n', -2)) self.assertEqual(flt.duration.evaluate_numeric(n=9, d=100, m=14), 3 * 100 + 2 * (14 + 12 + 10)) flt = ForLoopPulseTemplate(body=dt, loop_index='idx', loop_range=('m', 'n', -2)) self.assertEqual(flt.duration.evaluate_numeric(n=9, d=100, m=14), 3 * 100 + 2*(14 + 12 + 10))
def test_body_parameter_generator(self): dt = DummyPulseTemplate(parameter_names={'i', 'k'}) flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('a', 'b', 'c')) expected_range = range(2, 17, 3) outer_params = dict(k=ConstantParameter(5), a=ConstantParameter(expected_range.start), b=ConstantParameter(expected_range.stop), c=ConstantParameter(expected_range.step)) forward_parameter_dicts = list(flt._body_parameter_generator(outer_params, forward=True)) backward_parameter_dicts = list(flt._body_parameter_generator(outer_params, forward=False)) self.assertEqual(forward_parameter_dicts, list(reversed(backward_parameter_dicts))) for local_params, i in zip(forward_parameter_dicts, expected_range): expected_local_params = dict(k=ConstantParameter(5), i=ConstantParameter(i)) self.assertEqual(expected_local_params, local_params)
def test_get_serialization_data_all_features(self): measurements = [('a', 0, 1), ('b', 1, 1)] parameter_constraints = ['foo < 3'] dt = DummyPulseTemplate(parameter_names={'i'}) flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('A', 'B'), measurements=measurements, parameter_constraints=parameter_constraints) def check_dt(to_dictify) -> str: self.assertIs(to_dictify, dt) return 'dt' serializer = DummySerializer(serialize_callback=check_dt) data = flt.get_serialization_data(serializer) expected_data = dict(body='dt', loop_range=('A', 'B', 1), loop_index='i', measurements=measurements, parameter_constraints=parameter_constraints) self.assertEqual(data, expected_data)
def test_build_sequence(self): dt = DummyPulseTemplate(parameter_names={'i'}) flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('a', 'b', 'c'), measurements=[('A', 0, 1)], parameter_constraints=['c > 1']) sequencer = DummySequencer() block = DummyInstructionBlock() invalid_parameters = {'a': ConstantParameter(1), 'b': ConstantParameter(4), 'c': ConstantParameter(1)} parameters = {'a': ConstantParameter(1), 'b': ConstantParameter(4), 'c': ConstantParameter(2)} measurement_mapping = dict(A='B') channel_mapping = dict(C='D') with self.assertRaises(ParameterConstraintViolation): flt.build_sequence(sequencer, invalid_parameters, dict(), measurement_mapping, channel_mapping, block) self.assertEqual(block.instructions, []) self.assertNotIn(block, sequencer.sequencing_stacks) flt.build_sequence(sequencer, parameters, dict(), measurement_mapping, channel_mapping, block) self.assertEqual(block.instructions, [MEASInstruction(measurements=[('B', 0, 1)])]) expected_stack = [(dt, {'i': ConstantParameter(3)}, dict(), measurement_mapping, channel_mapping), (dt, {'i': ConstantParameter(1)}, dict(), measurement_mapping, channel_mapping)] self.assertEqual(sequencer.sequencing_stacks[block], expected_stack)
def test_init(self): dt = DummyPulseTemplate(parameter_names={'i', 'k'}) self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=5).loop_range.to_tuple(), (0, 5, 1)) self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range='s').loop_range.to_tuple(), (0, 's', 1)) self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=(2, 5)).loop_range.to_tuple(), (2, 5, 1)) self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=range(1, 2, 5)).loop_range.to_tuple(), (1, 2, 5)) self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=ParametrizedRange('a', 'b', 'c')).loop_range.to_tuple(), ('a', 'b', 'c')) with self.assertRaises(InvalidParameterNameException): ForLoopPulseTemplate(body=dt, loop_index='1i', loop_range=6) with self.assertRaises(ValueError): ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=slice(None)) with self.assertRaises(LoopIndexNotUsedException): ForLoopPulseTemplate(body=DummyPulseTemplate(), loop_index='i', loop_range=1)
def test_deserialize_minimal(self): body_str = 'dt' dt = DummyPulseTemplate(parameter_names={'i'}) def make_dt(ident: str): self.assertEqual(body_str, ident) return ident data = dict(body=body_str, loop_range=('A', 'B', 1), loop_index='i', identifier='meh') serializer = DummySerializer(deserialize_callback=make_dt) serializer.subelements['dt'] = dt flt = ForLoopPulseTemplate.deserialize(serializer, **data) self.assertEqual(flt.identifier, 'meh') self.assertEqual(flt.body, dt) self.assertEqual(flt.loop_index, 'i') self.assertEqual(flt.loop_range.to_tuple(), ('A', 'B', 1))
def test_parameter_names(self): dt = DummyPulseTemplate(parameter_names={'i', 'k'}) flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('a', 'b', 'c')) self.assertEqual(flt.parameter_names, {'k', 'a', 'b', 'c'})
def test_loop_index(self): loop_index = 'i' dt = DummyPulseTemplate(parameter_names={'i', 'k'}) flt = ForLoopPulseTemplate(body=dt, loop_index=loop_index, loop_range=('a', 'b', 'c')) self.assertIs(loop_index, flt.loop_index)