Skip to content

Commit

Permalink
rename string function (#4278)
Browse files Browse the repository at this point in the history
  • Loading branch information
soranjh authored and mudit2812 committed Jun 21, 2023
1 parent 2161756 commit 1e309e9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion doc/releases/changelog-0.31.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
`qml.FermiC(0)` and `qml.FermiA(3)`.
[(#4200)](https://github.com/PennyLaneAI/pennylane/pull/4200)

* Added the function `string_to_fermi_word` to create a `FermiWord` object from a compact string
* Added the function `from_string` to create a `FermiWord` object from a compact string
representation.
[(#4229)](https://github.com/PennyLaneAI/pennylane/pull/4229)

Expand Down
2 changes: 1 addition & 1 deletion pennylane/fermi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@


from .conversion import jordan_wigner
from .fermionic import FermiWord, FermiC, FermiA, FermiSentence, string_to_fermi_word
from .fermionic import FermiWord, FermiC, FermiA, FermiSentence, from_string
10 changes: 5 additions & 5 deletions pennylane/fermi/fermionic.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def simplify(self, tol=1e-8):
del self[fw]


def string_to_fermi_word(fermi_string):
def from_string(fermi_string):
r"""Return a fermionic operator object from its string representation.
The string representation is a compact format that uses the orbital index and ``'+'`` or ``'-'``
Expand All @@ -480,17 +480,17 @@ def string_to_fermi_word(fermi_string):
**Example**
>>> string_to_fermi_word('0+ 1- 0+ 1-')
>>> from_string('0+ 1- 0+ 1-')
a⁺(0) a(1) a⁺(0) a(1)
>>> string_to_fermi_word('0+ 1 0+ 1')
>>> from_string('0+ 1 0+ 1')
a⁺(0) a(1) a⁺(0) a(1)
>>> string_to_fermi_word('0^ 1 0^ 1')
>>> from_string('0^ 1 0^ 1')
a⁺(0) a(1) a⁺(0) a(1)
>>> op1 = FermiC(0) * FermiA(1) * FermiC(2) * FermiA(3)
>>> op2 = string_to_fermi_word('0+ 1- 2+ 3-')
>>> op2 = from_string('0+ 1- 2+ 3-')
>>> op1 == op2
True
"""
Expand Down
10 changes: 5 additions & 5 deletions tests/fermi/test_fermionic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


from pennylane import numpy as pnp
from pennylane.fermi.fermionic import FermiSentence, FermiWord, string_to_fermi_word
from pennylane.fermi.fermionic import FermiSentence, FermiWord, from_string

# pylint: disable=too-many-public-methods

Expand Down Expand Up @@ -994,18 +994,18 @@ def test_rmul_error(self, fs, bad_type):
)

@pytest.mark.parametrize("string, result_fw", tup_fw_string)
def test_string_to_fermi_word(self, string, result_fw):
assert string_to_fermi_word(string) == result_fw
def test_from_string(self, string, result_fw):
assert from_string(string) == result_fw

tup_fw_string_error = (
"0+ a-",
"0+ 1-? 3+ 4-",
)

@pytest.mark.parametrize("string", tup_fw_string_error)
def test_string_to_fermi_word_error(self, string):
def test_from_string_error(self, string):
with pytest.raises(ValueError, match="Invalid character encountered in string "):
string_to_fermi_word(string) # pylint: disable=pointless-statement
from_string(string) # pylint: disable=pointless-statement

@pytest.mark.parametrize(
"method_name", ("__add__", "__sub__", "__mul__", "__radd__", "__rsub__", "__rmul__")
Expand Down

0 comments on commit 1e309e9

Please sign in to comment.