• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

qutech / qupulse / 12787699459

15 Jan 2025 11:50AM UTC coverage: 88.09%. First build
12787699459

Pull #881

github

web-flow
Merge afd82f439 into 21469b7f0
Pull Request #881: complicated test for linspacebuilder

5 of 24 new or added lines in 1 file covered. (20.83%)

18462 of 20958 relevant lines covered (88.09%)

5.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.37
/tests/program/linspace_tests.py
1
import copy
6✔
2
import unittest
6✔
3
from unittest import TestCase
6✔
4

5
from qupulse.pulses import *
6✔
6
from qupulse.program.linspace import *
6✔
7
from qupulse.program.transformation import *
6✔
8

9

10
def assert_vm_output_almost_equal(test: TestCase, expected, actual):
6✔
11
    """Compare two vm outputs with default TestCase.assertAlmostEqual accuracy"""
12
    test.assertEqual(len(expected), len(actual))
6✔
13
    for idx, ((t_e, vals_e), (t_a, vals_a)) in enumerate(zip(expected, actual)):
6✔
14
        test.assertEqual(t_e, t_a, f"Differing times in {idx} element")
6✔
15
        test.assertEqual(len(vals_e), len(vals_a), f"Differing channel count in {idx} element")
6✔
16
        for ch, (val_e, val_a) in enumerate(zip(vals_e, vals_a)):
6✔
17
            test.assertAlmostEqual(val_e, val_a, msg=f"Differing values in {idx} of {len(expected)} element channel {ch}")
6✔
18

19

20
class SingleRampTest(TestCase):
6✔
21
    def setUp(self):
6✔
22
        hold = ConstantPT(10 ** 6, {'a': '-1. + idx * 0.01'})
6✔
23
        self.pulse_template = hold.with_iteration('idx', 200)
6✔
24

25
        self.program = LinSpaceIter(
6✔
26
            length=200,
27
            body=(LinSpaceHold(
28
                bases=(-1.,),
29
                factors=((0.01,),),
30
                duration_base=TimeType(10**6),
31
                duration_factors=None
32
            ),)
33
        )
34

35
        key = DepKey.from_voltages((0.01,), DEFAULT_INCREMENT_RESOLUTION)
6✔
36

37
        self.commands = [
6✔
38
            Set(0, -1.0, key),
39
            Wait(TimeType(10 ** 6)),
40
            LoopLabel(0, 199),
41
            Increment(0, 0.01, key),
42
            Wait(TimeType(10 ** 6)),
43
            LoopJmp(0)
44
        ]
45

46
        self.output = [
6✔
47
            (TimeType(10**6 * idx), [sum([-1.0] + [0.01] * idx)]) for idx in range(200)
48
        ]
49

50
    def test_program(self):
6✔
51
        program_builder = LinSpaceBuilder(('a',))
6✔
52
        program = self.pulse_template.create_program(program_builder=program_builder)
6✔
53
        self.assertEqual([self.program], program)
6✔
54

55
    def test_commands(self):
6✔
56
        commands = to_increment_commands([self.program])
6✔
57
        self.assertEqual(self.commands, commands)
6✔
58

59
    def test_output(self):
6✔
60
        vm = LinSpaceVM(1)
6✔
61
        vm.set_commands(commands=self.commands)
6✔
62
        vm.run()
6✔
63
        assert_vm_output_almost_equal(self, self.output, vm.history)
6✔
64

65

66
class SequencedRepetitionTest():
6✔
67
    def setUp(self):
6✔
68
        
NEW
69
        base_time = 1e2
×
NEW
70
        rep_factor = 2
×
71
          
NEW
72
        wait = AtomicMultiChannelPT(
×
73
            ConstantPT(f'{base_time}', {'a': '-1. + idx_a * 0.01', }),
74
            ConstantPT(f'{base_time}', {'b': '-0.5 + idx_b * 0.05'})
75
            )
76
        
NEW
77
        dependent_constant = AtomicMultiChannelPT(
×
78
            ConstantPT(base_time, {'a': '-1.0 '}),
79
            ConstantPT(base_time, {'b': '-0.5 + idx_b*0.05',}),            
80
            )
81
        
