Skip to content

Commit 07d027b

Browse files
fix regression with cache (#95)
1 parent 6efb6f5 commit 07d027b

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

oqpy/base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class CachedExpressionConvertible(Protocol):
363363

364364
_oqpy_cache_key: Hashable
365365

366-
def _to_cached_oqpy_expression(self) -> HasToAst: ... # pragma: no cover
366+
def _to_cached_oqpy_expression(self) -> AstConvertible: ... # pragma: no cover
367367

368368

369369
class OQPyUnaryExpression(OQPyExpression):
@@ -490,8 +490,10 @@ def to_ast(program: Program, item: AstConvertible) -> ast.Expression:
490490
if item._oqpy_cache_key is None:
491491
item._oqpy_cache_key = uuid.uuid1()
492492
if item._oqpy_cache_key not in program.expr_cache:
493-
program.expr_cache[item._oqpy_cache_key] = item._to_cached_oqpy_expression()
494-
item = program.expr_cache[item._oqpy_cache_key]
493+
program.expr_cache[item._oqpy_cache_key] = to_ast(
494+
program, item._to_cached_oqpy_expression()
495+
)
496+
return program.expr_cache[item._oqpy_cache_key]
495497
if isinstance(item, (complex, np.complexfloating)):
496498
if item.imag == 0:
497499
return to_ast(program, item.real)

tests/test_directives.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,7 @@ def _to_cached_oqpy_expression(self):
16651665
assert dur.count == 2
16661666
# This gets computed just once
16671667
assert frame.count == 1
1668+
assert all(isinstance(v, ast.QASMNode) for v in prog.expr_cache.values())
16681669

16691670

16701671
def test_waveform_extern_arg_passing():

0 commit comments

Comments
 (0)