def test_build_path_o2_m1_i2_tt_m1_i0_two_elements_main_block(self) -> None:
        sequencer = Sequencer()

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

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

        sequence = sequencer.build()

        self.assertTrue(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.assertIs(elem2, sequence[1].elem)
        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.assertEqual(STOPInstruction(), sequence[2])
        self.assertEqual(3, len(sequence))
    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)
    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))
示例#4
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)
示例#5
0
    def test_build_path_o2_m2_i0_i2_tt_m2_i0_i0_two_elements_custom_block(
            self) -> None:
        sequencer = Sequencer()

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

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

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

        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)
示例#6
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)
示例#7
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)
示例#8
0
 def build_sequence(self, sequencer: Sequencer, parameters: Dict[str,
                                                                 Parameter],
                    conditions: Dict[str, 'Condition'],
                    instruction_block: InstructionBlock) -> None:
     self.build_call_counter += 1
     self.target_block = instruction_block
     instruction_block.add_instruction(DummyInstruction(self))
     self.parameters = parameters
     self.conditions = conditions
     if self.push_elements is not None:
         for element in self.push_elements[1]:
             sequencer.push(element, parameters, conditions,
                            self.push_elements[0])
 def build_sequence(self,
                    sequencer: Sequencer,
                    parameters: Dict[str, Parameter],
                    conditions: Dict[str, 'Condition'],
                    instruction_block: InstructionBlock) -> None:
     self.build_call_counter += 1
     self.target_block = instruction_block
     instruction_block.add_instruction(DummyInstruction(self))
     self.parameters = parameters
     self.conditions = conditions
     if self.push_elements is not None:
         for element in self.push_elements[1]:
             sequencer.push(element, parameters, conditions, self.push_elements[0])
示例#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))
示例#11
0
    def build_sequence_loop(self, delegator: SequencingElement,
                            body: SequencingElement, sequencer: Sequencer,
                            parameters: Dict[str, Parameter],
                            conditions: Dict[str, Condition],
                            instruction_block: InstructionBlock) -> None:

        evaluation_result = self.__callback(self.__loop_iteration)
        if evaluation_result is None:
            raise ConditionEvaluationException()
        if evaluation_result is True:
            sequencer.push(delegator, parameters, conditions,
                           instruction_block)
            sequencer.push(body, parameters, conditions, instruction_block)
            self.__loop_iteration += 1  # next time, evaluate for next iteration
示例#12
0
    def build_sequence(self, sequencer: Sequencer, parameters: Dict[str,
                                                                    Parameter],
                       conditions: Dict[str, Condition],
                       instruction_block: InstructionBlock) -> None:
        # todo: currently ignores is_interruptable

        # detect missing or unnecessary parameters
        missing = self.parameter_names - parameters.keys()
        if missing:
            raise ParameterNotProvidedException(missing.pop())

        # push subtemplates to sequencing stack with mapped parameters
        for template in reversed(self.__subtemplates):
            inner_parameters = self.__parameter_mapping.map_parameters(
                template, parameters)
            sequencer.push(template, inner_parameters, conditions,
                           instruction_block)
