-
Notifications
You must be signed in to change notification settings - Fork 355
feat: Expressions without parameters have improved compatibility with numpy. #1757
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
Changes from 3 commits
45811d2
2d52fe0
a0980f9
c2f693a
541629f
050efae
e601852
3e585b5
b4b6ac0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -537,6 +537,51 @@ def __neg__(self) -> "Mul": | |
| def _substitute(self, d: Any) -> ExpressionDesignator: | ||
| return self | ||
|
|
||
| def _evaluate(self) -> np.complex128: | ||
| """ | ||
| Attempts to evaluate the expression to by simplifying it to a complex number. | ||
|
|
||
| Expression simplification can be slow, especially for large recursive expressions. | ||
| This method will raise a ValueError if the expression cannot be simplified to a complex | ||
| number. | ||
| """ | ||
| expr = quil_rs_expr.Expression.parse(str(self)) | ||
| expr.simplify() # type: ignore | ||
| if not expr.is_number(): | ||
| raise ValueError(f"Cannot evaluate expression {self} to a number. Got {expr}.") | ||
| return np.complex128(expr.to_number()) | ||
|
|
||
| def __float__(self) -> float: | ||
| """ | ||
| Returns a copy of the expression as a float by attempting to simplify the expression. | ||
|
|
||
| Expression simplification can be slow, especially for large recursive expressions. | ||
| This cast will raise a ValueError if simplification doesn't result in a real number. | ||
| """ | ||
| value = self._evaluate() | ||
| if value.imag != 0: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| raise ValueError(f"Cannot convert complex value with non-zero imaginary value to float: {value}") | ||
| return float(value.real) | ||
|
|
||
| def __array__(self, dtype: Optional[np.dtype] = None) -> np.ndarray: | ||
| """ | ||
| Implements the numpy array protocol for this expression. | ||
|
|
||
| If the dtype is not object, then there will be an attempt to simplify the expression to a | ||
| complex number. If the expression cannot be simplified to one, then fallback to the | ||
| object representation of the expression. | ||
|
|
||
| Note that expression simplification can be slow for large recursive expressions. | ||
| """ | ||
| try: | ||
| if dtype != object: | ||
| return np.asarray(self._evaluate(), dtype=dtype) | ||
| raise ValueError | ||
| except ValueError: | ||
|
MarquessV marked this conversation as resolved.
|
||
| # Note: The `None` here is a placeholder for the expression in the numpy array. | ||
| # The expression instance will still be accessible in the array. | ||
|
MarquessV marked this conversation as resolved.
|
||
| return np.array(None, dtype=object) | ||
|
MarquessV marked this conversation as resolved.
|
||
|
|
||
|
|
||
| ParameterSubstitutionsMapDesignator = Mapping[Union["Parameter", "MemoryReference"], ExpressionValueDesignator] | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.