Skip to content

Commit

Permalink
Use symbolic prod and sum
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesyuan314 committed Jul 2, 2024
1 parent 39b51f7 commit 320a6b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions qualtran/bloqs/block_encoding/tensor_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions qualtran/symbolics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
slen,
smax,
smin,
sum,
)
from qualtran.symbolics.types import (
HasLength,
Expand Down
7 changes: 7 additions & 0 deletions qualtran/symbolics/math_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 320a6b8

Please sign in to comment.