Exemplo n.º 1
0
    def test_call(self):
        in_chs = ('a', 'b', 'c')
        out_chs = ('transformed_a', 'transformed_b')
        matrix = np.array([[1, -1, 0], [1, 1, 1]])
        trafo = LinearTransformation(matrix, in_chs, out_chs)

        raw_data = (np.arange(12.) + 1).reshape((3, 4))
        data = dict(zip('abc', raw_data))

        data['ignored'] = np.arange(116., 120.)

        transformed = trafo(np.full(4, np.NaN), data)

        expected = {
            'transformed_a': data['a'] - data['b'],
            'transformed_b': np.sum(raw_data, axis=0),
            'ignored': np.arange(116., 120.)
        }

        np.testing.assert_equal(expected, transformed)

        data.pop('c')
        with self.assertRaisesRegex(KeyError, 'Invalid input channels'):

            trafo(np.full(4, np.NaN), data)

        in_chs = ('a', 'b', 'c')
        out_chs = ('a', 'b', 'c')
        matrix = np.eye(3)
        trafo = LinearTransformation(matrix, in_chs, out_chs)

        data_in = {'ignored': np.arange(116., 120.)}
        transformed = trafo(np.full(4, np.NaN), data_in)
        np.testing.assert_equal(transformed, data_in)
        self.assertIs(data_in['ignored'], transformed['ignored'])
Exemplo n.º 2
0
    def test_scalar_trafo_works(self):
        in_chs = ('a', 'b', 'c')
        out_chs = ('transformed_a', 'transformed_b')
        matrix = np.array([[1, -1, 0], [1, 1, 1]])
        trafo = LinearTransformation(matrix, in_chs, out_chs)

        assert_scalar_trafo_works(self, trafo, {'a': 0., 'b': 0.3, 'c': 0.6})
    def test_internal_create_program(self):
        template = DummyPulseTemplate(duration='t1', defined_channels={'X', 'Y'}, parameter_names={'a', 'b'},
                                      measurement_names={'M'}, waveform=DummyWaveform())
        overwritten_channels = {'Y': 'c', 'Z': 'a'}

        parent_loop = object()
        measurement_mapping = object()
        channel_mapping = object()
        to_single_waveform = object()

        other_kwargs = dict(measurement_mapping=measurement_mapping,
                            channel_mapping=channel_mapping,
                            to_single_waveform=to_single_waveform,
                            parent_loop=parent_loop)
        pccpt = ParallelConstantChannelPulseTemplate(template, overwritten_channels)

        scope = DictScope.from_kwargs(c=1.2, a=3.4)
        kwargs = {**other_kwargs, 'scope': scope, 'global_transformation': None}

        expected_overwritten_channels = {'Y': 1.2, 'Z': 3.4}
        expected_transformation = ParallelConstantChannelTransformation(expected_overwritten_channels)
        expected_kwargs = {**kwargs, 'global_transformation': expected_transformation}

        with mock.patch.object(template, '_create_program', spec=template._create_program) as cp_mock:
            pccpt._internal_create_program(**kwargs)
            cp_mock.assert_called_once_with(**expected_kwargs)

        global_transformation = LinearTransformation(numpy.zeros((0, 0)), [], [])
        expected_transformation = chain_transformations(global_transformation, expected_transformation)
        kwargs = {**other_kwargs, 'scope': scope, 'global_transformation': global_transformation}
        expected_kwargs = {**kwargs, 'global_transformation': expected_transformation}

        with mock.patch.object(template, '_create_program', spec=template._create_program) as cp_mock:
            pccpt._internal_create_program(**kwargs)
            cp_mock.assert_called_once_with(**expected_kwargs)
Exemplo n.º 4
0
    def test_get_output_channels(self):
        in_chs = ('a', 'b', 'c')
        out_chs = ('transformed_a', 'transformed_b')
        matrix = np.array([[1, -1, 0], [1, 1, 1]])
        trafo = LinearTransformation(matrix, in_chs, out_chs)

        self.assertEqual(trafo.get_output_channels({'a', 'b', 'c'}),
                         {'transformed_a', 'transformed_b'})
        with self.assertRaisesRegex(KeyError, 'Invalid input channels'):
            trafo.get_output_channels({'a', 'b'})
