Skip to content

FIx ValueError of chsh#116

Merged
marcosfrenkel merged 2 commits into
masterfrom
chsh_raise_value_error_fix
Oct 3, 2025
Merged

FIx ValueError of chsh#116
marcosfrenkel merged 2 commits into
masterfrom
chsh_raise_value_error_fix

Conversation

@marcosfrenkel
Copy link
Copy Markdown
Collaborator

Raise HTTPException for impossible negative expectation values in chsh instead of ValueError

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)
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.

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)

Comment thread src/pqnstack/app/api/routes/chsh.py Outdated
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)
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.

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]
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.

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)

Copy link
Copy Markdown
Contributor

@SoroushHoseini SoroushHoseini left a comment

Choose a reason for hiding this comment

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

Sounds good

@marcosfrenkel marcosfrenkel merged commit 23c2fd9 into master Oct 3, 2025
6 checks passed
@marcosfrenkel marcosfrenkel deleted the chsh_raise_value_error_fix branch October 3, 2025 14:29
@marcosfrenkel
Copy link
Copy Markdown
Collaborator Author

We should discuss that feedback in person

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