Exemplo n.º 1
0
    def test_amend_segments_iter(self):
        channel_pair = TaborChannelPair(self.instrument,
                                        identifier='asd',
                                        channels=(1, 2))
        # prevent entering and exiting configuration mode
        channel_pair._configuration_guard_count = 2

        self.instrument.paranoia_level = 0
        self.reset_instrument_logs()

        channel_pair._segment_references = np.array([1, 2, 0, 1],
                                                    dtype=np.uint32)
        channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32],
                                                        dtype=np.uint32)
        channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16],
                                                       dtype=np.uint32)

        channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64)

        data = np.ones(192, dtype=np.uint16)
        segments = [
            TaborSegment.from_sampled(0 * data, 1 * data, None, None),
            TaborSegment.from_sampled(1 * data, 2 * data, None, None)
        ]

        indices = channel_pair._amend_segments(segments)

        expected_references = np.array([1, 2, 0, 1, 1, 1], dtype=np.uint32)
        expected_capacities = 192 + np.array([0, 16, 32, 32, 0, 0],
                                             dtype=np.uint32)
        expected_lengths = 192 + np.array([0, 0, 16, 16, 0, 0],
                                          dtype=np.uint32)
        expected_hashes = np.array(
            [1, 2, 3, 4, hash(segments[0]),
             hash(segments[1])], dtype=np.int64)

        np.testing.assert_equal(channel_pair._segment_references,
                                expected_references)
        np.testing.assert_equal(channel_pair._segment_capacity,
                                expected_capacities)
        np.testing.assert_equal(channel_pair._segment_lengths,
                                expected_lengths)
        np.testing.assert_equal(channel_pair._segment_hashes, expected_hashes)

        np.testing.assert_equal(indices, np.array([4, 5], dtype=np.int64))

        expected_commands = [
            ':INST:SEL 1', ':TRAC:DEF 5,{}'.format(2 * 192 + 16),
            ':TRAC:SEL 5', ':TRAC:MODE COMB', ':TRAC:DEF 5,192',
            ':TRAC:DEF 6,192'
        ]
        expected_log = [
            ((),
             dict(cmd_str=cmd,
                  paranoia_level=channel_pair.internal_paranoia_level))
            for cmd in expected_commands
        ]
        self.assertAllCommandLogsEqual(expected_log)

        # no segment lengths written
        self.assert_written_segment_lengths()

        expected_bin_blob = make_combined_wave(segments)
        self.assert_written_segment_data(expected_bin_blob)
Exemplo n.º 2
0
class TaborMemoryReadTests(TaborSimulatorBasedTest):
    def setUp(self):
        super().setUp()

        ramp_up = np.linspace(0, 2**14-1, num=192, dtype=np.uint16)
        ramp_down = ramp_up[::-1]
        zero = np.ones(192, dtype=np.uint16) * 2**13
        sine = ((np.sin(np.linspace(0, 2*np.pi, 192+64)) + 1) / 2 * (2**14 - 1)).astype(np.uint16)

        self.segments = [TaborSegment.from_sampled(ramp_up, ramp_up, None, None),
                         TaborSegment.from_sampled(ramp_down, zero, None, None),
                         TaborSegment.from_sampled(sine, sine, None, None)]

        self.zero_segment = TaborSegment.from_sampled(zero, zero, None, None)

        # program 1
        self.sequence_tables_raw = [[(10, 0, 0), (10, 1, 0), (10, 0, 0), (10, 1, 0)],
                                    [(1, 0, 0), (1, 1, 0), (1, 0, 0), (1, 1, 0)]]
        self.advanced_sequence_table = [(1, 1, 0), (1, 2, 0)]

        self.sequence_tables = self.to_new_sequencer_tables(self.sequence_tables_raw)
        self.advanced_sequence_table = self.to_new_advanced_sequencer_table(self.advanced_sequence_table)

        self.channel_pair = TaborChannelPair(self.instrument, (1, 2), 'tabor_unit_test')

    def arm_program(self, sequencer_tables, advanced_sequencer_table, mode, waveform_to_segment_index):
        class DummyProgram:
            @staticmethod
            def get_sequencer_tables():
                return sequencer_tables

            @staticmethod
            def get_advanced_sequencer_table():
                return advanced_sequencer_table

            @staticmethod
            def update_volatile_parameters(parameters):
                modifications = {1: TableEntry(repetition_count=5, element_number=2, jump_flag=0),
                                 (0, 1): TableDescription(repetition_count=50, element_id=1, jump_flag=0)}
                return modifications

            markers = (None, None)
            channels = (1, 2)

            waveform_mode = mode

        self.channel_pair._known_programs['dummy_program'] = (waveform_to_segment_index, DummyProgram)
        self.channel_pair.change_armed_program('dummy_program')

    def test_read_waveforms(self):
        self.channel_pair._amend_segments(self.segments)

        waveforms = self.channel_pair.read_waveforms()

        segments = [TaborSegment.from_binary_segment(waveform)
                    for waveform in waveforms]

        expected = [self.zero_segment, *self.segments]

        for ex, r in zip(expected, segments):
            ex1, ex2 = ex.data_a, ex.data_b
            r1, r2 = r.data_a, r.data_b
            np.testing.assert_equal(ex1, r1)
            np.testing.assert_equal(ex2, r2)

        self.assertEqual(expected, segments)

    def test_read_sequence_tables(self):
        self.channel_pair._amend_segments(self.segments)
        self.arm_program(self.sequence_tables, self.advanced_sequence_table, None, np.asarray([1, 2]))

        sequence_tables = self.channel_pair.read_sequence_tables()

        actual_sequence_tables = [self.channel_pair._idle_sequence_table] + [[(rep, index+2, jump)
                                                                             for rep, index, jump in table]
                                                                             for table in self.sequence_tables_raw]

        expected = list(tuple(np.asarray(d)
                              for d in zip(*table))
                        for table in actual_sequence_tables)

        np.testing.assert_equal(sequence_tables, expected)

    def test_read_advanced_sequencer_table(self):
        self.channel_pair._amend_segments(self.segments)
        self.arm_program(self.sequence_tables, self.advanced_sequence_table, None, np.asarray([1, 2]))

        actual_advanced_table = [(1, 1, 1)] + [(rep, idx+1, jmp) for rep, idx, jmp in self.advanced_sequence_table]

        expected = list(np.asarray(d)
                        for d in zip(*actual_advanced_table))

        advanced_table = self.channel_pair.read_advanced_sequencer_table()
        np.testing.assert_equal(advanced_table, expected)

    def test_set_volatile_parameter(self):
        self.channel_pair._amend_segments(self.segments)
        self.arm_program(self.sequence_tables, self.advanced_sequence_table, None, np.asarray([1, 2]))

        para = {'a': 5}
        actual_sequence_tables = [self.channel_pair._idle_sequence_table] + [[(rep, index + 2, jump)
                                                                              for rep, index, jump in table]
                                                                             for table in self.sequence_tables_raw]

        actual_advanced_table = [(1, 1, 1)] + [(rep, idx + 1, jmp) for rep, idx, jmp in self.advanced_sequence_table]

        self.channel_pair.set_volatile_parameters('dummy_program', parameters=para)

        actual_sequence_tables[1][1] = (50, 3, 0)
        actual_advanced_table[2] = (5, 3, 0)

        sequence_table = self.channel_pair.read_sequence_tables()
        expected = list(tuple(np.asarray(d)
                              for d in zip(*table))
                        for table in actual_sequence_tables)
        np.testing.assert_equal(sequence_table, expected)

        advanced_table = self.channel_pair.read_advanced_sequencer_table()
        expected = list(np.asarray(d)
                        for d in zip(*actual_advanced_table))
        np.testing.assert_equal(advanced_table, expected)