示例#13
0
    def test_build_path_o2_m2_i1_t_i2_tt_m2_i0_i0_two_elements_custom_block_one_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_main = DummySequencingElement(False)
        sequencer.push(elem_main, 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_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.assertEqual(STOPInstruction(), sequence[1])
        self.assertEqual(2, len(sequence))
示例#14
0
    def test_build_path_o2_m1_i1_t_m1_i0_one_element_main_block(self) -> None:
        sequencer = Sequencer()

        elem = DummySequencingElement(False)
        ps = {"foo": ConstantParameter(1), "bar": ConstantParameter(7.3)}
        cs = {"foo": DummyCondition()}
        sequencer.push(elem, ps, cs)
        sequence = sequencer.build()

        self.assertTrue(sequencer.has_finished())
        self.assertIs(elem, sequence[0].elem)
        self.assertEqual(ps, elem.parameters)
        self.assertEqual(cs, elem.conditions)
        self.assertEqual(1, elem.requires_stop_call_counter)
        self.assertEqual(1, elem.build_call_counter)
        self.assertEqual(STOPInstruction(), sequence[1])
        self.assertEqual(2, len(sequence))
示例#15
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:

        evaluation_result = self.__callback(self.__loop_iteration)
        if evaluation_result is None:
            raise ConditionEvaluationException()
        if evaluation_result is True:
            sequencer.push(if_branch, parameters, conditions,
                           instruction_block)
        else:
            sequencer.push(else_branch, parameters, conditions,
                           instruction_block)
示例#16
0
    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)
 def build_sequence(self,
                    sequencer: Sequencer,
                    parameters: Dict[str, Parameter],
                    conditions: Dict[str, Condition],
                    measurement_mapping: Dict[str, str],
                    channel_mapping: Dict['ChannelID', 'ChannelID'],
                    instruction_block: InstructionBlock) -> None:
     self.validate_parameter_constraints(parameters=parameters)
     self.insert_measurement_instruction(instruction_block=instruction_block,
                                         parameters=parameters,
                                         measurement_mapping=measurement_mapping)
     for subtemplate in reversed(self.subtemplates):
         sequencer.push(subtemplate,
                        parameters=parameters,
                        conditions=conditions,
                        window_mapping=measurement_mapping,
                        channel_mapping=channel_mapping,
                        target_block=instruction_block)
    def build_sequence(self, sequencer: Sequencer, parameters: Dict[str,
                                                                    Parameter],
                       conditions: Dict[str, Condition],
                       instruction_block: InstructionBlock) -> None:
        repetition_count = self.__repetition_count
        if isinstance(repetition_count, ParameterDeclaration):
            repetition_count = repetition_count.get_value(parameters)
            if not repetition_count.is_integer():
                raise ParameterNotIntegerException(
                    self.__repetition_count.name, repetition_count)

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

        instruction_block.add_instruction_repj(int(repetition_count),
                                               body_block)
        sequencer.push(self.body, parameters, conditions, body_block)
示例#19
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
示例#20
0
    def integrated_test_with_sequencer_and_pulse_templates(self) -> None:
        # Setup test data
        square = TablePulseTemplate()
        square.add_entry('up', 'v', 'hold')
        square.add_entry('down', 0, 'hold')
        square.add_entry('length', 0)

        mapping1 = {
            'up': 'uptime',
            'down': 'uptime + length',
            'v': 'voltage',
            'length': '0.5 * pulse_length'
        }

        outer_parameters = ['uptime', 'length', 'pulse_length', 'voltage']

        parameters = {}
        parameters['uptime'] = 5
        parameters['length'] = 10
        parameters['pulse_length'] = 100
        parameters['voltage'] = 10

        sequence = SequencePulseTemplate([(square, mapping1),
                                          (square, mapping1)],
                                         outer_parameters)

        # run the sequencer and render the plot
        sample_rate = 20
        plotter = Plotter(sample_rate=sample_rate)
        sequencer = Sequencer(plotter)
        sequencer.push(sequence, parameters)
        block = sequencer.build()
        times, voltages = plotter.render(block)

        # compute expected values
        expected_times = numpy.linspace(0, 100, sample_rate)
        expected_voltages = numpy.zeros_like(expected_times)
        expected_voltages[100:300] = numpy.ones(200) * parameters['voltage']

        # compare
        self.assertEqual(expected_times, times)
        self.assertEqual(expected_voltages, voltages)
示例#21
0
    def build_sequence(self, sequencer: Sequencer, parameters: Dict[str,
                                                                    Parameter],
                       conditions: Dict[str, Condition],
                       measurement_mapping: Dict[str, str],
                       channel_mapping: Dict[ChannelID, ChannelID],
                       instruction_block: InstructionBlock) -> None:
        self.validate_parameter_constraints(parameters=parameters)

        self.insert_measurement_instruction(
            instruction_block=instruction_block,
            parameters=parameters,
            measurement_mapping=measurement_mapping)

        for local_parameters in self._body_parameter_generator(parameters,
                                                               forward=False):
            sequencer.push(self.body,
                           parameters=local_parameters,
                           conditions=conditions,
                           window_mapping=measurement_mapping,
                           channel_mapping=channel_mapping,
                           target_block=instruction_block)
