def test_constrained(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) st = MappingPulseTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}, parameter_constraints=['t < m']) external_params = {'t', 'l', 'k', 'm'} self.assertEqual(st.parameter_names, external_params) with self.assertRaises(ParameterConstraintViolation): st.map_parameters(dict(t=1, l=2, k=3, m=0))
def test_map_parameters(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) st = MappingPulseTemplate(template, parameter_mapping={ 'foo': 't*k', 'bar': 't*l' }) parameters = { 't': ConstantParameter(3), 'k': ConstantParameter(2), 'l': ConstantParameter(7) } values = {'foo': 6, 'bar': 21} for k, v in st.map_parameters(parameters).items(): self.assertEqual(v.get_value(), values[k]) parameters.popitem() with self.assertRaises(ParameterNotProvidedException): st.map_parameters(parameters) parameters = dict(t=3, k=2, l=7) values = {'foo': 6, 'bar': 21} for k, v in st.map_parameters(parameters).items(): self.assertEqual(v, values[k]) with self.assertRaisesRegex(ValueError, "type of return value"): st.map_parameters({}) parameters = dict(t=3, k=2, l=ConstantParameter(7)) with self.assertRaisesRegex(TypeError, "neither all Parameter nor Real"): st.map_parameters(parameters)
def test_build_sequence(self): measurement_mapping = {'meas1': 'meas2'} parameter_mapping = {'t': 'k'} template = DummyPulseTemplate( measurement_names=set(measurement_mapping.keys()), parameter_names=set(parameter_mapping.keys())) st = MappingPulseTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=measurement_mapping) sequencer = DummySequencer() block = DummyInstructionBlock() pre_parameters = {'k': ConstantParameter(5)} pre_measurement_mapping = {'meas2': 'meas3'} pre_channel_mapping = {'default': 'A'} conditions = dict(a=True) st.build_sequence(sequencer, pre_parameters, conditions, pre_measurement_mapping, pre_channel_mapping, block) self.assertEqual(template.build_sequence_calls, 1) forwarded_args = template.build_sequence_arguments[0] self.assertEqual(forwarded_args[0], sequencer) self.assertEqual(forwarded_args[1], st.map_parameters(pre_parameters)) self.assertEqual(forwarded_args[2], conditions) self.assertEqual( forwarded_args[3], st.get_updated_measurement_mapping(pre_measurement_mapping)) self.assertEqual(forwarded_args[4], st.get_updated_channel_mapping(pre_channel_mapping)) self.assertEqual(forwarded_args[5], block)
def test_create_program_subtemplate_none(self) -> None: measurement_mapping = {'meas1': 'meas2'} parameter_mapping = {'t': 'k'} channel_mapping = {'B': 'default'} template = DummyPulseTemplate(measurements=[('meas1', 0, 1)], measurement_names={'meas1'}, defined_channels={'B'}, waveform=None, duration=0, parameter_names={'t'}) st = MappingPulseTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=measurement_mapping, channel_mapping=channel_mapping) pre_parameters = {'k': ConstantParameter(5)} pre_measurement_mapping = {'meas2': 'meas3'} pre_channel_mapping = {'default': 'A'} program = Loop() st._internal_create_program( parameters=pre_parameters, measurement_mapping=pre_measurement_mapping, channel_mapping=pre_channel_mapping, to_single_waveform=set(), global_transformation=None, parent_loop=program) self.assertEqual(1, len(template.create_program_calls)) self.assertEqual( (st.map_parameters(pre_parameters), st.get_updated_measurement_mapping(pre_measurement_mapping), st.get_updated_channel_mapping(pre_channel_mapping), program), template.create_program_calls[-1]) self.assertEqual(1, program.repetition_count) self.assertEqual(0, len(program.children)) self.assertIsNone(program._measurements) # ensure same result as from Sequencer sequencer = Sequencer() sequencer.push(st, parameters=pre_parameters, conditions={}, window_mapping=pre_measurement_mapping, channel_mapping=pre_channel_mapping) block = sequencer.build() program_old = MultiChannelProgram( block, channels={'A'}).programs[frozenset({'A'})] self.assertEqual(program_old, program)
def test_create_program(self) -> None: measurement_mapping = {'meas1': 'meas2'} parameter_mapping = {'t': 'k'} channel_mapping = {'B': 'default'} global_transformation = TransformationStub() to_single_waveform = {'tom', 'jerry'} template = DummyPulseTemplate(measurements=[('meas1', 0, 1)], measurement_names={'meas1'}, defined_channels={'B'}, waveform=DummyWaveform(duration=2.0), duration=2, parameter_names={'t'}) st = MappingPulseTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=measurement_mapping, channel_mapping=channel_mapping) pre_parameters = {'k': ConstantParameter(5)} pre_measurement_mapping = {'meas2': 'meas3'} pre_channel_mapping = {'default': 'A'} program = Loop() expected_inner_args = dict( parameters=st.map_parameters(pre_parameters), measurement_mapping=st.get_updated_measurement_mapping( pre_measurement_mapping), channel_mapping=st.get_updated_channel_mapping( pre_channel_mapping), to_single_waveform=to_single_waveform, global_transformation=global_transformation, parent_loop=program) with mock.patch.object(template, '_create_program') as inner_create_program: st._internal_create_program( parameters=pre_parameters, measurement_mapping=pre_measurement_mapping, channel_mapping=pre_channel_mapping, to_single_waveform=to_single_waveform, global_transformation=global_transformation, parent_loop=program) inner_create_program.assert_called_once_with(**expected_inner_args) # as we mock the inner function there shouldnt be any changes self.assertEqual(program, Loop())