20
20
21
21
import numpy as np
22
22
import pytest
23
+ from openpulse import ast
23
24
from openpulse .printer import dumps
24
25
25
26
import oqpy
26
27
from oqpy import *
27
- from oqpy .base import expr_matches , logical_and , logical_or
28
+ from oqpy .base import OQPyExpression , expr_matches , logical_and , logical_or
28
29
from oqpy .quantum_types import PhysicalQubits
29
30
from oqpy .timing import OQDurationLiteral
30
31
@@ -283,9 +284,11 @@ def test_binary_expressions():
283
284
i = IntVar (5 , "i" )
284
285
j = IntVar (2 , "j" )
285
286
k = IntVar (0 , "k" )
287
+ f = FloatVar (0.0 , "f" )
286
288
b1 = BoolVar (False , "b1" )
287
289
b2 = BoolVar (True , "b2" )
288
290
b3 = BoolVar (False , "b3" )
291
+ d = DurationVar (5e-9 , "d" )
289
292
prog .set (i , 2 * (i + j ))
290
293
prog .set (j , 2 % (2 - i ) % 2 )
291
294
prog .set (j , 1 + oqpy .pi )
@@ -310,6 +313,8 @@ def test_binary_expressions():
310
313
prog .set (b1 , logical_or (b2 , b3 ))
311
314
prog .set (b1 , logical_and (b2 , True ))
312
315
prog .set (b1 , logical_or (False , b3 ))
316
+ prog .set (d , d + make_duration (10e-9 ))
317
+ prog .set (f , d / make_duration (1 ))
313
318
314
319
expected = textwrap .dedent (
315
320
"""
@@ -320,6 +325,8 @@ def test_binary_expressions():
320
325
bool b1 = false;
321
326
bool b2 = true;
322
327
bool b3 = false;
328
+ duration d = 5.0ns;
329
+ float[64] f = 0.0;
323
330
i = 2 * (i + j);
324
331
j = 2 % (2 - i) % 2;
325
332
j = 1 + pi;
@@ -344,6 +351,8 @@ def test_binary_expressions():
344
351
b1 = b2 || b3;
345
352
b1 = b2 && true;
346
353
b1 = false || b3;
354
+ d = d + 10.0ns;
355
+ f = d / 1000000000.0ns;
347
356
"""
348
357
).strip ()
349
358
@@ -1552,6 +1561,35 @@ def test_program_tracks_frame_waveform_vars():
1552
1561
assert expr_matches (list (prog .waveform_vars ), [constant_wf , discrete_wf ])
1553
1562
1554
1563
1564
+ def test_duration_literal_arithmetic ():
1565
+ # Test that duration literals can be used as a part of expression.
1566
+ port = oqpy .PortVar ("myport" )
1567
+ frame = oqpy .FrameVar (port , 1e9 , name = "myframe" )
1568
+ delay_time = oqpy .make_duration (50e-9 ) # 50 ns
1569
+ one_second = oqpy .make_duration (1 ) # 1 second
1570
+ delay_repetition = 10
1571
+
1572
+ program = oqpy .Program ()
1573
+ repeated_delay = delay_repetition * delay_time
1574
+ assert isinstance (repeated_delay , OQPyExpression )
1575
+ assert repeated_delay .type == ast .DurationType
1576
+
1577
+ program .delay (repeated_delay , frame )
1578
+ program .shift_phase (frame , 2 * oqpy .pi * (delay_time / one_second ))
1579
+
1580
+ expected = textwrap .dedent (
1581
+ """
1582
+ OPENQASM 3.0;
1583
+ port myport;
1584
+ frame myframe = newframe(myport, 1000000000.0, 0);
1585
+ delay[10 * 50.0ns] myframe;
1586
+ shift_phase(myframe, 2 * pi * (50.0ns / 1000000000.0ns));
1587
+ """
1588
+ ).strip ()
1589
+
1590
+ assert program .to_qasm () == expected
1591
+
1592
+
1555
1593
def test_make_duration ():
1556
1594
assert expr_matches (make_duration (1e-3 ), OQDurationLiteral (1e-3 ))
1557
1595
assert expr_matches (make_duration (OQDurationLiteral (1e-4 )), OQDurationLiteral (1e-4 ))
0 commit comments