示例#22
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))
示例#23
0
    def test_build_path_o2_m1_i2_tt_m1_i0_two_elements_main_block(
            self) -> None:
        sequencer = Sequencer()

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

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

        sequence = sequencer.build()

        self.assertTrue(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.assertIs(elem2, sequence[1].elem)
        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.assertEqual(STOPInstruction(), sequence[2])
        self.assertEqual(3, len(sequence))
示例#24
0
    def integrated_test_with_sequencer_and_pulse_templates(self) -> None:
        # Setup test data
        square = TablePulseTemplate()
        square.add_entry('up', 'v', 'hold')
        square.add_entry('down', 0, 'hold')
        square.add_entry('length', 0)

        mapping1 = {
            'up': 'uptime',
            'down': 'uptime + length',
            'v': 'voltage',
            'length': '0.5 * pulse_length'
        }

        outer_parameters = ['uptime', 'length', 'pulse_length', 'voltage']

        parameters = {}
        parameters['uptime'] = 5
        parameters['length'] = 10
        parameters['pulse_length'] = 100
        parameters['voltage'] = 10

        sequence = SequencePulseTemplate([(square, mapping1), (square, mapping1)], outer_parameters)

        # run the sequencer and render the plot
        sample_rate = 20
        plotter = Plotter(sample_rate=sample_rate)
        sequencer = Sequencer(plotter)
        sequencer.push(sequence, parameters)
        block = sequencer.build()
        times, voltages = plotter.render(block)

        # compute expected values
        expected_times = numpy.linspace(0, 100, sample_rate)
        expected_voltages = numpy.zeros_like(expected_times)
        expected_voltages[100:300] = numpy.ones(200) * parameters['voltage']

        # compare
        self.assertEqual(expected_times, times)
        self.assertEqual(expected_voltages, voltages)
示例#25
0
    def test_push(self) -> None:
        sequencer = Sequencer()

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

        sequencer.push(elem, ps, cs)
        self.assertFalse(sequencer.has_finished())
        sequencer.build()
        self.assertEqual(ps, elem.parameters)
示例#26
0
    def test_push_float_params(self) -> None:
        sequencer = Sequencer()

        ps = {'foo': 1, 'bar': 7.3}
        cs = {'foo': DummyCondition()}
        elem = DummySequencingElement()
        sequencer.push(elem, ps, cs)
        self.assertFalse(sequencer.has_finished())
        sequencer.build()
        self.assertIsInstance(elem.parameters['foo'], ConstantParameter)
        self.assertIsInstance(elem.parameters['bar'], ConstantParameter)
        self.assertEqual(cs, elem.conditions)
示例#27
0
    def test_push(self) -> None:
        sequencer = Sequencer()

        ps = {"foo": ConstantParameter(1), "bar": ConstantParameter(7.3)}
        cs = {"foo": DummyCondition()}
        elem = DummySequencingElement()

        sequencer.push(elem, ps, cs)
        self.assertFalse(sequencer.has_finished())
        sequencer.build()
        self.assertEqual(ps, elem.parameters)
示例#28
0
    def test_build_path_o1_m1_i1_f_single_element_requires_stop_main_block(
            self) -> None:
        sequencer = Sequencer()

        elem = DummySequencingElement(True)
        ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)}
        cs = {'foo': DummyCondition()}
        sequencer.push(elem, ps, cs)
        sequencer.build()

        self.assertFalse(sequencer.has_finished())
        self.assertEqual(ps, elem.parameters)
        self.assertEqual(cs, elem.conditions)
        self.assertEqual(1, elem.requires_stop_call_counter)
        self.assertEqual(0, elem.build_call_counter)
示例#29
0
    def test_push_float_params(self) -> None:
        sequencer = Sequencer()

        ps = {"foo": 1, "bar": 7.3}
        cs = {"foo": DummyCondition()}
        elem = DummySequencingElement()
        sequencer.push(elem, ps, cs)
        self.assertFalse(sequencer.has_finished())
        sequencer.build()
        self.assertIsInstance(elem.parameters["foo"], ConstantParameter)
        self.assertIsInstance(elem.parameters["bar"], ConstantParameter)
        self.assertEqual(cs, elem.conditions)
示例#30
0
def plot(pulse: PulseTemplate,
         parameters: Dict[str, Parameter]=None,
         sample_rate: int=10) -> None: # pragma: no cover
    """Plot a pulse using matplotlib.

    The given pulse will first be sequenced using the Sequencer class. The resulting
    InstructionSequence will be converted into sampled value arrays using the Plotter class. These
    arrays are then plotted in a matplotlib figure.

    Args:
        pulse (PulseTemplate): The pulse to be plotted.
        parameters (Dict(str -> Parameter)): An optional mapping of parameter names to Parameter
            objects.
        sample_rate (int): The rate with which the waveforms are sampled for the plot in
            samples per time unit. (default = 10)
    Raises:
        PlottingNotPossibleException if the sequencing is interrupted before it finishes, e.g.,
            because a parameter value could not be evaluated
        all Exceptions possibly raised during sequencing
    """
    if parameters is None:
        parameters = dict()
    plotter = Plotter(sample_rate=sample_rate)
    sequencer = Sequencer()
    sequencer.push(pulse, parameters)
    sequence = sequencer.build()
    if not sequencer.has_finished():
        raise PlottingNotPossibleException(pulse)
    times, voltages = plotter.render(sequence)

    # plot!
    figure = plt.figure()
    ax = figure.add_subplot(111)
    for index, channel in enumerate(voltages):
        ax.step(times, channel, where='post', label='channel {}'.format(index))

    ax.legend()

    max_voltage = max(max(channel) for channel in voltages)
    min_voltage = min(min(channel) for channel in voltages)

    # add some margins in the presentation
    plt.plot()
    plt.xlim(-0.5, times[-1] + 0.5)
    plt.ylim(min_voltage - 0.5, max_voltage + 0.5)
    plt.xlabel('Time in ns')
    plt.ylabel('Voltage')

    if pulse.identifier:
        plt.title(pulse.identifier)

    figure.show()
示例#31
0
    def test_build_path_o1_m1_i1_f_single_element_requires_stop_main_block(self) -> None:
        sequencer = Sequencer()

        elem = DummySequencingElement(True)
        ps = {"foo": ConstantParameter(1), "bar": ConstantParameter(7.3)}
        cs = {"foo": DummyCondition()}
        sequencer.push(elem, ps, cs)
        sequencer.build()

        self.assertFalse(sequencer.has_finished())
        self.assertEqual(ps, elem.parameters)
        self.assertEqual(cs, elem.conditions)
        self.assertEqual(1, elem.requires_stop_call_counter)
        self.assertEqual(0, elem.build_call_counter)
    def test_crash(self) -> None:
        table = TablePulseTemplate(
            {
                'default': [('ta', 'va', 'hold'), ('tb', 'vb', 'linear'),
                            ('tend', 0, 'jump')]
            },
            identifier='foo')

        external_parameters = ['ta', 'tb', 'tc', 'td', 'va', 'vb', 'tend']
        first_mapping = {
            'ta': 'ta',
            'tb': 'tb',
            'va': 'va',
            'vb': 'vb',
            'tend': 'tend'
        }
        second_mapping = {
            'ta': 'tc',
            'tb': 'td',
            'va': 'vb',
            'vb': 'va + vb',
            'tend': '2 * tend'
        }
        sequence = SequencePulseTemplate(
            (table, first_mapping, {}), (table, second_mapping, {}),
            external_parameters=external_parameters)

        parameters = {
            'ta': ConstantParameter(2),
            'va': ConstantParameter(2),
            'tb': ConstantParameter(4),
            'vb': ConstantParameter(3),
            'tc': ConstantParameter(5),
            'td': ConstantParameter(11),
            'tend': ConstantParameter(6)
        }

        sequencer = DummySequencer()
        block = DummyInstructionBlock()
        self.assertFalse(sequence.requires_stop(parameters, {}))
        sequence.build_sequence(sequencer,
                                parameters=parameters,
                                conditions={},
                                measurement_mapping={},
                                channel_mapping={'default': 'default'},
                                instruction_block=block)
        from qctoolkit.pulses.sequencing import Sequencer
        s = Sequencer()
        s.push(sequence, parameters, channel_mapping={'default': 'EXAMPLE_A'})
        s.build()
示例#33
0
    def test_build_path_o1_m2_i1_f_i0_one_element_custom_block_requires_stop(self) -> None:
        sequencer = Sequencer()

        elem = DummySequencingElement(True)
        ps = {"foo": ConstantParameter(1), "bar": ConstantParameter(7.3)}
        cs = {"foo": DummyCondition()}
        target_block = InstructionBlock()
        sequencer.push(elem, ps, cs, target_block)
        sequencer.build()

        self.assertFalse(sequencer.has_finished())
        self.assertEqual(ps, elem.parameters)
        self.assertEqual(cs, elem.conditions)
        self.assertEqual(1, elem.requires_stop_call_counter)
        self.assertEqual(0, elem.build_call_counter)
