-
Notifications
You must be signed in to change notification settings - Fork 91
PCVL 1267 EMT - Dectector loss balancing #772
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
Open
Benoit-F-Q
wants to merge
8
commits into
Quandela:develop
Choose a base branch
from
Benoit-F-Q:PCVL_1267_Loss_balancing
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a63c6aa
PCVL 1267 EMT - Dectector loss balancing
Benoit-F-Q b5ce2da
fix test
Benoit-F-Q d5c3ed8
code review
Benoit-F-Q e98a875
tests
Benoit-F-Q 77f23f3
code review
Benoit-F-Q 272f23f
fix
Benoit-F-Q 5505db9
temp
Benoit-F-Q 0356b8c
fix
Benoit-F-Q File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,73 @@ | ||
| # MIT License | ||
| # | ||
| # Copyright (c) 2022 Quandela | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in all | ||
| # copies or substantial portions of the Software. | ||
| # | ||
| # As a special exception, the copyright holders of exqalibur library give you | ||
| # permission to combine exqalibur with code included in the standard release of | ||
| # Perceval under the MIT license (or modified versions of such code). You may | ||
| # copy and distribute such a combined system following the terms of the MIT | ||
| # license for both exqalibur and Perceval. This exception for the usage of | ||
| # exqalibur is limited to the python bindings used by Perceval. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| from copy import deepcopy, copy | ||
| import math | ||
| import warnings | ||
|
|
||
| from perceval.utils.logging import get_logger | ||
|
|
||
| from .abstract_mitigation import AbstractMitigation | ||
| from ..computation import Computation | ||
|
|
||
| from perceval.utils import NoiseModel | ||
| from perceval.utils.constants import KEY_RESULTS | ||
|
|
||
| class DetectorBalancing(AbstractMitigation): | ||
| # Note: we do not know if it behaves correctly with Feef-Forward | ||
|
|
||
| APPLY_MIN_PHOTONS = False | ||
| APPLY_LOGICAL_SELECTION = False | ||
|
|
||
| def __init__(self): | ||
| """ | ||
| A mitigation process that adjusts the probabilities of each output state based on the output | ||
| loss and number of photons in each mode. | ||
| """ | ||
|
|
||
| def extend_computation(self, computation: Computation, noise: NoiseModel) -> list[Computation]: | ||
| comp = deepcopy(computation) | ||
| comp.command.name = "probs" | ||
| return [comp] | ||
|
|
||
| def _parse_results(self, computation: Computation, results: list[dict], noise: NoiseModel) -> dict: | ||
| ratios = noise.loss_ratios | ||
| if len(ratios) < computation.experiment.m: | ||
| get_logger().warn( | ||
| "Not enough loss ratio for DetectorBalancing: " | ||
| "defaulting missing ones to 1." | ||
| ) | ||
| ratios.extend([1.] * (computation.experiment.m - len(ratios))) | ||
|
|
||
| res = copy(results[0]) # We are going to modify this to keep custom fields as much as we can | ||
| for k in res[KEY_RESULTS].keys(): | ||
| res[KEY_RESULTS][k] /= math.prod([ratios[k.photon2mode(i)] for i in range(k.n)]) | ||
| res[KEY_RESULTS].normalize() | ||
|
|
||
| return res |
This file contains hidden or 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 hidden or 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 hidden or 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
115 changes: 115 additions & 0 deletions
115
tests/runtime/error_mitigation/test_detector_balancing.py
This file contains hidden or 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,115 @@ | ||
| # MIT License | ||
| # | ||
| # Copyright (c) 2022 Quandela | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in all | ||
| # copies or substantial portions of the Software. | ||
| # | ||
| # As a special exception, the copyright holders of exqalibur library give you | ||
| # permission to combine exqalibur with code included in the standard release of | ||
| # Perceval under the MIT license (or modified versions of such code). You may | ||
| # copy and distribute such a combined system following the terms of the MIT | ||
| # license for both exqalibur and Perceval. This exception for the usage of | ||
| # exqalibur is limited to the python bindings used by Perceval. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| from copy import copy | ||
| import random | ||
|
|
||
| import pytest | ||
| from exqalibur import FockState | ||
| from exqalibur.exqalibur import PostSelect | ||
|
|
||
| from perceval import DetectorBalancing, Computation, CommandFactory, Experiment, NoiseModel, Command, BSCount, \ | ||
| apply_min_photons, apply_post_select | ||
| from perceval.runtime.simulated_computer import SimulatedComputer | ||
| from perceval.utils.bsdistribution import BSDistribution | ||
| from perceval.utils.constants import KEY_SHOTS_USED | ||
| from perceval.utils.states import BasicState | ||
|
|
||
| def prepare_test(): | ||
| raw_results = BSDistribution({FockState([0, 1]): 1, | ||
| FockState([1, 0]): 1, | ||
| FockState([1, 1]): 1, | ||
| FockState([2, 0]): 1, | ||
| FockState([0, 2]): 1}) | ||
| raw_results.normalize() | ||
|
|
||
| shots_used = 1_000_000 | ||
| min_photons = 2 | ||
| post_select = PostSelect("[1] >= 1") | ||
| heralds = {} | ||
|
|
||
| raw_results, phys_perf = apply_min_photons(raw_results, min_photons) | ||
| raw_results, log_perf = apply_post_select(raw_results, post_select, heralds, True) | ||
|
|
||
| sub_results = [] | ||
|
|
||
| sub_results.append({"results": copy(raw_results), | ||
| "physical_perf": phys_perf, | ||
| "logical_perf": log_perf, | ||
| "global_perf": phys_perf * log_perf, | ||
| KEY_SHOTS_USED: shots_used}) | ||
|
|
||
| expected = {"results": raw_results, | ||
| "physical_perf": phys_perf, | ||
| "logical_perf": log_perf, | ||
| "global_perf": phys_perf * log_perf, | ||
| KEY_SHOTS_USED: shots_used} | ||
|
|
||
| return expected, sub_results | ||
|
|
||
|
|
||
| def test_computation_extension(): | ||
| computation = Computation(CommandFactory.samples, Experiment()) | ||
| averaging = DetectorBalancing() | ||
| comp_list = averaging.extend_computation(computation, NoiseModel()) | ||
|
|
||
| assert len(comp_list) == 1 | ||
|
|
||
| assert all(comp.command.name == "probs" for comp in comp_list) | ||
|
|
||
|
|
||
| def test_recombination(): | ||
| expected, sub_results = prepare_test() | ||
| noise = NoiseModel() | ||
| noise.loss_ratios = [1., 1.] | ||
|
Contributor
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. We'll need to decide where to put this |
||
|
|
||
| averaging = DetectorBalancing() | ||
|
|
||
| computation = Computation(CommandFactory.probs, Experiment(2)) | ||
| computation.add_params(max_shots = 50000, max_samples = 10000) | ||
|
|
||
| res = averaging.parse_results(computation, sub_results, noise) | ||
|
|
||
| assert res == expected | ||
|
|
||
|
|
||
| def test_run_through(): | ||
| computer = SimulatedComputer("SLOS") | ||
| computer.mitigations = [ DetectorBalancing() ] | ||
| noise = NoiseModel() | ||
| noise.loss_ratios = [1., .5] | ||
| computer.noise = noise | ||
|
|
||
| e = Experiment(2) | ||
| e.min_detected_photons_filter(1) | ||
| e.with_input(BasicState([1, 0])) | ||
| computation = Computation(CommandFactory.probs, e) | ||
| computation.add_params(max_shots = 50000, max_samples = 10000) | ||
|
|
||
| computer.execute(computation) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but this should be done before replacing invalid values by 1
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need precise specs on this.
Setting [ .1, .1 ], I'd imagined that modes 0 and 1 have 90% loss and other modes are perfect
The "median scaled to 100%" thing seems specific to
perf <-> noisemethodsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right
In that case, something here should ensure that the values are not greater than 1