🐛 Bug: mu reassignment breaks shape handling in HamHeisenberg when using connectivity
Description
When using the HamHeisenberg class with a connectivity matrix and scalar parameters (J_eq, J_ax, mu), the initialization logic appears to incorrectly overwrite self.mu, leading to shape inconsistencies and runtime errors during one-body integral generation. It seems to me that it was added for the second method for defining the Hamiltonian by providing exchange couplings, $J^{ax}$ $J^{eq}$ , and $\mu$ as numpy arrays. as mentioned in the tutorials.
Relevant Code
The issue seems to originate from:
if connectivity is not None:
self.connectivity = connectivity
self.n_sites = connectivity.shape[0]
if isinstance(J_eq, (int, float)):
self.J_eq = J_eq * connectivity
self.J_ax = J_ax * connectivity
self.mu = mu * np.ones(self.n_sites)
else:
raise TypeError("Connectivity matrix is provided, "
"J_eq, J_ax, and mu should be floats")
However, later in the code, self.mu is reassigned:
This overrides the earlier initialization and can collapse self.mu into a scalar array when mu = 0.
Steps to Reproduce
- Provide a valid
connectivity matrix.
- Use scalar values for
J_eq, J_ax, and mu (e.g., mu = 0).
- Call:
h1 = ham.generate_one_body_integral(dense=True, basis='spinorbital basis')
Observed Behavior
A TypeError is raised:
TypeError: mu array has wrong basis
This occurs due to:
if self.mu.shape != (2 * self.n_sites,) and \
self.mu.shape == (self.n_sites,):
mu = np.hstack([self.mu, self.mu])
else:
raise TypeError("mu array has wrong basis")
Because self.mu becomes a scalar array after reassignment, it no longer satisfies the expected shape conditions.
Expected Behavior
- When
connectivity is provided and mu is scalar, self.mu should remain an array of shape (n_sites,).
- It should not be overwritten later in initialization.
generate_one_body_integral should run without errors.
Proposed Fix
Avoid overwriting self.mu after it has been initialized based on connectivity.
if connectivity is not None:
self.connectivity = connectivity
self.n_sites = connectivity.shape[0]
if isinstance(J_eq, (int, float)):
self.J_eq = J_eq * connectivity
self.J_ax = J_ax * connectivity
self.mu = mu * np.ones(self.n_sites)
else:
raise TypeError("Connectivity matrix is provided, "
"J_eq, J_ax, and mu should be floats")
else:
if isinstance(J_eq, np.ndarray) and \
isinstance(J_ax, np.ndarray) and \
isinstance(mu, np.ndarray) and \
J_eq.shape == J_ax.shape and \
mu.shape[0] == J_eq.shape[0]:
self.n_sites = J_eq.shape[0]
self.J_eq = J_eq
self.J_ax = J_ax
self.mu = mu
else:
raise TypeError("J_eq and J_ax should be numpy arrays of the same shape")
Can anyone please help me with this issue?
🐛 Bug:
mureassignment breaks shape handling inHamHeisenbergwhen using connectivityDescription
When using the$J^{ax}$ $J^{eq}$ , and $\mu$ as numpy arrays. as mentioned in the tutorials.
HamHeisenbergclass with aconnectivitymatrix and scalar parameters (J_eq,J_ax,mu), the initialization logic appears to incorrectly overwriteself.mu, leading to shape inconsistencies and runtime errors during one-body integral generation. It seems to me that it was added for the second method for defining the Hamiltonian by providing exchange couplings,Relevant Code
The issue seems to originate from:
However, later in the code,
self.muis reassigned:This overrides the earlier initialization and can collapse
self.muinto a scalar array whenmu = 0.Steps to Reproduce
connectivitymatrix.J_eq,J_ax, andmu(e.g.,mu = 0).Observed Behavior
A
TypeErroris raised:This occurs due to:
Because
self.mubecomes a scalar array after reassignment, it no longer satisfies the expected shape conditions.Expected Behavior
connectivityis provided andmuis scalar,self.mushould remain an array of shape(n_sites,).generate_one_body_integralshould run without errors.Proposed Fix
Avoid overwriting
self.muafter it has been initialized based onconnectivity.Can anyone please help me with this issue?