forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_expr_constructors.py
650 lines (592 loc) · 25.2 KB
/
test_expr_constructors.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
import ddt
from qiskit.circuit import Clbit, ClassicalRegister, Duration
from qiskit.circuit.classical import expr, types
from test import QiskitTestCase # pylint: disable=wrong-import-order
@ddt.ddt
class TestExprConstructors(QiskitTestCase):
def test_lift_legacy_condition(self):
cr = ClassicalRegister(3, "c")
clbit = Clbit()
cond = (cr, 7)
self.assertEqual(
expr.lift_legacy_condition(cond),
expr.Binary(
expr.Binary.Op.EQUAL,
expr.Var(cr, types.Uint(cr.size)),
expr.Value(7, types.Uint(cr.size)),
types.Bool(),
),
)
cond = (cr, 255)
self.assertEqual(
expr.lift_legacy_condition(cond),
expr.Binary(
expr.Binary.Op.EQUAL,
expr.Cast(expr.Var(cr, types.Uint(cr.size)), types.Uint(8), implicit=True),
expr.Value(255, types.Uint(8)),
types.Bool(),
),
)
cond = (clbit, False)
self.assertEqual(
expr.lift_legacy_condition(cond),
expr.Unary(
expr.Unary.Op.LOGIC_NOT,
expr.Var(clbit, types.Bool()),
types.Bool(),
),
)
cond = (clbit, True)
self.assertEqual(
expr.lift_legacy_condition(cond),
expr.Var(clbit, types.Bool()),
)
def test_value_lifts_qiskit_scalars(self):
cr = ClassicalRegister(3, "c")
self.assertEqual(expr.lift(cr), expr.Var(cr, types.Uint(cr.size)))
clbit = Clbit()
self.assertEqual(expr.lift(clbit), expr.Var(clbit, types.Bool()))
duration = Duration.dt(1000)
self.assertEqual(expr.lift(duration), expr.Value(duration, types.Duration()))
self.assertEqual(
expr.lift(duration, types.Stretch()), expr.Value(duration, types.Stretch())
)
def test_value_lifts_python_builtins(self):
self.assertEqual(expr.lift(True), expr.Value(True, types.Bool()))
self.assertEqual(expr.lift(False), expr.Value(False, types.Bool()))
self.assertEqual(expr.lift(7), expr.Value(7, types.Uint(3)))
self.assertEqual(expr.lift(7.0), expr.Value(7.0, types.Float()))
def test_value_ensures_nonzero_width(self):
self.assertEqual(expr.lift(0), expr.Value(0, types.Uint(1)))
def test_value_type_representation(self):
self.assertEqual(expr.lift(5), expr.Value(5, types.Uint((5).bit_length())))
self.assertEqual(expr.lift(5, types.Uint(8)), expr.Value(5, types.Uint(8)))
cr = ClassicalRegister(3, "c")
self.assertEqual(expr.lift(cr, types.Uint(8)), expr.Var(cr, types.Uint(8)))
def test_value_does_not_allow_downcast(self):
with self.assertRaisesRegex(TypeError, "the explicit type .* is not suitable"):
expr.lift(0xFF, types.Uint(2))
with self.assertRaisesRegex(TypeError, "the explicit type .* is not suitable"):
expr.lift(1.1, types.Uint(2))
def test_value_rejects_bad_values(self):
with self.assertRaisesRegex(TypeError, "failed to infer a type"):
expr.lift("1")
with self.assertRaisesRegex(ValueError, "cannot represent a negative value"):
expr.lift(-1)
def test_cast_adds_explicit_nodes(self):
"""A specific request to add a cast in means that we should respect that in the type tree,
even if the cast is a no-op."""
base = expr.Value(5, types.Uint(8))
self.assertEqual(
expr.cast(base, types.Uint(8)), expr.Cast(base, types.Uint(8), implicit=False)
)
def test_cast_allows_lossy_downcasting(self):
"""An explicit 'cast' call should allow lossy casts to be performed."""
base = expr.Value(5, types.Uint(16))
self.assertEqual(
expr.cast(base, types.Uint(8)), expr.Cast(base, types.Uint(8), implicit=False)
)
self.assertEqual(
expr.cast(base, types.Bool()), expr.Cast(base, types.Bool(), implicit=False)
)
self.assertEqual(
expr.cast(base, types.Float()), expr.Cast(base, types.Float(), implicit=False)
)
@ddt.data(
(expr.bit_not, ClassicalRegister(3)),
(expr.logic_not, ClassicalRegister(3)),
(expr.logic_not, False),
(expr.logic_not, Clbit()),
)
@ddt.unpack
def test_unary_functions_lift_scalars(self, function, scalar):
self.assertEqual(function(scalar), function(expr.lift(scalar)))
def test_bit_not_explicit(self):
cr = ClassicalRegister(3)
self.assertEqual(
expr.bit_not(cr),
expr.Unary(
expr.Unary.Op.BIT_NOT, expr.Var(cr, types.Uint(cr.size)), types.Uint(cr.size)
),
)
clbit = Clbit()
self.assertEqual(
expr.bit_not(clbit),
expr.Unary(expr.Unary.Op.BIT_NOT, expr.Var(clbit, types.Bool()), types.Bool()),
)
@ddt.data(expr.bit_not)
def test_urnary_bitwise_forbidden(self, function):
with self.assertRaisesRegex(TypeError, "cannot apply"):
function(7.0)
with self.assertRaisesRegex(TypeError, "cannot apply"):
function(Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "cannot apply"):
function(expr.Var.new("a", types.Stretch()))
def test_logic_not_explicit(self):
cr = ClassicalRegister(3)
self.assertEqual(
expr.logic_not(cr),
expr.Unary(
expr.Unary.Op.LOGIC_NOT,
expr.Cast(expr.Var(cr, types.Uint(cr.size)), types.Bool(), implicit=True),
types.Bool(),
),
)
clbit = Clbit()
self.assertEqual(
expr.logic_not(clbit),
expr.Unary(expr.Unary.Op.LOGIC_NOT, expr.Var(clbit, types.Bool()), types.Bool()),
)
@ddt.data(expr.logic_not)
def test_urnary_logical_forbidden(self, function):
with self.assertRaisesRegex(TypeError, "cannot apply"):
function(7.0)
with self.assertRaisesRegex(TypeError, "cannot apply"):
function(Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "cannot apply"):
function(expr.Var.new("a", types.Stretch()))
@ddt.data(
(expr.bit_and, ClassicalRegister(3), ClassicalRegister(3)),
(expr.bit_or, ClassicalRegister(3), ClassicalRegister(3)),
(expr.bit_xor, ClassicalRegister(3), ClassicalRegister(3)),
(expr.logic_and, Clbit(), True),
(expr.logic_or, False, ClassicalRegister(3)),
(expr.equal, ClassicalRegister(8), 255),
(expr.not_equal, ClassicalRegister(8), 255),
(expr.less, ClassicalRegister(3), 6),
(expr.less_equal, ClassicalRegister(3), 5),
(expr.greater, 4, ClassicalRegister(3)),
(expr.greater_equal, ClassicalRegister(3), 5),
(expr.equal, 8.0, 255.0),
(expr.not_equal, 8.0, 255.0),
(expr.less, 3.0, 6.0),
(expr.less_equal, 3.0, 5.0),
(expr.greater, 4.0, 3.0),
(expr.greater_equal, 3.0, 5.0),
(expr.equal, Duration.dt(1000), Duration.dt(1000)),
(expr.not_equal, Duration.dt(1000), Duration.dt(1000)),
(expr.less, Duration.dt(1000), Duration.dt(1000)),
(expr.less_equal, Duration.dt(1000), Duration.dt(1000)),
(expr.greater, Duration.dt(1000), Duration.dt(1000)),
(expr.greater_equal, Duration.dt(1000), Duration.dt(1000)),
)
@ddt.unpack
def test_binary_functions_lift_scalars(self, function, left, right):
self.assertEqual(function(left, right), function(expr.lift(left), right))
self.assertEqual(function(left, right), function(left, expr.lift(right)))
self.assertEqual(function(left, right), function(expr.lift(left), expr.lift(right)))
@ddt.data(
(expr.bit_and, expr.Binary.Op.BIT_AND),
(expr.bit_or, expr.Binary.Op.BIT_OR),
(expr.bit_xor, expr.Binary.Op.BIT_XOR),
)
@ddt.unpack
def test_binary_bitwise_explicit(self, function, opcode):
cr = ClassicalRegister(8, "c")
self.assertEqual(
function(cr, 255),
expr.Binary(
opcode, expr.Var(cr, types.Uint(8)), expr.Value(255, types.Uint(8)), types.Uint(8)
),
)
self.assertFalse(function(cr, 255).const)
self.assertEqual(
function(255, cr),
expr.Binary(
opcode, expr.Value(255, types.Uint(8)), expr.Var(cr, types.Uint(8)), types.Uint(8)
),
)
self.assertFalse(function(255, cr).const)
clbit = Clbit()
self.assertEqual(
function(True, clbit),
expr.Binary(
opcode,
expr.Value(True, types.Bool()),
expr.Var(clbit, types.Bool()),
types.Bool(),
),
)
self.assertFalse(function(True, clbit).const)
self.assertEqual(
function(clbit, False),
expr.Binary(
opcode,
expr.Var(clbit, types.Bool()),
expr.Value(False, types.Bool()),
types.Bool(),
),
)
self.assertFalse(function(clbit, True).const)
self.assertEqual(
function(255, 255),
expr.Binary(
opcode,
expr.Value(255, types.Uint(8)),
expr.Value(255, types.Uint(8)),
types.Uint(8),
),
)
self.assertTrue(function(255, 255).const)
@ddt.data(
(expr.bit_and, expr.Binary.Op.BIT_AND),
(expr.bit_or, expr.Binary.Op.BIT_OR),
(expr.bit_xor, expr.Binary.Op.BIT_XOR),
)
@ddt.unpack
def test_binary_bitwise_uint_inference(self, function, opcode):
"""The binary bitwise functions have specialized inference for the widths of integer
literals, since the bitwise functions require the operands to already be of exactly the same
width without promotion."""
cr = ClassicalRegister(8, "c")
self.assertEqual(
function(cr, 5),
expr.Binary(
opcode,
expr.Var(cr, types.Uint(8)),
expr.Value(5, types.Uint(8)), # Note the inference should be Uint(8) not Uint(3).
types.Uint(8),
),
)
self.assertEqual(
function(5, cr),
expr.Binary(
opcode,
expr.Value(5, types.Uint(8)),
expr.Var(cr, types.Uint(8)),
types.Uint(8),
),
)
# Inference between two integer literals is "best effort". This behavior isn't super
# important to maintain if we want to change the expression system.
self.assertEqual(
function(5, 255),
expr.Binary(
opcode,
expr.Value(5, types.Uint(8)),
expr.Value(255, types.Uint(8)),
types.Uint(8),
),
)
@ddt.data(expr.bit_and, expr.bit_or, expr.bit_xor)
def test_binary_bitwise_forbidden(self, function):
with self.assertRaisesRegex(TypeError, "invalid types"):
function(ClassicalRegister(3, "c"), Clbit())
with self.assertRaisesRegex(TypeError, "invalid types"):
function(3.0, 3.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(3, 3.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(3.0, 3)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), expr.Var.new("a", types.Stretch()))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), expr.Var.new("b", types.Stretch()))
# Unlike most other functions, the bitwise functions should error if the two bit-like types
# aren't of the same width, except for the special inference for integer literals.
with self.assertRaisesRegex(TypeError, "binary bitwise operations .* same width"):
function(ClassicalRegister(3, "a"), ClassicalRegister(5, "b"))
@ddt.data(
(expr.logic_and, expr.Binary.Op.LOGIC_AND),
(expr.logic_or, expr.Binary.Op.LOGIC_OR),
)
@ddt.unpack
def test_binary_logical_explicit(self, function, opcode):
cr = ClassicalRegister(8, "c")
clbit = Clbit()
self.assertEqual(
function(cr, clbit),
expr.Binary(
opcode,
expr.Cast(expr.Var(cr, types.Uint(cr.size)), types.Bool(), implicit=True),
expr.Var(clbit, types.Bool()),
types.Bool(),
),
)
self.assertFalse(function(cr, clbit).const)
self.assertEqual(
function(cr, 3),
expr.Binary(
opcode,
expr.Cast(expr.Var(cr, types.Uint(cr.size)), types.Bool(), implicit=True),
expr.Cast(expr.Value(3, types.Uint(2)), types.Bool(), implicit=True),
types.Bool(),
),
)
self.assertFalse(function(cr, 3).const)
self.assertEqual(
function(False, clbit),
expr.Binary(
opcode,
expr.Value(False, types.Bool()),
expr.Var(clbit, types.Bool()),
types.Bool(),
),
)
self.assertFalse(function(False, clbit).const)
@ddt.data(expr.logic_and, expr.logic_or)
def test_binary_logic_forbidden(self, function):
with self.assertRaisesRegex(TypeError, "invalid types"):
function(3.0, 3.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(3, 3.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(3.0, 3)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), expr.Var.new("a", types.Stretch()))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), expr.Var.new("b", types.Stretch()))
@ddt.data(
(expr.equal, expr.Binary.Op.EQUAL),
(expr.not_equal, expr.Binary.Op.NOT_EQUAL),
)
@ddt.unpack
def test_binary_equal_explicit(self, function, opcode):
cr = ClassicalRegister(8, "c")
clbit = Clbit()
self.assertEqual(
function(cr, 255),
expr.Binary(
opcode, expr.Var(cr, types.Uint(8)), expr.Value(255, types.Uint(8)), types.Bool()
),
)
self.assertFalse(function(cr, 255).const)
self.assertEqual(
function(7, cr),
expr.Binary(
opcode,
expr.Value(7, types.Uint(8)),
expr.Var(cr, types.Uint(8)),
types.Bool(),
),
)
self.assertFalse(function(7, cr).const)
self.assertEqual(
function(clbit, True),
expr.Binary(
opcode,
expr.Var(clbit, types.Bool()),
expr.Value(True, types.Bool()),
types.Bool(),
),
)
self.assertFalse(function(clbit, True).const)
self.assertEqual(
function(expr.lift(7.0), 7.0),
expr.Binary(
opcode,
expr.Value(7.0, types.Float()),
expr.Value(7.0, types.Float()),
types.Bool(),
),
)
self.assertEqual(
function(expr.lift(Duration.ms(1000)), Duration.s(1)),
expr.Binary(
opcode,
expr.Value(Duration.ms(1000), types.Duration()),
expr.Value(Duration.s(1), types.Duration()),
types.Bool(),
),
)
self.assertTrue(function(expr.lift(Duration.ms(1000)), Duration.s(1)).const)
@ddt.data(expr.equal, expr.not_equal)
def test_binary_equal_forbidden(self, function):
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Clbit(), ClassicalRegister(3, "c"))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(ClassicalRegister(3, "c"), False)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(5, True)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(True, 5.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(5, 5.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(ClassicalRegister(3, "c"), 5.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), expr.Var.new("a", types.Stretch()))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), expr.Var.new("b", types.Stretch()))
@ddt.data(
(expr.less, expr.Binary.Op.LESS),
(expr.less_equal, expr.Binary.Op.LESS_EQUAL),
(expr.greater, expr.Binary.Op.GREATER),
(expr.greater_equal, expr.Binary.Op.GREATER_EQUAL),
)
@ddt.unpack
def test_binary_relation_explicit(self, function, opcode):
cr = ClassicalRegister(8, "c")
self.assertEqual(
function(cr, 200),
expr.Binary(
opcode, expr.Var(cr, types.Uint(8)), expr.Value(200, types.Uint(8)), types.Bool()
),
)
self.assertFalse(function(cr, 200).const)
self.assertEqual(
function(12, cr),
expr.Binary(
opcode,
expr.Value(12, types.Uint(8)),
expr.Var(cr, types.Uint(8)),
types.Bool(),
),
)
self.assertFalse(function(12, cr).const)
self.assertEqual(
function(expr.lift(12.0, types.Float()), expr.lift(12.0)),
expr.Binary(
opcode,
expr.Value(12.0, types.Float()),
expr.Value(12.0, types.Float()),
types.Bool(),
),
)
self.assertTrue(function(expr.lift(12.0, types.Float()), expr.lift(12.0)).const)
self.assertEqual(
function(
expr.lift(Duration.ms(1000), types.Duration()),
expr.lift(Duration.s(1)),
),
expr.Binary(
opcode,
expr.Value(Duration.ms(1000), types.Duration()),
expr.Value(Duration.s(1), types.Duration()),
types.Bool(),
),
)
self.assertTrue(
function(
expr.lift(Duration.ms(1000), types.Duration()),
expr.lift(Duration.s(1)),
).const
)
@ddt.data(expr.less, expr.less_equal, expr.greater, expr.greater_equal)
def test_binary_relation_forbidden(self, function):
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Clbit(), ClassicalRegister(3, "c"))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(ClassicalRegister(3, "c"), False)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Clbit(), Clbit())
with self.assertRaisesRegex(TypeError, "invalid types"):
function(True, 5.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(5, 5.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(ClassicalRegister(3, "c"), 5.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), expr.Var.new("a", types.Stretch()))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), expr.Var.new("b", types.Stretch()))
def test_index_explicit(self):
cr = ClassicalRegister(4, "c")
a = expr.Var.new("a", types.Uint(8))
self.assertEqual(
expr.index(cr, 3),
expr.Index(expr.Var(cr, types.Uint(4)), expr.Value(3, types.Uint(2)), types.Bool()),
)
self.assertFalse(expr.index(cr, 3).const)
self.assertEqual(
expr.index(a, cr),
expr.Index(a, expr.Var(cr, types.Uint(4)), types.Bool()),
)
self.assertFalse(expr.index(a, cr).const)
self.assertEqual(
expr.index(255, 1),
expr.Index(expr.Value(255, types.Uint(8)), expr.Value(1, types.Uint(1)), types.Bool()),
)
self.assertTrue(expr.index(255, 1).const)
def test_index_forbidden(self):
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(Clbit(), 3)
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(ClassicalRegister(3, "a"), False)
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(ClassicalRegister(3, "a"), 1.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(0xFFFF, 1.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(Duration.dt(1000), 1)
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(Duration.dt(1000), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(expr.Var.new("a", types.Stretch()), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(Duration.dt(1000), expr.Var.new("a", types.Stretch()))
with self.assertRaisesRegex(TypeError, "invalid types"):
expr.index(expr.Var.new("a", types.Stretch()), expr.Var.new("b", types.Stretch()))
@ddt.data(
(expr.shift_left, expr.Binary.Op.SHIFT_LEFT),
(expr.shift_right, expr.Binary.Op.SHIFT_RIGHT),
)
@ddt.unpack
def test_shift_explicit(self, function, opcode):
cr = ClassicalRegister(8, "c")
a = expr.Var.new("a", types.Uint(4))
self.assertEqual(
function(cr, 5),
expr.Binary(
opcode, expr.Var(cr, types.Uint(8)), expr.Value(5, types.Uint(3)), types.Uint(8)
),
)
self.assertFalse(function(cr, 5).const)
self.assertEqual(
function(a, cr),
expr.Binary(opcode, a, expr.Var(cr, types.Uint(8)), types.Uint(4)),
)
self.assertFalse(function(a, cr).const)
self.assertEqual(
function(3, 5, types.Uint(8)),
expr.Binary(
opcode, expr.Value(3, types.Uint(8)), expr.Value(5, types.Uint(3)), types.Uint(8)
),
)
self.assertTrue(function(3, 5, types.Uint(8)).const)
@ddt.data(expr.shift_left, expr.shift_right)
def test_shift_forbidden(self, function):
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Clbit(), ClassicalRegister(3, "c"))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(ClassicalRegister(3, "c"), False)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Clbit(), Clbit())
with self.assertRaisesRegex(TypeError, "invalid types"):
function(0xFFFF, 2.0)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(255.0, 1)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), 1)
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), Duration.dt(1000))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(Duration.dt(1000), expr.Var.new("a", types.Stretch()))
with self.assertRaisesRegex(TypeError, "invalid types"):
function(expr.Var.new("a", types.Stretch()), expr.Var.new("b", types.Stretch()))