def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        # Setup test data
        self.square = TablePulseTemplate()
        self.square.add_entry('up', 'v', 'hold')
        self.square.add_entry('down', 0, 'hold')
        self.square.add_entry('length', 0)

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

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

        self.parameters = {}
        self.parameters['uptime'] = ConstantParameter(5)
        self.parameters['length'] = ConstantParameter(10)
        self.parameters['pulse_length'] = ConstantParameter(100)
        self.parameters['voltage'] = ConstantParameter(10)

        self.sequence = SequencePulseTemplate([(self.square, self.mapping1)],
                                              self.outer_parameters)
Exemplo n.º 2
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)
Exemplo n.º 3
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))
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        # Setup test data
        self.square = TablePulseTemplate(
            {
                'default': [(0, 0), ('up', 'v', 'hold'), ('down', 0, 'hold'),
                            ('length', 0)]
            },
            measurements=[('mw1', 'up', 'length-up')])
        self.mapping1 = {
            'up': 'uptime',
            'down': 'uptime + length',
            'v': 'voltage',
            'length': '0.5 * pulse_length'
        }

        self.window_name_mapping = {'mw1': 'test_window'}

        self.outer_parameters = {'uptime', 'length', 'pulse_length', 'voltage'}

        self.parameters = dict()
        self.parameters['uptime'] = ConstantParameter(5)
        self.parameters['length'] = ConstantParameter(10)
        self.parameters['pulse_length'] = ConstantParameter(100)
        self.parameters['voltage'] = ConstantParameter(10)

        self.sequence = SequencePulseTemplate(
            MappingPulseTemplate(self.square,
                                 parameter_mapping=self.mapping1,
                                 measurement_mapping=self.window_name_mapping),
            external_parameters=self.outer_parameters)
Exemplo n.º 5
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))
Exemplo n.º 6
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))
Exemplo n.º 7
0
 def test_get_serialization_data(self) -> None:
     constant_parameter = ConstantParameter(-0.2)
     serializer = DummySerializer()
     data = constant_parameter.get_serialization_data(serializer)
     self.assertEqual(
         dict(type=serializer.get_type_identifier(constant_parameter),
              constant=-0.2), data)
Exemplo n.º 8
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)
Exemplo n.º 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)
Exemplo n.º 10
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)
Exemplo n.º 11
0
    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))
Exemplo n.º 12
0
 def test_requires_stop_true_mapped_parameters(self) -> None:
     dummy = DummyPulseTemplate(parameter_names={'foo'}, requires_stop=True)
     pulse = MultiChannelPulseTemplate([(dummy, dict(foo='2*bar'), [0]),
                                        (dummy, dict(foo='rab-5'), [1])],
                                       {'bar', 'rab'})
     self.assertEqual({'bar', 'rab'}, pulse.parameter_names)
     self.assertEqual(
         {ParameterDeclaration('bar'),
          ParameterDeclaration('rab')}, pulse.parameter_declarations)
     parameters = dict(bar=ConstantParameter(-3.6),
                       rab=ConstantParameter(35.26))
     self.assertTrue(pulse.requires_stop(parameters, dict()))
Exemplo n.º 13
0
    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)
 def test_map_parameters(self) -> None:
     map = PulseTemplateParameterMapping({'bar', 'barbar'})
     dummy = DummyPulseTemplate(parameter_names={'foo', 'hugo'})
     map.add(dummy, 'hugo', '4*bar')
     map.add(dummy, 'foo', Expression('barbar'))
     mapped = map.map_parameters(
         dummy, dict(bar=ConstantParameter(3), barbar=ConstantParameter(5)))
     self.assertEqual(
         dict(hugo=MappedParameter(Expression('4*bar'),
                                   dict(bar=ConstantParameter(3))),
              foo=MappedParameter(Expression('barbar'),
                                  dict(barbar=ConstantParameter(5)))),
         mapped)
Exemplo n.º 15
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_map_parameters_not_provided(self) -> None:
     map = PulseTemplateParameterMapping({'bar', 'barbar'})
     dummy = DummyPulseTemplate(parameter_names={'foo', 'hugo'})
     map.add(dummy, 'hugo', '4*bar')
     map.add(dummy, 'foo', Expression('barbar'))
     with self.assertRaises(ParameterNotProvidedException):
         map.map_parameters(dummy, dict(bar=ConstantParameter(3)))
