Example #1
0
    def test__create_program(self):
        parameters = {'a': ConstantParameter(.1), 'b': ConstantParameter(.2)}
        measurement_mapping = {'M': 'N'}
        channel_mapping = {'B': 'A'}
        global_transformation = TransformationStub()
        to_single_waveform = {'voll', 'toggo'}
        parent_loop = Loop()

        template = PulseTemplateStub()
        with mock.patch.object(
                template,
                '_internal_create_program') as _internal_create_program:
            template._create_program(
                parameters=parameters,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                global_transformation=global_transformation,
                to_single_waveform=to_single_waveform,
                parent_loop=parent_loop)

            _internal_create_program.assert_called_once_with(
                parameters=parameters,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                global_transformation=global_transformation,
                to_single_waveform=to_single_waveform,
                parent_loop=parent_loop)

            self.assertEqual(parent_loop, Loop())
Example #2
0
    def test_const_value(self):
        output_channels = {'c', 'd', 'e'}
        trafo = TransformationStub()
        inner_wf = WaveformStub()

        trafo_wf = TransformingWaveform(inner_wf, trafo)

        self.assertFalse(trafo_wf.is_constant())
        self.assertIsNone(trafo_wf.constant_value_dict())

        with mock.patch.object(trafo, 'is_constant_invariant', return_value=False) as is_constant_invariant:
            self.assertIsNone(trafo_wf.constant_value('A'))
            is_constant_invariant.assert_called_once_with()

        with mock.patch.object(trafo, 'is_constant_invariant', return_value=True):
            # all inputs constant
            inner_const_values = {'A': 1.1, 'B': 2.2}

            with mock.patch.object(trafo, 'get_input_channels', return_value=inner_const_values.keys()):
                with mock.patch.object(inner_wf, 'constant_value', side_effect=inner_const_values.values()) as constant_value:
                    with mock.patch.object(TransformationStub, '__call__', return_value={'C': mock.sentinel}) as call:
                        self.assertIs(trafo_wf.constant_value('C'), call.return_value['C'])
                        call.assert_called_once_with(0., inner_const_values)
                    self.assertEqual([mock.call(ch) for ch in inner_const_values], constant_value.call_args_list)

                inner_const_values['B'] = None
                with mock.patch.object(inner_wf, 'constant_value', side_effect=inner_const_values.values()) as constant_value:
                    self.assertIsNone(trafo_wf.constant_value('C'))
Example #3
0
    def test_internal_create_program(self):
        wf = DummyWaveform(duration=2.)
        body = PulseTemplateStub()

        rpt = RepetitionPulseTemplate(body,
                                      'n_rep*mul',
                                      measurements=[('m', 'a', 'b')])

        scope = DictScope.from_kwargs(n_rep=3,
                                      mul=2,
                                      a=.1,
                                      b=.2,
                                      irrelevant=42)
        measurement_mapping = {'m': 'l'}
        channel_mapping = {'x': 'Y'}
        global_transformation = TransformationStub()
        to_single_waveform = {'to', 'single', 'waveform'}

        program = Loop()
        expected_program = Loop(
            children=[Loop(children=[Loop(waveform=wf)], repetition_count=6)],
            measurements=[('l', .1, .2)])

        real_relevant_parameters = dict(n_rep=3, mul=2, a=0.1, b=0.2)

        with mock.patch.object(body,
                               '_create_program',
                               wraps=get_appending_internal_create_program(
                                   wf,
                                   always_append=True)) as body_create_program:
            with mock.patch.object(rpt, 'validate_scope') as validate_scope:
                with mock.patch.object(
                        rpt, 'get_repetition_count_value',
                        return_value=6) as get_repetition_count_value:
                    with mock.patch.object(rpt,
                                           'get_measurement_windows',
                                           return_value=[('l', .1, .2)
                                                         ]) as get_meas:
                        rpt._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=program)

                        self.assertEqual(program, expected_program)
                        body_create_program.assert_called_once_with(
                            scope=scope,
                            measurement_mapping=measurement_mapping,
                            channel_mapping=channel_mapping,
                            global_transformation=global_transformation,
                            to_single_waveform=to_single_waveform,
                            parent_loop=program.children[0])
                        validate_scope.assert_called_once_with(scope)
                        get_repetition_count_value.assert_called_once_with(
                            scope)
                        get_meas.assert_called_once_with(
                            scope, measurement_mapping)
Example #4
0
    def test_create_program(self) -> None:
        template = PulseTemplateStub(defined_channels={'A'},
                                     parameter_names={'foo'})
        parameters = {
            'foo': 2.126,
            'bar': -26.2,
            'hugo': 'exp(sin(pi/2))',
            'append_a_child': '1'
        }
        previous_parameters = parameters.copy()
        measurement_mapping = {'M': 'N'}
        previos_measurement_mapping = measurement_mapping.copy()
        channel_mapping = {'A': 'B'}
        previous_channel_mapping = channel_mapping.copy()
        volatile = {'foo'}

        expected_scope = DictScope.from_kwargs(foo=2.126,
                                               bar=-26.2,
                                               hugo=math.exp(
                                                   math.sin(math.pi / 2)),
                                               volatile=volatile,
                                               append_a_child=1)
        to_single_waveform = {'voll', 'toggo'}
        global_transformation = TransformationStub()

        expected_internal_kwargs = dict(
            scope=expected_scope,
            measurement_mapping=measurement_mapping,
            channel_mapping=channel_mapping,
            global_transformation=global_transformation,
            to_single_waveform=to_single_waveform)

        dummy_waveform = DummyWaveform()
        expected_program = Loop(children=[Loop(waveform=dummy_waveform)])

        with mock.patch.object(template,
                               '_create_program',
                               wraps=get_appending_internal_create_program(
                                   dummy_waveform)) as _create_program:
            program = template.create_program(
                parameters=parameters,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                to_single_waveform=to_single_waveform,
                global_transformation=global_transformation,
                volatile=volatile)
            _create_program.assert_called_once_with(**expected_internal_kwargs,
                                                    parent_loop=program)
        self.assertEqual(expected_program, program)
        self.assertEqual(previos_measurement_mapping, measurement_mapping)
        self.assertEqual(previous_channel_mapping, channel_mapping)
        self.assertEqual(previous_parameters, parameters)
Example #5
0
    def test_internal_create_program(self):
        sub_templates = PulseTemplateStub(defined_channels={'a'}, duration=ExpressionScalar('t1')),\
                        PulseTemplateStub(defined_channels={'a'}, duration=ExpressionScalar('t2'))

        wfs = DummyWaveform(duration=1), DummyWaveform(duration=2)

        spt = SequencePulseTemplate(*sub_templates,
                                    measurements=[('m', 'a', 'b')])

        kwargs = dict(scope=DictScope.from_kwargs(t1=.4,
                                                  t2=.5,
                                                  a=.1,
                                                  b=.2,
                                                  irrelevant=42),
                      measurement_mapping={'m': 'l'},
                      channel_mapping={'g': 'h'},
                      global_transformation=TransformationStub(),
                      to_single_waveform={'to', 'single', 'waveform'})

        program = Loop()

        expected_program = Loop(
            children=[Loop(waveform=wfs[0]),
                      Loop(waveform=wfs[1])],
            measurements=[('l', .1, .2)])

        with mock.patch.object(spt, 'validate_scope') as validate_scope:
            with mock.patch.object(spt,
                                   'get_measurement_windows',
                                   return_value=[('l', .1, .2)
                                                 ]) as get_measurement_windows:
                with mock.patch.object(sub_templates[0], '_create_program',
                                       wraps=get_appending_internal_create_program(wfs[0], True)) as create_0,\
                    mock.patch.object(sub_templates[1], '_create_program',
                                       wraps=get_appending_internal_create_program(wfs[1], True)) as create_1:

                    spt._internal_create_program(**kwargs, parent_loop=program)

                    self.assertEqual(expected_program, program)

                    validate_scope.assert_called_once_with(kwargs['scope'])
                    get_measurement_windows.assert_called_once_with(
                        kwargs['scope'], kwargs['measurement_mapping'])
                    create_0.assert_called_once_with(**kwargs,
                                                     parent_loop=program)
                    create_1.assert_called_once_with(**kwargs,
                                                     parent_loop=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())
Example #7
0
    def test_create_program(self) -> None:
        dt = DummyPulseTemplate(parameter_names={'i'},
                                waveform=DummyWaveform(duration=4.0, defined_channels={'A'}),
                                duration=4,
                                measurements=[('b', .2, .3)])
        flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('a', 'b', 'c'),
                                   measurements=[('A', 'meas_param', 1)], parameter_constraints=['c > 1'])

        scope = DictScope.from_kwargs(a=1, b=4, c=2, meas_param=.1, volatile={'inner'})
        measurement_mapping = dict(A='B', b='b')
        channel_mapping = dict(C='D')

        to_single_waveform = {'tom', 'jerry'}
        global_transformation = TransformationStub()

        program = Loop()

        # inner _create_program does nothing
        expected_program = Loop(measurements=[('B', .1, 1)])

        expected_create_program_kwargs = dict(measurement_mapping=measurement_mapping,
                                              channel_mapping=channel_mapping,
                                              global_transformation=global_transformation,
                                              to_single_waveform=to_single_waveform,
                                              parent_loop=program)
        expected_create_program_calls = [mock.call(**expected_create_program_kwargs,
                                                   scope=scope.overwrite(dict(i=i)))
                                         for i in (1, 3)]

        with mock.patch.object(flt, 'validate_scope') as validate_scope:
            with mock.patch.object(dt, '_create_program') as body_create_program:
                with mock.patch.object(flt, 'get_measurement_windows',
                                       wraps=flt.get_measurement_windows) as get_measurement_windows:
                    flt._internal_create_program(scope=scope,
                                                 measurement_mapping=measurement_mapping,
                                                 channel_mapping=channel_mapping,
                                                 parent_loop=program,
                                                 to_single_waveform=to_single_waveform,
                                                 global_transformation=global_transformation)

                    validate_scope.assert_called_once_with(scope=scope)
                    get_measurement_windows.assert_called_once_with(scope, measurement_mapping)
                    self.assertEqual(body_create_program.call_args_list, expected_create_program_calls)

        self.assertEqual(expected_program, program)
Example #8
0
    def test_create_program(self) -> None:
        template = PulseTemplateStub(defined_channels={'A'},
                                     parameter_names={'foo'})
        parameters = {
            'foo': ConstantParameter(2.126),
            'bar': -26.2,
            'hugo': 'exp(sin(pi/2))',
            'append_a_child': '1'
        }
        measurement_mapping = {'M': 'N'}
        channel_mapping = {'A': 'B'}

        expected_parameters = {
            'foo': ConstantParameter(2.126),
            'bar': ConstantParameter(-26.2),
            'hugo': ConstantParameter('exp(sin(pi/2))'),
            'append_a_child': ConstantParameter('1')
        }
        to_single_waveform = {'voll', 'toggo'}
        global_transformation = TransformationStub()

        expected_internal_kwargs = dict(
            parameters=expected_parameters,
            measurement_mapping=measurement_mapping,
            channel_mapping=channel_mapping,
            global_transformation=global_transformation,
            to_single_waveform=to_single_waveform)

        dummy_waveform = DummyWaveform()
        expected_program = Loop(children=[Loop(waveform=dummy_waveform)])

        with mock.patch.object(template,
                               '_create_program',
                               wraps=get_appending_internal_create_program(
                                   dummy_waveform)) as _create_program:
            program = template.create_program(
                parameters=parameters,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                to_single_waveform=to_single_waveform,
                global_transformation=global_transformation)
            _create_program.assert_called_once_with(**expected_internal_kwargs,
                                                    parent_loop=program)
        self.assertEqual(expected_program, program)
Example #9
0
    def test__create_program(self):
        scope = DictScope.from_kwargs(a=1., b=2., volatile={'c'})
        measurement_mapping = {'M': 'N'}
        channel_mapping = {'B': 'A'}
        global_transformation = TransformationStub()
        to_single_waveform = {'voll', 'toggo'}
        parent_loop = Loop()

        template = PulseTemplateStub()
        with mock.patch.object(
                template,
                '_internal_create_program') as _internal_create_program:
            template._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)

            _internal_create_program.assert_called_once_with(
                scope=scope,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                global_transformation=global_transformation,
                to_single_waveform=to_single_waveform,
                parent_loop=parent_loop)

            self.assertEqual(parent_loop, Loop())

            with self.assertRaisesRegex(NotImplementedError, "volatile"):
                template._parameter_names = {'c'}
                template._create_program(
                    scope=scope,
                    measurement_mapping=measurement_mapping,
                    channel_mapping=channel_mapping,
                    global_transformation=global_transformation,
                    to_single_waveform={template},
                    parent_loop=parent_loop)
Example #10
0
    def test_internal_create_program_transformation(self):
        inner_wf = DummyWaveform()
        template = AtomicPulseTemplateStub(parameter_names=set())
        program = Loop()
        global_transformation = TransformationStub()
        scope = DictScope.from_kwargs()
        expected_program = Loop(children=[
            Loop(
                waveform=TransformingWaveform(inner_wf, global_transformation))
        ])

        with mock.patch.object(template,
                               'build_waveform',
                               return_value=inner_wf):
            template._internal_create_program(
                scope=scope,
                measurement_mapping={},
                channel_mapping={},
                parent_loop=program,
                to_single_waveform=set(),
                global_transformation=global_transformation)

        self.assertEqual(expected_program, program)
