Exemplo n.º 1
0
def get_two_chan_test_block(wfg=WaveformGenerator(2)):
    generate_waveform = wfg.generate_single_channel_waveform
    generate_multi_channel_waveform = wfg.generate_multi_channel_waveform

    loop_block11 = InstructionBlock()
    loop_block11.add_instruction_exec(generate_multi_channel_waveform())

    loop_block1 = InstructionBlock()
    loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(loop_block11))

    loop_block21 = InstructionBlock()
    loop_block21.add_instruction_exec(generate_multi_channel_waveform())
    loop_block21.add_instruction_exec(generate_multi_channel_waveform())

    loop_block2 = InstructionBlock()
    loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(loop_block21))
    loop_block2.add_instruction_exec(generate_multi_channel_waveform())

    loop_block3 = InstructionBlock()
    loop_block3.add_instruction_exec(generate_multi_channel_waveform())
    loop_block3.add_instruction_exec(generate_multi_channel_waveform())

    loop_block411 = InstructionBlock()
    loop_block411.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')]))
    loop_block412 = InstructionBlock()
    loop_block412.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')]))

    loop_block41 = InstructionBlock()
    loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(loop_block411))
    loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(loop_block412))

    loop_block421 = InstructionBlock()
    loop_block421.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')]))
    loop_block422 = InstructionBlock()
    loop_block422.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')]))

    loop_block42 = InstructionBlock()
    loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(loop_block421))
    loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(loop_block422))

    chan_block4A = InstructionBlock()
    chan_block4A.add_instruction_repj(6, ImmutableInstructionBlock(loop_block41))

    chan_block4B = InstructionBlock()
    chan_block4B.add_instruction_repj(9, ImmutableInstructionBlock(loop_block42))

    loop_block4 = InstructionBlock()
    loop_block4.add_instruction_chan({frozenset('A'): ImmutableInstructionBlock(chan_block4A),
                                           frozenset('B'): ImmutableInstructionBlock(chan_block4B)})

    root_block = InstructionBlock()
    root_block.add_instruction_exec(generate_multi_channel_waveform())
    root_block.add_instruction_repj(10, ImmutableInstructionBlock(loop_block1))
    root_block.add_instruction_repj(17, ImmutableInstructionBlock(loop_block2))
    root_block.add_instruction_repj(3, ImmutableInstructionBlock(loop_block3))
    root_block.add_instruction_repj(4, ImmutableInstructionBlock(loop_block4))

    return root_block
Exemplo n.º 2
0
    def test_internal_create_program(self) -> None:
        measurement_windows = [('M', 0, 5)]
        single_wf = DummyWaveform(duration=6, defined_channels={'A'})
        wf = MultiChannelWaveform([single_wf])

        template = AtomicPulseTemplateStub(measurements=measurement_windows,
                                           parameter_names={'foo'})
        scope = DictScope.from_kwargs(foo=7.2, volatile={'gutes_zeuch'})
        measurement_mapping = {'M': 'N'}
        channel_mapping = {'B': 'A'}
        program = Loop()

        expected_program = Loop(children=[Loop(waveform=wf)],
                                measurements=[('N', 0, 5)])

        with mock.patch.object(template, 'build_waveform',
                               return_value=wf) as build_waveform:
            template._internal_create_program(
                scope=scope,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                parent_loop=program,
                to_single_waveform=set(),
                global_transformation=None)
            build_waveform.assert_called_once_with(
                parameters=scope, channel_mapping=channel_mapping)

        self.assertEqual(expected_program, program)

        # MultiChannelProgram calls cleanup
        program.cleanup()
Exemplo n.º 3
0
    def build_waveform(
        self, parameters: Dict[str, numbers.Real],
        channel_mapping: Dict[ChannelID, Optional[ChannelID]]
    ) -> Optional[Union[TableWaveform, MultiChannelWaveform]]:
        self.validate_parameter_constraints(parameters, volatile=set())

        if all(channel_mapping[channel] is None
               for channel in self.defined_channels):
            return None

        instantiated = [(channel_mapping[channel], instantiated_channel)
                        for channel, instantiated_channel in
                        self.get_entries_instantiated(parameters).items()
                        if channel_mapping[channel] is not None]

        if self.duration.evaluate_numeric(**parameters) == 0:
            return None

        waveforms = [
            TableWaveform(*ch_instantiated) for ch_instantiated in instantiated
        ]

        if len(waveforms) == 1:
            return waveforms.pop()
        else:
            return MultiChannelWaveform(waveforms)
