Skip to content

Support for stimcirq gates and operations in cirq_google protos#7101

Merged
dstrain115 merged 14 commits intoquantumlib:mainfrom
dstrain115:deserialize_stimcirq_gates
Mar 7, 2025
Merged

Support for stimcirq gates and operations in cirq_google protos#7101
dstrain115 merged 14 commits intoquantumlib:mainfrom
dstrain115:deserialize_stimcirq_gates

Conversation

@dstrain115
Copy link
Copy Markdown
Collaborator

@dstrain115 dstrain115 commented Feb 26, 2025

  • Adds special casing for stimcirq gates and operations.
  • Note that this only supports gates and operations where the arguments can be serialized.
  • Serializing the stimcirq gates uses the json dictionary in order to gather arguments from the operations.
  • Tests will only be run if stimcirq is installed
  • This also adds stimcirq as a dev dependency so that we can test the interaction.
  • (end users will not be affected, just developers)

- Adds special casing for stimcirq gates and operations.
- Note that this only supports gates and operations where the arguments
can be serialized.
- Serializing the stimcirq gates uses the json dictionary in order to
gather arguments from the operations.
- Tests will only be run if stimcirq is installed (manual use only)
@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 26, 2025
@gemini-code-assist
Copy link
Copy Markdown

Important

The terms of service for this installation has not been accepted. Please ask the Organization owners to visit the Gemini Code Assist Admin Console to sign it.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.16%. Comparing base (34b9c81) to head (b96e5dd).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7101   +/-   ##
=======================================
  Coverage   98.16%   98.16%           
=======================================
  Files        1093     1093           
  Lines       95420    95456   +36     
=======================================
+ Hits        93665    93701   +36     
  Misses       1755     1755           

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

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dstrain115 dstrain115 marked this pull request as ready for review February 27, 2025 14:57
@dstrain115 dstrain115 requested review from a team, verult, vtomole and wcourtney as code owners February 27, 2025 14:57
@dstrain115 dstrain115 requested a review from senecameeks March 3, 2025 20:00
arg_func_langs.float_arg_to_proto(
gate.q1_detune_mhz, out=msg.couplerpulsegate.q1_detune_mhz
)
elif op.__module__.startswith('stimcirq') or (
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

prefer getattr(op, "__module__", "")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good idea, done.

gate.q1_detune_mhz, out=msg.couplerpulsegate.q1_detune_mhz
)
elif op.__module__.startswith('stimcirq') or (
gate is not None and gate.__module__.startswith('stimcirq')
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can replace this entire line with "getattr(gate, "module", "")" since it handles None values gracefully

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good idea, done.

msg.internalgate.num_qubits = len(stimcirq_obj.qubits)

# Store json_dict objects in gate_args
for k, v in stimcirq_obj._json_dict_().items():
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Instead of relying on the stimcirq object to always have _json_dict_ defined, prefer to use getattr instead. Also from your comment on line 266, it's not clear if stimcirq operations will also always have _json_dict_ defined.

Copy link
Copy Markdown
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 what you are proposing. How do we know which attributes to get from the stimcirq object otherwise? Also, how do we know how to instantiate the appropriate object on deserialization.

I think saying that a stimcirq gate/operation must have a json_dict to be imported/exported by cirq_google protos is probably a reasonable requirement (plus, we control both repos, so we can add it if it's missing).

I did add a value error if this condition is not met though.

op = arg_func_langs.internal_gate_from_proto(operation_proto.internalgate)(*qubits)
parsed_as_stimcirq = False
msg = operation_proto.internalgate
if msg.module == 'stimcirq':
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: Can stimcirq be defined as a constant and used here and above?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@dstrain115 dstrain115 requested a review from senecameeks March 6, 2025 14:04
Copy link
Copy Markdown
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

LGTM with a couple of small tweaks.

op = cirq.ResetChannel(dimension=dimensions)(*qubits)
elif which_gate_type == 'internalgate':
op = arg_func_langs.internal_gate_from_proto(operation_proto.internalgate)(*qubits)
parsed_as_stimcirq = False
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we move this code to a local _deserialize_with_stimcirq_if_installed function which would return Optional[cirq.Operation] - and if None it will fallback to internal_gate_from_proto below?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

After factoring out the import, this code block is pretty short. No one gates have a local function.
Let me know if you still think this is a good idea.

Comment on lines +703 to +704
try:
import stimcirq
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If stimcirq is not installed this would search sys.path for every deserialized gate.

Consider moving this to a @functools.cache decorated function which returns stimcirq.JSON_RESOLVERS_DICT if installed or an empty dictionary otherwise.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think it's only for operations with a module name of stimcirq, but, still, this is a good idea, and done.

Comment thread cirq-google/cirq_google/serialization/circuit_serializer_test.py Outdated
@dstrain115 dstrain115 requested a review from pavoljuhas March 7, 2025 18:40
@dstrain115 dstrain115 added this pull request to the merge queue Mar 7, 2025
Merged via the queue into quantumlib:main with commit 2167552 Mar 7, 2025
38 checks passed
@dstrain115 dstrain115 deleted the deserialize_stimcirq_gates branch March 7, 2025 22:55
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…tumlib#7101)

* Support for stimcirq gates and operations in cirq_google protos

- Adds special casing for stimcirq gates and operations.
- Note that this only supports gates and operations where the arguments
can be serialized.
- Serializing the stimcirq gates uses the json dictionary in order to
gather arguments from the operations.
- Tests will only be run if stimcirq is installed (manual use only)

* Fix some tests.

* Add requirements for stimcirq

* fix coverage

* format

* Address comments.

* Fix coverage.

* format

* Update cirq-google/cirq_google/serialization/circuit_serializer_test.py

Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>

* Move import to cached function

---------

Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: M 50< lines changed <250

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants