Skip to content

Commit 6e098d2

Browse files
committed
Include more info in the exception message
Include info about the number of terms actually found.
1 parent 64c2d5c commit 6e098d2

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

src/openfermion/transforms/opconversions/reverse_jordan_wigner.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ def reverse_jordan_wigner(qubit_operator, n_qubits=None):
8282

8383
# Get next non-identity operator acting below 'working_qubit'.
8484
if len(working_term.terms) != 1:
85-
raise ValueError('QubitOperator must contain exactly one term')
85+
raise ValueError(
86+
'QubitOperator must contain exactly one term. '
87+
f'Found {len(working_term.terms)} terms: {working_term!r}'
88+
)
8689
working_qubit = pauli_operator[0] - 1
8790
for working_operator in reversed(list(working_term.terms)[0]):
8891
if working_operator[0] <= working_qubit:

src/openfermion/transforms/opconversions/reverse_jordan_wigner_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ def test_reverse_jw_multi_term_error(self):
166166
'openfermion.transforms.opconversions.reverse_jordan_wigner.' 'QubitOperator.__mul__'
167167
) as mock_mul:
168168
mock_mul.return_value = QubitOperator('X0') + QubitOperator('Y0')
169-
with self.assertRaisesRegex(ValueError, 'QubitOperator must contain exactly one term'):
169+
with self.assertRaisesRegex(
170+
ValueError, 'QubitOperator must contain exactly one term. Found 2 terms'
171+
):
170172
reverse_jordan_wigner(QubitOperator('X1'))
171173

172174
def test_jw_convention(self):

0 commit comments

Comments
 (0)