Skip to content

Commit

Permalink
Cleaning up docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
willjmax committed Sep 13, 2024
1 parent 6cadb44 commit 4580d9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
21 changes: 14 additions & 7 deletions pennylane/fermi/fermionic.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,18 @@ def to_mat(self, n_orbitals=None, format="dense", buffer_size=None):
)

def shift_operator(self, initial_position, final_position):
r"""Shifts an operator in the Fermi word from ``initial_position`` to ``final_position`` by applying the fermionic anti-commutation relations.
r"""Shifts an operator in the FermiWord from ``initial_position`` to ``final_position`` by applying the fermionic anti-commutation relations.
There are four anti-commutator relations:
1) a(0)a(1) = -a(1)a(0)
2) a⁺(0)a⁺(1) = -a⁺(1)a⁺(0)
3) a(0)a⁺(1) = -a⁺(1)a(0)
4) a(0)a⁺(0) = 1 - a⁺(0)a(0)
See https://en.wikipedia.org/wiki/Creation_and_annihilation_operators#Creation_and_annihilation_operators_in_quantum_field_theories for more information.
Args:
initial_position (int): The position of the operator to be shifted.
final_position (int): The desired position of the operator occupying the initial position.
final_position (int): The desired position of the operator.
Returns:
FermiSentence: The ``FermiSentence`` obtained after applying the anti-commutator relations.
Expand All @@ -341,19 +348,19 @@ def shift_operator(self, initial_position, final_position):
**Example**
>>> w = FermiWord({(0, 0): '+', (1, 1): '-'})
>>> w.commute(0, 1)
>>> w = qml.fermi.FermiWord({(0, 0): '+', (1, 1): '-'})
>>> w.shift_operator(0, 1)
-1 * a(1) a⁺(0)
"""

if not isinstance(initial_position, int) or not isinstance(final_position, int):
raise TypeError("Positions must be integers")
raise TypeError("Positions must be integers.")

if initial_position < 0 or final_position < 0:
raise ValueError("Positions must be positive integers")
raise ValueError("Positions must be positive integers.")

if initial_position > len(self.sorted_dic) - 1 or final_position > len(self.sorted_dic) - 1:
raise ValueError("Positions out of range")
raise ValueError("Positions are out of range.")

if initial_position == final_position:
return FermiSentence({self: 1})
Expand Down
6 changes: 3 additions & 3 deletions tests/fermi/test_fermionic.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ def test_shift_operator(self, fw, i, j, fs):

def test_shift_operator_errors(self):
"""Test that the shift_operator method correctly raises exceptions."""
with pytest.raises(TypeError, match="Positions must be integers"):
with pytest.raises(TypeError, match="Positions must be integers."):
fw8.shift_operator(0.5, 1)

with pytest.raises(ValueError, match="Positions must be positive integers"):
with pytest.raises(ValueError, match="Positions must be positive integers."):
fw8.shift_operator(-1, 0)

with pytest.raises(ValueError, match="Positions out of range"):
with pytest.raises(ValueError, match="Positions are out of range."):
fw8.shift_operator(1, 2)

tup_fw_dag = (
Expand Down

0 comments on commit 4580d9e

Please sign in to comment.