Repository: PennyLaneAI/pennylane
Pull request #5671 (2024-05-08 18:54:03)

[BUG] Fix a fermi sentence bug in bravyi_kitaev
**Context:**
There is a wrong call in bravyi_kitaev when the input object is FermiSentence, which is only observed for some specific inputs that were not covered by our current tests. The PR fixes the bug and adds an extra test to check the accuracy.

**Description of the Change:**

**Benefits:**

**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://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5671?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 99.68%. Comparing base [(`ab8d739`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/ab8d73907d0fbc48515138afac6fa0aef933766d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) to head [(`ca5edf7`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5671?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #5671      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         412      412              
  Lines       38663    38372     -291     
==========================================
- Hits        38545    38252     -293     
- Misses        118      120       +2     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5671?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 0b381609d..1a667c45a 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -81,10 +81,14 @@
   with circuits with non-commuting measurements.
   [(#5424)](https://github.com/PennyLaneAI/pennylane/pull/5424)
 
+* A correction is added to `bravyi_kitaev` to call the correct function for a FermiSentence input.
+  [(#5671)](https://github.com/PennyLaneAI/pennylane/pull/5671)
+
 <h3>Contributors ✍️</h3>
 
 This release contains contributions from (in alphabetical order):
 
 Pietropaolo Frisoni,
+Soran Jahangiri,
 Christina Lee,
 David Wierichs.
\ No newline at end of file
diff --git a/pennylane/fermi/conversion.py b/pennylane/fermi/conversion.py
index aef3fa8e0..8f27a39d2 100644
--- a/pennylane/fermi/conversion.py
+++ b/pennylane/fermi/conversion.py
@@ -556,7 +556,7 @@ def _(fermi_operator: FermiSentence, n, ps=False, wire_map=None, tol=None):
     qubit_operator = PauliSentence()  # Empty PS as 0 operator to add Pws to
 
     for fw, coeff in fermi_operator.items():
-        fermi_word_as_ps = parity_transform(fw, n, ps=True)
+        fermi_word_as_ps = bravyi_kitaev(fw, n, ps=True)
 
         for pw in fermi_word_as_ps:
             qubit_operator[pw] = qubit_operator[pw] + fermi_word_as_ps[pw] * coeff


Test patch:
diff --git a/tests/fermi/test_bravyi_kitaev.py b/tests/fermi/test_bravyi_kitaev.py
index e04090867..9fffa4af9 100644
--- a/tests/fermi/test_bravyi_kitaev.py
+++ b/tests/fermi/test_bravyi_kitaev.py
@@ -928,6 +928,7 @@ fw1 = FermiWord({(0, 0): "+", (1, 1): "-"})
 fw2 = FermiWord({(0, 0): "+", (1, 0): "-"})
 fw3 = FermiWord({(0, 0): "+", (1, 3): "-", (2, 0): "+", (3, 4): "-"})
 fw4 = FermiWord({})
+fw5 = FermiWord({(0, 3): "+", (1, 2): "-"})
 
 
 def test_empty_fermi_sentence():
@@ -995,6 +996,22 @@ FERMI_AND_PAULI_SENTENCES = [
             }
         ),
     ),
+    (
+        FermiSentence({fw1: -2, fw5: 1j}),
+        4,
+        PauliSentence(
+            {
+                PauliWord({0: "X"}): -0.5,
+                PauliWord({0: "X", 1: "Z"}): 0.5,
+                PauliWord({0: "Y"}): 0.5j,
+                PauliWord({0: "Y", 1: "Z"}): -0.5j,
+                PauliWord({1: "Z", 2: "X", 3: "Z"}): -0.25j,
+                PauliWord({1: "Z", 2: "Y", 3: "Z"}): 0.25,
+                PauliWord({2: "X"}): 0.25j,
+                PauliWord({2: "Y"}): -0.25,
+            }
+        ),
+    ),
 ]