def test_empty_returning_block(self) -> None: return_block = InstructionBlock() block = InstructionBlock() block.return_ip = InstructionPointer(return_block, 7) context = { return_block: ImmutableInstructionBlock(return_block, dict()) } immutable_block = ImmutableInstructionBlock(block, context) self.__verify_block(block, immutable_block, context)
def build(self) -> ImmutableInstructionBlock: """Start the translation process. Translate all elements currently on the translation stacks into an InstructionBlock hierarchy. Processes all sequencing stacks (for each InstructionBlock) until each stack is either empty or its topmost element requires a stop. If build is called after a previous translation process where some elements required a stop (i.e., has_finished returned False), it will append new instructions to the previously generated and returned blocks. Returns: The instruction block (hierarchy) resulting from the translation of the (remaining) SequencingElements on the sequencing stacks. """ if not self.has_finished(): shall_continue = True # shall_continue will only be False, if the first element on all # stacks requires a stop or all stacks are empty while shall_continue: shall_continue = False for target_block, sequencing_stack in self.__sequencing_stacks.copy( ).items(): while sequencing_stack: (element, parameters, conditions, window_mapping, channel_mapping) = sequencing_stack[-1] if not element.requires_stop(parameters, conditions): shall_continue |= True sequencing_stack.pop() element.build_sequence(self, parameters, conditions, window_mapping, channel_mapping, target_block) else: break return ImmutableInstructionBlock(self.__main_block, dict())
def test_nested_goto(self) -> None: parent_block = InstructionBlock() block = InstructionBlock() block.return_ip = InstructionPointer(parent_block, 1) parent_block.add_instruction_goto(block) context = dict() immutable_block = ImmutableInstructionBlock(parent_block, context) self.__verify_block(parent_block, immutable_block, context)
def test_nested_no_context_argument(self) -> None: parent_block = InstructionBlock() block = InstructionBlock() block.return_ip = InstructionPointer(parent_block, 1) parent_block.add_instruction_goto(block) immutable_block = ImmutableInstructionBlock(parent_block) context = { parent_block: immutable_block, block: immutable_block.instructions[0].target.block } self.__verify_block(parent_block, immutable_block, context)
def test_multiple_nested_block_construction(self) -> None: main_block = InstructionBlock() blocks = [] waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] main_block.add_instruction_exec(waveforms[0]) block = InstructionBlock() trigger = Trigger() ip = InstructionPointer(block) main_block.add_instruction_cjmp(trigger, block) block.return_ip = InstructionPointer(main_block, len(main_block)) blocks.append(block) block = InstructionBlock() trigger = Trigger() ip = InstructionPointer(block) main_block.add_instruction_cjmp(trigger, block) block.return_ip = InstructionPointer(main_block, len(main_block)) blocks.append(block) WAVEFORM_LOOKUP = [[2, 2, 1, 1], [0, 1, 1, 0, 2, 1]] for i in [0, 1]: block = blocks[i] lookup = WAVEFORM_LOOKUP[i] for id in lookup: waveform = waveforms[id] block.add_instruction_exec(waveform) block = InstructionBlock() ip = InstructionPointer(block) blocks[0].add_instruction_cjmp(trigger, block) block.return_ip = InstructionPointer(blocks[0], len(blocks[0])) blocks.append(block) for id in [1, 2, 0, 2]: waveform = waveforms[id] block.add_instruction_exec(waveform) context = dict() immutable_block = ImmutableInstructionBlock(main_block, context) self.__verify_block(main_block, immutable_block, context.copy())
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
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=None, 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
def test_empty_unreturning_block(self) -> None: block = InstructionBlock() context = dict() immutable_block = ImmutableInstructionBlock(block, context) self.__verify_block(block, immutable_block, context.copy())