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_identity_result(self):
        self.assertIs(chain_transformations(), IdentityTransformation())

        self.assertIs(
            chain_transformations(IdentityTransformation(),
                                  IdentityTransformation()),
            IdentityTransformation())
Example #3
0
    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)
Example #4
0
    def test_build_waveform(self):
        pt = DummyPulseTemplate(defined_channels={'a'})

        parameters = dict(x=5., y=5.7)
        channel_mapping = dict(a='u', b='v')

        inner_wf = mock.Mock(spec=DummyWaveform)
        trafo = mock.Mock(spec=IdentityTransformation())

        arith = ArithmeticPulseTemplate(pt, '-', 6)

        with mock.patch.object(pt, 'build_waveform', return_value=None) as inner_build:
            with mock.patch.object(arith, '_get_transformation') as _get_transformation:
                self.assertIsNone(arith.build_waveform(parameters=parameters, channel_mapping=channel_mapping))
                inner_build.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)
                _get_transformation.assert_not_called()

        expected = TransformingWaveform(inner_wf, trafo)

        with mock.patch.object(pt, 'build_waveform', return_value=inner_wf) as inner_build:
            with mock.patch.object(arith, '_get_transformation', return_value=trafo) as _get_transformation:
                result = arith.build_waveform(parameters=parameters, channel_mapping=channel_mapping)
                inner_build.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)
                _get_transformation.assert_called_once_with(parameters=parameters, channel_mapping=channel_mapping)
        self.assertEqual(expected, result)
    def test_chaining(self):
        trafo = TransformationStub()

        expected = ChainedTransformation(trafo, trafo)

        result = chain_transformations(trafo, IdentityTransformation(), trafo)

        self.assertEqual(result, expected)
    def test_chain(self):
        trafo = TransformationStub()

        self.assertIs(trafo.chain(IdentityTransformation()), trafo)

        with mock.patch(
                'qupulse._program.transformation.chain_transformations',
                return_value='asd') as chain_transformations:
            self.assertEqual(trafo.chain(trafo), 'asd')
            chain_transformations.assert_called_once_with(trafo, trafo)
    def test_single_transformation(self):
        trafo = TransformationStub()

        self.assertIs(chain_transformations(trafo), trafo)
        self.assertIs(chain_transformations(trafo, IdentityTransformation()),
                      trafo)
 def test_constant_propagation(self):
     self.assertTrue(IdentityTransformation().is_constant_invariant())
 def test_repr(self):
     trafo = IdentityTransformation()
     self.assertEqual(trafo, eval(repr(trafo)))
 def test_scalar_trafo_works(self):
     assert_scalar_trafo_works(self, IdentityTransformation(), {
         'a': 0.,
         'b': 0.3,
         'c': 0.6
     })
 def test_chain(self):
     trafo = TransformationStub()
     self.assertIs(IdentityTransformation().chain(trafo), trafo)
 def test_input_channels(self):
     chans = {'a', 'b'}
     self.assertIs(IdentityTransformation().get_input_channels(chans),
                   chans)
 def test_call(self):
     time = np.arange(12)
     data = dict(zip('abc', (np.arange(12.) + 1).reshape((3, 4))))
     self.assertIs(IdentityTransformation()(time, data), data)
 def test_singleton(self):
     self.assertIs(IdentityTransformation(), IdentityTransformation())
 def test_compare_key(self):
     self.assertIsNone(IdentityTransformation().compare_key)