Exemplo n.º 4
0
    def build_waveform(self,
                       parameters: Dict[str, Real],
                       channel_mapping: Dict[ChannelID, Optional[ChannelID]]) -> Optional[Union[TableWaveform,
                                                                                                MultiChannelWaveform]]:
        self.validate_parameter_constraints(parameters=parameters, volatile=set())

        if all(channel_mapping[channel] is None
               for channel in self.defined_channels):
            return None

        if self.duration.evaluate_numeric(**parameters) == 0:
            return None

        mapped_channels = tuple(channel_mapping[c] for c in self._channels)

        waveform_entries = list([] for _ in range(len(self._channels)))
        for entry in self._entries:
            instantiated_entries = entry.instantiate(parameters, len(self._channels))
            for ch_entries, wf_entry in zip(waveform_entries, instantiated_entries):
                ch_entries.append(wf_entry)

        if waveform_entries[0][0].t > 0:
            for ch_entries in waveform_entries:
                ch_entries[:0] = [PointWaveformEntry(0, ch_entries[0].v, ch_entries[0].interp)]

        # filter mappings to None
        channel_entries = [(ch, ch_entries)
                           for (ch, ch_entries) in zip(mapped_channels, waveform_entries)
                           if ch is not None]
        mapped_channels, waveform_entries = zip(*channel_entries)

        waveforms = [PointWaveform.from_table(mapped_channel, ch_entries)
                     for mapped_channel, ch_entries in zip(mapped_channels, waveform_entries)]

        return MultiChannelWaveform.from_parallel(waveforms)
Exemplo n.º 5
0
    def test_build_sequence(self):
        wfs = [
            DummyWaveform(duration=1.1, defined_channels={'A'}),
            DummyWaveform(duration=1.1, defined_channels={'B'})
        ]
        sts = [
            DummyPulseTemplate(duration='t1',
                               defined_channels={'A'},
                               waveform=wfs[0],
                               measurements=[('m', 0, 1)]),
            DummyPulseTemplate(duration='t1',
                               defined_channels={'B'},
                               waveform=wfs[1]),
            DummyPulseTemplate(duration='t1',
                               defined_channels={'C'},
                               waveform=None)
        ]

        pt = AtomicMultiChannelPulseTemplate(*sts,
                                             parameter_constraints=['a < b'],
                                             measurements=[('n', .1, .2)])

        params = dict(a=ConstantParameter(1.0), b=ConstantParameter(1.1))
        measurement_mapping = dict(m='foo', n='bar')
        channel_mapping = {'A': 'A', 'B': 'B', 'C': None}

        block = InstructionBlock()
        pt.build_sequence(None,
                          parameters=params,
                          conditions={},
                          measurement_mapping=measurement_mapping,
                          channel_mapping=channel_mapping,
                          instruction_block=block)

        expected_waveform = MultiChannelWaveform(wfs)

        expected_block = InstructionBlock()
        measurements = [('bar', .1, .2), ('foo', 0, 1)]
        expected_block.add_instruction_meas(measurements)
        expected_block.add_instruction_exec(waveform=expected_waveform)

        self.assertEqual(len(block.instructions),
                         len(expected_block.instructions))
        self.assertEqual(block.instructions[0].compare_key,
                         expected_block.instructions[0].compare_key)
        self.assertEqual(block.instructions[1].compare_key,
                         expected_block.instructions[1].compare_key)