NEW
82
        dependent_constant2 = AtomicMultiChannelPT(
×
83
            ConstantPT(base_time, {'a': '-0.5 '}),
84
            ConstantPT(base_time, {'b': '-0.3 + idx_b*0.05',}),            
85
            )
86
        
87
        #not working
88
        # self.pulse_template = (dependent_constant @ dependent_constant2.with_repetition(rep_factor) @ (wait.with_iteration('idx_a', rep_factor))).with_iteration('idx_b', rep_factor)\
89
        #not working
NEW
90
        self.pulse_template = (dependent_constant2.with_repetition(rep_factor) @ (wait.with_iteration('idx_a', rep_factor))).with_iteration('idx_b', rep_factor)\
×
91
        #working
92
        # self.pulse_template = (dependent_constant2.with_repetition(rep_factor)).with_iteration('idx_b', rep_factor)\
93
        
94
        
NEW
95
        self.program = []#TODO
×
96
        # self.program = LinSpaceIter(
97
        #     length=rep_factor,
98
        #     body=(LinSpaceRepeat(body=LinSpaceHold(
99
        #         bases=(-0.3,),
100
        #         factors=((0.05,),),
101
        #         duration_base=TimeType(base_time),
102
        #         duration_factors=None
103
        #     ),count=rep_factor),)
104
        # )
105

NEW
106
        key = DepKey.from_voltages((0.05,), DEFAULT_INCREMENT_RESOLUTION)
×
107

NEW
108
        self.commands = [
×
109
        ]#TODO
110
        
NEW
111
        self.output = [
×
112
        ]#TODO
113
        
114
        # self.output = [
115
        #     (TimeType(base_time*r + base_time*rep_factor*idx), [sum([-0.3] + [0.05] * idx)]) for idx in range(rep_factor) for r in range(rep_factor)
116
        # ]
117

118
    def test_program(self):
6✔
NEW
119
        program_builder = LinSpaceBuilder(('a','b'))
×
NEW
120
        program = self.pulse_template.create_program(program_builder=program_builder)
×
NEW
121
        self.assertEqual([self.program], program)
×
122

123
    def test_commands(self):
6✔
NEW
124
        commands = to_increment_commands([self.program])
×
NEW
125
        self.assertEqual(self.commands, commands)
×
126

127
    def test_output(self):
6✔
NEW
128
        vm = LinSpaceVM(2)
×
NEW
129
        vm.set_commands(commands=self.commands)
×
NEW
130
        vm.run()
×
NEW
131
        assert_vm_output_almost_equal(self, self.output, vm.history)
×
132

133
        
134

135
class PlainCSDTest(TestCase):
6✔
136
    def setUp(self):
6✔
137
        hold = ConstantPT(10**6, {'a': '-1. + idx_a * 0.01', 'b': '-.5 + idx_b * 0.02'})
6✔
138
        scan_a = hold.with_iteration('idx_a', 200)
6✔
139
        self.pulse_template = scan_a.with_iteration('idx_b', 100)
6✔
140

141
        self.program = LinSpaceIter(length=100, body=(LinSpaceIter(
6✔
142
            length=200,
143
            body=(LinSpaceHold(
144
                bases=(-1., -0.5),
145
                factors=((0.0, 0.01),
146
                         (0.02, 0.0)),
147
                duration_base=TimeType(10**6),
148
                duration_factors=None
149
            ),)
150
        ),))
151

152
        key_0 = DepKey.from_voltages((0, 0.01,), DEFAULT_INCREMENT_RESOLUTION)
6✔
153
        key_1 = DepKey.from_voltages((0.02,), DEFAULT_INCREMENT_RESOLUTION)
6✔
154

155
        self.commands = [
6✔
156
            Set(0, -1.0, key_0),
157
            Set(1, -0.5, key_1),
158
            Wait(TimeType(10 ** 6)),
159

160
            LoopLabel(0, 199),
161
            Increment(0, 0.01, key_0),
162
            Wait(TimeType(10 ** 6)),
163
            LoopJmp(0),
164

165
            LoopLabel(1, 99),
166

167
            Increment(0, -1.99, key_0),
168
            Increment(1, 0.02, key_1),
169
            Wait(TimeType(10 ** 6)),
170

171
            LoopLabel(2, 199),
172
            Increment(0, 0.01, key_0),
173
            Wait(TimeType(10 ** 6)),
174
            LoopJmp(2),
175

176
            LoopJmp(1),
177
        ]
