30
30
31
31
import oqpy
32
32
from oqpy import *
33
- from oqpy .base import OQPyBinaryExpression , OQPyExpression , expr_matches , logical_and , logical_or
33
+ from oqpy .base import (
34
+ OQPyBinaryExpression ,
35
+ OQPyExpression ,
36
+ expr_matches ,
37
+ logical_and ,
38
+ logical_or ,
39
+ )
34
40
from oqpy .classical_types import OQIndexExpression
35
41
from oqpy .quantum_types import PhysicalQubits
36
42
from oqpy .timing import OQDurationLiteral
@@ -227,19 +233,36 @@ def test_array_declaration():
227
233
b = ArrayVar (name = "b" , init_expression = [True , False ], dimensions = [2 ], base_type = BoolVar )
228
234
i = ArrayVar (name = "i" , init_expression = [0 , 1 , 2 , 3 , 4 ], dimensions = [5 ], base_type = IntVar )
229
235
i55 = ArrayVar (
230
- name = "i55" , init_expression = [0 , 1 , 2 , 3 , 4 ], dimensions = [5 ], base_type = IntVar [55 ]
236
+ name = "i55" ,
237
+ init_expression = [0 , 1 , 2 , 3 , 4 ],
238
+ dimensions = [5 ],
239
+ base_type = IntVar [55 ],
231
240
)
232
241
u = ArrayVar (name = "u" , init_expression = [0 , 1 , 2 , 3 , 4 ], dimensions = [5 ], base_type = UintVar )
233
242
x = ArrayVar (
234
- name = "x" , init_expression = [0e-9 , 1e-9 , 2e-9 ], dimensions = [3 ], base_type = DurationVar
243
+ name = "x" ,
244
+ init_expression = [0e-9 , 1e-9 , 2e-9 ],
245
+ dimensions = [3 ],
246
+ base_type = DurationVar ,
247
+ )
248
+ y = ArrayVar (
249
+ name = "y" ,
250
+ init_expression = [0.0 , 1.0 , 2.0 , 3.0 ],
251
+ dimensions = [4 ],
252
+ base_type = FloatVar ,
235
253
)
236
- y = ArrayVar (name = "y" , init_expression = [0.0 , 1.0 , 2.0 , 3.0 ], dimensions = [4 ], base_type = FloatVar )
237
254
ang = ArrayVar (
238
- name = "ang" , init_expression = [0.0 , 1.0 , 2.0 , 3.0 ], dimensions = [4 ], base_type = AngleVar
255
+ name = "ang" ,
256
+ init_expression = [0.0 , 1.0 , 2.0 , 3.0 ],
257
+ dimensions = [4 ],
258
+ base_type = AngleVar ,
239
259
)
240
260
comp = ArrayVar (name = "comp" , init_expression = [0 , 1 + 1j ], dimensions = [2 ], base_type = ComplexVar )
241
261
comp55 = ArrayVar (
242
- name = "comp55" , init_expression = [0 , 1 + 1j ], dimensions = [2 ], base_type = ComplexVar [float_ (55 )]
262
+ name = "comp55" ,
263
+ init_expression = [0 , 1 + 1j ],
264
+ dimensions = [2 ],
265
+ base_type = ComplexVar [float_ (55 )],
243
266
)
244
267
ang_partial = ArrayVar [AngleVar , 2 ](name = "ang_part" , init_expression = [oqpy .pi , oqpy .pi / 2 ])
245
268
simple = ArrayVar [FloatVar ](name = "no_init" , dimensions = [5 ])
@@ -253,7 +276,21 @@ def test_array_declaration():
253
276
base_type = DurationVar ,
254
277
)
255
278
256
- vars = [b , i , i55 , u , x , y , ang , comp , comp55 , ang_partial , simple , multidim , npinit ]
279
+ vars = [
280
+ b ,
281
+ i ,
282
+ i55 ,
283
+ u ,
284
+ x ,
285
+ y ,
286
+ ang ,
287
+ comp ,
288
+ comp55 ,
289
+ ang_partial ,
290
+ simple ,
291
+ multidim ,
292
+ npinit ,
293
+ ]
257
294
258
295
prog = oqpy .Program (version = None )
259
296
prog .declare (vars )
@@ -728,7 +765,10 @@ def test_for_in_var_types():
728
765
program = oqpy .Program ()
729
766
pyphases = [0 ] + [oqpy .pi / i for i in range (10 , 1 , - 2 )]
730
767
phases = ArrayVar (
731
- name = "phases" , dimensions = [len (pyphases )], init_expression = pyphases , base_type = AngleVar
768
+ name = "phases" ,
769
+ dimensions = [len (pyphases )],
770
+ init_expression = pyphases ,
771
+ base_type = AngleVar ,
732
772
)
733
773
734
774
with oqpy .ForIn (program , range (len (pyphases )), "idx" ) as idx :
@@ -1199,7 +1239,8 @@ def test_defcals():
1199
1239
).strip ()
1200
1240
assert (
1201
1241
dumps (
1202
- prog .defcals [(("$1" , "$2" ), "xy" , ("angle[32] theta" , "pi / 2" ))], indent = " "
1242
+ prog .defcals [(("$1" , "$2" ), "xy" , ("angle[32] theta" , "pi / 2" ))],
1243
+ indent = " " ,
1203
1244
).strip ()
1204
1245
== expect_defcal_xy_theta_pio2
1205
1246
)
@@ -1320,7 +1361,12 @@ def test_ramsey_example():
1320
1361
constant = declare_waveform_generator ("constant" , [("length" , duration ), ("iq" , complex128 )])
1321
1362
gaussian = declare_waveform_generator (
1322
1363
"gaussian" ,
1323
- [("length" , duration ), ("sigma" , duration ), ("amplitude" , float64 ), ("phase" , float64 )],
1364
+ [
1365
+ ("length" , duration ),
1366
+ ("sigma" , duration ),
1367
+ ("amplitude" , float64 ),
1368
+ ("phase" , float64 ),
1369
+ ],
1324
1370
)
1325
1371
tx_waveform = constant (2.4e-6 , 0.2 )
1326
1372
@@ -1442,7 +1488,12 @@ def test_rabi_example():
1442
1488
constant = declare_waveform_generator ("constant" , [("length" , duration ), ("iq" , complex128 )])
1443
1489
gaussian = declare_waveform_generator (
1444
1490
"gaussian" ,
1445
- [("length" , duration ), ("sigma" , duration ), ("amplitude" , float64 ), ("phase" , float64 )],
1491
+ [
1492
+ ("length" , duration ),
1493
+ ("sigma" , duration ),
1494
+ ("amplitude" , float64 ),
1495
+ ("phase" , float64 ),
1496
+ ],
1446
1497
)
1447
1498
1448
1499
zcu216_dac231_0 = PortVar ("zcu216_dac231_0" )
@@ -1665,6 +1716,7 @@ def _to_cached_oqpy_expression(self):
1665
1716
assert dur .count == 2
1666
1717
# This gets computed just once
1667
1718
assert frame .count == 1
1719
+ assert all (isinstance (v , ast .QASMNode ) for v in prog .expr_cache .values ())
1668
1720
1669
1721
1670
1722
def test_waveform_extern_arg_passing ():
@@ -1825,14 +1877,22 @@ def test_annotate():
1825
1877
prog = Program ()
1826
1878
gaussian = declare_waveform_generator (
1827
1879
"gaussian" ,
1828
- [("length" , duration ), ("sigma" , duration ), ("amplitude" , float64 ), ("phase" , float64 )],
1880
+ [
1881
+ ("length" , duration ),
1882
+ ("sigma" , duration ),
1883
+ ("amplitude" , float64 ),
1884
+ ("phase" , float64 ),
1885
+ ],
1829
1886
annotations = ["annotating_extern_decl" ],
1830
1887
)
1831
1888
1832
1889
some_port = PortVar ("some_port" , annotations = ["makeport" , ("some_keyword" , "some_command" )])
1833
1890
another_port = PortVar ("another_port" , annotations = ["makeport" ])
1834
1891
q0_transmon_xy_frame = FrameVar (
1835
- some_port , 3911851971.26885 , name = "q0_transmon_xy_frame" , annotations = ["makeframe" ]
1892
+ some_port ,
1893
+ 3911851971.26885 ,
1894
+ name = "q0_transmon_xy_frame" ,
1895
+ annotations = ["makeframe" ],
1836
1896
)
1837
1897
rabi_pulse_wf = WaveformVar (
1838
1898
gaussian (5.2e-8 , 1.3e-8 , 1.0 , 0.0 ), "rabi_pulse_wf" , annotations = ["makepulse" ]
@@ -2191,7 +2251,11 @@ def test_ramsey_example_blog():
2191
2251
)
2192
2252
gaussian_waveform = oqpy .declare_waveform_generator (
2193
2253
"gaussian" ,
2194
- [("length" , oqpy .duration ), ("sigma" , oqpy .duration ), ("amplitude" , oqpy .float64 )],
2254
+ [
2255
+ ("length" , oqpy .duration ),
2256
+ ("sigma" , oqpy .duration ),
2257
+ ("amplitude" , oqpy .float64 ),
2258
+ ],
2195
2259
)
2196
2260
2197
2261
with oqpy .defcal (defcals_prog , qubit , "reset" ):
@@ -2471,7 +2535,11 @@ def test_gate_declarations():
2471
2535
prog ,
2472
2536
q ,
2473
2537
"u" ,
2474
- [oqpy .AngleVar (name = "alpha" ), oqpy .AngleVar (name = "beta" ), oqpy .AngleVar (name = "gamma" )],
2538
+ [
2539
+ oqpy .AngleVar (name = "alpha" ),
2540
+ oqpy .AngleVar (name = "beta" ),
2541
+ oqpy .AngleVar (name = "gamma" ),
2542
+ ],
2475
2543
) as (alpha , beta , gamma ):
2476
2544
prog .gate (q , "a" , alpha )
2477
2545
prog .gate (q , "b" , beta )
@@ -2629,6 +2697,7 @@ def test_box_with_negative_duration():
2629
2697
def test_expr_matches_handles_outside_data ():
2630
2698
x1 = oqpy .FloatVar (3 , name = "x" )
2631
2699
x2 = oqpy .FloatVar (3 , name = "x" )
2700
+
2632
2701
class MyEntity :
2633
2702
def __init__ (self ):
2634
2703
self .self_ref = self
@@ -2643,15 +2712,15 @@ def __eq__(self, other):
2643
2712
class MyEntityNoEq :
2644
2713
def __init__ (self ):
2645
2714
self .self_ref = self
2715
+
2646
2716
def __eq__ (self , other ):
2647
2717
raise RuntimeError ("Eq not allowed" )
2648
2718
2649
2719
x1 ._entity = MyEntityNoEq ()
2650
2720
x2 ._entity = x1 ._entity
2651
2721
oqpy .base .expr_matches (x1 , x2 )
2652
2722
2653
- class MyFloatVar (oqpy .FloatVar ):
2654
- ...
2723
+ class MyFloatVar (oqpy .FloatVar ): ...
2655
2724
2656
2725
x1 = MyFloatVar (3 , name = "x" )
2657
2726
x2 = MyFloatVar (3 , name = "x" )
@@ -2660,6 +2729,7 @@ class MyFloatVar(oqpy.FloatVar):
2660
2729
2661
2730
class MyFloatVarWithIgnoredData (oqpy .FloatVar ):
2662
2731
ignored : int
2732
+
2663
2733
def _expr_matches (self , other ):
2664
2734
if not isinstance (other , type (self )):
2665
2735
return False
0 commit comments