Skip to content

Allow Experiment.set_postselection to accept string.#725

Merged
Benoit-F-Q merged 6 commits intoQuandela:release/1.2.0from
anthonyrtw:postselect-string
Apr 9, 2026
Merged

Allow Experiment.set_postselection to accept string.#725
Benoit-F-Q merged 6 commits intoQuandela:release/1.2.0from
anthonyrtw:postselect-string

Conversation

@anthonyrtw
Copy link
Copy Markdown
Contributor

Post-selection rules are defined using strings which contain logical instructions on how to postprocess the results.
The PostSelect class 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 a PostSelect object 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 avoidable TypeError.

experiment.set_postselection("[0, 1, 2] == 1 AND [3, 4] < 2 OR [5] == 1") # Error
  
experiment.set_postselection(PostSelect("[0, 1, 2] == 1 AND [3, 4] < 2 OR [5] == 1"))

To prevent this error, input str arguments in Experiment.set_postselection are wrapped as PostSelect objects 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 a TypeError. I have amended the function to allow None arguments to clear the post-selection rule.

def set_selection(self,
min_detected_photons_filter: int = None,
postselect: PostSelect = None,
postselect: PostSelect = PostSelect(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

@Benoit-F-Q Benoit-F-Q merged commit 3923457 into Quandela:release/1.2.0 Apr 9, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants