rigetti / rigetti/pyquil

Improve error messaging around Pauli string parsing

Open
#1,008 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug :bug: good first issue :baby:
Dominant language
Python
Stars
1.5k
Forks
358
Avg merge
1d 58m
Merged PRs (30d)
4

Description

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+))+$

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pyquil/paulis.py with PauliTerm.from_compact_str and the related PauliSum and PauliTerm parsing paths. Use the examples in this issue to trace coefficient and operator failures, then verify that invalid Pauli strings produce distinct, actionable errors pointing users toward valid input.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.