Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messaging around Pauli string parsing #1008

Open
karalekas opened this issue Sep 19, 2019 · 0 comments
Open

Improve error messaging around Pauli string parsing #1008

karalekas opened this issue Sep 19, 2019 · 0 comments
Labels
bug 🐛 An issue that needs fixing. good first issue 👶 A place to get started.

Comments

@karalekas
Copy link
Contributor

karalekas commented Sep 19, 2019

Following on from #1007, @jlbosse also improved in #984 some of the error messages for PauliSum and PauliTerm objects, but they certainly could continue to improve. A valid approach would be to separate errors into those concerned with operators and those concerned with coefficients, and to create custom exceptions that point users in the right direction for creating a valid object from a Pauli string.

In [5]: PauliTerm.from_compact_str("(1.0+9i)*X0")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/code/rigetti/github/pyquil/pyquil/paulis.py in from_compact_str(cls, str_pauli_term)
    371         try:
--> 372             coef = float(str_coef)
    373         except ValueError:

ValueError: could not convert string to float: '(1.0+9i)'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~/code/rigetti/github/pyquil/pyquil/paulis.py in from_compact_str(cls, str_pauli_term)
    374             try:
--> 375                 coef = complex(str_coef)
    376             except ValueError:

ValueError: complex() arg is a malformed string

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-5-e7b8a524abb7> in <module>
----> 1 PauliTerm.from_compact_str("(1.0+9i)*X0")

~/code/rigetti/github/pyquil/pyquil/paulis.py in from_compact_str(cls, str_pauli_term)
    375                 coef = complex(str_coef)
    376             except ValueError:
--> 377                 raise ValueError("Could not parse the coefficient "
    378                                  f"{str_coef}")
    379

ValueError: Could not parse the coefficient (1.0+9i)

In [6]: PauliTerm.from_compact_str("(1.0+0j)*A0")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-c2a2c5e2d3b7> in <module>
----> 1 PauliTerm.from_compact_str("(1.0+0j)*A0")

~/code/rigetti/github/pyquil/pyquil/paulis.py in from_compact_str(cls, str_pauli_term)
    385         str_op = re.sub(r'\*', '', str_op)
    386         if not re.match(r'^(([XYZ])(\d+))+$', str_op):
--> 387             raise ValueError(f"Could not parse operator string {str_op}. "
    388                              r"It should match ^(([XYZ])(\d+))+$")
    389

ValueError: Could not parse operator string A0. It should match ^(([XYZ])(\d+))+$
@karalekas karalekas added bug 🐛 An issue that needs fixing. good first issue 👶 A place to get started. labels Sep 19, 2019
@dwillmer dwillmer mentioned this issue Dec 31, 2020
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An issue that needs fixing. good first issue 👶 A place to get started.
Projects
None yet
Development

No branches or pull requests

1 participant