Skip to content

Replace assert in reverse_jordan_wigner.py#1257

Open
mhucka wants to merge 9 commits intoquantumlib:mainfrom
mhucka:remove-assert-in-jw
Open

Replace assert in reverse_jordan_wigner.py#1257
mhucka wants to merge 9 commits intoquantumlib:mainfrom
mhucka:remove-assert-in-jw

Conversation

@mhucka
Copy link
Copy Markdown
Contributor

@mhucka mhucka commented Apr 6, 2026

Replace an assert in a non-test context with raising an exception.

Replace another `assert` in a non-test context.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces an assertion with an explicit ValueError in the reverse Jordan-Wigner transformation to ensure the QubitOperator contains exactly one term. The review feedback suggests refining the error message for better clarity and notes that the check should ideally occur before any indexing to avoid shadowing the error with an IndexError.

@mhucka mhucka changed the title Replace another assert with an exception Replace assert in src/openfermion/transforms/opconversions/reverse_jordan_wigner.py Apr 6, 2026
@mhucka mhucka changed the title Replace assert in src/openfermion/transforms/opconversions/reverse_jordan_wigner.py Replace assert in reverse_jordan_wigner.py Apr 6, 2026
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@mhucka mhucka requested review from mpharrigan and ncrubin April 6, 2026 04:40
Comment on lines +164 to +170
def test_reverse_jw_multi_term_error(self):
with patch(
'openfermion.transforms.opconversions.reverse_jordan_wigner.' 'QubitOperator.__mul__'
) as mock_mul:
mock_mul.return_value = QubitOperator('X0') + QubitOperator('Y0')
with self.assertRaisesRegex(ValueError, 'QubitOperator must contain exactly one term'):
reverse_jordan_wigner(QubitOperator('X1'))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why do you need to mock stuff?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's to simulate an internal failure condition. The code uses unittest.mock.patch to replace the __mul__ method of QubitOperator, specifically where it is used inside the reverse_jordan_wigner module, so that it can force any multiplication operation to return a multiterm operator (X0 + Y0) instead of a single Pauli string.

Maybe I'm missing another, easier way? (Entirely possible …)

mhucka added 2 commits April 8, 2026 01:27
Include info about the number of terms actually found.
@mhucka mhucka requested a review from mpharrigan April 8, 2026 02:03
@mhucka mhucka enabled auto-merge (squash) April 8, 2026 02:03
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.

2 participants