Exemplo n.º 17
0
 def test_build_sequence_declaration_exceeds_bounds(self) -> None:
     parameters = dict(foo=ConstantParameter(9))
     conditions = dict(foo=DummyCondition(requires_stop=True))
     with self.assertRaises(ParameterConstraintViolation):
         self.template.build_sequence(self.sequencer, parameters,
                                      conditions, {}, {}, self.block)
     self.assertFalse(self.sequencer.sequencing_stacks)
    def test_build_sequence(self):
        measurement_mapping = {'meas1': 'meas2'}
        parameter_mapping = {'t': 'k'}

        template = DummyPulseTemplate(
            measurement_names=set(measurement_mapping.keys()),
            parameter_names=set(parameter_mapping.keys()))
        st = MappingPulseTemplate(template,
                                  parameter_mapping=parameter_mapping,
                                  measurement_mapping=measurement_mapping)
        sequencer = DummySequencer()
        block = DummyInstructionBlock()
        pre_parameters = {'k': ConstantParameter(5)}
        pre_measurement_mapping = {'meas2': 'meas3'}
        pre_channel_mapping = {'default': 'A'}
        conditions = dict(a=True)
        st.build_sequence(sequencer, pre_parameters, conditions,
                          pre_measurement_mapping, pre_channel_mapping, block)

        self.assertEqual(template.build_sequence_calls, 1)
        forwarded_args = template.build_sequence_arguments[0]
        self.assertEqual(forwarded_args[0], sequencer)
        self.assertEqual(forwarded_args[1], st.map_parameters(pre_parameters))
        self.assertEqual(forwarded_args[2], conditions)
        self.assertEqual(
            forwarded_args[3],
            st.get_updated_measurement_mapping(pre_measurement_mapping))
        self.assertEqual(forwarded_args[4],
                         st.get_updated_channel_mapping(pre_channel_mapping))
        self.assertEqual(forwarded_args[5], block)
Exemplo n.º 19
0
 def test_build_sequence_declaration_parameter_value_not_whole(
         self) -> None:
     parameters = dict(foo=ConstantParameter(3.3))
     conditions = dict(foo=DummyCondition(requires_stop=True))
     with self.assertRaises(ParameterNotIntegerException):
         self.template.build_sequence(self.sequencer, parameters,
                                      conditions, {}, {}, self.block)
     self.assertFalse(self.sequencer.sequencing_stacks)
Exemplo n.º 20
0
    def test_build_sequence(self):
        dt = DummyPulseTemplate(parameter_names={'i'})
        flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('a', 'b', 'c'),
                                   measurements=[('A', 0, 1)], parameter_constraints=['c > 1'])

        sequencer = DummySequencer()
        block = DummyInstructionBlock()
        invalid_parameters = {'a': ConstantParameter(1), 'b': ConstantParameter(4), 'c': ConstantParameter(1)}
        parameters = {'a': ConstantParameter(1), 'b': ConstantParameter(4), 'c': ConstantParameter(2)}
        measurement_mapping = dict(A='B')
        channel_mapping = dict(C='D')

        with self.assertRaises(ParameterConstraintViolation):
            flt.build_sequence(sequencer, invalid_parameters, dict(), measurement_mapping, channel_mapping, block)

        self.assertEqual(block.instructions, [])
        self.assertNotIn(block, sequencer.sequencing_stacks)

        flt.build_sequence(sequencer, parameters, dict(), measurement_mapping, channel_mapping, block)

        self.assertEqual(block.instructions, [MEASInstruction(measurements=[('B', 0, 1)])])

        expected_stack = [(dt, {'i': ConstantParameter(3)}, dict(), measurement_mapping, channel_mapping),
                          (dt, {'i': ConstantParameter(1)}, dict(), measurement_mapping, channel_mapping)]

        self.assertEqual(sequencer.sequencing_stacks[block], expected_stack)
