Example #1
0
 def build_sequence_branch(self,
                           delegator: sequencing.SequencingElement,
                           if_branch: sequencing.SequencingElement,
                           else_branch: sequencing.SequencingElement,
                           sequencer: sequencing.Sequencer,
                           parameters: Dict[str, Parameter],
                           conditions: Dict[str, Condition],
                           measurement_mapping: Dict[str, str],
                           channel_mapping: Dict[ChannelID, ChannelID],
                           instruction_block: InstructionBlock) -> None:
     if_block = InstructionBlock()
     else_block = InstructionBlock()
     
     instruction_block.add_instruction_cjmp(self.__trigger, if_block)
     sequencer.push(if_branch, parameters, conditions, window_mapping=measurement_mapping,
                    channel_mapping=channel_mapping,
                    target_block=if_block)
     
     instruction_block.add_instruction_goto(else_block)
     sequencer.push(else_branch, parameters, conditions, window_mapping=measurement_mapping,
                    channel_mapping=channel_mapping,
                    target_block=else_block)
     
     if_block.return_ip = InstructionPointer(instruction_block,
                                             len(instruction_block.instructions))
     else_block.return_ip = if_block.return_ip
Example #2
0
 def test_create_embedded_block(self) -> None:
     parent_block = InstructionBlock()
     block = InstructionBlock()
     block.return_ip = InstructionPointer(parent_block, 18)
     self.__verify_block(
         block, [], [GOTOInstruction(InstructionPointer(parent_block, 18))],
         InstructionPointer(parent_block, 18))
     self.__verify_block(parent_block, [], [STOPInstruction()], None)
Example #3
0
 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)
Example #4
0
 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)
Example #5
0
 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)
Example #6
0
 def test_equality(self) -> None:
     blocks = [InstructionBlock(), InstructionBlock()]
     blocks.append(InstructionBlock())
     ips = []
     for block in blocks:
         for offset in [0, 1, 2352]:
             ip = InstructionPointer(block, offset)
             self.assertEqual(ip, ip)
             for other in ips:
                 self.assertNotEqual(ip, other)
                 self.assertNotEqual(other, ip)
                 self.assertNotEqual(hash(ip), hash(other))
             ips.append(ip)
    def test_build_path_o2_m2_i0_i1_t_m2_i0_i0_one_element_custom_block(
            self) -> None:
        sequencer = Sequencer()

        ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)}
        cs = {'foo': DummyCondition()}
        wm = {'foo': 'bar'}
        cm = {'A': 'B'}

        target_block = InstructionBlock()
        elem = DummySequencingElement(False)
        sequencer.push(elem,
                       ps,
                       cs,
                       window_mapping=wm,
                       channel_mapping=cm,
                       target_block=target_block)

        sequencer.build()

        self.assertTrue(sequencer.has_finished())
        self.assertIs(target_block, elem.target_block)
        self.assertEqual(ps, elem.parameters)
        self.assertEqual(cs, elem.conditions)
        self.assertEqual(wm, elem.window_mapping)
        self.assertEqual(cm, elem.channel_mapping)
        self.assertEqual(1, elem.requires_stop_call_counter)
        self.assertEqual(1, elem.build_call_counter)
Example #8
0
 def __init__(self) -> None:
     """Create a Sequencer."""
     super().__init__()
     self.__waveforms = dict()  # type: Dict[int, Waveform]
     self.__main_block = InstructionBlock()
     self.__sequencing_stacks = \
         {self.__main_block: []}  # type: Dict[InstructionBlock, List[Sequencer.StackElement]]
Example #9
0
    def test_build_path_o2_m2_i1_f_i2_tf_m2_i1_f_i1_f_two_elements_custom_block_last_requires_stop_one_element_requires_stop_main_block(
            self) -> None:
        sequencer = Sequencer()

        ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)}
        cs = {'foo': DummyCondition()}

        target_block = InstructionBlock()
        elem2 = DummySequencingElement(True)
        sequencer.push(elem2, ps, cs, target_block)

        elem1 = DummySequencingElement(False)
        sequencer.push(elem1, ps, cs, target_block)

        elem_main = DummySequencingElement(True)
        sequencer.push(elem_main, ps, cs)

        sequencer.build()

        self.assertFalse(sequencer.has_finished())
        self.assertIs(target_block, elem1.target_block)
        self.assertEqual(ps, elem1.parameters)
        self.assertEqual(cs, elem1.conditions)
        self.assertEqual(1, elem1.requires_stop_call_counter)
        self.assertEqual(1, elem1.build_call_counter)
        self.assertEqual(ps, elem2.parameters)
        self.assertEqual(cs, elem2.conditions)
        self.assertEqual(2, elem2.requires_stop_call_counter)
        self.assertEqual(0, elem2.build_call_counter)
        self.assertEqual(ps, elem_main.parameters)
        self.assertEqual(cs, elem_main.conditions)
        self.assertEqual(2, elem_main.requires_stop_call_counter)
        self.assertEqual(0, elem_main.build_call_counter)