Exemplo n.º 6
0
    def test_internal_create_program(self) -> None:
        measurement_windows = [('M', 0, 5)]
        single_wf = DummyWaveform(duration=6, defined_channels={'A'})
        wf = MultiChannelWaveform([single_wf])

        template = AtomicPulseTemplateStub(measurements=measurement_windows,
                                           parameter_names={'foo'})
        parameters = {'foo': ConstantParameter(7.2)}
        measurement_mapping = {'M': 'N'}
        channel_mapping = {'B': 'A'}
        program = Loop()

        expected_parameters = {k: p.get_value() for k, p in parameters.items()}
        expected_program = Loop(children=[Loop(waveform=wf)],
                                measurements=[('N', 0, 5)])

        with mock.patch.object(template, 'build_waveform',
                               return_value=wf) as build_waveform:
            template._internal_create_program(
                parameters=parameters,
                measurement_mapping=measurement_mapping,
                channel_mapping=channel_mapping,
                parent_loop=program,
                to_single_waveform=set(),
                global_transformation=None)
            build_waveform.assert_called_once_with(
                parameters=expected_parameters,
                channel_mapping=channel_mapping)

        self.assertEqual(expected_program, program)

        # ensure same result as from Sequencer
        sequencer = Sequencer()
        sequencer.push(template,
                       parameters=parameters,
                       conditions={},
                       window_mapping=measurement_mapping,
                       channel_mapping=channel_mapping)
        with mock.patch.object(template, 'build_waveform', return_value=wf):
            block = sequencer.build()
        old_program = MultiChannelProgram(block, channels={'A'})
        self.assertEqual(old_program.programs[frozenset({'A'})], program)
Exemplo n.º 7
0
    def build_waveform(
        self, parameters: Dict[str, numbers.Real],
        channel_mapping: Dict[ChannelID, Optional[ChannelID]]
    ) -> Optional[Union[ConstantWaveform, MultiChannelWaveform]]:
        logging.debug(
            f'build_waveform of ConstantPulse: channel_mapping {channel_mapping}, defined_channels {self.defined_channels} '
        )
        if all(
                channel_mapping.get(channel, None) is None
                for channel in self.defined_channels):
            return None

        waveforms = [
            ConstantWaveform(self.duration, amplitude, channel)
            for channel, amplitude in self._amplitude_dict.items()
            if channel_mapping[channel] is not None
        ]

        if len(waveforms) == 1:
            return waveforms.pop()
        else:
            return MultiChannelWaveform(waveforms)
Exemplo n.º 8
0
    def test_build_sequence(self) -> None:
        measurement_windows = [('M', 0, 5)]
        single_wf = DummyWaveform(duration=6, defined_channels={'A'})
        wf = MultiChannelWaveform([single_wf])

        sequencer = DummySequencer()
        block = DummyInstructionBlock()

        parameters = {
            'a': ConstantParameter(1),
            'b': ConstantParameter(2),
            'c': ConstantParameter(3)
        }
        expected_parameters = {'a': 1, 'b': 2}
        channel_mapping = {'B': 'A'}

        template = AtomicPulseTemplateStub(measurements=measurement_windows,
                                           parameter_names={'a', 'b'})
        with mock.patch.object(template, 'build_waveform',
                               return_value=wf) as build_waveform:
            template.build_sequence(sequencer,
                                    parameters=parameters,
                                    conditions={},
                                    measurement_mapping={'M': 'N'},
                                    channel_mapping=channel_mapping,
                                    instruction_block=block)
            build_waveform.assert_called_once_with(
                parameters=expected_parameters,
                channel_mapping=channel_mapping)
        self.assertEqual(len(block.instructions), 2)

        meas, exec = block.instructions
        self.assertIsInstance(meas, MEASInstruction)
        self.assertEqual(meas.measurements, [('N', 0, 5)])

        self.assertIsInstance(exec, EXECInstruction)
        self.assertEqual(exec.waveform.defined_channels, {'A'})
Exemplo n.º 9
0
 def generate_multi_channel_waveform(self):
     return MultiChannelWaveform([self.generate_single_channel_waveform(self.channel_names[ch_i])
                                  for ch_i in range(self.num_channels)])
Exemplo n.º 10
0
 def generate_multi_channel_waveform():
     return MultiChannelWaveform(
         [generate_waveform('A'),
          generate_waveform('B')])
Exemplo n.º 11
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        wf = DummyWaveform()
        self.descriptionA = \
"""\
LOOP 1 times:
  ->EXEC {} 1 times
  ->EXEC {} 50 times
  ->LOOP 17 times:
      ->LOOP 2 times:
          ->EXEC {} 1 times
          ->EXEC {} 1 times
      ->EXEC {} 1 times
  ->LOOP 3 times:
      ->EXEC {} 1 times
      ->EXEC {} 1 times
  ->LOOP 24 times:
      ->EXEC {} 7 times
      ->EXEC {} 8 times"""
        self.descriptionB = \
