Skip to content

fix(security): 2 improvements across 2 files#1367

Open
tomaioo wants to merge 2 commits into
quantumlib:mainfrom
tomaioo:fix/security/potential-division-by-zero-in-reduced-ha
Open

fix(security): 2 improvements across 2 files#1367
tomaioo wants to merge 2 commits into
quantumlib:mainfrom
tomaioo:fix/security/potential-division-by-zero-in-reduced-ha

Conversation

@tomaioo

@tomaioo tomaioo commented Jun 22, 2026

Copy link
Copy Markdown

Summary

fix(security): 2 improvements across 2 files

Problem

Severity: Medium | File: src/openfermion/chem/reduced_hamiltonian.py:L48

In make_reduced_hamiltonian, the function calculates normalization = 1 / (4 * (n_electrons - 1)) without validating that n_electrons > 1. If n_electrons is 1, this causes a ZeroDivisionError. Additionally, if n_electrons is 0 or negative, the behavior is undefined and could lead to incorrect scientific results.

Solution

Add input validation at the beginning of the function to ensure n_electrons >= 2, raising a ValueError with a descriptive message if not. Consider also validating that n_electrons is a positive integer.

Changes

  • src/openfermion/chem/reduced_hamiltonian.py (modified)
  • src/openfermion/linalg/rdm_reconstruction.py (modified)

tomaioo added 2 commits June 21, 2026 17:09
- Security: Potential Division by Zero in Reduced Hamiltonian
- Security: Missing Input Validation in Valdemoro Reconstruction

Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
- Security: Potential Division by Zero in Reduced Hamiltonian
- Security: Missing Input Validation in Valdemoro Reconstruction

Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
@google-cla

google-cla Bot commented Jun 22, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces validation checks to ensure that n_electrons is at least 2 in both make_reduced_hamiltonian and valdemoro_reconstruction to prevent division by zero. However, in make_reduced_hamiltonian, the validation check was placed before the docstring, which prevents Python from recognizing it as a docstring and breaks documentation generation. This check should be moved after the docstring.

Comment on lines +9 to +12
if n_electrons < 2:
raise ValueError(
'n_electrons must be at least 2 to avoid division by zero.'
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In Python, a function's docstring must be the very first statement in the function body. Placing the if n_electrons < 2: validation check before the docstring prevents Python from recognizing it as a docstring, which breaks documentation generation and tools. Please move this validation block to be after the docstring (after line 39).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant