Skip to content

Commit 01e82bf

Browse files
authored
Adjust grid_device for optimization (#7693)
- Slightly optimize validation to avoid calling op.qubits several times. This also avoids an isinstance check. - Change instance GateFamily for cirq.I into a type GateFamily for IdentityGate which is faster and more general. - Also add ignore_global_phase=False versions of gatesets. - These gatesets are faster since they do not perform unitary decomposition to check equivalency. - Internal versions will switch to these versions once this change propagates.
1 parent 2beca80 commit 01e82bf

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

cirq-google/cirq_google/devices/grid_device.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@
3636
_SQRT_ISWAP_INV_FSIM_GATE_FAMILY = ops.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV])
3737
_CZ_FSIM_GATE_FAMILY = ops.FSimGateFamily(gates_to_accept=[cirq.CZ])
3838
_SYC_GATE_FAMILY = cirq.GateFamily(ops.SYC)
39+
_SYC_IGNORE_PHASE = cirq.GateFamily(ops.SYC, ignore_global_phase=False)
3940
_SQRT_ISWAP_GATE_FAMILY = cirq.GateFamily(cirq.SQRT_ISWAP)
41+
_SQRT_ISWAP_IGNORE_PHASE = cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False)
4042
_SQRT_ISWAP_INV_GATE_FAMILY = cirq.GateFamily(cirq.SQRT_ISWAP_INV)
43+
_SQRT_ISWAP_INV_IGNORE_PHASE = cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False)
4144
_CZ_GATE_FAMILY = cirq.GateFamily(cirq.CZ)
45+
_CZ_IGNORE_PHASE = cirq.GateFamily(cirq.CZ, ignore_global_phase=False)
4246
_CZ_POW_GATE_FAMILY = cirq.GateFamily(cirq.CZPowGate)
4347

4448