示例#34
0
    def test_build_path_o1_m2_i1_f_i0_one_element_custom_block_requires_stop(
            self) -> None:
        sequencer = Sequencer()

        elem = DummySequencingElement(True)
        ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)}
        cs = {'foo': DummyCondition()}
        wm = {}
        target_block = InstructionBlock()
        sequencer.push(elem,
                       ps,
                       cs,
                       window_mapping=wm,
                       target_block=target_block)
        sequencer.build()

        self.assertFalse(sequencer.has_finished())
        self.assertEqual(ps, elem.parameters)
        self.assertEqual(cs, elem.conditions)
        self.assertEqual(1, elem.requires_stop_call_counter)
        self.assertEqual(0, elem.build_call_counter)
    def test_crash(self) -> None:
        table = TablePulseTemplate(identifier='foo')
        table.add_entry('ta', 'va', interpolation='hold')
        table.add_entry('tb', 'vb', interpolation='linear')
        table.add_entry('tend', 0, interpolation='jump')

        external_parameters = ['ta', 'tb', 'tc', 'td', 'va', 'vb', 'tend']
        first_mapping = {
            'ta': 'ta',
            'tb': 'tb',
            'va': 'va',
            'vb': 'vb',
            'tend': 'tend'
        }
        second_mapping = {
            'ta': 'tc',
            'tb': 'td',
            'va': 'vb',
            'vb': 'va + vb',
            'tend': '2 * tend'
        }
        sequence = SequencePulseTemplate([(table, first_mapping),
                                          (table, second_mapping)],
                                         external_parameters)

        parameters = {
            'ta': ConstantParameter(2),
            'va': ConstantParameter(2),
            'tb': ConstantParameter(4),
            'vb': ConstantParameter(3),
            'tc': ConstantParameter(5),
            'td': ConstantParameter(11),
            'tend': ConstantParameter(6)
        }

        sequencer = DummySequencer()
        block = DummyInstructionBlock()
        self.assertFalse(sequence.requires_stop(parameters, {}))
        sequence.build_sequence(sequencer, parameters, {}, block)
        from qctoolkit.pulses.sequencing import Sequencer
        s = Sequencer()
        s.push(sequence, parameters)
        s.build()
示例#36
0
def exec_test():
    import time
    import numpy as np

    t = []
    names = []

    def tic(name):
        t.append(time.time())
        names.append(name)

    from qctoolkit.hardware.awgs.tabor import TaborChannelPair, TaborAWGRepresentation
    tawg = TaborAWGRepresentation(r'USB0::0x168C::0x2184::0000216488::INSTR',
                                  reset=True)

    tchannelpair = TaborChannelPair(tawg, (1, 2), 'TABOR_AB')
    tawg.paranoia_level = 2

    # warnings.simplefilter('error', Warning)

    from qctoolkit.hardware.setup import HardwareSetup, PlaybackChannel, MarkerChannel
    hardware_setup = HardwareSetup()

    hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair, 0))
    hardware_setup.set_channel('TABOR_B', PlaybackChannel(tchannelpair, 1))
    hardware_setup.set_channel('TABOR_A_MARKER',
                               MarkerChannel(tchannelpair, 0))
    hardware_setup.set_channel('TABOR_B_MARKER',
                               MarkerChannel(tchannelpair, 1))

    if with_alazar:
        from qctoolkit.hardware.dacs.alazar import AlazarCard
        import atsaverage.server

        if not atsaverage.server.Server.default_instance.running:
            atsaverage.server.Server.default_instance.start(key=b'guest')

        import atsaverage.core

        alazar = AlazarCard(atsaverage.core.getLocalCard(1, 1))
        alazar.register_mask_for_channel('A', 0)
        alazar.config = get_alazar_config()

        alazar.register_operations('test', get_operations())

        window = get_window(atsaverage.core.getLocalCard(1, 1))

        hardware_setup.register_dac(alazar)

    repeated = get_pulse()

    from qctoolkit.pulses.sequencing import Sequencer

    tic('init')
    sequencer = Sequencer()
    sequencer.push(repeated,
                   parameters=dict(n=10000, tau=1920, U=0.5),
                   channel_mapping={
                       'out': 'TABOR_A',
                       'trigger': 'TABOR_A_MARKER'
                   },
                   window_mapping=dict(A='A'))
    instruction_block = sequencer.build()

    tic('sequence')

    hardware_setup.register_program('test', instruction_block)

    tic('register')

    if with_alazar:
        from atsaverage.masks import PeriodicMask
        m = PeriodicMask()
        m.identifier = 'D'
        m.begin = 0
        m.end = 1
        m.period = 1
        m.channel = 0
        alazar._registered_programs['test'].masks.append(m)

    tic('per_mask')

    hardware_setup.arm_program('test')

    tic('arm')

    for d, name in zip(np.diff(np.asarray(t)), names[1:]):
        print(name, d)

    d = 1
