diff --git a/src/openfermion/chem/reduced_hamiltonian.py b/src/openfermion/chem/reduced_hamiltonian.py index 98a58d1ef..42c993b4d 100644 --- a/src/openfermion/chem/reduced_hamiltonian.py +++ b/src/openfermion/chem/reduced_hamiltonian.py @@ -6,6 +6,10 @@ def make_reduced_hamiltonian( molecular_hamiltonian: InteractionOperator, n_electrons: int ) -> InteractionOperator: + if n_electrons < 2: + raise ValueError( + 'n_electrons must be at least 2 to avoid division by zero.' + ) r""" Construct the reduced Hamiltonian. diff --git a/src/openfermion/linalg/rdm_reconstruction.py b/src/openfermion/linalg/rdm_reconstruction.py index 6bd01cc04..3b7819524 100644 --- a/src/openfermion/linalg/rdm_reconstruction.py +++ b/src/openfermion/linalg/rdm_reconstruction.py @@ -16,6 +16,10 @@ def valdemoro_reconstruction(tpdm, n_electrons): Returns: six-tensor reprsenting the three-RDM """ + if n_electrons < 2: + raise ValueError( + 'n_electrons must be at least 2 to avoid division by zero.' + ) opdm = (2 / (n_electrons - 1)) * np.einsum('ijjk', tpdm) unconnected_tpdm = wedge(opdm, opdm, (1, 1), (1, 1)) unconnected_d3 = wedge(opdm, unconnected_tpdm, (1, 1), (2, 2))