Skip to content

Commit 234c020

Browse files
committed
success prob
1 parent 6a1d49d commit 234c020

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

qualtran/resource_counting/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333

3434
from ._bloq_counts import BloqCount, QECGatesCost
3535
from ._qubit_counts import QubitCount
36+
from ._success_prob import SuccessProb
3637

3738
from . import generalizers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
from qualtran.bloqs.for_testing.costing import CostingBloq
15+
from qualtran.resource_counting import get_cost_cache, get_cost_value, SuccessProb
16+
17+
18+
def test_coin_flip():
19+
flip = CostingBloq('CoinFlip', num_qubits=1, static_costs=[(SuccessProb(), 0.5)])
20+
algo = CostingBloq('Algo', num_qubits=0, callees=[(flip, 4)])
21+
22+
p = get_cost_value(algo, SuccessProb())
23+
assert p == 0.5**4
24+
25+
costs = get_cost_cache(algo, SuccessProb())
26+
assert costs == {algo: p, flip: 0.5}

0 commit comments

Comments
 (0)