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

feat: Expressions without parameters have improved compatibility with numpy. #1757

Conversation

MarquessV
Copy link
Contributor

Description

Closes #1682.

This implements the numpy array protocol by leaning on quil-rs expression simplification to get a numpy compatible number from the expression, if possible. If not, we still fallback to the default numpy behavior as to not break existing code.

Checklist

  • The PR targets the master branch
  • The above description motivates these changes.
  • The change is atomic and can be described by a single commit (your PR will be squashed on merge).
  • All changes to code are covered via unit tests.
  • Parameters and return values have type hints with PEP 484 syntax.
  • Functions and classes have useful Sphinx-style docstrings.
  • (New Feature) The docs have been updated accordingly.
  • (Bugfix) The associated issue is referenced above using auto-close keywords.

@MarquessV MarquessV changed the title feat: Expressionx without parameters have improved compatibility with numpy. feat: Expressions without parameters have improved compatibility with numpy. Mar 26, 2024
Copy link

github-actions bot commented Mar 26, 2024

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
7185 6330 88% 87% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
pyquil/paulis.py 98% 🟢
pyquil/quilatom.py 83% 🟢
TOTAL 91% 🟢

updated for commit: b4b6ac0 by action🐍

@MarquessV MarquessV marked this pull request as ready for review March 26, 2024 20:15
@MarquessV MarquessV requested a review from a team as a code owner March 26, 2024 20:15
This cast will raise a ValueError if simplification doesn't result in a real number.
"""
value = self._evaluate()
if value.imag != 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use np.isclose with a fairly moderate threshold here? Perhaps 1e-8?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd thought about this too but decided it wasn't necessary; if the inputs are all real, imag == 0.0 in all of the inputs and there are no complex operations I can think of that would accumulate a rounding error. So I'd be inclined to leave it unless you have a specific case that would trip it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bramathon Any thoughts on the above? Just want to make sure before I merge as-is.

pyquil/quilatom.py Outdated Show resolved Hide resolved
pyquil/quilatom.py Show resolved Hide resolved
pyquil/quilatom.py Show resolved Hide resolved
@MarquessV MarquessV merged commit 4d51725 into master Apr 12, 2024
22 checks passed
@MarquessV MarquessV deleted the 1682-defgate-construction-fails-if-matrix-contains-expressions-but-not-parameters branch April 12, 2024 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DefGate construction fails if matrix contains Expressions but not parameters
4 participants