-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add tuples, ndarrays, and complex numbers to cirq_google proto #7226
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
Add tuples, ndarrays, and complex numbers to cirq_google proto #7226
Conversation
dstrain115
commented
Apr 3, 2025
- Adds support for (potentially nested) tuples of mismatched types
- Adds support for serializing ndarrays
- Adds support for serializing complex numbers
- Adds support for (potentially nested) tuples of mismatched types - Adds support for serializing ndarrays - Adds support for serializing complex numbers
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7226 +/- ##
========================================
Coverage 98.65% 98.65%
========================================
Files 1106 1106
Lines 95869 95969 +100
========================================
+ Hits 94581 94681 +100
Misses 1288 1288 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please enable handling of empty numpy arrays. Also consider storing the list or tuple type in the proto so they can be restored as the same type (can be a follow-up PR).
Otherwise LGTM.
double real_value = 1; | ||
double imaginary_value = 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use real
and imag
for parity with builtin complex and numpy numeric types?
match value.dtype.name: | ||
case 'float64': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can match value.dtype
rather than the name. This would protect against typo in string or a rename of data type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
return ( | ||
arg_value.complex_value.real_value | ||
+ 1j * arg_value.complex_value.imaginary_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return ( | |
arg_value.complex_value.real_value | |
+ 1j * arg_value.complex_value.imaginary_value | |
return complex( | |
arg_value.complex_value.real_value, | |
arg_value.complex_value.imaginary_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
elif isinstance(value, (list, tuple, np.ndarray)): | ||
if len(value): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a round trip this converts (i) uniform-type lists or tuples to lists and (ii) non-uniform lists and tuples to tuples. It also discards empty lists, tuples and arrays, which return as None from round trip.
We can move the array conversion out of the if len(value)
block as _ndarray_to_proto
and _ndarray_from_proto
seem to work with empty arrays.
As for tuples and lists, we could add a sequence_type
enum to the ArgValue
message which could be (UNSPECIFIED, LIST, TUPLE)
and would let us reconstruct either list
or tuple
from a round trip. It would also allow us to express either an empty list or an empty tuple, in such case ArgValue
would be empty except of a (new) sequence_type
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I went ahead and did that, and also added set support while in there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a tiny suggestion.
Co-authored-by: Pavol Juhas <[email protected]>
…umlib#7226) * Add tuples, ndarrays, and complex numbers to cirq_google proto - Adds support for (potentially nested) tuples of mismatched types - Adds support for serializing ndarrays - Adds support for serializing complex numbers * my o my, it's mypy * Fix all the review comments, add set support. * Fix invert mask, which assumes list, so it can accept tuples. * Update cirq-google/cirq_google/serialization/arg_func_langs.py Co-authored-by: Pavol Juhas <[email protected]> * Fix coverage. --------- Co-authored-by: Pavol Juhas <[email protected]>