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

Support for stimcirq gates and operations in cirq_google protos #7101

Merged
merged 14 commits into from
Mar 7, 2025

Conversation

dstrain115
Copy link
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
Copy link

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.

Copy link

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 wcourtney, vtomole, verult and a team as code owners February 27, 2025 14:57
@dstrain115 dstrain115 requested a review from senecameeks March 3, 2025 20:00
@@ -258,6 +257,23 @@ def _serialize_gate_op(
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
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
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.

@@ -258,6 +257,23 @@ def _serialize_gate_op(
arg_func_langs.float_arg_to_proto(
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
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
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
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
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
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
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
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.

@@ -670,7 +696,31 @@ def _deserialize_gate_op(
raise ValueError(f"dimensions {dimensions} for ResetChannel must be an integer!")
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
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
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
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
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.

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