Skip to content

Commit edd8393

Browse files
authored
Merge serializable_forms and deserialized_forms (#6520)
* Merge serializable_forms and deserialized_forms * Documentation for adding gates * Update documentation for adding CompilationTargetGatesets
1 parent edda3a5 commit edd8393

File tree

2 files changed

+70
-59
lines changed

2 files changed

+70
-59
lines changed

cirq-google/cirq_google/devices/grid_device.py

+47-50
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,31 @@
4949
_SQRT_ISWAP_FSIM_GATE_FAMILY = ops.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP])
5050
_SQRT_ISWAP_INV_FSIM_GATE_FAMILY = ops.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV])
5151
_CZ_FSIM_GATE_FAMILY = ops.FSimGateFamily(gates_to_accept=[cirq.CZ])
52+
_SYC_GATE_FAMILY = cirq.GateFamily(ops.SYC)
53+
_SQRT_ISWAP_GATE_FAMILY = cirq.GateFamily(cirq.SQRT_ISWAP)
54+
_SQRT_ISWAP_INV_GATE_FAMILY = cirq.GateFamily(cirq.SQRT_ISWAP_INV)
55+
_CZ_GATE_FAMILY = cirq.GateFamily(cirq.CZ)
5256

5357

5458
# TODO(#5050) Add GlobalPhaseGate
5559
# Target gates of `cirq_google.GoogleCZTargetGateset`.
56-
_CZ_TARGET_GATES = [_CZ_FSIM_GATE_FAMILY, _PHASED_XZ_GATE_FAMILY, _MEASUREMENT_GATE_FAMILY]
60+
_CZ_TARGET_GATES = [
61+
_CZ_FSIM_GATE_FAMILY,
62+
_CZ_GATE_FAMILY,
63+
_PHASED_XZ_GATE_FAMILY,
64+
_MEASUREMENT_GATE_FAMILY,
65+
]
5766
# Target gates of `cirq_google.SycamoreTargetGateset`.
58-
_SYC_TARGET_GATES = [_SYC_FSIM_GATE_FAMILY, _PHASED_XZ_GATE_FAMILY, _MEASUREMENT_GATE_FAMILY]
67+
_SYC_TARGET_GATES = [
68+
_SYC_FSIM_GATE_FAMILY,
69+
_SYC_GATE_FAMILY,
70+
_PHASED_XZ_GATE_FAMILY,
71+
_MEASUREMENT_GATE_FAMILY,
72+
]
5973
# Target gates of `cirq.SqrtIswapTargetGateset`
6074
_SQRT_ISWAP_TARGET_GATES = [
6175
_SQRT_ISWAP_FSIM_GATE_FAMILY,
76+
_SQRT_ISWAP_GATE_FAMILY,
6277
_PHASED_XZ_GATE_FAMILY,
6378
_MEASUREMENT_GATE_FAMILY,
6479
]
@@ -77,51 +92,44 @@ class _GateRepresentations:
7792
7893
Attributes:
7994
gate_spec_name: The name of gate type in `GateSpecification`.
80-
deserialized_forms: Gate representations to be included when the corresponding
81-
`GateSpecification` gate type is deserialized into gatesets and gate durations.
82-
serializable_forms: GateFamilies used to check whether a given gate can be serialized to the
83-
gate type in this _GateRepresentation.
95+
supported_gates: A list of gates that can be serialized into the `GateSpecification` with
96+
the matching name.
8497
"""
8598

8699
gate_spec_name: str
87-
deserialized_forms: List[GateOrFamily]
88-
serializable_forms: List[cirq.GateFamily]
100+
supported_gates: List[cirq.GateFamily]
101+
89102

103+
# Gates recognized by the GridDevice class. This controls the (de)serialization between
104+
# `DeviceSpecification.valid_gates` and `cirq.Gateset`.
90105

91-
"""Valid gates for a GridDevice."""
106+
# This is a superset of valid gates for a given `GridDevice` instance. The specific gateset depends
107+
# on the underlying device.
108+
109+
# Edit this list to add support for new gates. If a new `_GateRepresentations` is added, add a new
110+
# `GateSpecification` message in cirq-google/cirq_google/api/v2/device.proto.
111+
112+
# Update `_build_compilation_target_gatesets()` if the gate you are updating affects an existing
113+
# CompilationTargetGateset there, or if you'd like to add another `CompilationTargetGateset` to
114+
# allow users to transform their circuits that include your gate.
92115
_GATES: List[_GateRepresentations] = [
93116
_GateRepresentations(
94-
gate_spec_name='syc',
95-
deserialized_forms=[_SYC_FSIM_GATE_FAMILY],
96-
serializable_forms=[_SYC_FSIM_GATE_FAMILY, cirq.GateFamily(ops.SYC)],
117+
gate_spec_name='syc', supported_gates=[_SYC_FSIM_GATE_FAMILY, _SYC_GATE_FAMILY]
97118
),
98119
_GateRepresentations(
99120
gate_spec_name='sqrt_iswap',
100-
deserialized_forms=[_SQRT_ISWAP_FSIM_GATE_FAMILY],
101-
serializable_forms=[_SQRT_ISWAP_FSIM_GATE_FAMILY, cirq.GateFamily(cirq.SQRT_ISWAP)],
121+
supported_gates=[_SQRT_ISWAP_FSIM_GATE_FAMILY, _SQRT_ISWAP_GATE_FAMILY],
102122
),
103123
_GateRepresentations(
104124
gate_spec_name='sqrt_iswap_inv',
105-
deserialized_forms=[_SQRT_ISWAP_INV_FSIM_GATE_FAMILY],
106-
serializable_forms=[_SQRT_ISWAP_INV_FSIM_GATE_FAMILY, cirq.GateFamily(cirq.SQRT_ISWAP_INV)],
125+
supported_gates=[_SQRT_ISWAP_INV_FSIM_GATE_FAMILY, _SQRT_ISWAP_INV_GATE_FAMILY],
107126
),
108127
_GateRepresentations(
109-
gate_spec_name='cz',
110-
deserialized_forms=[_CZ_FSIM_GATE_FAMILY],
111-
serializable_forms=[_CZ_FSIM_GATE_FAMILY, cirq.GateFamily(cirq.CZ)],
128+
gate_spec_name='cz', supported_gates=[_CZ_FSIM_GATE_FAMILY, _CZ_GATE_FAMILY]
112129
),
113130
_GateRepresentations(
114131
gate_spec_name='phased_xz',
115-
deserialized_forms=[
116-
cirq.PhasedXZGate,
117-
cirq.XPowGate,
118-
cirq.YPowGate,
119-
cirq.PhasedXPowGate,
120-
cirq.HPowGate,
121-
cirq.GateFamily(cirq.I),
122-
cirq.ops.SingleQubitCliffordGate,
123-
],
124-
serializable_forms=[
132+
supported_gates=[
125133
# TODO: Extend support to cirq.IdentityGate.
126134
cirq.GateFamily(cirq.I),
127135
cirq.GateFamily(cirq.PhasedXZGate),
@@ -134,29 +142,20 @@ class _GateRepresentations:
134142
),
135143
_GateRepresentations(
136144
gate_spec_name='virtual_zpow',
137-
deserialized_forms=[cirq.GateFamily(cirq.ZPowGate, tags_to_ignore=[ops.PhysicalZTag()])],
138-
serializable_forms=[cirq.GateFamily(cirq.ZPowGate, tags_to_ignore=[ops.PhysicalZTag()])],
145+
supported_gates=[cirq.GateFamily(cirq.ZPowGate, tags_to_ignore=[ops.PhysicalZTag()])],
139146
),
140147
_GateRepresentations(
141148
gate_spec_name='physical_zpow',
142-
deserialized_forms=[cirq.GateFamily(cirq.ZPowGate, tags_to_accept=[ops.PhysicalZTag()])],
143-
serializable_forms=[cirq.GateFamily(cirq.ZPowGate, tags_to_accept=[ops.PhysicalZTag()])],
149+
supported_gates=[cirq.GateFamily(cirq.ZPowGate, tags_to_accept=[ops.PhysicalZTag()])],
144150
),
145151
_GateRepresentations(
146152
gate_spec_name='coupler_pulse',
147-
deserialized_forms=[experimental_ops.CouplerPulse],
148-
serializable_forms=[cirq.GateFamily(experimental_ops.CouplerPulse)],
149-
),
150-
_GateRepresentations(
151-
gate_spec_name='meas',
152-
deserialized_forms=[cirq.MeasurementGate],
153-
serializable_forms=[cirq.GateFamily(cirq.MeasurementGate)],
153+
supported_gates=[cirq.GateFamily(experimental_ops.CouplerPulse)],
154154
),
155155
_GateRepresentations(
156-
gate_spec_name='wait',
157-
deserialized_forms=[cirq.WaitGate],
158-
serializable_forms=[cirq.GateFamily(cirq.WaitGate)],
156+
gate_spec_name='meas', supported_gates=[cirq.GateFamily(cirq.MeasurementGate)]
159157
),
158+
_GateRepresentations(gate_spec_name='wait', supported_gates=[cirq.GateFamily(cirq.WaitGate)]),
160159
]
161160

162161

@@ -216,7 +215,7 @@ def _serialize_gateset_and_gate_durations(
216215
for gate_family in gateset.gates:
217216
gate_spec = v2.device_pb2.GateSpecification()
218217
gate_rep = next(
219-
(gr for gr in _GATES for gf in gr.serializable_forms if gf == gate_family), None
218+
(gr for gr in _GATES for gf in gr.supported_gates if gf == gate_family), None
220219
)
221220
if gate_rep is None:
222221
raise ValueError(f'Unrecognized gate: {gate_family}.')
@@ -228,13 +227,13 @@ def _serialize_gateset_and_gate_durations(
228227
# Set gate duration
229228
gate_durations_picos = {
230229
int(gate_durations[gf].total_picos())
231-
for gf in gate_rep.serializable_forms
230+
for gf in gate_rep.supported_gates
232231
if gf in gate_durations
233232
}
234233
if len(gate_durations_picos) > 1:
235234
raise ValueError(
236235
'Multiple gate families in the following list exist in the gate duration dict, and '
237-
f'they are expected to have the same duration value: {gate_rep.serializable_forms}'
236+
f'they are expected to have the same duration value: {gate_rep.supported_gates}'
238237
)
239238
elif len(gate_durations_picos) == 1:
240239
gate_spec.gate_duration_picos = gate_durations_picos.pop()
@@ -269,10 +268,8 @@ def _deserialize_gateset_and_gate_durations(
269268
)
270269
continue
271270

272-
gates_list.extend(gate_rep.deserialized_forms)
273-
for g in gate_rep.deserialized_forms:
274-
if not isinstance(g, cirq.GateFamily):
275-
g = cirq.GateFamily(g)
271+
gates_list.extend(gate_rep.supported_gates)
272+
for g in gate_rep.supported_gates:
276273
gate_durations[g] = cirq.Duration(picos=gate_spec.gate_duration_picos)
277274

278275
# TODO(#5050) Add GlobalPhaseGate support

cirq-google/cirq_google/devices/grid_device_test.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,26 @@ def _create_device_spec_with_horizontal_couplings():
8989
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
9090
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
9191
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
92-
cirq.ops.phased_x_z_gate.PhasedXZGate,
93-
cirq.ops.common_gates.XPowGate,
94-
cirq.ops.common_gates.YPowGate,
92+
cirq.GateFamily(cirq_google.SYC),
93+
cirq.GateFamily(cirq.SQRT_ISWAP),
94+
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
95+
cirq.GateFamily(cirq.CZ),
96+
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate),
97+
cirq.GateFamily(cirq.ops.common_gates.XPowGate),
98+
cirq.GateFamily(cirq.ops.common_gates.YPowGate),
9599
cirq.GateFamily(cirq.I),
96-
cirq.ops.SingleQubitCliffordGate,
97-
cirq.ops.HPowGate,
98-
cirq.ops.phased_x_gate.PhasedXPowGate,
100+
cirq.GateFamily(cirq.ops.SingleQubitCliffordGate),
101+
cirq.GateFamily(cirq.ops.HPowGate),
102+
cirq.GateFamily(cirq.ops.phased_x_gate.PhasedXPowGate),
99103
cirq.GateFamily(
100104
cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
101105
),
102106
cirq.GateFamily(
103107
cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
104108
),
105-
cirq_google.experimental.ops.coupler_pulse.CouplerPulse,
106-
cirq.ops.measurement_gate.MeasurementGate,
107-
cirq.ops.wait_gate.WaitGate,
109+
cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse),
110+
cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate),
111+
cirq.GateFamily(cirq.ops.wait_gate.WaitGate),
108112
)
109113

110114
base_duration = cirq.Duration(picos=1_000)
@@ -113,6 +117,10 @@ def _create_device_spec_with_horizontal_couplings():
113117
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]): base_duration * 1,
114118
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]): base_duration * 2,
115119
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]): base_duration * 3,
120+
cirq.GateFamily(cirq_google.SYC): base_duration * 0,
121+
cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1,
122+
cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2,
123+
cirq.GateFamily(cirq.CZ): base_duration * 3,
116124
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
117125
cirq.GateFamily(cirq.ops.common_gates.XPowGate): base_duration * 4,
118126
cirq.GateFamily(cirq.ops.common_gates.YPowGate): base_duration * 4,
@@ -139,6 +147,9 @@ def _create_device_spec_with_horizontal_couplings():
139147
cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]),
140148
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
141149
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
150+
cirq.GateFamily(cirq_google.SYC),
151+
cirq.GateFamily(cirq.SQRT_ISWAP),
152+
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
142153
cirq.ops.common_gates.XPowGate,
143154
cirq.ops.common_gates.YPowGate,
144155
cirq.ops.common_gates.HPowGate,
@@ -161,6 +172,9 @@ def _create_device_spec_with_horizontal_couplings():
161172
cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]),
162173
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
163174
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
175+
cirq.GateFamily(cirq_google.SYC),
176+
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
177+
cirq.GateFamily(cirq.CZ),
164178
cirq.ops.common_gates.XPowGate,
165179
cirq.ops.common_gates.YPowGate,
166180
cirq.ops.common_gates.HPowGate,

0 commit comments

Comments
 (0)