Example #11
0
    def test__create_program_single_waveform(self):
        template = PulseTemplateStub(identifier='pt_identifier',
                                     parameter_names={'alpha'})

        for to_single_waveform in ({template}, {template.identifier}):
            for global_transformation in (None, TransformationStub()):
                scope = DictScope.from_kwargs(a=1., b=2., volatile={'a'})
                measurement_mapping = {'M': 'N'}
                channel_mapping = {'B': 'A'}
                parent_loop = Loop()

                wf = DummyWaveform()
                single_waveform = DummyWaveform()
                measurements = [('m', 0, 1), ('n', 0.1, .9)]

                expected_inner_program = Loop(children=[Loop(waveform=wf)],
                                              measurements=measurements)

                appending_create_program = get_appending_internal_create_program(
                    wf, measurements=measurements, always_append=True)

                if global_transformation:
                    final_waveform = TransformingWaveform(
                        single_waveform, global_transformation)
                else:
                    final_waveform = single_waveform

                expected_program = Loop(
                    children=[Loop(waveform=final_waveform)],
                    measurements=measurements)

                with mock.patch.object(template,
                                       '_internal_create_program',
                                       wraps=appending_create_program
                                       ) as _internal_create_program:
                    with mock.patch(
                            'qupulse.pulses.pulse_template.to_waveform',
                            return_value=single_waveform) as to_waveform:
                        template._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)

                        _internal_create_program.assert_called_once_with(
                            scope=scope,
                            measurement_mapping=measurement_mapping,
                            channel_mapping=channel_mapping,
                            global_transformation=None,
                            to_single_waveform=to_single_waveform,
                            parent_loop=expected_inner_program)

                        to_waveform.assert_called_once_with(
                            expected_inner_program)

                        expected_program._measurements = set(
                            expected_program._measurements)
                        parent_loop._measurements = set(
                            parent_loop._measurements)

                        self.assertEqual(expected_program, parent_loop)
Example #12
0
    def test_create_program(self) -> None:
        dt = DummyPulseTemplate(parameter_names={'i'},
                                waveform=DummyWaveform(duration=4.0,
                                                       defined_channels={'A'}),
                                duration=4,
                                measurements=[('b', .2, .3)])
        flt = ForLoopPulseTemplate(body=dt,
                                   loop_index='i',
                                   loop_range=('a', 'b', 'c'),
                                   measurements=[('A', 'meas_param', 1)],
                                   parameter_constraints=['c > 1'])

        parameters = {
            'a': ConstantParameter(1),
            'b': ConstantParameter(4),
            'c': ConstantParameter(2),
            'meas_param': ConstantParameter(.1)
        }
        measurement_mapping = dict(A='B', b='b')
        channel_mapping = dict(C='D')

        to_single_waveform = {'tom', 'jerry'}
        global_transformation = TransformationStub()

        expected_meas_params = {'meas_param': .1}

        program = Loop()

        # inner _create_program does nothing
        expected_program = Loop(measurements=[('B', .1, 1)])

        expected_create_program_kwargs = dict(
            measurement_mapping=measurement_mapping,
            channel_mapping=channel_mapping,
            global_transformation=global_transformation,
            to_single_waveform=to_single_waveform,
            parent_loop=program)
        expected_create_program_calls = [
            mock.call(**expected_create_program_kwargs,
                      parameters=dict(i=ConstantParameter(i))) for i in (1, 3)
        ]

        with mock.patch.object(flt, 'validate_parameter_constraints'
                               ) as validate_parameter_constraints:
            with mock.patch.object(dt,
                                   '_create_program') as body_create_program:
                with mock.patch.object(flt,
                                       'get_measurement_windows',
                                       wraps=flt.get_measurement_windows
                                       ) as get_measurement_windows:
                    flt._internal_create_program(
                        parameters=parameters,
                        measurement_mapping=measurement_mapping,
                        channel_mapping=channel_mapping,
                        parent_loop=program,
                        to_single_waveform=to_single_waveform,
                        global_transformation=global_transformation)

                    validate_parameter_constraints.assert_called_once_with(
                        parameters=parameters)
                    get_measurement_windows.assert_called_once_with(
                        expected_meas_params, measurement_mapping)
                    self.assertEqual(body_create_program.call_args_list,
                                     expected_create_program_calls)

        self.assertEqual(expected_program, program)