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

[WIP] Use concise JSON serialization for FrozenCircuit. #3655

Closed

Conversation

95-martin-orion
Copy link
Collaborator

Fixes #3633. This PR applies the following changes:

  • FrozenCircuit uses the efficient JSON serialization format.
  • Users can optionally assign names to Circuit and FrozenCircuit objects. These have no special behavior for Circuits, but FrozenCircuits use their name and hash value as their serialization key.
  • The JSON serialization test is now agnostic to hash values (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. call freeze() twice on the same Circuit, producing two FrozenCircuits with identical names. This is the desired behavior, as shown below:

  • If the Circuit has not been mutated, the FrozenCircuits will have matching serialization keys and will be deduped in serialization.
  • If the Circuit has been mutated, the FrozenCircuits' serialization keys will differ, and they will appear separately in serialization.

@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Jan 8, 2021
@95-martin-orion
Copy link
Collaborator Author

@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.

@95-martin-orion
Copy link
Collaborator Author

Experiments with a deeply nested circuit, defined as:

cs = [cirq.FrozenCircuit(cirq.H(a), cirq.CX(a, b))]
for i in range(1, 12):
    cs.append(cirq.FrozenCircuit(cs[i-1].to_op(), cs[i-1].to_op()))
test_circuit = cs[11]

show a distinct difference between compression times and sizes for gzip vs. this PR.

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.

value this PR gzip
nested circuits 0.435255354 s 7.016707049 s
serialized size (bytes) 17500 299304
flattened circuit 2.14050902 s 1.278540381 s
serialized size (bytes) 4948515 20911

gzip code and benchmark can be seen in #3662; the benchmark for this PR is identical but with to_json/read_json in place of to_gzip/read_gzip.

CirqBot pushed a commit that referenced this pull request Feb 1, 2021
For comparison with #3655. Observed benchmark results, running locally:
```
nested: 7.016707049 s
size: 299304
flattened: 1.278540381 s
size: 20911
```
CirqBot pushed a commit that referenced this pull request Feb 13, 2021
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.
@95-martin-orion
Copy link
Collaborator Author

Closing in favor of #3815.

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.

Efficient JSON serialization of FrozenCircuit
1 participant