Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ def reverse_jordan_wigner(qubit_operator, n_qubits=None):
working_term.terms[list(working_term.terms)[0]] = 1.0

# Get next non-identity operator acting below 'working_qubit'.
assert len(working_term.terms) == 1
if len(working_term.terms) != 1:
raise ValueError(
'QubitOperator must contain exactly one term. '
f'Found {len(working_term.terms)} terms: {working_term!r}'
)
working_qubit = pauli_operator[0] - 1
for working_operator in reversed(list(working_term.terms)[0]):
if working_operator[0] <= working_qubit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""Tests reverse_jordan_wigner.py."""

import unittest
from unittest.mock import patch

from openfermion.ops.operators import FermionOperator, QubitOperator
from openfermion.transforms.opconversions import jordan_wigner, normal_ordered
Expand Down Expand Up @@ -160,6 +161,16 @@ def test_bad_type(self):
with self.assertRaises(TypeError):
reverse_jordan_wigner(3)

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. Found 2 terms'
):
reverse_jordan_wigner(QubitOperator('X1'))
Comment on lines +164 to +172
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 …)


def test_jw_convention(self):
"""Test that the Jordan-Wigner convention places the Z-string on
lower indices."""
Expand Down
Loading