"""\
LOOP 1 times:
  ->EXEC {} 1 times
  ->EXEC {} 50 times
  ->LOOP 17 times:
      ->LOOP 2 times:
          ->EXEC {} 1 times
          ->EXEC {} 1 times
      ->EXEC {} 1 times
  ->LOOP 3 times:
      ->EXEC {} 1 times
      ->EXEC {} 1 times
  ->LOOP 36 times:
      ->EXEC {} 10 times
      ->EXEC {} 11 times"""

        def generate_waveform(channel):
            return DummyWaveform(sample_output=None,
                                 duration=1.,
                                 defined_channels={channel})

        def generate_multi_channel_waveform():
            return MultiChannelWaveform(
                [generate_waveform('A'),
                 generate_waveform('B')])

        self.loop_block11 = InstructionBlock()
        self.loop_block11.add_instruction_exec(
            generate_multi_channel_waveform())

        self.loop_block1 = InstructionBlock()
        self.loop_block1.add_instruction_repj(
            5, ImmutableInstructionBlock(self.loop_block11))

        self.loop_block21 = InstructionBlock()
        self.loop_block21.add_instruction_exec(
            generate_multi_channel_waveform())
        self.loop_block21.add_instruction_exec(
            generate_multi_channel_waveform())

        self.loop_block2 = InstructionBlock()
        self.loop_block2.add_instruction_repj(
            2, ImmutableInstructionBlock(self.loop_block21))
        self.loop_block2.add_instruction_exec(
            generate_multi_channel_waveform())

        self.loop_block3 = InstructionBlock()
        self.loop_block3.add_instruction_exec(
            generate_multi_channel_waveform())
        self.loop_block3.add_instruction_exec(
            generate_multi_channel_waveform())

        self.loop_block411 = InstructionBlock()
        self.loop_block411.add_instruction_exec(
            MultiChannelWaveform([generate_waveform('A')]))
        self.loop_block412 = InstructionBlock()
        self.loop_block412.add_instruction_exec(
            MultiChannelWaveform([generate_waveform('A')]))

        self.loop_block41 = InstructionBlock()
        self.loop_block41.add_instruction_repj(
            7, ImmutableInstructionBlock(self.loop_block411))
        self.loop_block41.add_instruction_repj(
            8, ImmutableInstructionBlock(self.loop_block412))

        self.loop_block421 = InstructionBlock()
        self.loop_block421.add_instruction_exec(
            MultiChannelWaveform([generate_waveform('B')]))
        self.loop_block422 = InstructionBlock()
        self.loop_block422.add_instruction_exec(
            MultiChannelWaveform([generate_waveform('B')]))

        self.loop_block42 = InstructionBlock()
        self.loop_block42.add_instruction_repj(
            10, ImmutableInstructionBlock(self.loop_block421))
        self.loop_block42.add_instruction_repj(
            11, ImmutableInstructionBlock(self.loop_block422))

        self.chan_block4A = InstructionBlock()
        self.chan_block4A.add_instruction_repj(
            6, ImmutableInstructionBlock(self.loop_block41))

        self.chan_block4B = InstructionBlock()
        self.chan_block4B.add_instruction_repj(
            9, ImmutableInstructionBlock(self.loop_block42))

        self.loop_block4 = InstructionBlock()
        self.loop_block4.add_instruction_chan({
            frozenset('A'):
            ImmutableInstructionBlock(self.chan_block4A),
            frozenset('B'):
            ImmutableInstructionBlock(self.chan_block4B)
        })

        self.root_block = InstructionBlock()
        self.root_block.add_instruction_exec(generate_multi_channel_waveform())
        self.root_block.add_instruction_repj(
            10, ImmutableInstructionBlock(self.loop_block1))
        self.root_block.add_instruction_repj(
            17, ImmutableInstructionBlock(self.loop_block2))
        self.root_block.add_instruction_repj(
            3, ImmutableInstructionBlock(self.loop_block3))
        self.root_block.add_instruction_repj(
            4, ImmutableInstructionBlock(self.loop_block4))

        self.maxDiff = None