Skip to content

Commit 1266bc4

Browse files
authored
Fix expressions with duration literals (#38)
* Add type for duration literals * Add tests * Test type of expression * Review
1 parent e9e034b commit 1266bc4

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

oqpy/timing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class OQDurationLiteral(OQPyExpression):
6363
def __init__(self, duration: float) -> None:
6464
super().__init__()
6565
self.duration = duration
66+
self.type = ast.DurationType
6667

6768
def to_ast(self, program: Program) -> ast.DurationLiteral:
6869
# Todo (#53): make better units?

tests/test_directives.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020

2121
import numpy as np
2222
import pytest
23+
from openpulse import ast
2324
from openpulse.printer import dumps
2425

2526
import oqpy
2627
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
2829
from oqpy.quantum_types import PhysicalQubits
2930
from oqpy.timing import OQDurationLiteral
3031

@@ -283,9 +284,11 @@ def test_binary_expressions():
283284
i = IntVar(5, "i")
284285
j = IntVar(2, "j")
285286
k = IntVar(0, "k")
287+
f = FloatVar(0.0, "f")
286288
b1 = BoolVar(False, "b1")
287289
b2 = BoolVar(True, "b2")
288290
b3 = BoolVar(False, "b3")
291+
d = DurationVar(5e-9, "d")
289292
prog.set(i, 2 * (i + j))
290293
prog.set(j, 2 % (2 - i) % 2)
291294
prog.set(j, 1 + oqpy.pi)
@@ -310,6 +313,8 @@ def test_binary_expressions():
310313
prog.set(b1, logical_or(b2, b3))
311314
prog.set(b1, logical_and(b2, True))
312315
prog.set(b1, logical_or(False, b3))
316+
prog.set(d, d + make_duration(10e-9))
317+
prog.set(f, d / make_duration(1))
313318

314319
expected = textwrap.dedent(
315320
"""
@@ -320,6 +325,8 @@ def test_binary_expressions():
320325
bool b1 = false;
321326
bool b2 = true;
322327
bool b3 = false;
328+
duration d = 5.0ns;
329+
float[64] f = 0.0;
323330
i = 2 * (i + j);
324331
j = 2 % (2 - i) % 2;
325332
j = 1 + pi;
@@ -344,6 +351,8 @@ def test_binary_expressions():
344351
b1 = b2 || b3;
345352
b1 = b2 && true;
346353
b1 = false || b3;
354+
d = d + 10.0ns;
355+
f = d / 1000000000.0ns;
347356
"""
348357
).strip()
349358

@@ -1552,6 +1561,35 @@ def test_program_tracks_frame_waveform_vars():
15521561
assert expr_matches(list(prog.waveform_vars), [constant_wf, discrete_wf])
15531562

15541563

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+
15551593
def test_make_duration():
15561594
assert expr_matches(make_duration(1e-3), OQDurationLiteral(1e-3))
15571595
assert expr_matches(make_duration(OQDurationLiteral(1e-4)), OQDurationLiteral(1e-4))

0 commit comments

Comments
 (0)