예제 #1
0
    def test_cleanup(self):
        channel_pair = TaborChannelPair(self.instrument,
                                        identifier='asd',
                                        channels=(1, 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)

        channel_pair.cleanup()
        np.testing.assert_equal(channel_pair._segment_references,
                                np.array([1, 2, 0, 1], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_capacity,
            192 + np.array([0, 16, 32, 32], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_lengths,
            192 + np.array([0, 0, 16, 16], dtype=np.uint32))
        np.testing.assert_equal(channel_pair._segment_hashes,
                                np.array([1, 2, 3, 4], dtype=np.int64))

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

        channel_pair.cleanup()
        np.testing.assert_equal(channel_pair._segment_references,
                                np.array([1, 2, 0, 1], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_capacity,
            192 + np.array([0, 16, 32, 32], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_lengths,
            192 + np.array([0, 0, 16, 16], dtype=np.uint32))
        np.testing.assert_equal(channel_pair._segment_hashes,
                                np.array([1, 2, 3, 4], dtype=np.int64))
예제 #2
0
    def test_upload_segment(self):
        channel_pair = TaborChannelPair(self.instrument,
                                        identifier='asd',
                                        channels=(1, 2))

        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 = channel_pair._segment_capacity.copy()

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

        # prevent entering and exiting configuration mode
        channel_pair._configuration_guard_count = 2

        segment = TaborSegment.from_sampled(
            np.ones(192 + 16, dtype=np.uint16),
            np.zeros(192 + 16, dtype=np.uint16), None, None)
        segment_binary = segment.get_as_binary()
        with self.assertRaises(ValueError):
            channel_pair._upload_segment(3, segment)

        with self.assertRaises(ValueError):
            channel_pair._upload_segment(0, segment)

        channel_pair._upload_segment(2, segment)
        np.testing.assert_equal(
            channel_pair._segment_capacity,
            192 + np.array([0, 16, 32, 32], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_lengths,
            192 + np.array([0, 16, 16, 32], dtype=np.uint32))
        np.testing.assert_equal(
            channel_pair._segment_hashes,
            np.array([1, 2, hash(segment), 4], dtype=np.int64))

        expected_commands = [
            ':INST:SEL 1', ':INST:SEL 1', ':INST:SEL 1', ':TRAC:DEF 3, 208',
            ':TRAC:SEL 3', ':TRAC:MODE COMB'
        ]
        expected_log = [
            ((),
             dict(cmd_str=cmd,
                  paranoia_level=channel_pair.internal_paranoia_level))
            for cmd in expected_commands
        ]
        self.assertAllCommandLogsEqual(expected_log)

        self.assert_written_segment_data(segment_binary)
예제 #3
0
    def test_free_program(self):
        channel_pair = TaborChannelPair(self.instrument,
                                        identifier='asd',
                                        channels=(1, 2))

        with self.assertRaises(KeyError):
            channel_pair.free_program('test')

        program = TaborProgramMemory(np.array([1, 2], dtype=np.int64), None)

        channel_pair._segment_references = np.array([1, 3, 1, 0])
        channel_pair._known_programs['test'] = program
        self.assertIs(channel_pair.free_program('test'), program)

        np.testing.assert_equal(channel_pair._segment_references,
                                np.array([1, 2, 0, 0]))
예제 #4
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)
예제 #5
0
    def test_find_place_for_segments_in_memory(self):
        def hash_based_on_dir(ch):
            hash_list = []
            for d in dir(ch):
                o = getattr(ch, d)
                if isinstance(o, np.ndarray):
                    hash_list.append(hash(o.tobytes()))
                else:
                    try:
                        hash_list.append(hash(o))
                    except TypeError:
                        pass
            return hash(tuple(hash_list))

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

        # empty
        segments = np.asarray([-5, -6, -7, -8, -9])
        segment_lengths = 192 + np.asarray([32, 16, 64, 32, 16])

        hash_before = hash_based_on_dir(channel_pair)

        w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(
            segments, segment_lengths)
        self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1])
        self.assertEqual(ta.tolist(), [True, True, True, True, True])
        self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1])
        self.assertEqual(hash_before, hash_based_on_dir(channel_pair))

        # all new segments
        channel_pair._segment_capacity = 192 + np.asarray([0, 16, 32, 16, 0],
                                                          dtype=np.uint32)
        channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5],
                                                  dtype=np.int64)
        channel_pair._segment_references = np.asarray([1, 1, 1, 2, 1],
                                                      dtype=np.int32)
        hash_before = hash_based_on_dir(channel_pair)

        w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(
            segments, segment_lengths)
        self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1])
        self.assertEqual(ta.tolist(), [True, True, True, True, True])
        self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1])
        self.assertEqual(hash_before, hash_based_on_dir(channel_pair))

        # some known segments
        channel_pair._segment_capacity = 192 + np.asarray(
            [0, 16, 32, 64, 0, 16], dtype=np.uint32)
        channel_pair._segment_hashes = np.asarray([1, 2, 3, -7, 5, -9],
                                                  dtype=np.int64)
        channel_pair._segment_references = np.asarray([1, 1, 1, 2, 1, 3],
                                                      dtype=np.int32)
        hash_before = hash_based_on_dir(channel_pair)

        w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(
            segments, segment_lengths)
        self.assertEqual(w2s.tolist(), [-1, -1, 3, -1, 5])
        self.assertEqual(ta.tolist(), [True, True, False, True, False])
        self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1])
        self.assertEqual(hash_before, hash_based_on_dir(channel_pair))

        # insert some segments with same length
        channel_pair._segment_capacity = 192 + np.asarray(
            [0, 16, 32, 64, 0, 16], dtype=np.uint32)
        channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5, 6],
                                                  dtype=np.int64)
        channel_pair._segment_references = np.asarray([1, 0, 1, 0, 1, 3],
                                                      dtype=np.int32)
        hash_before = hash_based_on_dir(channel_pair)

        w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(
            segments, segment_lengths)
        self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1])
        self.assertEqual(ta.tolist(), [True, False, False, True, True])
        self.assertEqual(ti.tolist(), [-1, 1, 3, -1, -1])
        self.assertEqual(hash_before, hash_based_on_dir(channel_pair))

        # insert some segments with smaller length
        channel_pair._segment_capacity = 192 + np.asarray(
            [0, 80, 32, 64, 96, 16], dtype=np.uint32)
        channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5, 6],
                                                  dtype=np.int64)
        channel_pair._segment_references = np.asarray([1, 0, 1, 1, 0, 3],
                                                      dtype=np.int32)
        hash_before = hash_based_on_dir(channel_pair)

        w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(
            segments, segment_lengths)
        self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1])
        self.assertEqual(ta.tolist(), [True, True, False, False, True])
        self.assertEqual(ti.tolist(), [-1, -1, 4, 1, -1])
        self.assertEqual(hash_before, hash_based_on_dir(channel_pair))

        # mix everything
        segments = np.asarray([-5, -6, -7, -8, -9, -10, -11])
        segment_lengths = 192 + np.asarray([32, 16, 64, 32, 16, 0, 0])

        channel_pair._segment_capacity = 192 + np.asarray(
            [0, 80, 32, 64, 32, 16], dtype=np.uint32)
        channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, -8, 6],
                                                  dtype=np.int64)
        channel_pair._segment_references = np.asarray([1, 0, 1, 0, 1, 0],
                                                      dtype=np.int32)
        hash_before = hash_based_on_dir(channel_pair)

        w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(
            segments, segment_lengths)
        self.assertEqual(w2s.tolist(), [-1, -1, -1, 4, -1, -1, -1])
        self.assertEqual(ta.tolist(),
                         [False, True, False, False, True, True, True])
        self.assertEqual(ti.tolist(), [1, -1, 3, -1, -1, -1, -1])
        self.assertEqual(hash_before, hash_based_on_dir(channel_pair))
예제 #6
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))