def test_constant_propagation(self): trafo = ChainedTransformation(ScalingTransformation({'a': 1.1}), OffsetTransformation({'b': 6.6})) self.assertTrue(trafo.is_constant_invariant()) trafo = ChainedTransformation(ScalingTransformation({'a': 1.1}), TransformationStub()) self.assertFalse(trafo.is_constant_invariant())
def _get_transformation( self, parameters: Mapping[str, Real], channel_mapping: Mapping[ChannelID, ChannelID]) -> Transformation: transformation = IdentityTransformation() scalar_value = self._get_scalar_value(parameters=parameters, channel_mapping=channel_mapping) if self._pulse_template is self._rhs: if self._arithmetic_operator == '-': # negate the pulse template transformation = transformation.chain( ScalingTransformation({ channel_mapping[ch]: -1 for ch in self.defined_channels if channel_mapping[ch] })) else: if self._arithmetic_operator == '-': for channel, value in scalar_value.items(): scalar_value[channel] = -value elif self._arithmetic_operator == '/': for channel, value in scalar_value.items(): scalar_value[channel] = 1 / value if self._arithmetic_operator in ('+', '-'): return transformation.chain(OffsetTransformation(scalar_value)) else: return transformation.chain(ScalingTransformation(scalar_value))
def test_trafo(self): trafo = OffsetTransformation(self.offsets) time = np.asarray([.5, .6]) in_data = {'A': np.asarray([.1, .2]), 'C': np.asarray([3., 4.])} expected = {'A': np.asarray([1.1, 1.2]), 'C': in_data['C']} out_data = trafo(time, in_data) self.assertIs(expected['C'], out_data['C']) np.testing.assert_equal(expected, out_data)
def test_internal_create_program(self): lhs = 'x + y' rhs = DummyPulseTemplate(defined_channels={'u', 'v', 'w'}) arith = ArithmeticPulseTemplate(lhs, '-', rhs) scope = DictScope.from_kwargs(x=3, y=5, z=8, volatile={'some_parameter'}) channel_mapping = dict(u='a', v='b', w=None) measurement_mapping = dict(m1='m2') global_transformation = OffsetTransformation({'unrelated': 1.}) to_single_waveform = {'something_else'} parent_loop = mock.Mock() expected_transformation = mock.Mock(spec=IdentityTransformation()) inner_trafo = mock.Mock(spec=IdentityTransformation()) inner_trafo.chain.return_value = expected_transformation with mock.patch.object(rhs, '_create_program') as inner_create_program: with mock.patch.object( arith, '_get_transformation', return_value=inner_trafo) as get_transformation: arith._internal_create_program( scope=scope, measurement_mapping=measurement_mapping, channel_mapping=channel_mapping, global_transformation=global_transformation, to_single_waveform=to_single_waveform, parent_loop=parent_loop) get_transformation.assert_called_once_with( parameters=scope, channel_mapping=channel_mapping) inner_trafo.chain.assert_called_once_with(global_transformation) inner_create_program.assert_called_once_with( scope=scope, measurement_mapping=measurement_mapping, channel_mapping=channel_mapping, global_transformation=expected_transformation, to_single_waveform=to_single_waveform, parent_loop=parent_loop) with self.assertRaisesRegex(NotImplementedError, 'volatile'): arith._internal_create_program( scope=DictScope.from_kwargs(x=3, y=5, z=8, volatile={'x'}), measurement_mapping=measurement_mapping, channel_mapping=channel_mapping, global_transformation=global_transformation, to_single_waveform=to_single_waveform, parent_loop=parent_loop)
def test_get_transformation(self): pulse_template = DummyPulseTemplate(defined_channels={'u', 'v', 'w'}) scalar = dict(a=1., b=2.) neg_scalar = dict(a=-1., b=-2.) inv_scalar = dict(a=1., b=1/2.) neg_trafo = ScalingTransformation(dict(a=-1, b=-1)) parameters = dict(x=3, y=5, z=8) channel_mapping = dict(u='a', v='b', w=None) # (PT + scalar) arith = ArithmeticPulseTemplate(pulse_template, '+', 'd') with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value: trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping) get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping) expected_trafo = OffsetTransformation(scalar) self.assertEqual(expected_trafo, trafo) # (scalar + PT) arith = ArithmeticPulseTemplate('d', '+', pulse_template) with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value: trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping) get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping) expected_trafo = OffsetTransformation(scalar) self.assertEqual(expected_trafo, trafo) # (PT - scalar) arith = ArithmeticPulseTemplate(pulse_template, '-', 'd') with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value: trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping) get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping) expected_trafo = OffsetTransformation(neg_scalar) self.assertEqual(expected_trafo, trafo) # (scalar - PT) arith = ArithmeticPulseTemplate('d', '-', pulse_template) with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value: trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping) get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping) expected_trafo = neg_trafo.chain(OffsetTransformation(scalar)) self.assertEqual(expected_trafo, trafo) # (PT * scalar) arith = ArithmeticPulseTemplate(pulse_template, '*', 'd') with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value: trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping) get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping) expected_trafo = ScalingTransformation(scalar) self.assertEqual(expected_trafo, trafo) # (scalar * PT) arith = ArithmeticPulseTemplate('d', '*', pulse_template) with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value: trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping) get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping) expected_trafo = ScalingTransformation(scalar) self.assertEqual(expected_trafo, trafo) # (PT / scalar) arith = ArithmeticPulseTemplate(pulse_template, '/', 'd') with mock.patch.object(arith, '_get_scalar_value', return_value=scalar.copy()) as get_scalar_value: trafo = arith._get_transformation(parameters=parameters, channel_mapping=channel_mapping) get_scalar_value.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping) expected_trafo = ScalingTransformation(inv_scalar) self.assertEqual(expected_trafo, trafo)
def test_repr(self): trafo = OffsetTransformation(self.scales) self.assertEqual(trafo, eval(repr(trafo)))
def test_compare_key(self): trafo = OffsetTransformation(self.scales) _ = hash(trafo) self.assertEqual(frozenset([('A', 1.5), ('B', 1.2)]), trafo.compare_key)
def test_constant_propagation(self): trafo = OffsetTransformation(self.offsets) self.assertTrue(trafo.is_constant_invariant())
def test_scalar_trafo_works(self): trafo = OffsetTransformation(self.offsets) assert_scalar_trafo_works(self, trafo, {'A': 0., 'B': 0.3, 'c': 0.6})
def test_get_input_channels(self): trafo = OffsetTransformation(self.offsets) channels = {'A', 'C'} self.assertIs(channels, trafo.get_input_channels(channels)) self.assertIs(channels, trafo.get_output_channels(channels))
def test_init(self): trafo = OffsetTransformation(self.offsets) # test copy self.assertIsNot(trafo._offsets, self.offsets) self.assertEqual(trafo._offsets, self.offsets)
def test_repr(self): trafo = ChainedTransformation(ScalingTransformation({'a': 1.1}), OffsetTransformation({'b': 6.6})) self.assertEqual(trafo, eval(repr(trafo)))