예제 #1
0
 def build_sequence_branch(self,
                           delegator: SequencingElement,
                           if_branch: SequencingElement,
                           else_branch: SequencingElement,
                           sequencer: 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
    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)
        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)

        repetition_count = self.get_repetition_count_value(real_parameters)
        if repetition_count > 0:
            body_block = InstructionBlock()
            body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block))

            instruction_block.add_instruction_repj(repetition_count, body_block)
            sequencer.push(self.body, parameters=parameters, conditions=conditions,
                           window_mapping=measurement_mapping, channel_mapping=channel_mapping, target_block=body_block)
예제 #3
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)
예제 #4
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)
예제 #5
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)
예제 #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)
예제 #7
0
    def test_render_block_time_slice(self) -> None:
        with self.assertWarnsRegex(DeprecationWarning, ".*InstructionBlock.*"):
            with self.assertRaises(ValueError):
                wf1 = DummyWaveform(duration=19)
                wf2 = DummyWaveform(duration=21)

                block = InstructionBlock()
                block.add_instruction_exec(wf1)
                block.add_instruction_exec(wf2)

                times, voltages, _ = render(block,
                                            sample_rate=0.5,
                                            time_slice=(1, 16))
예제 #8
0
    def test_render_loop_compare(self) -> None:
        wf1 = DummyWaveform(duration=19)
        wf2 = DummyWaveform(duration=21)

        block = InstructionBlock()
        block.add_instruction_exec(wf1)
        block.add_instruction_meas([('asd', 0, 1), ('asd', 1, 1)])
        block.add_instruction_exec(wf2)

        mcp = MultiChannelProgram(block)
        loop = next(iter(mcp.programs.values()))

        block_times, block_voltages, _ = render(block, sample_rate=0.5)
        loop_times, loop_voltages, _ = render(loop, sample_rate=0.5)

        numpy.testing.assert_equal(block_times, loop_times)
        numpy.testing.assert_equal(block_voltages, loop_voltages)

        block_times, block_voltages, block_measurements = render(
            block, sample_rate=0.5, render_measurements=True)
        loop_times, loop_voltages, loop_measurements = render(
            loop, sample_rate=0.5, render_measurements=True)
        numpy.testing.assert_equal(block_times, loop_times)
        numpy.testing.assert_equal(block_voltages, loop_voltages)
        self.assertEqual(block_measurements, loop_measurements)
예제 #9
0
    def test_render(self) -> None:
        with self.assertWarnsRegex(DeprecationWarning, ".*InstructionBlock.*"):
            wf1 = DummyWaveform(duration=19)
            wf2 = DummyWaveform(duration=21)

            block = InstructionBlock()
            block.add_instruction_exec(wf1)
            block.add_instruction_meas([('asd', 0, 1)])
            block.add_instruction_exec(wf2)

            wf1_expected = ('A', [0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
            wf2_expected = ('A', [
                x - 19 for x in [20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40]
            ])
            wf1_output_array_len_expected = len(wf1_expected[1])
            wf2_output_array_len_expected = len(wf2_expected[1])

            wf1.sample_output = numpy.linspace(start=4,
                                               stop=5,
                                               num=len(wf1_expected[1]))
            wf2.sample_output = numpy.linspace(6, 7, num=len(wf2_expected[1]))

            expected_times = numpy.arange(start=0, stop=42, step=2)
            expected_result = numpy.concatenate(
                (wf1.sample_output, wf2.sample_output))

            times, voltages, _ = render(block, sample_rate=0.5)

            self.assertEqual(len(wf1.sample_calls), 1)
            self.assertEqual(len(wf2.sample_calls), 1)

            self.assertEqual(wf1_expected[0], wf1.sample_calls[0][0])
            self.assertEqual(wf2_expected[0], wf2.sample_calls[0][0])

            numpy.testing.assert_almost_equal(wf1_expected[1],
                                              wf1.sample_calls[0][1])
            numpy.testing.assert_almost_equal(wf2_expected[1],
                                              wf2.sample_calls[0][1])

            self.assertEqual(wf1_output_array_len_expected,
                             len(wf1.sample_calls[0][2]))
            self.assertEqual(wf2_output_array_len_expected,
                             len(wf2.sample_calls[0][2]))

            self.assertEqual(voltages.keys(), dict(A=0).keys())

            numpy.testing.assert_almost_equal(expected_times, times)
            numpy.testing.assert_almost_equal(expected_result, voltages['A'])
            self.assertEqual(expected_result.shape, voltages['A'].shape)

            times, voltages, measurements = render(block,
                                                   sample_rate=0.5,
                                                   render_measurements=True)
            self.assertEqual(voltages.keys(), dict(A=0).keys())

            numpy.testing.assert_almost_equal(expected_times, times)
            numpy.testing.assert_almost_equal(expected_result, voltages['A'])
            self.assertEqual(expected_result.shape, voltages['A'].shape)

            self.assertEqual(measurements, [('asd', 19, 1)])
예제 #10
0
    def test_add_instruction_exec(self) -> None:
        block = InstructionBlock()
        expected_instructions = []

        waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()]
        LOOKUP = [0, 1, 1, 0, 2, 1, 0, 0, 0, 1, 2, 2]
        for id in LOOKUP:
            waveform = waveforms[id]
            instruction = EXECInstruction(waveform)
            expected_instructions.append(instruction)
            block.add_instruction_exec(waveform)

        expected_compiled_instructions = expected_instructions.copy()
        expected_compiled_instructions.append(STOPInstruction())
        self.__verify_block(block, expected_instructions,
                            expected_compiled_instructions, None)
예제 #11
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]]
예제 #12
0
    def test_build_path_o2_m1_i1_t_m2_i0_i1_f_one_element_main_block_adds_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)
        
        elem = DummySequencingElement(False, (new_block, [new_elem]))
        sequencer.push(elem, ps, cs)

        sequence = sequencer.build()
        
        self.assertFalse(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(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))
