-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b49e278
commit 1d2ba85
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import abc | ||
import logging | ||
from typing import Callable, Dict, Generic, Sequence, Tuple, TYPE_CHECKING | ||
|
||
import sympy | ||
from attrs import frozen | ||
|
||
from qualtran import Bloq, DecomposeNotImplementedError, DecomposeTypeError | ||
|
||
from . import CostValT | ||
from ._call_graph import get_bloq_callee_counts | ||
from ._costing import CostKey | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@frozen | ||
class SuccessProb(CostKey[float]): | ||
def compute(self, bloq: 'Bloq', get_callee_cost: Callable[['Bloq'], float]) -> float: | ||
tot: float = 1.0 | ||
callees = get_bloq_callee_counts(bloq) | ||
logger.info("Computing %s for %s from %d callee(s)", self, bloq, len(callees)) | ||
for callee, n in callees: | ||
v = get_callee_cost(callee) | ||
tot *= v**n | ||
return tot | ||
|
||
def zero(self) -> CostValT: | ||
return 1.0 # under multiplication, 1 is the identity. | ||
|
||
def __str__(self): | ||
return 'success prob' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from qualtran.bloqs.for_testing.costing import CostingBloq | ||
from qualtran.resource_counting import get_cost_cache, get_cost_value, SuccessProb | ||
|
||
|
||
def test_coin_flip(): | ||
flip = CostingBloq('CoinFlip', num_qubits=1, static_costs=[(SuccessProb(), 0.5)]) | ||
algo = CostingBloq('Algo', num_qubits=0, callees=[(flip, 4)]) | ||
|
||
p = get_cost_value(algo, SuccessProb()) | ||
assert p == 0.5**4 | ||
|
||
costs = get_cost_cache(algo, SuccessProb()) | ||
assert costs == {algo: p, flip: 0.5} |