Repository: PennyLaneAI/pennylane
Pull request #3816 (2023-02-24 02:31:55)

Copy one-electron integrals inside `_chemist_transform`
**Context:** one-body tensor wasn't being copied in `qml.qchem._chemist_transform`, which lead to it being changed (in place) whenever `qml.basis_rotation` was being called.

**Description of the Change:** added `.copy()`, when building the `chemist_one_body_coeffs` when one-body tensors are provided. 

**Benefits:** in-place manipulation of one-body tensors wouldn't happen.

**Possible Drawbacks:**

**Related GitHub Issues:**


github-actions[bot]: Hello. You may have forgotten to update the changelog!
Please edit [`doc/releases/changelog-dev.md`](/PennyLaneAI/pennylane/blob/master/doc/releases/changelog-dev.md) with:
* A one-to-two sentence description of the change. You may include a small working example for new features.
* A link back to this PR.
* Your name (or GitHub username) in the contributors section.
codecov[bot]: # [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3816?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> :exclamation: No coverage uploaded for pull request base (`v0.29.0-rc0@e576bcb`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#section-missing-base-commit).
> The diff coverage is `n/a`.

```diff
@@              Coverage Diff               @@
##             v0.29.0-rc0    #3816   +/-   ##
==============================================
  Coverage               ?   99.72%           
==============================================
  Files                  ?      336           
  Lines                  ?    29458           
  Branches               ?        0           
==============================================
  Hits                   ?    29378           
  Misses                 ?       80           
  Partials               ?        0           
```



Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)

timmysilv: can you add/update a test asserting that the provided `one_body_tensor` is not updated in place?

Patch:
diff --git a/pennylane/qchem/factorization.py b/pennylane/qchem/factorization.py
index 3f67a5785..2c3ae7ea5 100644
--- a/pennylane/qchem/factorization.py
+++ b/pennylane/qchem/factorization.py
@@ -390,7 +390,7 @@ def _chemist_transform(one_body_tensor=None, two_body_tensor=None, spatial_basis
     chemist_two_body_coeffs, chemist_one_body_coeffs = None, None
 
     if one_body_tensor is not None:
-        chemist_one_body_coeffs = one_body_tensor
+        chemist_one_body_coeffs = one_body_tensor.copy()
 
     if two_body_tensor is not None:
         chemist_two_body_coeffs = np.swapaxes(two_body_tensor, 1, 3)


Test patch:
diff --git a/tests/qchem/test_factorization.py b/tests/qchem/test_factorization.py
index 558ef662d..0428366d9 100644
--- a/tests/qchem/test_factorization.py
+++ b/tests/qchem/test_factorization.py
@@ -710,3 +710,11 @@ def test_chemist_transform(
         one_body_tensor=one_body_correction, spatial_basis=spatial_basis
     )
     assert np.allclose(chemist_one_body, one_body_correction)
+
+    chemist_one_body, chemist_two_body = qml.qchem.factorization._chemist_transform(
+        one_body_tensor=one_body_correction,
+        two_body_tensor=two_body_tensor,
+        spatial_basis=spatial_basis,
+    )
+
+    assert np.allclose(one_body_corr, one_body_correction)