Skip to content

Commit

Permalink
Merge pull request #639 from qutech/performance/frozendict
Browse files Browse the repository at this point in the history
Use frozendict package
  • Loading branch information
terrorfisch authored Feb 11, 2022
2 parents c067b74 + 7bb1662 commit aa84488
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
2 changes: 2 additions & 0 deletions changes.d/639.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add frozendict dependency to replace handwritten solution. Not having it installed will break in a future release
when the old implementation is removed.
4 changes: 2 additions & 2 deletions qupulse/_program/volatile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

from qupulse.parameter_scope import Scope, MappedScope, JointScope
from qupulse.expressions import Expression, ExpressionScalar
from qupulse.utils.types import FrozenDict
from qupulse.utils.types import FrozenDict, FrozenMapping
from qupulse.utils import is_integer


VolatileProperty = NamedTuple('VolatileProperty', [('expression', Expression),
('dependencies', FrozenDict[str, Expression])])
('dependencies', FrozenMapping[str, Expression])])
VolatileProperty.__doc__ = """Hashable representation of a volatile program property. It does not contain the concrete
value. Using the dependencies attribute to calculate the value might yield unexpected results."""

Expand Down
19 changes: 10 additions & 9 deletions qupulse/parameter_scope.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Contains various implementations of the parameter lookup interface :class:`.Scope`"""

from abc import abstractmethod
from typing import Mapping, AbstractSet, ValuesView, Tuple
from typing import Mapping, AbstractSet, ValuesView, Tuple, Optional
from numbers import Number
import warnings
import itertools

from qupulse.expressions import Expression, ExpressionVariableMissingException
from qupulse.utils.types import FrozenMapping, FrozenDict
Expand All @@ -26,7 +27,7 @@ def __init__(self):
self._as_dict = None

@abstractmethod
def get_volatile_parameters(self) -> FrozenDict[str, Expression]:
def get_volatile_parameters(self) -> FrozenMapping[str, Expression]:
"""
Returns:
A mapping where the keys are the volatile parameters and the
Expand Down Expand Up @@ -75,7 +76,7 @@ def overwrite(self, to_overwrite: Mapping[str, Number]) -> 'Scope':
return MappedScope(self, FrozenDict((name, Expression(value))
for name, value in to_overwrite.items()))

def as_dict(self) -> FrozenDict[str, Number]:
def as_dict(self) -> FrozenMapping[str, Number]:
if self._as_dict is None:
self._as_dict = FrozenDict(self.items())
return self._as_dict
Expand All @@ -101,7 +102,7 @@ def items(self) -> AbstractSet[Tuple[str, Number]]:
def values(self) -> ValuesView[Number]:
return self.as_dict().values()

def as_dict(self) -> FrozenDict[str, Number]:
def as_dict(self) -> FrozenMapping[str, Number]:
if self._as_dict is None:
self._as_dict = FrozenDict((parameter_name, self.get_parameter(parameter_name))
for parameter_name in self.keys())
Expand Down Expand Up @@ -152,10 +153,10 @@ def change_constants(self, new_constants: Mapping[str, Number]) -> 'MappedScope'
mapping=self._mapping
)

def _collect_volatile_parameters(self) -> FrozenDict[str, Expression]:
def _collect_volatile_parameters(self) -> FrozenMapping[str, Expression]:
inner_volatile = self._scope.get_volatile_parameters()
if inner_volatile:
volatile = inner_volatile.to_dict()
volatile = dict(inner_volatile)
for mapped_parameter, expression in self._mapping.items():
volatile_expr_dep = inner_volatile.keys() & expression.variables
if volatile_expr_dep:
Expand All @@ -173,7 +174,7 @@ def _collect_volatile_parameters(self) -> FrozenDict[str, Expression]:
else:
return inner_volatile

def get_volatile_parameters(self) -> AbstractSet[str]:
def get_volatile_parameters(self) -> FrozenMapping[str, Expression]:
if self._volatile_parameters_cache is None:
self._volatile_parameters_cache = self._collect_volatile_parameters()
return self._volatile_parameters_cache
Expand Down Expand Up @@ -235,7 +236,7 @@ def change_constants(self, new_constants: Mapping[str, Number]) -> 'Scope':
else:
return self

def get_volatile_parameters(self) -> FrozenDict[str, Expression]:
def get_volatile_parameters(self) -> FrozenMapping[str, Expression]:
return self._volatile_parameters

@classmethod
Expand Down Expand Up @@ -282,7 +283,7 @@ def change_constants(self, new_constants: Mapping[str, Number]) -> 'JointScope':
(parameter_name, scope.change_constants(new_constants)) for parameter_name, scope in self._lookup.items()
))

def get_volatile_parameters(self) -> FrozenDict[str, Expression]:
def get_volatile_parameters(self) -> FrozenMapping[str, Expression]:
if self._volatile_parameters is None:
volatile_parameters = {}
for parameter_name, scope in self._lookup:
Expand Down
12 changes: 11 additions & 1 deletion qupulse/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
import numpy
import sympy

try:
from frozendict import frozendict
except ImportError:
warnings.warn("The frozendict package is not installed. We currently also ship a fallback frozendict which "
"will be removed in a future release.", category=DeprecationWarning)
frozendict = None

import qupulse.utils.numeric as qupulse_numeric

__all__ = ["MeasurementWindow", "ChannelID", "HashableNumpyArray", "TimeType", "time_from_float", "DocStringABCMeta",
Expand Down Expand Up @@ -505,7 +512,10 @@ def to_dict(self) -> typing.Dict[_KT_hash, _T_co_hash]:
return self._dict.copy()


FrozenDict = _FrozenDictByWrapping
if frozendict is None:
FrozenDict = _FrozenDictByWrapping
else:
FrozenDict = frozendict


class SequenceProxy(collections.abc.Sequence):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install_requires =
sympy>=1.1.1
numpy
cached_property
frozendict
test_suite = tests

[options.extras_require]
Expand Down

0 comments on commit aa84488

Please sign in to comment.