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

Add newly serializable gates to supported grid device gates #6499

Merged
merged 9 commits into from
Mar 20, 2024

Conversation

NoureldinYosri
Copy link
Collaborator

followup to #6479

@CirqBot CirqBot added the Size: XS <10 lines changed label Mar 13, 2024
@NoureldinYosri
Copy link
Collaborator Author

NoureldinYosri commented Mar 13, 2024

@verult @wcourtney is this what is needed to pass validation steps like ?

INVALID_PROGRAM: Operation I(q(9, 4)) contains a gate which is not supported.

@wcourtney
Copy link
Collaborator

@verult @wcourtney is this what is needed to pass validation steps like ?

INVALID_PROGRAM: Operation I(q(9, 4)) contains a gate which is not supported.

This validation happens client side using the gate set provided on the device description:

raise ValueError(f'Operation {operation} contains a gate which is not supported.')

The gateset is currently just a static superset of all supported gates, configured here, but should eventually rely on the gates specific to the device config.

@CirqBot CirqBot added the size: S 10< lines changed <50 label Mar 15, 2024
@NoureldinYosri
Copy link
Collaborator Author

NoureldinYosri commented Mar 15, 2024

I think we are talking about different things, the same gate/gateset can have different preresentations in Cirq. I think this is why we have serializable_forms and deserialized_forms

class _GateRepresentations:
"""Contains equivalent representations of a gate in both DeviceSpecification and GridDevice.
Attributes:
gate_spec_name: The name of gate type in `GateSpecification`.
deserialized_forms: Gate representations to be included when the corresponding
`GateSpecification` gate type is deserialized into gatesets and gate durations.
serializable_forms: GateFamilies used to check whether a given gate can be serialized to the
gate type in this _GateRepresentation.
"""
gate_spec_name: str
deserialized_forms: List[GateOrFamily]
serializable_forms: List[cirq.GateFamily]

deserialized_forms=[_SQRT_ISWAP_FSIM_GATE_FAMILY],
serializable_forms=[_SQRT_ISWAP_FSIM_GATE_FAMILY, cirq.GateFamily(cirq.SQRT_ISWAP)],

A gate failes validation if it's not a member of the serializable_forms of any _GateRepresentations

gate_rep = next((gr for gr in _GATES if gr.gate_spec_name == gate_name), None)
if gate_rep is None: # pragma: no cover
warnings.warn(
f"The DeviceSpecification contains the gate '{gate_name}' which is not recognized"
" by Cirq and will be ignored. This may be due to an out-of-date Cirq version.",
UserWarning,
)
continue
gates_list.extend(gate_rep.deserialized_forms)

This PR just says that cirq.I, cirq.HPowGate, cirq.ops.SinqleQubitCliffordGate can be passed along as XZ gates, we can change this later if we want.

Copy link

codecov bot commented Mar 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.76%. Comparing base (a2425eb) to head (0dc1a1c).
Report is 3 commits behind head on main.

❗ Current head 0dc1a1c differs from pull request most recent head 668c683. Consider uploading reports for the commit 668c683 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6499      +/-   ##
==========================================
- Coverage   97.76%   97.76%   -0.01%     
==========================================
  Files        1105     1105              
  Lines       95130    95030     -100     
==========================================
- Hits        93001    92902      -99     
+ Misses       2129     2128       -1     

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

@NoureldinYosri NoureldinYosri changed the title Add newly serializable gates to quantum engine gates Add newly serializable gates to supported grid device gates Mar 15, 2024
@wcourtney
Copy link
Collaborator

I think we are talking about different things, the same gate/gateset can have different preresentations in Cirq. I think this is why we have serializable_forms and deserialized_forms

Looking at the only usage of serializable_forms and deserialized_forms - in _serialize_gateset_and_gate_durations and _deserialize_gateset_and_gate_durations (respectively) - it looks like those lists are used to (de)serialize gatesets rather than operations.

@verult has more context about the gateset design

@verult
Copy link
Collaborator

verult commented Mar 19, 2024

GridDevice uses the metadata.gateset attribute to validate circuits. On the server side, this gateset is first constructed using the default gateset @wcourtney mentioned, but as part of instantiation, GridDevice uses the proto serialization and deserialization logic to compute the "canonical" gateset, which includes the most general GateFamilies possible for the provided gateset. The GridDevice instance on the cirq_google client side also uses these canonical gatesets.

serializable_forms is used to check whether a particular Cirq gate can be serialized to the corresponding GateSpecification proto message. deserialized_forms is the collection of gates and GateFamilies which will populate the gateset when the message is deserialized. So to make a gate pass validation, it needs to be added to both serializable_forms and deserialized_forms.

IIRC the main reason why the two fields are distinct is that ops.FSimGateFamily(gates_to_accept=[cirq.CZ]) does not accept the gate family cirq.GateFamily(cirq.CZ). We could reconsider this constraint if this makes adding gates harder.

Apologies for the confusion - I'm planning to add documentation for adding gates soon.

@NoureldinYosri
Copy link
Collaborator Author

@verult PTAL

Copy link
Collaborator

@verult verult left a comment

Choose a reason for hiding this comment

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

Last bit of change, everything else lgtm

cirq-google/cirq_google/devices/grid_device.py Outdated Show resolved Hide resolved
@NoureldinYosri NoureldinYosri requested a review from verult March 20, 2024 20:32
@NoureldinYosri NoureldinYosri enabled auto-merge (squash) March 20, 2024 20:33
@@ -118,11 +118,12 @@ class _GateRepresentations:
cirq.YPowGate,
cirq.PhasedXPowGate,
cirq.HPowGate,
cirq.IdentityGate,
cirq.GateFamily(cirq.I),
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Just cirq.I would be good, to be consistent with the other deserialized_forms. These are what we want to display in the gateset object, so here we keep it as simple as possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cirq.I is an object (an instance of cirq.IdentityGate), the others are classes. when I use cirq.I the code breaks because it assumes classes.

@NoureldinYosri NoureldinYosri merged commit 527af77 into quantumlib:main Mar 20, 2024
29 checks passed
jselig-rigetti pushed a commit to jselig-rigetti/Cirq that referenced this pull request May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50 Size: XS <10 lines changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants