From 320a6b82ea1e33822218e7c44be898bc4edd2a6f Mon Sep 17 00:00:00 2001 From: Charles Yuan Date: Mon, 1 Jul 2024 18:34:59 -0700 Subject: [PATCH] Use symbolic prod and sum --- qualtran/bloqs/block_encoding/tensor_product.py | 6 +++--- qualtran/symbolics/__init__.py | 1 + qualtran/symbolics/math_funcs.py | 7 +++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/qualtran/bloqs/block_encoding/tensor_product.py b/qualtran/bloqs/block_encoding/tensor_product.py index c440eceff..3a9bc992b 100644 --- a/qualtran/bloqs/block_encoding/tensor_product.py +++ b/qualtran/bloqs/block_encoding/tensor_product.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from functools import cached_property, reduce +from functools import cached_property from typing import Dict, Set, Tuple, TYPE_CHECKING from attrs import evolve, field, frozen, validators @@ -31,7 +31,7 @@ from qualtran.bloqs.block_encoding.lcu_select_and_prepare import PrepareOracle from qualtran.bloqs.bookkeeping import Partition from qualtran.resource_counting import BloqCountT, SympySymbolAllocator -from qualtran.symbolics import is_symbolic, SymbolicFloat, SymbolicInt +from qualtran.symbolics import is_symbolic, prod, sum, SymbolicFloat, SymbolicInt @frozen @@ -80,7 +80,7 @@ def pretty_name(self) -> str: @cached_property def alpha(self) -> SymbolicFloat: - return reduce(lambda a, b: a * b.alpha, self.block_encodings, 1.0) + return prod(u.alpha for u in self.block_encodings) @cached_property def ancilla_bitsize(self) -> SymbolicInt: diff --git a/qualtran/symbolics/__init__.py b/qualtran/symbolics/__init__.py index 10ad6bf00..848ebd51d 100644 --- a/qualtran/symbolics/__init__.py +++ b/qualtran/symbolics/__init__.py @@ -27,6 +27,7 @@ slen, smax, smin, + sum, ) from qualtran.symbolics.types import ( HasLength, diff --git a/qualtran/symbolics/math_funcs.py b/qualtran/symbolics/math_funcs.py index a87929eb5..002d88500 100644 --- a/qualtran/symbolics/math_funcs.py +++ b/qualtran/symbolics/math_funcs.py @@ -97,6 +97,13 @@ def prod(args: Iterable[SymbolicT]) -> SymbolicT: return ret +def sum(args: Iterable[SymbolicT]) -> SymbolicT: + ret: SymbolicT = 0 + for arg in args: + ret = ret + arg + return ret + + def acos(x: SymbolicFloat) -> SymbolicFloat: if not isinstance(x, sympy.Basic): return np.arccos(x)