Exemplo n.º 21
0
    def test_get_value(self) -> None:
        decl = ParameterDeclaration('foo')
        foo_param = ConstantParameter(2.1)
        self.assertEqual(foo_param.get_value(), decl.get_value({'foo': foo_param}))
        self.assertRaises(ParameterNotProvidedException, decl.get_value, {})
        
        decl = ParameterDeclaration('foo', default=2.7)
        self.assertEqual(decl.default_value, decl.get_value({}))

        decl = ParameterDeclaration('foo', min=1.3)
        self.assertEqual(1.4, decl.get_value({'foo': ConstantParameter(1.4)}))
        self.assertEqual(1.3, decl.get_value({'foo': ConstantParameter(1.3)}))
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'foo': ConstantParameter(1.1)})

        decl = ParameterDeclaration('foo', max=2.3)
        self.assertTrue(1.4, decl.get_value({'foo': ConstantParameter(1.4)}))
        self.assertTrue(2.3, decl.get_value({'foo': ConstantParameter(2.3)}))
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'foo': ConstantParameter(3.1)})

        decl = ParameterDeclaration('foo', min=1.3, max=2.3)
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'foo': ConstantParameter(0.9)})
        self.assertEqual(1.3, decl.get_value({'foo': ConstantParameter(1.3)}))
        self.assertEqual(1.4, decl.get_value({'foo': ConstantParameter(1.4)}))
        self.assertEqual(2.3, decl.get_value({'foo': ConstantParameter(2.3)}))
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'foo': ConstantParameter(3.1)})

        min_decl = ParameterDeclaration('min', min=1.2, max=2.3)
        max_decl = ParameterDeclaration('max', min=1.2, max=5.1)

        min_param = ConstantParameter(1.3)
        max_param = ConstantParameter(2.3)

        decl = ParameterDeclaration('foo', min=min_decl, max=max_decl)
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'min': min_param, 'max': max_param, 'foo': ConstantParameter(0.9)})
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.2)})
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.25)})
        self.assertEqual(1.3, decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.3)}))
        self.assertEqual(1.7, decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.7)}))
        self.assertEqual(2.3, decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(2.3)}))
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'min': min_param, 'max': max_param, 'foo': ConstantParameter(3.5)})
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'min': min_param, 'max': max_param, 'foo': ConstantParameter(5.1)})
        self.assertRaises(ParameterValueIllegalException, decl.get_value, {'min': min_param, 'max': max_param, 'foo': ConstantParameter(17.2)})
Exemplo n.º 22
0
    def test_integration_table_and_function_template(self) -> None:
        from qctoolkit.pulses import TablePulseTemplate, FunctionPulseTemplate, Sequencer, EXECInstruction, STOPInstruction

        table_template = TablePulseTemplate(channels=2)
        table_template.add_entry(1, 4, channel=0)
        table_template.add_entry('foo', 'bar', channel=0)
        table_template.add_entry(10, 0, channel=0)
        table_template.add_entry('foo', 2.7, interpolation='linear', channel=1)
        table_template.add_entry(9, 'bar', interpolation='linear', channel=1)

        function_template = FunctionPulseTemplate('sin(t)', '10')

        template = MultiChannelPulseTemplate(
            [(function_template, dict(), [1]),
             (table_template, dict(foo='5', bar='2 * hugo'), [2, 0])],
            {'hugo'})

        sample_times = numpy.linspace(98.5, 103.5, num=11)
        function_template_samples = function_template.build_waveform(
            dict()).sample(sample_times)
        table_template_samples = table_template.build_waveform(
            dict(foo=ConstantParameter(5),
                 bar=ConstantParameter(2 * (-1.3)))).sample(sample_times)

        template_waveform = template.build_waveform(
            dict(hugo=ConstantParameter(-1.3)))
        template_samples = template_waveform.sample(sample_times)

        self.assertTrue(
            numpy.all(table_template_samples[0] == template_samples[2]))
        self.assertTrue(
            numpy.all(table_template_samples[1] == template_samples[0]))
        self.assertTrue(
            numpy.all(function_template_samples[0] == template_samples[1]))

        sequencer = Sequencer()
        sequencer.push(template, parameters=dict(hugo=-1.3), conditions=dict())
        instructions = sequencer.build()
        self.assertEqual(2, len(instructions))
        self.assertIsInstance(instructions[0], EXECInstruction)
        self.assertIsInstance(instructions[1], STOPInstruction)
Exemplo n.º 23
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)
Exemplo n.º 24
0
    def test_build_sequence(self) -> None:
        dummy_wf1 = DummyWaveform(duration=2.3, num_channels=2)
        dummy_wf2 = DummyWaveform(duration=2.3, num_channels=1)
        dummy1 = DummyPulseTemplate(parameter_names={'foo'},
                                    num_channels=2,
                                    waveform=dummy_wf1)
        dummy2 = DummyPulseTemplate(parameter_names={},
                                    num_channels=1,
                                    waveform=dummy_wf2)

        pulse = MultiChannelPulseTemplate([(dummy1, {
            'foo': '2*bar'
        }, [2, 1]), (dummy2, {}, [0])], {'bar'})

        result = pulse.build_waveform({'bar': ConstantParameter(3)})
        expected = MultiChannelWaveform([(dummy_wf1, [2, 1]),
                                         (dummy_wf2, [0])])
        self.assertEqual(expected, result)
        self.assertEqual([{
            'foo':
            MappedParameter(Expression("2*bar"), {'bar': ConstantParameter(3)})
        }], dummy1.build_waveform_calls)
        self.assertEqual([{}], dummy2.build_waveform_calls)
