Skip to content

mu reassignment breaks shape handling in HamHeisenberg when using connectivity #183

Open

Description

@Manav939

🐛 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:

self.mu = np.array(mu)

This overrides the earlier initialization and can collapse self.mu into a scalar array when mu = 0.


Steps to Reproduce

  1. Provide a valid connectivity matrix.
  2. Use scalar values for J_eq, J_ax, and mu (e.g., mu = 0).
  3. 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?

Activity

  1. PaulWAyers commented on Apr 24, 2026

    @PaulWAyers
    Member

    Your thoughts @RichRick1 ?

  2. self-assigned this
    on Sep 17, 2026
  3. giovanni-br commented on Sep 17, 2026

    @giovanni-br
    Collaborator

    A bit late on the topic...

    I could reproduce the error.
    As you mentioned, the fix appears to be in the indentation of self.mu=mu in HamHeisenberg.__init__.

    I suggest opening a pull request with the fix and a regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions