Skip to content

Commit

Permalink
Fix 128 (Initialization with multi term FermionicOp) (#131) (#137)
Browse files Browse the repository at this point in the history
* fix 128

* add release note

Co-authored-by: Manoel Marques <[email protected]>
Co-authored-by: Steve Wood <[email protected]>
(cherry picked from commit 625939e)

Co-authored-by: Ikko Hamamura <[email protected]>
  • Loading branch information
mergify[bot] and ikkoham authored Apr 14, 2021
1 parent ab48066 commit f5a59c7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ def __init__(
if invalid_labels:
raise ValueError(f"Invalid labels for sparse labels are given: {invalid_labels}")
list_label = [["I"] * self._register_length for _ in labels]
prev_index: Optional[int] = None
for term, label in enumerate(labels):
prev_index: Optional[int] = None
for split_label in label.split():
op_label, index_str = split_label.split("_", 1)
index = int(index_str)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed an issue where :class:`~qiskit_nature.operators.FermionicOp` raises
unwanted `ValueError` when initialized with some list of sparse label.
10 changes: 8 additions & 2 deletions test/operators/second_quantization/test_fermionic_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,14 @@ def test_init_invalid_label(self, label, register_length):

def test_init_multiterm(self):
"""Test __init__ with multi terms"""
labels = [("N", 2), ("-", 3.14)]
self.assertListEqual(FermionicOp(labels).to_list(), labels)
with self.subTest("Test 1"):
labels = [("N", 2), ("-", 3.14)]
self.assertListEqual(FermionicOp(labels).to_list(), labels)

with self.subTest("Test 2"):
labels = [("+-", 1), ("-+", -1)]
op = FermionicOp([("+_0 -_1", 1.0), ("-_0 +_1", -1.0)], register_length=2)
self.assertListEqual(op.to_list(), labels)

def test_init_multiple_digits(self):
"""Test __init__ for sparse label with multiple digits"""
Expand Down

0 comments on commit f5a59c7

Please sign in to comment.