Commit 82c780a2 authored by Joseph Weston's avatar Joseph Weston

fix bug when multiplying BlochCoeff by sympy expression

parent ae1ed26b
Pipeline #23487 passed with stage
in 2 minutes and 44 seconds
......@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Bug when multiplying a model by a sympy expression (undefined variable)
- Bug when multiplying BlochCoeff with sympy expression (sympy multiplication was
being used, when BlochCoeff multiplication should have been used)
### Removed
- Removed the notebooks from the source repository
......
......@@ -288,7 +288,9 @@ def hamiltonian_from_family(family, coeffs=None, nsimplify=True, tosympy=True):
coeffs = list(sympy.symbols('c0:%d'%len(family), real=True))
else:
assert len(coeffs) == len(family), 'Length of family and coeffs do not match.'
ham = sum(c * term for c, term in zip(coeffs, family))
# The order of multiplication is important here, so that __mul__ of 'term'
# gets used. 'c' is a sympy symbol, which multiplies 'term' incorrectly.
ham = sum(term * c for c, term in zip(coeffs, family))
if tosympy:
return ham.tosympy(nsimplify=nsimplify)
else:
......
......@@ -398,7 +398,10 @@ class Model(UserDict):
result = self.__mul__(other)
elif isinstance(other, Basic):
keep = self.keep
result = sum((type(self)({other * key: copy(val)},
# The order 'key * other' is important: we want to force
# the implementation of __mul__ of 'key' to be used. This
# is correct as long as the symbols in 'key' and 'other' commute.
result = sum((type(self)({key * other: copy(val)},
keep=keep,
momenta=self.momenta)
for key, val in self.items()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment