Skip to content

Commit 43d4456

Browse files
authored
Specify cache key via protocol (#80)
* Specify cache key via protocol * formatting, v0.3.4
1 parent 25c3033 commit 43d4456

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

oqpy/base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
from __future__ import annotations
2323

2424
import math
25+
import uuid
2526
from abc import ABC, abstractmethod
2627
from typing import (
2728
TYPE_CHECKING,
2829
Any,
30+
Hashable,
2931
Iterable,
3032
Optional,
3133
Protocol,
@@ -351,6 +353,8 @@ class CachedExpressionConvertible(Protocol):
351353
no guarantees are made about this.
352354
"""
353355

356+
_oqpy_cache_key: Hashable
357+
354358
def _to_cached_oqpy_expression(self) -> HasToAst:
355359
... # pragma: no cover
356360

@@ -469,10 +473,14 @@ def to_ast(program: Program, item: AstConvertible) -> ast.Expression:
469473
item = cast(ExpressionConvertible, item)
470474
return item._to_oqpy_expression().to_ast(program)
471475
if hasattr(item, "_to_cached_oqpy_expression"):
472-
if id(item) not in program.expr_cache:
473-
item = cast(CachedExpressionConvertible, item)
474-
program.expr_cache[id(item)] = item._to_cached_oqpy_expression().to_ast(program)
475-
return program.expr_cache[id(item)]
476+
item = cast(CachedExpressionConvertible, item)
477+
if item._oqpy_cache_key is None:
478+
item._oqpy_cache_key = uuid.uuid1()
479+
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]
476484
if isinstance(item, (complex, np.complexfloating)):
477485
if item.imag == 0:
478486
return to_ast(program, item.real)

oqpy/program.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import warnings
2727
from copy import deepcopy
28-
from typing import Any, Iterable, Iterator, Optional
28+
from typing import Any, Hashable, Iterable, Iterator, Optional
2929

3030
from openpulse import ast
3131
from openpulse.printer import dumps
@@ -107,7 +107,7 @@ def __init__(self, version: Optional[str] = "3.0", simplify_constants: bool = Tr
107107
self.simplify_constants = simplify_constants
108108
self.declared_subroutines: set[str] = set()
109109
self.declared_gates: set[str] = set()
110-
self.expr_cache: dict[int, ast.Expression] = {}
110+
self.expr_cache: dict[Hashable, ast.Expression] = {}
111111
"""A cache of ast made by CachedExpressionConvertible objects used in this program.
112112
113113
This is used by `to_ast` to avoid repetitively evaluating ast conversion methods.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "oqpy"
3-
version = "0.3.3"
3+
version = "0.3.4"
44
description = "Generating OpenQASM 3 + OpenPulse in Python"
55
authors = ["OQpy Contributors <[email protected]>"]
66
license = "Apache-2.0"

tests/test_directives.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,7 @@ def test_cached_expression_convertible():
15751575
class A:
15761576
name: str
15771577
count: int = 0
1578+
_oqpy_cache_key = None
15781579

15791580
def _to_cached_oqpy_expression(self):
15801581
self.count += 1
@@ -1584,6 +1585,7 @@ def _to_cached_oqpy_expression(self):
15841585
class F:
15851586
name: str
15861587
count: int = 0
1588+
_oqpy_cache_key = None
15871589

15881590
def _to_cached_oqpy_expression(self):
15891591
self.count += 1

0 commit comments

Comments
 (0)