Example #10
0
    def test_build_path_o2_m2_i1_t_i1_t_m2_i0_i0_one_element_custom_block_one_element_main_block(
            self) -> None:
        sequencer = Sequencer()

        ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)}
        cs = {'foo': DummyCondition()}

        elem_main = DummySequencingElement(False)
        sequencer.push(elem_main, ps, cs)

        target_block = InstructionBlock()
        elem_cstm = DummySequencingElement(False)
        sequencer.push(elem_cstm, ps, cs, target_block)

        sequence = sequencer.build()

        self.assertTrue(sequencer.has_finished())
        self.assertIs(elem_main, sequence[0].elem)
        self.assertEqual(ps, elem_main.parameters)
        self.assertEqual(cs, elem_main.conditions)
        self.assertEqual(1, elem_main.requires_stop_call_counter)
        self.assertEqual(1, elem_main.build_call_counter)
        self.assertIs(target_block, elem_cstm.target_block)
        self.assertEqual(ps, elem_cstm.parameters)
        self.assertEqual(cs, elem_cstm.conditions)
        self.assertEqual(1, elem_cstm.requires_stop_call_counter)
        self.assertEqual(1, elem_cstm.build_call_counter)
        self.assertEqual(STOPInstruction(), sequence[1])
        self.assertEqual(2, len(sequence))
Example #11
0
    def build_sequence(self, sequencer: Sequencer, parameters: Dict[str,
                                                                    Parameter],
                       conditions: Dict[str, Condition],
                       measurement_mapping: Dict[str, Optional[str]],
                       channel_mapping: Dict[ChannelID, Optional[ChannelID]],
                       instruction_block: InstructionBlock) -> None:
        self.validate_parameter_constraints(parameters=parameters)

        body_block = InstructionBlock()
        body_block.return_ip = InstructionPointer(instruction_block,
                                                  len(instruction_block))

        try:
            real_parameters = {
                v: parameters[v].get_value()
                for v in self._repetition_count.variables
            }
        except KeyError:
            raise ParameterNotProvidedException(
                next(v for v in self.repetition_count.variables
                     if v not in parameters))
        self.insert_measurement_instruction(
            instruction_block,
            parameters=parameters,
            measurement_mapping=measurement_mapping)
        instruction_block.add_instruction_repj(
            self.get_repetition_count_value(real_parameters), body_block)
        sequencer.push(self.body,
                       parameters=parameters,
                       conditions=conditions,
                       window_mapping=measurement_mapping,
                       channel_mapping=channel_mapping,
                       target_block=body_block)
Example #12
0
    def test_build_path_o1_m2_i1_f_i1_f_one_element_custom_and_main_block_requires_stop(
            self) -> None:
        sequencer = Sequencer()

        ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)}
        cs = {'foo': DummyCondition()}

        elem_main = DummySequencingElement(True)
        sequencer.push(elem_main, ps, cs)

        elem_cstm = DummySequencingElement(True)
        target_block = InstructionBlock()
        sequencer.push(elem_cstm, ps, cs, target_block)

        sequencer.build()

        self.assertFalse(sequencer.has_finished())
        self.assertEqual(ps, elem_main.parameters)
        self.assertEqual(cs, elem_main.conditions)
        self.assertEqual(1, elem_main.requires_stop_call_counter)
        self.assertEqual(0, elem_main.build_call_counter)
        self.assertEqual(ps, elem_cstm.parameters)
        self.assertEqual(cs, elem_cstm.conditions)
        self.assertEqual(1, elem_cstm.requires_stop_call_counter)
        self.assertEqual(0, elem_cstm.build_call_counter)
