Allow Experiment.set_postselection to accept string.#725
Merged
Benoit-F-Q merged 6 commits intoQuandela:release/1.2.0from Apr 9, 2026
Merged
Allow Experiment.set_postselection to accept string.#725Benoit-F-Q merged 6 commits intoQuandela:release/1.2.0from
Benoit-F-Q merged 6 commits intoQuandela:release/1.2.0from
Conversation
Aubaert
reviewed
Apr 2, 2026
| def set_selection(self, | ||
| min_detected_photons_filter: int = None, | ||
| postselect: PostSelect = None, | ||
| postselect: PostSelect = PostSelect(), |
Contributor
There was a problem hiding this comment.
Be careful with non-immutable default arguments, as they are kept alive for the whole script duration (so if someone merges a PostSelect into this one, it will affect all default PostSelect)
| def set_selection(self, | ||
| min_detected_photons_filter: int = None, | ||
| postselect: PostSelect = None, | ||
| postselect: PostSelect = PostSelect(), |
Aubaert
approved these changes
Apr 3, 2026
…-string Conflicts: perceval/simulators/noisy_sampling_simulator.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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Post-selection rules are defined using strings which contain logical instructions on how to postprocess the results.
The
PostSelectclass verifies & processes the logic of these strings.A common friction point for a user is setting the postselection in an Experiment. Here, it is common for the user to pass a string to
Experiment.set_postselection. Even when users know that aPostSelectobject is required, passing a raw string is often the most natural thing to do when coding on the fly. Since the string already encodes the full logic, this leads to an avoidableTypeError.To prevent this error, input
strarguments inExperiment.set_postselectionare wrapped asPostSelectobjects before proceeding.Also, I have noticed a bug when a user attempts to do:
experiment.set_postselection(None)as mentioned in the function docstring. Here, this would formerly raise aTypeError. I have amended the function to allowNonearguments to clear the post-selection rule.