-
Notifications
You must be signed in to change notification settings - Fork 102
[Costs] Success probability #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4156b56
57f81ab
3b6d153
c9c4923
a616769
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Copyright 2024 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 logging | ||
| from typing import Callable | ||
|
|
||
| from attrs import frozen | ||
|
|
||
| from qualtran import Bloq | ||
|
|
||
| from ._call_graph import get_bloq_callee_counts | ||
| from ._costing import CostKey | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
fdmalone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @frozen | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of scope for this PR but it would be nice to have some notebook expanding on this as a concept (success probability) |
||
| class SuccessProb(CostKey[float]): | ||
| """The success probability of a bloq. | ||
|
|
||
| A bloq's success probability is the multiplicative product of its callees' | ||
| success probabilities. Bloqs that have a specific success probability should override | ||
| `my_static_costs` to provide their actual success probability. | ||
| """ | ||
|
|
||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we eventually need to deal with like the log of the success probability so things are additive. This looks like it runs the risk of potentially hitting numerical issues.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realized later that after writing this that if the success probability was so small to warrant something special it would be a dreadful quantum algorithm.
Comment on lines
+36
to
+42
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way this is written, would we ever need this as a separate cost? Can we just use Is there a good example that motivates this cost key that already exists in another branch / is easy to describe ?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need the contributions from inner nodes as well, not just the leafs that will show up in This was requested by someone at the IEEE meeting last year and is a good demo of a different type of cost |
||
| return tot | ||
|
|
||
| def zero(self) -> float: | ||
| return 1.0 # under multiplication, 1 is the identity. | ||
|
|
||
| def __str__(self): | ||
| return 'success prob' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Copyright 2024 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} |
Uh oh!
There was an error while loading. Please reload this page.