Skip to content

Commit 1a49ff0

Browse files
committed
Fix 2 problems in _multitensor.py
The `dual_basis` parameter to the `MultiTensor` class constructor was defaulted to `DualBasis()`. In Python, default arguments are evaluated only once at definition time, meaning every `MultiTensor` instance created without an explicit basis shared the same global `DualBasis` object. When tests were run in parallel or repeated, data from one test would leak into others, leading to errors. In addition, `MultiTensor.add_dual_elements` used `list.extend()` instead of `list.append()` when adding `DualBasisElement` objects. Since `DualBasisElement` is iterable (yielding its internal terms), using `extend()` incorrectly decomposed the object into its constituent tuples instead of adding the object as a single unit. This caused `MultiTensor.synthesize_dual_basis()` to fail in some cases.
1 parent cf66a25 commit 1a49ff0

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/openfermion/contrib/representability/_multitensor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __iter__(self):
2323

2424

2525
class MultiTensor(object):
26-
def __init__(self, tensors, dual_basis=DualBasis()):
26+
def __init__(self, tensors, dual_basis=None):
2727
"""
2828
A collection of tensor objects with maps from name to tensor
2929
@@ -46,6 +46,8 @@ def __init__(self, tensors, dual_basis=DualBasis()):
4646
self.off_set_map = self.make_offset_dict(self.tensors)
4747

4848
# An iterable object that provides access to the dual basis elements
49+
if dual_basis is None:
50+
dual_basis = DualBasis()
4951
self.dual_basis = dual_basis
5052
self.vec_dim = sum([vec.size for vec in self.tensors])
5153

@@ -82,7 +84,7 @@ def add_dual_elements(self, dual_element):
8284
raise TypeError("dual_element variable needs to be a DualBasisElement type")
8385

8486
# we should extend TMap to add
85-
self.dual_basis.elements.extend(dual_element)
87+
self.dual_basis.elements.append(dual_element)
8688

8789
def synthesize_dual_basis(self):
8890
"""

0 commit comments

Comments
 (0)