Skip to content

Conversation

@95-martin-orion
Copy link
Collaborator

Part of #3235. Also contributes to #3438.

This PR adds a SerializableByKey interface which any Cirq object can implement to enable more concise JSON serialization of repeated objects. Enabling this behavior for various types is left to future PRs.

For comparison, consider MatrixGate:

a, b, c = cirq.LineQubit.range(3)
op = cirq.MatrixGate(some_big_matrix)
circuit = cirq.Circuit(op.on(a), op.on(b), op.on(c))
cirq.to_json(circuit)

Without concise serialization for MatrixGate:

Details
{
  "cirq_type": "Circuit",
  "moments": [
    {
      "cirq_type": "GateOperation",
      "gate": {
        "cirq_type": "MatrixGate",
        "matrix": some_big_matrix
      }
      "qubits": a
    }
    {
      "cirq_type": "GateOperation",
      "gate": {
        "cirq_type": "MatrixGate",
        "matrix": some_big_matrix
      }
      "qubits": b
    }
    {
      "cirq_type": "GateOperation",
      "gate": {
        "cirq_type": "MatrixGate",
        "matrix": some_big_matrix
      }
      "qubits": c
    }
  ]
}

With concise serialization for MatrixGate:

Details
# If MatrixGate implements SerializableByKey, this becomes:
{
  "cirq_type": "_ContextualSerialization",
  "context_list": [
    {
      "cirq_type": "_SerializedContext",
      "key": op._serialization_key_()
      "value": {
        "cirq_type": "MatrixGate",
        "matrix": some_big_matrix
      },
    },
    {
      "cirq_type": "Circuit",
      "moments": [
        {
          "cirq_type": "GateOperation",
          "gate": {
            "cirq_type": "_SerializedKey",
            "key": op._serialization_key_()
          },
          "qubits": a
        },
        {
          "cirq_type": "GateOperation",
          "gate": {
            "cirq_type": "_SerializedKey",
            "key": op._serialization_key_()
          },
          "qubits": b
        },
        {
          "cirq_type": "GateOperation",
          "gate": {
            "cirq_type": "_SerializedKey",
            "key": op._serialization_key_()
          },
          "qubits": c
        },
      ]
    }
  ]
}

Note that some_big_matrix appears three times in the current serialization, but only once with concise serialization.

@95-martin-orion 95-martin-orion requested review from a team, cduck and vtomole as code owners December 10, 2020 15:45
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Dec 10, 2020
Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

Thanks for this! Added some comments!

@95-martin-orion 95-martin-orion added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Dec 15, 2020
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Dec 15, 2020
@CirqBot
Copy link
Collaborator

CirqBot commented Dec 15, 2020

Automerge cancelled: A required status check is not present.

Missing statuses: ['Build docs', 'Build protos', 'Changed files test', 'Coverage check', 'Doc test', 'Format check', 'Lint check', 'Misc check', 'Notebook formatting', 'Pytest MacOS (3.7)', 'Pytest MacOS (3.8)', 'Pytest Ubuntu (3.7)', 'Pytest Ubuntu (3.8)', 'Pytest Windows (3.7)', 'Pytest Windows (3.8)', 'Type check', 'cla/google']

@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Dec 15, 2020
@95-martin-orion 95-martin-orion merged commit 9edf75a into quantumlib:master Dec 15, 2020
@95-martin-orion 95-martin-orion deleted the cirq-deep-serial branch December 15, 2020 16:17
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.

3 participants