Replace assert in reverse_jordan_wigner.py#1257
Replace assert in reverse_jordan_wigner.py#1257mhucka wants to merge 9 commits intoquantumlib:mainfrom
Conversation
Replace another `assert` in a non-test context.
There was a problem hiding this comment.
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.
src/openfermion/transforms/opconversions/reverse_jordan_wigner.py
Outdated
Show resolved
Hide resolved
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This tests the corresponding change in reverse_jordan_wigner.py
| 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')) |
There was a problem hiding this comment.
why do you need to mock stuff?
There was a problem hiding this comment.
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 …)
src/openfermion/transforms/opconversions/reverse_jordan_wigner.py
Outdated
Show resolved
Hide resolved
Include info about the number of terms actually found.
Replace an
assertin a non-test context with raising an exception.