Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 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,8 @@ 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')
Comment thread
mhucka marked this conversation as resolved.
Outdated
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,14 @@ 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'):
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 …)

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.

Naively, I would think that if you can't trigger the failure condition without mucking up the library code itself (i.e. it can't be triggered by bad user input), then I would think an assertion would be the more idiomatic way to guard this


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