Exemplo n.º 25
0
    def test_parameter_not_provided(self):
        parameters = dict(foo=ConstantParameter(4))
        conditions = dict(foo=DummyCondition(requires_stop=True))
        measurement_mapping = dict(moth='fire')
        channel_mapping = dict(asd='f')

        template = RepetitionPulseTemplate(self.body,
                                           'foo*bar',
                                           parameter_constraints=['foo<9'])

        with self.assertRaises(ParameterNotProvidedException):
            template.build_sequence(self.sequencer, parameters, conditions,
                                    measurement_mapping, channel_mapping,
                                    self.block)
    def test_map_parameters(self):
        template = DummyPulseTemplate(parameter_names={'foo', 'bar'})
        st = MappingPulseTemplate(template,
                                  parameter_mapping={
                                      'foo': 't*k',
                                      'bar': 't*l'
                                  })

        parameters = {
            't': ConstantParameter(3),
            'k': ConstantParameter(2),
            'l': ConstantParameter(7)
        }
        values = {'foo': 6, 'bar': 21}
        for k, v in st.map_parameters(parameters).items():
            self.assertEqual(v.get_value(), values[k])
        parameters.popitem()
        with self.assertRaises(ParameterNotProvidedException):
            st.map_parameters(parameters)

        parameters = dict(t=3, k=2, l=7)
        values = {'foo': 6, 'bar': 21}
        for k, v in st.map_parameters(parameters).items():
            self.assertEqual(v, values[k])
Exemplo n.º 27
0
 def test_init_single_subtemplate_requires_stop_external_params(
         self) -> None:
     subtemplate = DummyPulseTemplate(parameter_names={'foo'},
                                      requires_stop=True,
                                      num_channels=2,
                                      duration=1.3)
     template = MultiChannelPulseTemplate([(subtemplate, {
         'foo': "2.3 ** bar"
     }, [1, 0])], {'bar'})
     self.assertEqual({'bar'}, template.parameter_names)
     self.assertEqual({ParameterDeclaration('bar')},
                      template.parameter_declarations)
     self.assertFalse(template.is_interruptable)
     self.assertTrue(
         template.requires_stop(dict(bar=ConstantParameter(3.5)), dict()))
     self.assertEqual(2, template.num_channels)
    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()
Exemplo n.º 29
0
    def _body_parameter_generator(self,
                                  parameters: Dict[str, Parameter],
                                  forward=True) -> Generator:
        loop_range_parameters = dict(
            (parameter_name, parameters[parameter_name].get_value())
            for parameter_name in self._loop_range.parameter_names)
        loop_range = self._loop_range.to_range(loop_range_parameters)

        parameters = dict((parameter_name, parameters[parameter_name])
                          for parameter_name in self.body.parameter_names
                          if parameter_name != self._loop_index)
        loop_range = loop_range if forward else reversed(loop_range)
        for loop_index_value in loop_range:
            local_parameters = parameters.copy()
            local_parameters[self._loop_index] = ConstantParameter(
                loop_index_value)
            yield local_parameters
Exemplo n.º 30
0
    def test_build_sequence_declaration_success(self) -> None:
        parameters = dict(foo=ConstantParameter(3))
        conditions = dict(foo=DummyCondition(requires_stop=True))
        measurement_mapping = dict(moth='fire')
        channel_mapping = dict(asd='f')
        self.template.build_sequence(self.sequencer, parameters, conditions,
                                     measurement_mapping, channel_mapping,
                                     self.block)

        self.assertTrue(self.block.embedded_blocks)
        body_block = self.block.embedded_blocks[0]
        self.assertEqual({body_block},
                         set(self.sequencer.sequencing_stacks.keys()))
        self.assertEqual([(self.body, parameters, conditions,
                           measurement_mapping, channel_mapping)],
                         self.sequencer.sequencing_stacks[body_block])
        self.assertEqual(
            [REPJInstruction(3, InstructionPointer(body_block, 0))],
            self.block.instructions)
    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()
Exemplo n.º 32
0
 def __test_valid_params(self, value: float) -> None:
     constant_parameter = ConstantParameter(value)
     self.assertEqual(value, constant_parameter.get_value())
     self.assertEqual(value, float(constant_parameter))
Exemplo n.º 33
0
 def test_get_serialization_data(self) -> None:
     constant_parameter = ConstantParameter(-0.2)
     serializer = DummySerializer()
     data = constant_parameter.get_serialization_data(serializer)
     self.assertEqual(dict(type=serializer.get_type_identifier(constant_parameter), constant=-0.2), data)
Exemplo n.º 34
0
 def test_deserialize(self) -> None:
     serializer = DummySerializer()
     constant_parameter = ConstantParameter.deserialize(serializer, constant=3.1)
     self.assertEqual(3.1, constant_parameter.get_value())
     self.assertIsNone(constant_parameter.identifier)