def test_get_serialization_data(self) -> None: exp = Expression("foo + bar * hugo") p = MappedParameter(exp) serializer = DummySerializer() data = p.get_serialization_data(serializer) self.assertEqual( dict(type=serializer.get_type_identifier(p), expression=str(id(exp))), data)
def test_map_parameters(self) -> None: map = PulseTemplateParameterMapping({'bar', 'barbar'}) dummy = DummyPulseTemplate(parameter_names={'foo', 'hugo'}) map.add(dummy, 'hugo', '4*bar') map.add(dummy, 'foo', Expression('barbar')) mapped = map.map_parameters( dummy, dict(bar=ConstantParameter(3), barbar=ConstantParameter(5))) self.assertEqual( dict(hugo=MappedParameter(Expression('4*bar'), dict(bar=ConstantParameter(3))), foo=MappedParameter(Expression('barbar'), dict(barbar=ConstantParameter(5)))), mapped)
def map_parameters( self, template: PulseTemplate, parameters: Dict[str, Parameter]) -> Dict[str, Parameter]: """Map parameter values according to the defined mappings for a given pulse template. Args: template (PulseTemplate): The pulse template for which to map the parameter values. parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects/values. Returns: A new dictionary which maps parameter names to parameter values which have been mapped according to the mappings defined for template. """ missing = self.__external_parameters - set(parameters.keys()) if missing: raise ParameterNotProvidedException(missing.pop()) template_map = self.__get_template_map(template) inner_parameters = { parameter: MappedParameter(mapping_function, { name: parameters[name] for name in mapping_function.variables() }) for (parameter, mapping_function) in template_map.items() } return inner_parameters
def map_parameters( self, parameters: Dict[str, Union[Parameter, numbers.Real]] ) -> Dict[str, Parameter]: """Map parameter values according to the defined mappings. Args: parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects/values. Returns: A new dictionary which maps parameter names to parameter values which have been mapped according to the mappings defined for template. """ missing = set(self.__external_parameters) - set(parameters.keys()) if missing: raise ParameterNotProvidedException(missing.pop()) self.validate_parameter_constraints(parameters=parameters) if all( isinstance(parameter, Parameter) for parameter in parameters.values()): return { parameter: MappedParameter(mapping_function, { name: parameters[name] for name in mapping_function.variables }) for (parameter, mapping_function) in self.__parameter_mapping.items() } if all( isinstance(parameter, numbers.Real) for parameter in parameters.values()): return { parameter: mapping_function.evaluate_numeric(**parameters) for parameter, mapping_function in self.__parameter_mapping.items() } raise TypeError( 'Values of parameter dict are neither all Parameter nor Real')
def test_build_sequence(self) -> None: dummy_wf1 = DummyWaveform(duration=2.3, num_channels=2) dummy_wf2 = DummyWaveform(duration=2.3, num_channels=1) dummy1 = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2, waveform=dummy_wf1) dummy2 = DummyPulseTemplate(parameter_names={}, num_channels=1, waveform=dummy_wf2) pulse = MultiChannelPulseTemplate([(dummy1, { 'foo': '2*bar' }, [2, 1]), (dummy2, {}, [0])], {'bar'}) result = pulse.build_waveform({'bar': ConstantParameter(3)}) expected = MultiChannelWaveform([(dummy_wf1, [2, 1]), (dummy_wf2, [0])]) self.assertEqual(expected, result) self.assertEqual([{ 'foo': MappedParameter(Expression("2*bar"), {'bar': ConstantParameter(3)}) }], dummy1.build_waveform_calls) self.assertEqual([{}], dummy2.build_waveform_calls)
def test_deserialize(self) -> None: serializer = DummySerializer() exp = Expression("foo + bar * hugo") serializer.subelements[id(exp)] = exp p = MappedParameter.deserialize(serializer, expression=id(exp)) self.assertIsNone(p.identifier)
def test_repr(self) -> None: p = MappedParameter(Expression("foo + bar * hugo")) self.assertIsInstance(repr(p), str)
def test_requires_stop_and_get_value(self) -> None: p = MappedParameter(Expression("foo + bar * hugo")) with self.assertRaises(ParameterNotProvidedException): p.requires_stop with self.assertRaises(ParameterNotProvidedException): p.get_value() foo = DummyParameter(-1.1) bar = DummyParameter(0.5) hugo = DummyParameter(5.2, requires_stop=True) ilse = DummyParameter(2356.4, requires_stop=True) p.dependencies = {'foo': foo, 'hugo': hugo, 'ilse': ilse} with self.assertRaises(ParameterNotProvidedException): p.requires_stop with self.assertRaises(ParameterNotProvidedException): p.get_value() p.dependencies = {'foo': foo, 'bar': bar, 'hugo': hugo} self.assertTrue(p.requires_stop) with self.assertRaises(Exception): p.get_value() hugo = DummyParameter(5.2, requires_stop=False) p.dependencies = {'foo': foo, 'bar': bar, 'hugo': hugo, 'ilse': ilse} self.assertFalse(p.requires_stop) self.assertEqual(1.5, p.get_value())