|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import abc |
| 15 | +import logging |
| 16 | +from typing import Callable, Dict, Generic, Sequence, Tuple, TYPE_CHECKING |
| 17 | + |
| 18 | +import sympy |
| 19 | +from attrs import frozen |
| 20 | + |
| 21 | +from qualtran import Bloq, DecomposeNotImplementedError, DecomposeTypeError |
| 22 | + |
| 23 | +from . import CostValT |
| 24 | +from ._call_graph import get_bloq_callee_counts |
| 25 | +from ._costing import CostKey |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +@frozen |
| 31 | +class SuccessProb(CostKey[float]): |
| 32 | + def compute(self, bloq: 'Bloq', get_callee_cost: Callable[['Bloq'], float]) -> float: |
| 33 | + tot: float = 1.0 |
| 34 | + callees = get_bloq_callee_counts(bloq) |
| 35 | + logger.info("Computing %s for %s from %d callee(s)", self, bloq, len(callees)) |
| 36 | + for callee, n in callees: |
| 37 | + v = get_callee_cost(callee) |
| 38 | + tot *= v**n |
| 39 | + return tot |
| 40 | + |
| 41 | + def zero(self) -> CostValT: |
| 42 | + return 1.0 # under multiplication, 1 is the identity. |
| 43 | + |
| 44 | + def __str__(self): |
| 45 | + return 'success prob' |
0 commit comments