FIx ValueError of chsh#116
Merged
Merged
Conversation
…h instead of ValueError
Benjamin-Nussbaum
requested changes
Oct 3, 2025
Comment on lines
102
to
103
| if len(negative_indices) > 1 or negative_indices[0] != 0: | ||
| logger.warning("Expectation values have unexpected negative indices: %s", negative_indices) |
Contributor
There was a problem hiding this comment.
This condition needs an explanatory comment. What is unexpected about these cases? What is special about expectation_values[0]?
Suggested change
| if len(negative_indices) > 1 or negative_indices[0] != 0: | |
| logger.warning("Expectation values have unexpected negative indices: %s", negative_indices) | |
| if negative_count > 1 or expectation_values[0] > 0: | |
| logger.warning("Expectation values have unexpected negative indices: %s", negative_indices) |
| if negative_count in impossible_counts: | ||
| msg = f"Impossible negative expectation values found: {negative_indices}, expectation_values = {expectation_values}, expectation_errors = {expectation_errors}" | ||
| raise ValueError(msg) | ||
| raise HTTPException(status_code=504, detail=msg) |
Contributor
There was a problem hiding this comment.
Use the status field rather than a raw int. What makes 504 the best code in this case? Would 502 or even just 500 be preferable?
Comment on lines
94
to
96
| negative_count = sum(1 for v in expectation_values if v < 0) | ||
| negative_indices = [i for i, v in enumerate(expectation_values) if v < 0] | ||
| impossible_counts = [0, 2, 4] |
Contributor
There was a problem hiding this comment.
Suggested change
| negative_indices = [i for i, v in enumerate(expectation_values) if v < 0] | |
| negative_count = len(negative_indices) | |
| impossible_counts = (0, 2, 4) |
Collaborator
Author
|
We should discuss that feedback in person |
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.
Raise HTTPException for impossible negative expectation values in chsh instead of ValueError