@@ -47,6 +51,7 @@
4751
_CZ_TARGET_GATES = [
4852
_CZ_FSIM_GATE_FAMILY,
4953
_CZ_GATE_FAMILY,
54+
_CZ_IGNORE_PHASE,
5055
_PHASED_XZ_GATE_FAMILY,
5156
_MEASUREMENT_GATE_FAMILY,
5257
]
@@ -56,13 +61,15 @@
5661
_SYC_TARGET_GATES = [
5762
_SYC_FSIM_GATE_FAMILY,
5863
_SYC_GATE_FAMILY,
64+
_SYC_IGNORE_PHASE,
5965
_PHASED_XZ_GATE_FAMILY,
6066
_MEASUREMENT_GATE_FAMILY,
6167
]
6268
# Target gates of `cirq.SqrtIswapTargetGateset`
6369
_SQRT_ISWAP_TARGET_GATES = [
6470
_SQRT_ISWAP_FSIM_GATE_FAMILY,
6571
_SQRT_ISWAP_GATE_FAMILY,
72+
_SQRT_ISWAP_IGNORE_PHASE,
6673
_PHASED_XZ_GATE_FAMILY,
6774
_MEASUREMENT_GATE_FAMILY,
6875
]
@@ -103,25 +110,34 @@ class _GateRepresentations:
103110
# allow users to transform their circuits that include your gate.
104111
_GATES: list[_GateRepresentations] = [
105112
_GateRepresentations(
106-
gate_spec_name='syc', supported_gates=[_SYC_FSIM_GATE_FAMILY, _SYC_GATE_FAMILY]
113+
gate_spec_name='syc',
114+
supported_gates=[_SYC_FSIM_GATE_FAMILY, _SYC_GATE_FAMILY, _SYC_IGNORE_PHASE],
107115
),
108116
_GateRepresentations(
109117
gate_spec_name='sqrt_iswap',
110-
supported_gates=[_SQRT_ISWAP_FSIM_GATE_FAMILY, _SQRT_ISWAP_GATE_FAMILY],
118+
supported_gates=[
119+
_SQRT_ISWAP_FSIM_GATE_FAMILY,
120+
_SQRT_ISWAP_GATE_FAMILY,
121+
_SQRT_ISWAP_IGNORE_PHASE,
122+
],
111123
),
112124
_GateRepresentations(
113125
gate_spec_name='sqrt_iswap_inv',
114-
supported_gates=[_SQRT_ISWAP_INV_FSIM_GATE_FAMILY, _SQRT_ISWAP_INV_GATE_FAMILY],
126+
supported_gates=[
127+
_SQRT_ISWAP_INV_FSIM_GATE_FAMILY,
128+
_SQRT_ISWAP_INV_GATE_FAMILY,
129+
_SQRT_ISWAP_INV_IGNORE_PHASE,
130+
],
115131
),
116132
_GateRepresentations(
117-
gate_spec_name='cz', supported_gates=[_CZ_FSIM_GATE_FAMILY, _CZ_GATE_FAMILY]
133+
gate_spec_name='cz',
134+
supported_gates=[_CZ_FSIM_GATE_FAMILY, _CZ_GATE_FAMILY, _CZ_IGNORE_PHASE],
118135
),
119136
_GateRepresentations(gate_spec_name='cz_pow_gate', supported_gates=[_CZ_POW_GATE_FAMILY]),
120137
_GateRepresentations(
121138
gate_spec_name='phased_xz',
122139
supported_gates=[
123-
# TODO: Extend support to cirq.IdentityGate.
124-
cirq.GateFamily(cirq.I),
140+
cirq.GateFamily(cirq.IdentityGate),
125141
cirq.GateFamily(cirq.PhasedXZGate),
126142
cirq.GateFamily(cirq.XPowGate),
127143
cirq.GateFamily(cirq.YPowGate),
@@ -230,7 +246,7 @@ def _serialize_gateset_and_gate_durations(
230246
(gr for gr in _GATES for gf in gr.supported_gates if gf == gate_family), None
231247
)
232248
if gate_rep is None:
233-
raise ValueError(f'Unrecognized gate: {gate_family}.')
249+
raise ValueError(f'Unrecognized gate: {repr(gate_family)}.')
234250
gate_name = gate_rep.gate_spec_name
235251

236252
# Set gate
@@ -652,22 +668,24 @@ def _validate_operations(self, operations: Iterator[cirq.Operation]) -> None:
652668
qubit_pairs = self._metadata.qubit_pairs
653669
qubits = self._metadata.qubit_set
654670
for operation in operations:
671+
op_qubits = operation.qubits
655672
if operation not in gateset:
656673
raise ValueError(f'Operation {operation} contains a gate which is not supported.')
657674

658-
for q in operation.qubits:
659-
if isinstance(q, ops.Coupler):
660-
if any(qc not in qubits for qc in q.qubits):
661-
raise ValueError(f'Qubits on coupler not on device: {q.qubits}.')
662-
if frozenset(q.qubits) not in qubit_pairs:
663-
raise ValueError(f'Coupler pair is not valid on device: {q.qubits}.')
664-
elif q not in qubits:
665-
raise ValueError(f'Qubit not on device: {q!r}.')
675+
for q in op_qubits:
676+
if q not in qubits:
677+
if isinstance(q, ops.Coupler):
678+
if any(qc not in qubits for qc in q.qubits):
679+
raise ValueError(f'Qubits on coupler not on device: {q.qubits}.')
680+
if frozenset(q.qubits) not in qubit_pairs:
681+
raise ValueError(f'Coupler pair is not valid on device: {q.qubits}.')
682+
else:
683+
raise ValueError(f'Qubit not on device: {q!r}.')
666684

667685
if (
668-
len(operation.qubits) == 2
686+
len(op_qubits) == 2
669687
and not isinstance(operation.gate, _VARIADIC_GATE_TYPES)
670-
and frozenset(operation.qubits) not in qubit_pairs
688+
and frozenset(op_qubits) not in qubit_pairs
671689
):
672690
raise ValueError(f'Qubit pair is not valid on device: {operation.qubits!r}.')
673691

cirq-google/cirq_google/devices/grid_device_test.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,17 @@ def _create_device_spec_with_horizontal_couplings():
9898
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
9999
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
100100
cirq.GateFamily(cirq_google.SYC),
101+
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False),
101102
cirq.GateFamily(cirq.SQRT_ISWAP),
103+
cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False),
102104
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
105+
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False),
103106
cirq.GateFamily(cirq.CZ),
107+
cirq.GateFamily(cirq.CZ, ignore_global_phase=False),
104108
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate),
105109
cirq.GateFamily(cirq.ops.common_gates.XPowGate),
106110
cirq.GateFamily(cirq.ops.common_gates.YPowGate),
107-
cirq.GateFamily(cirq.I),
111+
cirq.GateFamily(cirq.IdentityGate),
108112
cirq.GateFamily(cirq.ops.SingleQubitCliffordGate),
109113
cirq.GateFamily(cirq.ops.HPowGate),
110114
cirq.GateFamily(cirq.ops.phased_x_gate.PhasedXPowGate),
@@ -133,14 +137,18 @@ def _create_device_spec_with_horizontal_couplings():
133137
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]): base_duration * 2,
134138
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]): base_duration * 3,
135139
cirq.GateFamily(cirq_google.SYC): base_duration * 0,
140+
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False): base_duration * 0,
136141
cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1,
142+
cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False): base_duration * 1,
137143
cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2,
144+
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False): base_duration * 2,
138145
cirq.GateFamily(cirq.CZ): base_duration * 3,
146+
cirq.GateFamily(cirq.CZ, ignore_global_phase=False): base_duration * 3,
139147
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
140148
cirq.GateFamily(cirq.ops.common_gates.XPowGate): base_duration * 4,
141149
cirq.GateFamily(cirq.ops.common_gates.YPowGate): base_duration * 4,
142150
cirq.GateFamily(cirq.ops.common_gates.HPowGate): base_duration * 4,
143-
cirq.GateFamily(cirq.I): base_duration * 4,
151+
cirq.GateFamily(cirq.IdentityGate): base_duration * 4,
144152
cirq.GateFamily(cirq.ops.SingleQubitCliffordGate): base_duration * 4,
145153
cirq.GateFamily(cirq.ops.phased_x_gate.PhasedXPowGate): base_duration * 4,
146154
cirq.GateFamily(
@@ -173,13 +181,16 @@ def _create_device_spec_with_horizontal_couplings():
173181
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
174182
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
175183
cirq.GateFamily(cirq_google.SYC),
184+
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False),
176185
cirq.GateFamily(cirq.SQRT_ISWAP),
186+
cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False),
177187
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
188+
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False),
178189
cirq.GateFamily(cirq.CZPowGate),
179190
cirq.ops.common_gates.XPowGate,
180191
cirq.ops.common_gates.YPowGate,
181192
cirq.ops.common_gates.HPowGate,
182-
cirq.GateFamily(cirq.I),
193+
cirq.GateFamily(cirq.IdentityGate),
183194
cirq.ops.SingleQubitCliffordGate,
184195
cirq.ops.phased_x_gate.PhasedXPowGate,
185196
cirq.GateFamily(
@@ -205,13 +216,16 @@ def _create_device_spec_with_horizontal_couplings():
205216
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
206217
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
207218
cirq.GateFamily(cirq_google.SYC),
219+
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False),
208220
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
221+
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False),
209222
cirq.GateFamily(cirq.CZPowGate),
210223
cirq.GateFamily(cirq.CZ),
224+
cirq.GateFamily(cirq.CZ, ignore_global_phase=False),
211225
cirq.ops.common_gates.XPowGate,
212226
cirq.ops.common_gates.YPowGate,
213227
cirq.ops.common_gates.HPowGate,
214-
cirq.GateFamily(cirq.I),
228+
cirq.GateFamily(cirq.IdentityGate),
215229
cirq.ops.SingleQubitCliffordGate,
216230
cirq.ops.phased_x_gate.PhasedXPowGate,
217231
cirq.GateFamily(
@@ -238,13 +252,17 @@ def _create_device_spec_with_horizontal_couplings():
238252
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
239253
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
240254
cirq.GateFamily(cirq_google.SYC),
255+
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False),
241256
cirq.GateFamily(cirq.SQRT_ISWAP),
257+
cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False),
242258
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
259+
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False),
243260
cirq.GateFamily(cirq.CZ),
261+
cirq.GateFamily(cirq.CZ, ignore_global_phase=False),
244262
cirq.ops.common_gates.XPowGate,
245263
cirq.ops.common_gates.YPowGate,
246264
cirq.ops.common_gates.HPowGate,
247-
cirq.GateFamily(cirq.I),
265+
cirq.GateFamily(cirq.IdentityGate),
248266
cirq.ops.SingleQubitCliffordGate,
249267
cirq.ops.phased_x_gate.PhasedXPowGate,
250268
cirq.GateFamily(

0 commit comments

Comments
 (0)