Example #13
0
    def test_add_instruction_goto(self) -> None:
        block = InstructionBlock()
        expected_instructions = []

        targets = [InstructionBlock(), InstructionBlock(), InstructionBlock()]
        LOOKUP = [0, 1, 1, 0, 2, 1, 0, 0, 0, 1, 2, 2]
        for id in LOOKUP:
            target = targets[id]
            instruction = GOTOInstruction(InstructionPointer(target))
            expected_instructions.append(instruction)
            block.add_instruction_goto(target)

        expected_compiled_instructions = expected_instructions.copy()
        expected_compiled_instructions.append(STOPInstruction())
        self.__verify_block(block, expected_instructions,
                            expected_compiled_instructions, None)
Example #14
0
    def test_build_path_o2_m1_i2_tf_m2_i1_f_i1_f_two_elements_main_block_last_requires_stop_add_one_element_requires_stop_new_block(
            self) -> None:
        sequencer = Sequencer()

        ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)}
        cs = {'foo': DummyCondition()}

        new_block = InstructionBlock()
        new_elem = DummySequencingElement(True)

        elem1 = DummySequencingElement(False, (new_block, [new_elem]))
        elem2 = DummySequencingElement(True)
        sequencer.push(elem2, ps, cs)
        sequencer.push(elem1, ps, cs)

        sequence = sequencer.build()

        self.assertFalse(sequencer.has_finished())
        self.assertIs(elem1, sequence[0].elem)
        self.assertEqual(ps, elem1.parameters)
        self.assertEqual(cs, elem1.conditions)
        self.assertEqual(1, elem1.requires_stop_call_counter)
        self.assertEqual(1, elem1.build_call_counter)
        self.assertEqual(ps, elem2.parameters)
        self.assertEqual(cs, elem2.conditions)
        self.assertEqual(2, elem2.requires_stop_call_counter)
        self.assertEqual(0, elem2.build_call_counter)
        self.assertEqual(ps, new_elem.parameters)
        self.assertEqual(cs, new_elem.conditions)
        self.assertEqual(1, new_elem.requires_stop_call_counter)
        self.assertEqual(0, new_elem.build_call_counter)
        self.assertEqual(STOPInstruction(), sequence[1])
        self.assertEqual(2, len(sequence))
Example #15
0
 def test_initialization(self) -> None:
     block = InstructionBlock()
     trigger = Trigger()
     for offset in [0, 1, 23]:
         instr = CJMPInstruction(trigger, InstructionPointer(block, offset))
         self.assertEqual(trigger, instr.trigger)
         self.assertEqual(block, instr.target.block)
         self.assertEqual(offset, instr.target.offset)
Example #16
0
    def test_add_instruction_repj(self) -> None:
        block = InstructionBlock()
        expected_instructions = []
        targets = [InstructionBlock(), InstructionBlock(), InstructionBlock()]
        counts = [3, 8, 857]
        LOOKUP = [(0, 0), (0, 1), (1, 1), (0, 2), (2, 0), (1, 0), (2, 2),
                  (2, 1), (1, 0), (1, 2)]
        for i in LOOKUP:
            block.add_instruction_repj(counts[i[0]], targets[i[1]])
            expected_instructions.append(
                REPJInstruction(counts[i[0]],
                                InstructionPointer(targets[i[1]], 0)))

        expected_compiled_instructions = expected_instructions.copy()
        expected_compiled_instructions.append(STOPInstruction())
        self.__verify_block(block, expected_instructions,
                            expected_compiled_instructions, None)
Example #17
0
 def test_equality(self) -> None:
     blocks = [InstructionBlock(), InstructionBlock()]
     for offset in [0, 1, 23]:
         instrA = GOTOInstruction(InstructionPointer(blocks[0], offset))
         instrB = GOTOInstruction(InstructionPointer(blocks[0], offset))
         self.assertEqual(instrA, instrB)
         self.assertEqual(instrB, instrA)
     instrs = []
     for block in blocks:
         for offset in [0, 17]:
             instruction = GOTOInstruction(InstructionPointer(
                 block, offset))
             self.assertEqual(instruction, instruction)
             for other in instrs:
                 self.assertNotEqual(instruction, other)
                 self.assertNotEqual(other, instruction)
                 self.assertNotEqual(hash(instruction), hash(other))
             instrs.append(instruction)
Example #18
0
    def test_init(self):
        with self.assertRaises(ValueError):
            MultiChannelProgram(InstructionBlock())

        mcp = MultiChannelProgram(self.root_block, ['A', 'B'])
        self.assertEqual(mcp.channels, {'A', 'B'})

        with self.assertRaises(KeyError):
            mcp['C']