示例#37
0
 def test_initialization(self) -> None:
     sequencer = Sequencer()
     self.assertTrue(sequencer.has_finished())
示例#38
0
    def test_build_path_no_loop_nothing_to_do(self) -> None:
        sequencer = Sequencer()

        sequencer.build()
        self.assertTrue(sequencer.has_finished())
示例#39
0
    def test_build_path_no_loop_nothing_to_do(self) -> None:
        sequencer = Sequencer()

        sequencer.build()
        self.assertTrue(sequencer.has_finished())
示例#40
0
    def test_build_path_o2_m2_i2_tt_t_i2_tf_m2_i0_i1_f_two_elements_custom_block_last_requires_stop_two_element_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_main2 = DummySequencingElement(False)
        sequencer.push(elem_main2, ps, cs)

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

        sequence = 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.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))
示例#41
0
 def test_initialization(self) -> None:
     sequencer = Sequencer()
     self.assertTrue(sequencer.has_finished())
示例#42
0
    def test_all(self):
        from qctoolkit.hardware.awgs.tabor import TaborChannelPair, TaborAWGRepresentation
        #import warnings
        tawg = TaborAWGRepresentation(r'USB0::0x168C::0x2184::0000216488::INSTR')
        tchannelpair = TaborChannelPair(tawg, (1, 2), 'TABOR_AB')
        tawg.paranoia_level = 2

        #warnings.simplefilter('error', Warning)

        from qctoolkit.hardware.setup import HardwareSetup, PlaybackChannel, MarkerChannel
        hardware_setup = HardwareSetup()

        hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair, 0))
        hardware_setup.set_channel('TABOR_B', PlaybackChannel(tchannelpair, 1))
        hardware_setup.set_channel('TABOR_A_MARKER', MarkerChannel(tchannelpair, 0))
        hardware_setup.set_channel('TABOR_B_MARKER', MarkerChannel(tchannelpair, 1))

        if with_alazar:
            from qctoolkit.hardware.dacs.alazar import AlazarCard
            import atsaverage.server

            if not atsaverage.server.Server.default_instance.running:
                atsaverage.server.Server.default_instance.start(key=b'guest')

            import atsaverage.core

            alazar = AlazarCard(atsaverage.core.getLocalCard(1, 1))
            alazar.register_mask_for_channel('A', 0)
            alazar.register_mask_for_channel('B', 0)
            alazar.register_mask_for_channel('C', 0)
            alazar.config = get_alazar_config()

            alazar.register_operations('test', get_operations())
            window = get_window(atsaverage.core.getLocalCard(1, 1))
            hardware_setup.register_dac(alazar)

        repeated = get_pulse()

        from qctoolkit.pulses.sequencing import Sequencer

        sequencer = Sequencer()
        sequencer.push(repeated,
                       parameters=dict(n=1000, min=-0.5, max=0.5, tau=192*3),
                       channel_mapping={'out': 'TABOR_A', 'trigger': 'TABOR_A_MARKER'},
                       window_mapping=dict(A='A', B='B', C='C'))
        instruction_block = sequencer.build()

        hardware_setup.register_program('test', instruction_block)

        if with_alazar:
            from atsaverage.masks import PeriodicMask
            m = PeriodicMask()
            m.identifier = 'D'
            m.begin = 0
            m.end = 1
            m.period = 1
            m.channel = 0
            alazar._registered_programs['test'].masks.append(m)

        hardware_setup.arm_program('test')

        d = 1