예제 #13
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()}
     wm = {}
     cm = {}
     elem_main = DummySequencingElement(True)
     sequencer.push(elem_main, ps, cs, channel_mapping=cm)
     
     elem_cstm = DummySequencingElement(True)
     target_block = InstructionBlock()
     sequencer.push(elem_cstm, ps, cs, window_mapping=wm, channel_mapping=cm, target_block=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)
예제 #14
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()}
     wm = {'foo': 'bar'}
     
     target_block = InstructionBlock()
     elem2 = DummySequencingElement(True)
     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(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)
예제 #15
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=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))
예제 #16
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)

        instr, stop = sequencer.build()
        
        self.assertFalse(sequencer.has_finished())
        self.assertIs(elem1, instr.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(), stop)
예제 #17
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)
예제 #18
0
 def build_sequence_loop(self, 
                         delegator: SequencingElement,
                         body: SequencingElement,
                         sequencer: Sequencer,
                         parameters: Dict[str, Parameter],
                         conditions: Dict[str, Condition],
                         measurement_mapping: Dict[str, str],
                         channel_mapping: Dict[ChannelID, ChannelID],
                         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, window_mapping=measurement_mapping,
                    channel_mapping=channel_mapping,
                    target_block=body_block)
예제 #19
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']
예제 #20
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)
예제 #21
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)
예제 #22
0
    def test_render_warning(self) -> None:
        wf1 = DummyWaveform(duration=19)
        wf2 = DummyWaveform(duration=21)

        block = InstructionBlock()
        block.add_instruction_exec(wf1)
        block.add_instruction_meas([('asd', 0, 1)])
        block.add_instruction_exec(wf2)

        with self.assertWarns(UserWarning):
            render(block, sample_rate=0.51314323423)
예제 #23
0
파일: loop_tests.py 프로젝트: Hylta/qupulse
    def test_empty_repj(self):
        empty_block = InstructionBlock()

        root_block = InstructionBlock()
        root_block.add_instruction_repj(1, empty_block)

        with self.assertRaisesRegex(ValueError, 'no defined channels'):
            MultiChannelProgram(root_block)

        empty_block.add_instruction_exec(DummyWaveform(duration=1, defined_channels={'A', 'B'}))
        MultiChannelProgram(root_block)
예제 #24
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)
예제 #25
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
예제 #26
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)
예제 #27
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]
예제 #28
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)
예제 #29
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)
예제 #30
0
 def test_item_access_return(self) -> None:
     wf = DummyWaveform()
     parent_block = InstructionBlock()
     block = AbstractInstructionBlockStub([EXECInstruction(wf)],
                                          InstructionPointer(
                                              parent_block, 29))
     self.assertEqual(EXECInstruction(wf), block[0])
     self.assertEqual(GOTOInstruction(InstructionPointer(parent_block, 29)),
                      block[1])
     with self.assertRaises(IndexError):
         block[2]
     self.assertEqual(GOTOInstruction(InstructionPointer(parent_block, 29)),
                      block[-1])
     self.assertEqual(EXECInstruction(wf), block[-2])
     with self.assertRaises(IndexError):
         block[-3]