-
Notifications
You must be signed in to change notification settings - Fork 31
/
tabor_simulator_based_tests.py
232 lines (167 loc) · 8.37 KB
/
tabor_simulator_based_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import unittest
import subprocess
import time
import platform
import os
import pytabor
import numpy as np
from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborSegment, TaborChannelPair, PlottableProgram
class TaborSimulatorManager:
def __init__(self,
simulator_executable='WX2184C.exe',
simulator_path=os.path.realpath(os.path.dirname(__file__))):
self.simulator_executable = simulator_executable
self.simulator_path = simulator_path
self.started_simulator = False
self.simulator_process = None
self.instrument = None
def kill_running_simulators(self):
command = 'Taskkill', '/IM {simulator_executable}'.format(simulator_executable=self.simulator_executable)
try:
subprocess.run([command],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except FileNotFoundError:
pass
@property
def simulator_full_path(self):
return os.path.join(self.simulator_path, self.simulator_executable)
def start_simulator(self, try_connecting_to_existing_simulator=True, max_wait_time=30):
if try_connecting_to_existing_simulator:
if pytabor.open_session('127.0.0.1') is not None:
return
if not os.path.isfile(self.simulator_full_path):
raise RuntimeError('Cannot locate simulator executable.')
self.kill_running_simulators()
self.simulator_process = subprocess.Popen([self.simulator_full_path, '/switch-on', '/gui-in-tray'])
start = time.time()
while pytabor.open_session('127.0.0.1') is None:
if self.simulator_process.returncode:
raise RuntimeError('Simulator exited with return code {}'.format(self.simulator_process.returncode))
if time.time() - start > max_wait_time:
raise RuntimeError('Could not connect to simulator')
time.sleep(0.1)
def connect(self):
self.instrument = TaborAWGRepresentation('127.0.0.1',
reset=True,
paranoia_level=2)
if self.instrument.main_instrument.visa_inst is None:
raise RuntimeError('Could not connect to simulator')
return self.instrument
def disconnect(self):
for device in self.instrument.all_devices:
device.close()
self.instrument = None
def __del__(self):
if self.started_simulator and self.simulator_process:
self.simulator_process.kill()
@unittest.skipIf(platform.system() != 'Windows', "Simulator currently only available on Windows :(")
class TaborSimulatorBasedTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.instrument = None
@classmethod
def setUpClass(cls):
cls.simulator_manager = TaborSimulatorManager('WX2184C.exe', os.path.dirname(__file__))
try:
cls.simulator_manager.start_simulator()
except RuntimeError as err:
raise unittest.SkipTest(*err.args) from err
@classmethod
def tearDownClass(cls):
del cls.simulator_manager
def setUp(self):
self.instrument = self.simulator_manager.connect()
def tearDown(self):
self.instrument.reset()
self.simulator_manager.disconnect()
class TaborAWGRepresentationTests(TaborSimulatorBasedTest):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def test_sample_rate(self):
for ch in (1, 2, 3, 4):
self.assertIsInstance(self.instrument.sample_rate(ch), int)
with self.assertRaises(TaborException):
self.instrument.sample_rate(0)
self.instrument.send_cmd(':INST:SEL 1;')
self.instrument.send_cmd(':FREQ:RAST 2.3e9')
self.assertEqual(2300000000, self.instrument.sample_rate(1))
def test_amplitude(self):
for ch in (1, 2, 3, 4):
self.assertIsInstance(self.instrument.amplitude(ch), float)
with self.assertRaises(TaborException):
self.instrument.amplitude(0)
self.instrument.send_cmd(':INST:SEL 1; :OUTP:COUP DC')
self.instrument.send_cmd(':VOLT 0.7')
self.assertAlmostEqual(.7, self.instrument.amplitude(1))
def test_select_marker(self):
with self.assertRaises(TaborException):
self.instrument.select_marker(6)
self.instrument.select_marker(2)
selected = self.instrument.send_query(':SOUR:MARK:SEL?')
self.assertEqual(selected, '2')
self.instrument.select_marker(1)
selected = self.instrument.send_query(':SOUR:MARK:SEL?')
self.assertEqual(selected, '1')
def test_select_channel(self):
with self.assertRaises(TaborException):
self.instrument.select_channel(6)
self.instrument.select_channel(1)
self.assertEqual(self.instrument.send_query(':INST:SEL?'), '1')
self.instrument.select_channel(4)
self.assertEqual(self.instrument.send_query(':INST:SEL?'), '4')
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(ramp_up, ramp_up, None, None),
TaborSegment(ramp_down, zero, None, None),
TaborSegment(sine, sine, None, None)]
self.zero_segment = TaborSegment(zero, zero, None, None)
# program 1
self.sequence_tables = [[(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.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
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()
reformated = PlottableProgram._reformat_waveforms(waveforms)
expected = [self.zero_segment, *self.segments]
for (ex1, ex2), r1, r2 in zip(expected, *reformated):
np.testing.assert_equal(ex1, r1)
np.testing.assert_equal(ex2, r2)
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_sequece_tables = [self.channel_pair._idle_sequence_table] + [[(rep, index+2, jump)
for rep, index, jump in table]
for table in self.sequence_tables]
expected = list(tuple(np.asarray(d)
for d in zip(*table))
for table in actual_sequece_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)