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

Implement TrialResult JSON serialization #2576

Merged
merged 12 commits into from
Nov 23, 2019

Conversation

kevinsung
Copy link
Collaborator

This is an alternative to #2561 that works for qudits. As mentioned there, it is 8x less space efficient for qubits. We could have a special case to handle binary-valued measurements but maybe we want to be consistent and keep the code simple? I don't know. I should add more test cases but I just wanted to put this out to get feedback on.

@googlebot googlebot added the cla: yes Makes googlebot stop complaining. label Nov 20, 2019
@kevinsung kevinsung changed the title TrialResult JSON serialization that works for qudits (alternative to #2561) Implement TrialResult JSON serialization Nov 20, 2019
@kevinsung
Copy link
Collaborator Author

kevinsung commented Nov 20, 2019

Okay I've added a special case for binary-valued arrays. Also, note that this serialization method does not work when the measurement values are Python ints or arbitrary Python objects, since it uses numpy.save with "allow_pickle" set to False to ensure portability. The measurement values must be fixed-width numerical values. I don't think this is a practically relevant limitation. @Strilanc PTAL.

Copy link
Contributor

@Strilanc Strilanc left a comment

Choose a reason for hiding this comment

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

After looking this over, I think we should do something slightly different.

Instead of special casing the numpy array storage for this single method, we should include np.ndarray as one of our supported types and do all the fancy bitpacking stuff there. That way the benefit will be shared across the library.

This does have some backwards compatibility constraints. We need to continue supporting numpy arrays serialized as a raw list.

@Strilanc
Copy link
Contributor

Rough list of things to be done:

  1. Around like ~260 of json.py, add a case for isinstance(o, np.ndarray). It should call into a helper method that does possibly different things for the different dtypes.

  2. In the big dictionary at the start of json.py, include an np.ndarray entry associated with a method that supports deserializing the bitpacked stuff but also the raw lists.

  3. Add test files to json_test_data directory.

@kevinsung
Copy link
Collaborator Author

kevinsung commented Nov 21, 2019

@Strilanc I'm having trouble with (2.). When the JSON deserializer sees a list it will automatically treat it as such; how do you cause it to trigger specialized code upon encountering a generic list that it has no way of knowing originated as a Numpy array?

@kevinsung
Copy link
Collaborator Author

(2.) may be possible by implementing our own JSONDecoder subclass...

@kevinsung
Copy link
Collaborator Author

But still the issue remains which is that the decoder has no way of knowing whether the list originated from a Numpy array or not.

@mpharrigan
Copy link
Collaborator

Instead of special casing the numpy array storage for this single method, we should include np.ndarray as one of our supported types and do all the fancy bitpacking stuff there.

I'm not sure I agree with this. Decaying to lists by default plays nicest with json. There's automatic interop with other languages and it's eyeball-able. For special cases where you've got a massive ol' chunk of data (particularly bitstrings or wavefunctions or things which aren't eye-ballable anyways) they can be treated as a binary blob

@Strilanc
Copy link
Contributor

Alright, it's fine to just do the one case. We can target specific ones when we run into them, and extract common methods when needed.

@kevinsung
Copy link
Collaborator Author

@Strilanc so we're good to leave this PR as is then?

@kevinsung kevinsung merged commit 6d986e6 into quantumlib:master Nov 23, 2019
@kevinsung kevinsung deleted the trialresult_json_packbits_npy branch November 23, 2019 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants