Skip to content

Commit 8cb731a

Browse files
committed
use to_ast to create the ast node after _to_oqpy_expression
1 parent 8e7b08c commit 8cb731a

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

oqpy/base.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,14 @@ def to_ast(self, program: Program) -> ast.Expression:
470470
def to_ast(program: Program, item: AstConvertible) -> ast.Expression:
471471
"""Convert an object to an AST node."""
472472
if hasattr(item, "_to_oqpy_expression"):
473-
item = cast(ExpressionConvertible, item)
474-
return item._to_oqpy_expression().to_ast(program)
473+
item = cast(ExpressionConvertible, item)._to_oqpy_expression()
475474
if hasattr(item, "_to_cached_oqpy_expression"):
476475
item = cast(CachedExpressionConvertible, item)
477476
if item._oqpy_cache_key is None:
478477
item._oqpy_cache_key = uuid.uuid1()
479478
if item._oqpy_cache_key not in program.expr_cache:
480-
program.expr_cache[item._oqpy_cache_key] = item._to_cached_oqpy_expression().to_ast(
481-
program
482-
)
483-
return program.expr_cache[item._oqpy_cache_key]
479+
program.expr_cache[item._oqpy_cache_key] = item._to_cached_oqpy_expression()
480+
item = program.expr_cache[item._oqpy_cache_key]
484481
if isinstance(item, (complex, np.complexfloating)):
485482
if item.imag == 0:
486483
return to_ast(program, item.real)

tests/test_directives.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,21 +1586,26 @@ def _to_oqpy_expression(self):
15861586
class C:
15871587
def _to_oqpy_expression(self):
15881588
return 1e-7
1589+
def __rmul__(self, other):
1590+
return other * self._to_oqpy_expression()
15891591

15901592
frame = FrameVar(name="f1")
15911593
prog = Program()
15921594
prog.set(A("a1"), 2)
1595+
prog.set(FloatVar(name="c1"), 3 * C())
15931596
prog.delay(A("a2"), frame)
15941597
prog.delay(B("b1"), frame)
15951598
prog.delay(C(), frame)
15961599
expected = textwrap.dedent(
15971600
"""
15981601
OPENQASM 3.0;
15991602
duration a1 = 100.0ns;
1603+
float[64] c1;
16001604
duration a2 = 100.0ns;
16011605
frame f1;
16021606
float[64] b1 = 1e-07;
16031607
a1 = 2;
1608+
c1 = 3e-07;
16041609
delay[a2] f1;
16051610
delay[b1 * 1s] f1;
16061611
delay[100.0ns] f1;

0 commit comments

Comments
 (0)