Skip to content

Streamline proto serialization of circuits with duplicate moments. #6941

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

Merged
merged 3 commits into from
Jan 16, 2025

Conversation

dstrain115
Copy link
Collaborator

  • Many circuits, in practice, have moments that are repeated in the circuit. For instance, XEB are long circuits with duplicate layers of 2 qubits gates. Long QEC experiments also have duplicate moments.
  • Streamline the proto serialization of these circuits so that these moments are stored only once (in the constants table).

Note that this PR only adds deserialization by default and hides serialization behind a feature flag. Once this is deployed everywhere, then we can enable serialization.

- Many circuits, in practice, have moments that are repeated in the
  circuit.  For instance, XEB are long circuits with duplicate layers
  of 2 qubits gates.  Long QEC experiments also have duplicate moments.
- Streamline the proto serialization of these circuits so that these
moments are stored only once (in the constants table).

Note that this PR only adds deserialization by default and hides
serialization behind a feature flag.  Once this is deployed everywhere,
then we can enable serialization.
@dstrain115 dstrain115 requested review from wcourtney, vtomole, verult and a team as code owners January 10, 2025 23:32
@dstrain115 dstrain115 marked this pull request as draft January 10, 2025 23:32
Copy link

codecov bot commented Jan 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@5ffb3ad). Learn more about missing BASE report.
Report is 37 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6941   +/-   ##
=======================================
  Coverage        ?   97.87%           
=======================================
  Files           ?     1084           
  Lines           ?    94535           
  Branches        ?        0           
=======================================
  Hits            ?    92523           
  Misses          ?     2012           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dstrain115 dstrain115 marked this pull request as ready for review January 13, 2025 19:13
for moment_proto in circuit_proto.moments:
if constant_index := moment_proto.moment_constant_index:
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we split this into creation and condition? we use the constant_index object outside the if condition.. I couldn't find anything explaining when object references defined with := become invalid ... if it's anything like C++ their scope would be only within the if condition

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, the walrus operator just does assignment and condition check. It doesn't have a scope constrained to only the if statement.

if self.use_constants_table_for_moments:
# Add this moment to the constants table
constants.append(v2.program_pb2.Constant(moment_value=moment_proto))
raw_constants[moment] = len(constants) - 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is the constant at 0? ... it seems that the condition in the deserialization logic assume that the moment indicies are positive ... but they are just and index which could be 0, but since the tests pass there seem to be a constant at 0 ... is this by design? and is it always the case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not sure I understand this comment. raw_constants just tells you what the index in the constants table is for each object. It's a repeated proto list, so the index starts at zero. We append to the table in line 139, so there is at least one element, and then we set the index for this element to len(constants) - 1 which is the last index in the list (which is the one we just added).

Copy link
Collaborator

Choose a reason for hiding this comment

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

what I'm trying to say is that in lin 476 there is an if condition that looks like it assumes that the indicies are always $> 0$ otherwise the condition fails when constant_index = 0

            if constant_index:

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I see! Yeah, I think that is a serious issue. I changed the field to optional and now check presence rather than truthiness.

@dstrain115 dstrain115 added this pull request to the merge queue Jan 16, 2025
Merged via the queue into quantumlib:main with commit 57d9d21 Jan 16, 2025
37 checks passed
@dstrain115 dstrain115 deleted the streamline_duplicate_moments branch January 16, 2025 14:44
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…uantumlib#6941)

- Many circuits, in practice, have moments that are repeated in the
circuit. For instance, XEB are long circuits with duplicate layers of 2
qubits gates. Long QEC experiments also have duplicate moments.
- Streamline the proto serialization of these circuits so that these
moments are stored only once (in the constants table).

Note that this PR only adds deserialization by default and hides
serialization behind a feature flag. Once this is deployed everywhere,
then we can enable serialization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants