Skip to content

Avoid mutating optimal Givens input matrices#1344

Open
marko1olo wants to merge 2 commits into
quantumlib:mainfrom
marko1olo:fix-optimal-givens-input-mutation
Open

Avoid mutating optimal Givens input matrices#1344
marko1olo wants to merge 2 commits into
quantumlib:mainfrom
marko1olo:fix-optimal-givens-input-mutation

Conversation

@marko1olo

Copy link
Copy Markdown

Fixes #761.

What changed

  • Copy the caller-provided unitary before the optimal Givens decomposition uses it as an in-place workspace.
  • Add regression coverage proving repeated decompositions with the same matrix leave the input unchanged and produce the same circuit.

Verification

  • .venv\Scripts\python.exe -m pytest -p no:cacheprovider src\openfermion\circuits\primitives\optimal_givens_decomposition_test.py -q
  • .venv\Scripts\python.exe -m black --check --workers 1 src\openfermion\circuits\primitives\optimal_givens_decomposition.py src\openfermion\circuits\primitives\optimal_givens_decomposition_test.py
  • .venv\Scripts\python.exe -B -c "import ast, pathlib; [ast.parse(pathlib.Path(p).read_text(encoding='utf-8'), filename=p) for p in ['src/openfermion/circuits/primitives/optimal_givens_decomposition.py','src/openfermion/circuits/primitives/optimal_givens_decomposition_test.py']]"
  • git diff --check

marko1olo added 2 commits June 6, 2026 11:13
Cover repeated optimal_givens_decomposition calls with the same unitary matrix so the implementation must leave caller-owned arrays unchanged.
Use a private workspace copy for the in-place Givens rotations and phase rewrites so repeated decomposition calls with the same unitary remain deterministic.
@google-cla

google-cla Bot commented Jun 6, 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 ensures that the optimal_givens_decomposition function does not mutate its input unitary matrix by creating a copy of it. A corresponding unit test has been added to verify that the input matrix remains unmodified and that consecutive function calls produce identical circuits. There are no review comments, so I have no feedback to provide.

@marko1olo

Copy link
Copy Markdown
Author

CI note: the two non-CLA failures are the same stochastic VPE test failure, not from the optimal Givens mutation path changed in this PR.

Failing test in both Python code coverage tests and Unit tests (ubuntu-24.04, 1.4.1):

src/openfermion/circuits/vpe_circuits_test.py::test_single_timestep
AssertionError: assert 0.0107... < 0.01

The test comment says the standard deviation is about 0.005 and the bound should pass about 95% of the time, so this appears to be the expected occasional sampling tail just over the 1e-2 threshold.

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.

optimal_givens_decomposition has bad side effects

1 participant