Example #19
0
 def test_add_instruction_stop(self) -> None:
     block = InstructionBlock()
     expected_instructions = [STOPInstruction(), STOPInstruction()]
     block.add_instruction_stop()
     block.add_instruction_stop()
     expected_compiled_instructions = expected_instructions.copy()
     expected_compiled_instructions.append(STOPInstruction())
     self.__verify_block(block, expected_instructions,
                         expected_compiled_instructions, None)
Example #20
0
    def test_add_instruction_cjmp(self) -> None:
        block = InstructionBlock()
        expected_instructions = []

        targets = [InstructionBlock(), InstructionBlock(), InstructionBlock()]
        triggers = [Trigger(), Trigger()]
        LOOKUP = [(0, 0), (1, 0), (1, 1), (0, 1), (2, 0), (1, 0), (0, 1),
                  (0, 1), (0, 0), (1, 0), (2, 1), (2, 1)]
        for i in LOOKUP:
            block.add_instruction_cjmp(triggers[i[1]], targets[i[0]])
            expected_instructions.append(
                CJMPInstruction(triggers[i[1]],
                                InstructionPointer(targets[i[0]], 0)))

        expected_compiled_instructions = expected_instructions.copy()
        expected_compiled_instructions.append(STOPInstruction())
        self.__verify_block(block, expected_instructions,
                            expected_compiled_instructions, None)
Example #21
0
 def test_initialization(self) -> None:
     block = InstructionBlock()
     for count in [0, 1, 47]:
         for offset in [0, 1, 23]:
             instr = REPJInstruction(count,
                                     InstructionPointer(block, offset))
             self.assertEqual(count, instr.count)
             self.assertEqual(block, instr.target.block)
             self.assertEqual(offset, instr.target.offset)
Example #22
0
    def build_sequence_branch(self, delegator: SequencingElement,
                              if_branch: SequencingElement,
                              else_branch: SequencingElement,
                              sequencer: Sequencer,
                              parameters: Dict[str, Parameter],
                              conditions: Dict[str, Condition],
                              instruction_block: InstructionBlock) -> None:
        if_block = InstructionBlock()
        else_block = InstructionBlock()

        instruction_block.add_instruction_cjmp(self.__trigger, if_block)
        sequencer.push(if_branch, parameters, conditions, if_block)

        instruction_block.add_instruction_goto(else_block)
        sequencer.push(else_branch, parameters, conditions, else_block)

        if_block.return_ip = InstructionPointer(
            instruction_block, len(instruction_block.instructions))
        else_block.return_ip = if_block.return_ip
Example #23
0
    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())
Example #24
0
    def build_sequence_loop(self, delegator: SequencingElement,
                            body: SequencingElement, sequencer: Sequencer,
                            parameters: Dict[str, Parameter],
                            conditions: Dict[str, Condition],
                            instruction_block: InstructionBlock) -> None:
        body_block = InstructionBlock()
        body_block.return_ip = InstructionPointer(
            instruction_block, len(instruction_block.instructions))

        instruction_block.add_instruction_cjmp(self.__trigger, body_block)
        sequencer.push(body, parameters, conditions, body_block)
    def test_build_path_o2_m2_i2_tt_t_i2_tt_m2_i0_i0_two_elements_custom_block_two_element_main_block(
            self) -> None:
        sequencer = Sequencer()

        ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)}
        cs = {'foo': DummyCondition()}
        wm = {'foo': 'bar'}

        target_block = InstructionBlock()
        elem2 = DummySequencingElement(False)
        sequencer.push(elem2,
                       ps,
                       cs,
                       window_mapping=wm,
                       target_block=target_block)

        elem1 = DummySequencingElement(False)
        sequencer.push(elem1,
                       ps,
                       cs,
                       window_mapping=wm,
                       target_block=target_block)

        elem_main2 = DummySequencingElement(False)
        sequencer.push(elem_main2, ps, cs)

        elem_main1 = DummySequencingElement(False)
        sequencer.push(elem_main1, ps, cs)

        sequence = sequencer.build()

        self.assertTrue(sequencer.has_finished())
        self.assertIs(target_block, elem1.target_block)
        self.assertEqual(ps, elem1.parameters)
        self.assertEqual(cs, elem1.conditions)
        self.assertEqual(1, elem1.requires_stop_call_counter)
        self.assertEqual(1, elem1.build_call_counter)
        self.assertIs(target_block, elem2.target_block)
        self.assertEqual(ps, elem2.parameters)
        self.assertEqual(cs, elem2.conditions)
        self.assertEqual(1, elem2.requires_stop_call_counter)
        self.assertEqual(1, elem2.build_call_counter)
        self.assertIs(elem_main1, sequence[0].elem)
        self.assertEqual(ps, elem_main1.parameters)
        self.assertEqual(cs, elem_main1.conditions)
        self.assertEqual(1, elem_main1.requires_stop_call_counter)
        self.assertEqual(1, elem_main1.build_call_counter)
        self.assertIs(elem_main2, sequence[1].elem)
        self.assertEqual(ps, elem_main2.parameters)
        self.assertEqual(cs, elem_main2.conditions)
        self.assertEqual(1, elem_main2.requires_stop_call_counter)
        self.assertEqual(1, elem_main2.build_call_counter)
        self.assertEqual(STOPInstruction(), sequence[2])
        self.assertEqual(3, len(sequence))
Example #26
0
    def test_remove_program(self):
        wf_1 = DummyWaveform(duration=1.1, defined_channels={'A', 'B'})
        wf_2 = DummyWaveform(duration=1.1, defined_channels={'A', 'C'})

        block_1 = InstructionBlock()
        block_2 = InstructionBlock()

        block_1.add_instruction_meas([('m1', 0., 1.)])
        block_1.add_instruction_exec(wf_1)

        block_2.add_instruction_meas([('m2', 0., 1.)])
        block_2.add_instruction_exec(wf_2)

        awg1 = DummyAWG()
        awg2 = DummyAWG()
        awg3 = DummyAWG()

        dac1 = DummyDAC()
        dac2 = DummyDAC()

        setup = HardwareSetup()

        setup.set_channel('A', PlaybackChannel(awg1, 0))
        setup.set_channel('B', MarkerChannel(awg2, 0))
        setup.set_channel('C', MarkerChannel(awg3, 0))

        setup.set_measurement('m1', MeasurementMask(dac1, 'DAC_1'))
        setup.set_measurement('m2', MeasurementMask(dac2, 'DAC_2'))

        setup.register_program('test_1', block_1)

        setup.register_program('test_2', block_2)

        setup.arm_program('test_1')

        setup.remove_program('test_1')

        self.assertEqual(setup.registered_programs.keys(), {'test_2'})

        self.assertIsNone(awg1._armed)
        self.assertIsNone(awg2._armed)
Example #27
0
 def test_iterable_empty_return(self) -> None:
     parent_block = InstructionBlock()
     block = AbstractInstructionBlockStub([],
                                          InstructionPointer(
                                              parent_block, 13))
     count = 0
     for instruction in block:
         self.assertEqual(0, count)
         self.assertIsInstance(instruction, GOTOInstruction)
         self.assertEqual(InstructionPointer(parent_block, 13),
                          instruction.target)
         count += 1
Example #28
0
 def test_item_access_empty_return(self) -> None:
     parent_block = InstructionBlock()
     block = AbstractInstructionBlockStub([],
                                          InstructionPointer(
                                              parent_block, 84))
     self.assertEqual(GOTOInstruction(InstructionPointer(parent_block, 84)),
                      block[0])
     with self.assertRaises(IndexError):
         block[1]
     self.assertEqual(GOTOInstruction(InstructionPointer(parent_block, 84)),
                      block[-1])
     with self.assertRaises(IndexError):
         block[-2]
    def test_insert_measurement_instruction(self):
        pulse = self.to_test_constructor(measurements=[('mw', 'a', 'd')])
        parameters = {'a': ConstantParameter(0), 'd': ConstantParameter(0.9)}
        measurement_mapping = {'mw': 'as'}

        block = InstructionBlock()
        pulse.insert_measurement_instruction(
            instruction_block=block,
            parameters=parameters,
            measurement_mapping=measurement_mapping)

        expected_block = [MEASInstruction([('as', 0, 0.9)])]
        self.assertEqual(block.instructions, expected_block)
Example #30
0
 def test_iterable_return(self) -> None:
     parent_block = InstructionBlock()
     wf = DummyWaveform()
     block = AbstractInstructionBlockStub([EXECInstruction(wf)],
                                          InstructionPointer(
                                              parent_block, 11))
     count = 0
     for expected_instruction, instruction in zip([
             EXECInstruction(wf),
             GOTOInstruction(InstructionPointer(parent_block, 11))
     ], block):
         self.assertEqual(expected_instruction, instruction)
         count += 1
     self.assertEqual(2, count)