178

179
        a_values = [sum([-1.] + [0.01] * i) for i in range(200)]
6✔
180
        b_values = [sum([-.5] + [0.02] * j) for j in range(100)]
6✔
181

182
        self.output = [
6✔
183
            (
184
                TimeType(10 ** 6 * (i + 200 * j)),
185
                [a_values[i], b_values[j]]
186
            ) for j in range(100) for i in range(200)
187
        ]
188

189
    def test_program(self):
6✔
190
        program_builder = LinSpaceBuilder(('a', 'b'))
6✔
191
        program = self.pulse_template.create_program(program_builder=program_builder)
6✔
192
        self.assertEqual([self.program], program)
6✔
193

194
    def test_increment_commands(self):
6✔
195
        commands = to_increment_commands([self.program])
6✔
196
        self.assertEqual(self.commands, commands)
6✔
197

198
    def test_output(self):
6✔
199
        vm = LinSpaceVM(2)
6✔
200
        vm.set_commands(self.commands)
6✔
201
        vm.run()
6✔
202
        assert_vm_output_almost_equal(self, self.output, vm.history)
6✔
203

204

205
class TiltedCSDTest(TestCase):
6✔
206
    def setUp(self):
6✔
207
        repetition_count = 3
6✔
208
        hold = ConstantPT(10**6, {'a': '-1. + idx_a * 0.01 + idx_b * 1e-3', 'b': '-.5 + idx_b * 0.02 - 3e-3 * idx_a'})
6✔
209
        scan_a = hold.with_iteration('idx_a', 200)
6✔
210
        self.pulse_template = scan_a.with_iteration('idx_b', 100)
6✔
211
        self.repeated_pt = self.pulse_template.with_repetition(repetition_count)
6✔
212

213
        self.program = LinSpaceIter(length=100, body=(LinSpaceIter(
6✔
214
            length=200,
215
            body=(LinSpaceHold(
216
                bases=(-1., -0.5),
217
                factors=((1e-3, 0.01),
218
                         (0.02, -3e-3)),
219
                duration_base=TimeType(10**6),
220
                duration_factors=None
221
            ),)
222
        ),))
223
        self.repeated_program = LinSpaceRepeat(body=(self.program,), count=repetition_count)
6✔
224

225
        key_0 = DepKey.from_voltages((1e-3, 0.01,), DEFAULT_INCREMENT_RESOLUTION)
6✔
226
        key_1 = DepKey.from_voltages((0.02, -3e-3), DEFAULT_INCREMENT_RESOLUTION)
6✔
227

228
        self.commands = [
6✔
229
            Set(0, -1.0, key_0),
230
            Set(1, -0.5, key_1),
231
            Wait(TimeType(10 ** 6)),
232

233
            LoopLabel(0, 199),
234
            Increment(0, 0.01, key_0),
235
            Increment(1, -3e-3, key_1),
236
            Wait(TimeType(10 ** 6)),
237
            LoopJmp(0),
238

239
            LoopLabel(1, 99),
240

241
            Increment(0, 1e-3 + -199 * 1e-2, key_0),
242
            Increment(1, 0.02 + -199 * -3e-3, key_1),
243
            Wait(TimeType(10 ** 6)),
244

245
            LoopLabel(2, 199),
246
            Increment(0, 0.01, key_0),
247
            Increment(1, -3e-3, key_1),
248
            Wait(TimeType(10 ** 6)),
249
            LoopJmp(2),
250

251
            LoopJmp(1),
252
        ]
253
        inner_commands = copy.deepcopy(self.commands)
6✔
254
        for cmd in inner_commands:
6✔
255
            if hasattr(cmd, 'idx'):
6✔
256
                cmd.idx += 1
6✔
257
        self.repeated_commands = [LoopLabel(0, repetition_count)] + inner_commands + [LoopJmp(0)]
6✔
258

