Skip to content

Fixed expectation_computational_basis_state - #1194

Merged
mhucka merged 3 commits into
quantumlib:mainfrom
S-Erik:expectation_computational_basis_state_fix
Mar 13, 2026
Merged

Fixed expectation_computational_basis_state#1194
mhucka merged 3 commits into
quantumlib:mainfrom
S-Erik:expectation_computational_basis_state_fix

Conversation

@S-Erik

@S-Erik S-Erik commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

Trying to fix expectation_computational_basis_state. The implemented fix was proposed in issue #515.
I also added a test that covers the new exception that is raised when the provided operator is normal ordered.

Changes I made:

  1. Checking for occupied orbitals in the inner j-loop:
            for j in range(i + 1, len(occupied_orbitals)):
                if occupied_orbitals[j]:
                    expectation_value -= operator.terms.get(((j, 1), (i, 1), (j, 0), (i, 0)), 0.0)
  1. Making more clear how a list of occupations has to be provided:
            computational basis state (if scipy.sparse vector), or list of
            zeros and ones for occupied and unoccupied orbitals, respectively.
  1. Adding a test that varifies that the new ValueError exception is raised if the provided operator is not normal-ordered:
    def test_expectation_bad_operator_order(self):
        operator = (
            FermionOperator('2^ 2', 1.9)
            + FermionOperator('2^ 1')
            + FermionOperator('2^ 1 2 1^', -1.7)
        )
        state = [0, 1, 1]

        with self.assertRaises(ValueError):
            expectation_computational_basis_state(operator, state)

@google-cla

google-cla Bot commented Feb 1, 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.

@mhucka mhucka self-assigned this Feb 3, 2026
@S-Erik
S-Erik force-pushed the expectation_computational_basis_state_fix branch from 6641a80 to 9ae2967 Compare March 3, 2026 09:58
@mhucka

mhucka commented Mar 12, 2026

Copy link
Copy Markdown
Collaborator

@S-Erik Thank you for this work. Quick request: could you comment on issue #515? Any comment will do, like mentioning you did this PR – it's just for GitHub to allow me to assign you to that issue, to indicate it's being worked on.

@mhucka

mhucka commented Mar 12, 2026

Copy link
Copy Markdown
Collaborator

Regarding the current failures in CI: I'm waiting until I can merge #1204 and then will run CI on this PR again.

@mhucka mhucka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@mhucka
mhucka added this pull request to the merge queue Mar 13, 2026
@mhucka

mhucka commented Mar 13, 2026

Copy link
Copy Markdown
Collaborator

@S-Erik There remained some trivial code formatting issues. In order to expedite things, I took the liberty of pushing a fix to your branch – I hope you don't mind. The result worked and this PR is now in the merge queue. I hope the push to your branch does not cause you any problems on your end.

Thanks again for this contribution.

Merged via the queue into quantumlib:main with commit a4d0bf2 Mar 13, 2026
45 of 46 checks passed
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.

2 participants