-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[WIP] Use concise JSON serialization for FrozenCircuit. #3655
Conversation
@Strilanc has pointed out that hashes should not be used for serialization. I'm leaving this PR up for reference, but if we continue with this serialization format I'll need to replace the hash with something else. |
Experiments with a deeply nested circuit, defined as:
show a distinct difference between compression times and sizes for In the results below (collected from a single run), "nested" uses the circuit as described above, while "flattened" uses a decomposition of that circuit re-composed into a single-level circuit.
gzip code and benchmark can be seen in #3662; the benchmark for this PR is identical but with |
For comparison with #3655. Observed benchmark results, running locally: ``` nested: 7.016707049 s size: 299304 flattened: 1.278540381 s size: 20911 ```
This PR modifies the `SerializableByKey` serialization process to assign a unique suffix to each serialized object. This way keys are guaranteed to be unique within a serialized object - we no longer rely on `_serialization_key_` to provide unique values. Keys generated this way are consistent between serializations of the same top-level object, provided that each component object has a consistent serialization. However, the key of an object may change between serializations if the top-level object changes. For example: ``` # Pseudocode - each letter represents a SerializableByKey object. a = [x, y] # suffix of key(x) is "_1" b = [y, x] # suffix of key(x) is "_2" ``` Prerequisite to #3655, which will require updates to remove hashes from serialization keys. Technically a breaking change, but only for unreleased behavior.
Closing in favor of #3815. |
Fixes #3633. This PR applies the following changes:
FrozenCircuit
uses the efficient JSON serialization format.Circuit
andFrozenCircuit
objects. These have no special behavior forCircuit
s, butFrozenCircuit
s use their name and hash value as their serialization key.0x
+ 16 hex characters) in serialization keys. This allows hardcoded JSON to match the JSON generated from code despite hash values not being consistent between Python sessions.Note that since names are preserved across the
Circuit
/FrozenCircuit
boundary, it is possible to e.g. callfreeze()
twice on the sameCircuit
, producing twoFrozenCircuit
s with identical names. This is the desired behavior, as shown below:Circuit
has not been mutated, theFrozenCircuit
s will have matching serialization keys and will be deduped in serialization.Circuit
has been mutated, theFrozenCircuit
s' serialization keys will differ, and they will appear separately in serialization.