Exemplo n.º 5
0
    def test_compare_key_and_init(self):
        in_chs = ('a', 'b', 'c')
        out_chs = ('transformed_a', 'transformed_b')
        matrix = np.array([[1, -1, 0], [1, 1, 1]])

        with self.assertRaisesRegex(ValueError, 'Shape'):
            LinearTransformation(matrix, in_chs[:-1], out_chs)
        trafo = LinearTransformation(matrix, in_chs, out_chs)

        in_chs_2 = ('a', 'c', 'b')
        out_chs_2 = ('transformed_b', 'transformed_a')
        matrix_2 = np.array([[1, 1, 1], [1, 0, -1]])
        trafo_2 = LinearTransformation(matrix_2, in_chs_2, out_chs_2)

        self.assertEqual(trafo.compare_key, trafo_2.compare_key)
        self.assertEqual(trafo, trafo_2)
        self.assertEqual(hash(trafo), hash(trafo_2))
        self.assertEqual(trafo.compare_key,
                         (in_chs, out_chs, matrix.tobytes()))
Exemplo n.º 6
0
    def test_internal_create_program(self):
        template = DummyPulseTemplate(duration='t1',
                                      defined_channels={'X', 'Y'},
                                      parameter_names={'a', 'b'},
                                      measurement_names={'M'},
                                      waveform=DummyWaveform())
        overwritten_channels = {'Y': 'c', 'Z': 'a'}

        parent_loop = object()
        measurement_mapping = object()
        channel_mapping = object()
        to_single_waveform = object()

        other_kwargs = dict(measurement_mapping=measurement_mapping,
                            channel_mapping=channel_mapping,
                            to_single_waveform=to_single_waveform,
                            parent_loop=parent_loop)
        pccpt = ParallelConstantChannelPulseTemplate(template,
                                                     overwritten_channels)

        parameters = {'c': ConstantParameter(1.2), 'a': ConstantParameter(3.4)}
        kwargs = {
            **other_kwargs, 'parameters': parameters.copy(),
            'global_transformation': None
        }

        expected_overwritten_channels = {'Y': 1.2, 'Z': 3.4}
        expected_transformation = ParallelConstantChannelTransformation(
            expected_overwritten_channels)
        expected_kwargs = {
            **kwargs, 'global_transformation': expected_transformation
        }

        template._create_program = mock.Mock()
        pccpt._internal_create_program(**kwargs)
        template._create_program.assert_called_once_with(**expected_kwargs)

        global_transformation = LinearTransformation(numpy.zeros((0, 0)), [],
                                                     [])
        expected_transformation = chain_transformations(
            global_transformation, expected_transformation)
        kwargs = {
            **other_kwargs, 'parameters': parameters.copy(),
            'global_transformation': global_transformation
        }
        expected_kwargs = {
            **kwargs, 'global_transformation': expected_transformation
        }

        template._create_program = mock.Mock()
        pccpt._internal_create_program(**kwargs)
        template._create_program.assert_called_once_with(**expected_kwargs)
Exemplo n.º 7
0
    def test_get_input_channels(self):
        in_chs = ('a', 'b', 'c')
        out_chs = ('transformed_a', 'transformed_b')
        matrix = np.array([[1, -1, 0], [1, 1, 1]])
        trafo = LinearTransformation(matrix, in_chs, out_chs)

        self.assertEqual(trafo.get_input_channels({'transformed_a'}),
                         {'a', 'b', 'c'})
        self.assertEqual(trafo.get_input_channels({'transformed_a', 'd'}),
                         {'a', 'b', 'c', 'd'})
        self.assertEqual(trafo.get_input_channels({'d'}), {'d'})
        with self.assertRaisesRegex(KeyError, 'Is input channel'):
            self.assertEqual(trafo.get_input_channels({'transformed_a', 'a'}),
                             {'a', 'b', 'c', 'd'})

        in_chs = ('a', 'b', 'c')
        out_chs = ('a', 'b', 'c')
        matrix = np.eye(3)

        trafo = LinearTransformation(matrix, in_chs, out_chs)
        in_set = {'transformed_a'}
        self.assertIs(trafo.get_input_channels(in_set), in_set)
        self.assertEqual(trafo.get_input_channels({'transformed_a', 'a'}),
                         {'transformed_a', 'a', 'b', 'c'})
Exemplo n.º 8
0
 def test_constant_propagation(self):
     in_chs = ('a', 'b', 'c')
     out_chs = ('transformed_a', 'transformed_b')
     matrix = np.array([[1, -1, 0], [1, 1, 1]])
     trafo = LinearTransformation(matrix, in_chs, out_chs)
     self.assertTrue(trafo.is_constant_invariant())
Exemplo n.º 9
0
 def test_repr(self):
     in_chs = ('a', 'b', 'c')
     out_chs = ('transformed_a', 'transformed_b')
     matrix = np.array([[1, -1, 0], [1, 1, 1]])
     trafo = LinearTransformation(matrix, in_chs, out_chs)
     self.assertEqual(trafo, eval(repr(trafo)))