-
Notifications
You must be signed in to change notification settings - Fork 196
Add pymatching correlated to sinter #1046
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
m-mcewen
wants to merge
3
commits into
main
Choose a base branch
from
sinter-pymatching-correlated
base: main
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
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,26 +1,53 @@ | ||
| from sinter._decoding._decoding_decoder_class import Decoder, CompiledDecoder | ||
|
|
||
|
|
||
| def check_pymatching_version_for_correlated_decoding(pymatching): | ||
| v = pymatching.__version__.split('.') | ||
| try: | ||
| a = int(v[0]) | ||
| b = int(v[1] | ||
| c = int(''.join(e for e in v[2] if e in '0123456789')) # In case dev version | ||
| except ValueError, IndexError: | ||
| return # Probably it's the future. | ||
|
|
||
| if (a, b, c) < (2, 3, 1): | ||
| if not (int(v[0]) >= 2 or int(v[1]) >= 3 or int(v[2]) >= 1): | ||
|
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. drop redundant if here |
||
| raise ValueError( | ||
| "Pymatching version must be at least 2.3.1 for correlated decoding.\n" | ||
| f"Installed version: {pymatching.__version__}\n" | ||
| "To fix this, install a newer version of pymatching into your environment.\n" | ||
| "For example, if you are using pip, run `pip install pymatching --upgrade`.\n" | ||
| ) | ||
|
|
||
|
|
||
| class PyMatchingCompiledDecoder(CompiledDecoder): | ||
| def __init__(self, matcher: 'pymatching.Matching'): | ||
| def __init__(self, matcher: 'pymatching.Matching', use_correlated_decoding: bool): | ||
| self.matcher = matcher | ||
| self.use_correlated_decoding = use_correlated_decoding | ||
|
|
||
| def decode_shots_bit_packed( | ||
| self, | ||
| *, | ||
| bit_packed_detection_event_data: 'np.ndarray', | ||
| ) -> 'np.ndarray': | ||
| kwargs = {} | ||
| if self.use_correlated_decoding: | ||
| kwargs['enable_correlations'] = True | ||
| return self.matcher.decode_batch( | ||
| shots=bit_packed_detection_event_data, | ||
| bit_packed_shots=True, | ||
| bit_packed_predictions=True, | ||
| return_weights=False, | ||
| **kwargs, | ||
| ) | ||
|
|
||
|
|
||
| class PyMatchingDecoder(Decoder): | ||
| """Use pymatching to predict observables from detection events.""" | ||
|
|
||
| def __init__(self, use_correlated_decoding: bool = False): | ||
| self.use_correlated_decoding = use_correlated_decoding | ||
|
|
||
| def compile_decoder_for_dem(self, *, dem: 'stim.DetectorErrorModel') -> CompiledDecoder: | ||
| try: | ||
| import pymatching | ||
|
|
@@ -31,7 +58,14 @@ def compile_decoder_for_dem(self, *, dem: 'stim.DetectorErrorModel') -> Compiled | |
| "For example, if you are using pip, run `pip install pymatching`.\n" | ||
| ) from ex | ||
|
|
||
| return PyMatchingCompiledDecoder(pymatching.Matching.from_detector_error_model(dem)) | ||
| kwargs = {} | ||
| if self.use_correlated_decoding: | ||
| check_pymatching_version_for_correlated_decoding(pymatching) | ||
| kwargs['enable_correlations'] = True | ||
| return PyMatchingCompiledDecoder( | ||
| pymatching.Matching.from_detector_error_model(dem, **kwargs), | ||
| use_correlated_decoding=self.use_correlated_decoding, | ||
| ) | ||
|
|
||
| def decode_via_files(self, | ||
| *, | ||
|
|
@@ -60,7 +94,9 @@ def decode_via_files(self, | |
| if not hasattr(pymatching, 'cli'): | ||
| raise ValueError(""" | ||
| The installed version of pymatching has no `pymatching.cli` method. | ||
|
|
||
| sinter requires pymatching 2.1.0 or later. | ||
|
|
||
| If you're using pip to install packages, this can be fixed by running | ||
|
|
||
| ``` | ||
|
|
@@ -69,13 +105,18 @@ def decode_via_files(self, | |
|
|
||
| """) | ||
|
|
||
| result = pymatching.cli(command_line_args=[ | ||
| args = [ | ||
| "predict", | ||
| "--dem", str(dem_path), | ||
| "--in", str(dets_b8_in_path), | ||
| "--in_format", "b8", | ||
| "--out", str(obs_predictions_b8_out_path), | ||
| "--out_format", "b8", | ||
| ]) | ||
| ] | ||
| if self.use_correlated_decoding: | ||
| check_pymatching_version_for_correlated_decoding(pymatching) | ||
| args.append("--enable_correlations") | ||
|
|
||
| result = pymatching.cli(command_line_args=args) | ||
| if result: | ||
| raise ValueError("pymatching.cli returned a non-zero exit code") | ||
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
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.
... I made a syntax error