Exemplo n.º 3
0
    def test_upload(self):
        segments = np.array([1, 2, 3, 4, 5])
        segment_lengths = np.array([0, 16, 0, 16, 0], dtype=np.uint16).tolist()

        segment_references = np.array([1, 1, 2, 0, 1], dtype=np.uint32)

        w2s = np.array([-1, -1, 1, 2, -1], dtype=np.int64)
        ta = np.array([True, False, False, False, True])
        ti = np.array([-1, 3, -1, -1, -1])

        channels = (1, None)
        markers = (None, None)
        voltage_transformations = (lambda x: x, lambda x: x)
        sample_rate = TimeType.from_fraction(1, 1)

        with mock.patch('qupulse.hardware.awgs.tabor.TaborProgram',
                        specs=TaborProgram) as DummyTaborProgram:
            tabor_program = DummyTaborProgram.return_value
            tabor_program.get_sampled_segments.return_value = (segments,
                                                               segment_lengths)

            program = Loop(waveform=DummyWaveform(duration=192))

            channel_pair = TaborChannelPair(self.instrument,
                                            identifier='asd',
                                            channels=(1, 2))
            channel_pair._segment_references = segment_references

            def dummy_find_place(segments_, segement_lengths_):
                self.assertIs(segments_, segments)
                self.assertIs(segment_lengths, segement_lengths_)
                return w2s, ta, ti

            def dummy_upload_segment(segment_index, segment):
                self.assertEqual(segment_index, 3)
                self.assertEqual(segment, 2)

            def dummy_amend_segments(segments_):
                np.testing.assert_equal(segments_, np.array([1, 5]))
                return np.array([5, 6], dtype=np.int64)

            self.instrument.amplitude = mock.Mock(return_value=1.)
            self.instrument.sample_rate = mock.Mock(return_value=10**9)

            channel_pair._find_place_for_segments_in_memory = dummy_find_place
            channel_pair._upload_segment = dummy_upload_segment
            channel_pair._amend_segments = dummy_amend_segments

            channel_pair.upload('test', program, channels, markers,
                                voltage_transformations)

            DummyTaborProgram.assert_called_once_with(
                program,
                channels=tuple(channels),
                markers=markers,
                device_properties=channel_pair.device.dev_properties,
                sample_rate=sample_rate,
                amplitudes=(.5, .5),
                offsets=(0., 0.),
                voltage_transformations=voltage_transformations)

            self.assertEqual(self.instrument.amplitude.call_args_list,
                             [mock.call(1), mock.call(2)])
            self.instrument.sample_rate.assert_called_once_with(1)

            # the other references are increased in amend and upload segment method
            np.testing.assert_equal(channel_pair._segment_references,
                                    np.array([1, 2, 3, 0, 1]))

            self.assertEqual(len(channel_pair._known_programs), 1)
            np.testing.assert_equal(
                channel_pair._known_programs['test'].waveform_to_segment,
                np.array([5, 3, 1, 2, 6], dtype=np.int64))