259
        self.output = [
6✔
260
            (
261
                TimeType(10 ** 6 * (i + 200 * j)),
262
                [-1. + i * 0.01 + j * 1e-3, -.5 + j * 0.02 - 3e-3 * i]
263
            ) for j in range(100) for i in range(200)
264
        ]
265
        self.repeated_output = [
6✔
266
            (t + TimeType(10**6) * (n * 100 * 200), vals)
267
            for n in range(repetition_count)
268
            for t, vals in self.output
269
        ]
270

271
    def test_program(self):
6✔
272
        program_builder = LinSpaceBuilder(('a', 'b'))
6✔
273
        program = self.pulse_template.create_program(program_builder=program_builder)
6✔
274
        self.assertEqual([self.program], program)
6✔
275

276
    def test_repeated_program(self):
6✔
277
        program_builder = LinSpaceBuilder(('a', 'b'))
6✔
278
        program = self.repeated_pt.create_program(program_builder=program_builder)
6✔
279
        self.assertEqual([self.repeated_program], program)
6✔
280

281
    def test_increment_commands(self):
6✔
282
        commands = to_increment_commands([self.program])
6✔
283
        self.assertEqual(self.commands, commands)
6✔
284

285
    def test_repeated_increment_commands(self):
6✔
286
        commands = to_increment_commands([self.repeated_program])
6✔
287
        self.assertEqual(self.repeated_commands, commands)
6✔
288

289
    def test_output(self):
6✔
290
        vm = LinSpaceVM(2)
6✔
291
        vm.set_commands(self.commands)
6✔
292
        vm.run()
6✔
293
        assert_vm_output_almost_equal(self, self.output, vm.history)
6✔
294

295
    def test_repeated_output(self):
6✔
296
        vm = LinSpaceVM(2)
6✔
297
        vm.set_commands(self.repeated_commands)
6✔
298
        vm.run()
6✔
299
        assert_vm_output_almost_equal(self, self.repeated_output, vm.history)
6✔
300

301

302
class SingletLoadProcessing(TestCase):
6✔
303
    def setUp(self):
6✔
304
        wait = ConstantPT(10 ** 6, {'a': '-1. + idx_a * 0.01', 'b': '-.5 + idx_b * 0.02'})
6✔
305
        load_random = ConstantPT(10 ** 5, {'a': -.4, 'b': -.3})
6✔
306
        meas = ConstantPT(10 ** 5, {'a': 0.05, 'b': 0.06})
6✔
307

308
        singlet_scan = (load_random @ wait @ meas).with_iteration('idx_a', 200).with_iteration('idx_b', 100)
6✔
309
        self.pulse_template = singlet_scan
6✔
310

311
        self.program = LinSpaceIter(length=100, body=(LinSpaceIter(
6✔
312
            length=200,
313
            body=(
314
                LinSpaceHold(bases=(-0.4, -0.3), factors=(None, None), duration_base=TimeType(10 ** 5),
315
                             duration_factors=None),
316
                LinSpaceHold(bases=(-1., -0.5),
317
                             factors=((0.0, 0.01),
318
                                      (0.02, 0.0)),
319
                             duration_base=TimeType(10 ** 6),
320
                             duration_factors=None),
321
                LinSpaceHold(bases=(0.05, 0.06), factors=(None, None), duration_base=TimeType(10 ** 5),
322
                             duration_factors=None),
323
            )
324
        ),))
325

326
        key_0 = DepKey.from_voltages((0, 0.01,), DEFAULT_INCREMENT_RESOLUTION)
6✔
327
        key_1 = DepKey.from_voltages((0.02,), DEFAULT_INCREMENT_RESOLUTION)
6✔
328

329
        self.commands = [
6✔
330
            Set(0, -0.4),
331
            Set(1, -0.3),
332
            Wait(TimeType(10 ** 5)),
333
            Set(0, -1.0, key_0),
334
            Set(1, -0.5, key_1),
335
            Wait(TimeType(10 ** 6)),
336
            Set(0, 0.05),
337
            Set(1, 0.06),
338
            Wait(TimeType(10 ** 5)),
339

340
            LoopLabel(0, 199),
341
            Set(0, -0.4),
342
            Set(1, -0.3),
343
            Wait(TimeType(10 ** 5)),
344
            Increment(0, 0.01, key_0),
345
            Increment(1, 0.00, key_1),
346
            Wait(TimeType(10 ** 6)),
347
            Set(0, 0.05),
348
            Set(1, 0.06),
349
            Wait(TimeType(10 ** 5)),
350
            LoopJmp(0),
351

352
            LoopLabel(1, 99),
353

354
            Set(0, -0.4),
355
            Set(1, -0.3),
356
            Wait(TimeType(10 ** 5)),
357
            Increment(0, -1.99, key_0),
358
            Increment(1, 0.02, key_1),
359
            Wait(TimeType(10 ** 6)),
360
            Set(0, 0.05),
361
            Set(1, 0.06),
362
            Wait(TimeType(10 ** 5)),
363

364
            LoopLabel(2, 199),
365

366
            Set(0, -0.4),
367
            Set(1, -0.3),
368
            Wait(TimeType(10 ** 5)),
369
            Increment(0, 0.01, key_0),
370
            Increment(1, 0.00, key_1),
371
            Wait(TimeType(10 ** 6)),
372
            Set(0, 0.05),
373
            Set(1, 0.06),
374
            Wait(TimeType(10 ** 5)),
375

376
            LoopJmp(2),
377

378
            LoopJmp(1),
379
        ]
380

381
        self.output = []
6✔
382
        time = TimeType(0)
6✔
383
        for idx_b in range(100):
6✔
384
            for idx_a in range(200):
6✔
385
                self.output.append(
6✔
386
                    (time, [-.4, -.3])
387
                )
388
                time += 10 ** 5
6✔
389
                self.output.append(
6✔
390
                    (time, [-1. + idx_a * 0.01, -.5 + idx_b * 0.02])
391
                )
392
                time += 10 ** 6
6✔
393
                self.output.append(
6✔
394
                    (time, [0.05, 0.06])
395
                )
396
                time += 10 ** 5
6✔
397

398
    def test_singlet_scan_program(self):
6✔
399
        program_builder = LinSpaceBuilder(('a', 'b'))
6✔
400
        program = self.pulse_template.create_program(program_builder=program_builder)
6✔
401
        self.assertEqual([self.program], program)
6✔
402

403
    def test_singlet_scan_commands(self):
6✔
404
        commands = to_increment_commands([self.program])
6✔
405
        self.assertEqual(self.commands, commands)
6✔
406

407
    def test_singlet_scan_output(self):
6✔
408
        vm = LinSpaceVM(2)
6✔
409
        vm.set_commands(self.commands)
6✔
410
        vm.run()
6✔
411
        assert_vm_output_almost_equal(self, self.output, vm.history)
6✔
412

413

414
class TransformedRampTest(TestCase):
6✔
415
    def setUp(self):
6✔
416
        hold = ConstantPT(10 ** 6, {'a': '-1. + idx * 0.01'})
6✔
417
        self.pulse_template = hold.with_iteration('idx', 200)
6✔
418
        self.transformation = ScalingTransformation({'a': 2.0})
6✔
419

420
        self.program = LinSpaceIter(
6✔
421
            length=200,
422
            body=(LinSpaceHold(
423
                bases=(-2.,),
424
                factors=((0.02,),),
425
                duration_base=TimeType(10 ** 6),
426
                duration_factors=None
427
            ),)
428
        )
429

430
    def test_global_trafo_program(self):
6✔
431
        program_builder = LinSpaceBuilder(('a',))
6✔
432
        program = self.pulse_template.create_program(program_builder=program_builder,
6✔
433
                                                     global_transformation=self.transformation)
434
        self.assertEqual([self.program], program)
6✔
435

436
    def test_local_trafo_program(self):
6✔
437
        program_builder = LinSpaceBuilder(('a',))
6✔
438
        with self.assertRaises(NotImplementedError):
6✔
439
            # not implemented yet. This test should work as soon as its implemented
440
            program = self.pulse_template.create_program(program_builder=program_builder,
6✔
441
                                                         global_transformation=self.transformation,
442
                                                         to_single_waveform={self.pulse_template})
443
            self.assertEqual([self.program], program)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc