Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions cirq-google/cirq_google/devices/grid_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@
_SQRT_ISWAP_INV_FSIM_GATE_FAMILY = ops.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV])
_CZ_FSIM_GATE_FAMILY = ops.FSimGateFamily(gates_to_accept=[cirq.CZ])
_SYC_GATE_FAMILY = cirq.GateFamily(ops.SYC)
_SYC_IGNORE_PHASE = cirq.GateFamily(ops.SYC, ignore_global_phase=False)
_SQRT_ISWAP_GATE_FAMILY = cirq.GateFamily(cirq.SQRT_ISWAP)
_SQRT_ISWAP_IGNORE_PHASE = cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False)
_SQRT_ISWAP_INV_GATE_FAMILY = cirq.GateFamily(cirq.SQRT_ISWAP_INV)
_SQRT_ISWAP_INV_IGNORE_PHASE = cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False)
_CZ_GATE_FAMILY = cirq.GateFamily(cirq.CZ)
_CZ_IGNORE_PHASE = cirq.GateFamily(cirq.CZ, ignore_global_phase=False)
_CZ_POW_GATE_FAMILY = cirq.GateFamily(cirq.CZPowGate)


Expand All @@ -47,6 +51,7 @@
_CZ_TARGET_GATES = [
_CZ_FSIM_GATE_FAMILY,
_CZ_GATE_FAMILY,
_CZ_IGNORE_PHASE,
_PHASED_XZ_GATE_FAMILY,
_MEASUREMENT_GATE_FAMILY,
]
Expand All @@ -56,13 +61,15 @@
_SYC_TARGET_GATES = [
_SYC_FSIM_GATE_FAMILY,
_SYC_GATE_FAMILY,
_SYC_IGNORE_PHASE,
_PHASED_XZ_GATE_FAMILY,
_MEASUREMENT_GATE_FAMILY,
]
# Target gates of `cirq.SqrtIswapTargetGateset`
_SQRT_ISWAP_TARGET_GATES = [
_SQRT_ISWAP_FSIM_GATE_FAMILY,
_SQRT_ISWAP_GATE_FAMILY,
_SQRT_ISWAP_IGNORE_PHASE,
_PHASED_XZ_GATE_FAMILY,
_MEASUREMENT_GATE_FAMILY,
]
Expand Down Expand Up @@ -103,25 +110,34 @@ class _GateRepresentations:
# allow users to transform their circuits that include your gate.
_GATES: list[_GateRepresentations] = [
_GateRepresentations(
gate_spec_name='syc', supported_gates=[_SYC_FSIM_GATE_FAMILY, _SYC_GATE_FAMILY]
gate_spec_name='syc',
supported_gates=[_SYC_FSIM_GATE_FAMILY, _SYC_GATE_FAMILY, _SYC_IGNORE_PHASE],
),
_GateRepresentations(
gate_spec_name='sqrt_iswap',
supported_gates=[_SQRT_ISWAP_FSIM_GATE_FAMILY, _SQRT_ISWAP_GATE_FAMILY],
supported_gates=[
_SQRT_ISWAP_FSIM_GATE_FAMILY,
_SQRT_ISWAP_GATE_FAMILY,
_SQRT_ISWAP_IGNORE_PHASE,
],
),
_GateRepresentations(
gate_spec_name='sqrt_iswap_inv',
supported_gates=[_SQRT_ISWAP_INV_FSIM_GATE_FAMILY, _SQRT_ISWAP_INV_GATE_FAMILY],
supported_gates=[
_SQRT_ISWAP_INV_FSIM_GATE_FAMILY,
_SQRT_ISWAP_INV_GATE_FAMILY,
_SQRT_ISWAP_INV_IGNORE_PHASE,
],
),
_GateRepresentations(
gate_spec_name='cz', supported_gates=[_CZ_FSIM_GATE_FAMILY, _CZ_GATE_FAMILY]
gate_spec_name='cz',
supported_gates=[_CZ_FSIM_GATE_FAMILY, _CZ_GATE_FAMILY, _CZ_IGNORE_PHASE],
),
_GateRepresentations(gate_spec_name='cz_pow_gate', supported_gates=[_CZ_POW_GATE_FAMILY]),
_GateRepresentations(
gate_spec_name='phased_xz',
supported_gates=[
# TODO: Extend support to cirq.IdentityGate.
cirq.GateFamily(cirq.I),
cirq.GateFamily(cirq.IdentityGate),
cirq.GateFamily(cirq.PhasedXZGate),
cirq.GateFamily(cirq.XPowGate),
cirq.GateFamily(cirq.YPowGate),
Expand Down Expand Up @@ -230,7 +246,7 @@ def _serialize_gateset_and_gate_durations(
(gr for gr in _GATES for gf in gr.supported_gates if gf == gate_family), None
)
if gate_rep is None:
raise ValueError(f'Unrecognized gate: {gate_family}.')
raise ValueError(f'Unrecognized gate: {repr(gate_family)}.')
gate_name = gate_rep.gate_spec_name

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

for q in operation.qubits:
if isinstance(q, ops.Coupler):
if any(qc not in qubits for qc in q.qubits):
raise ValueError(f'Qubits on coupler not on device: {q.qubits}.')
if frozenset(q.qubits) not in qubit_pairs:
raise ValueError(f'Coupler pair is not valid on device: {q.qubits}.')
elif q not in qubits:
raise ValueError(f'Qubit not on device: {q!r}.')
for q in op_qubits:
if q not in qubits:
if isinstance(q, ops.Coupler):
if any(qc not in qubits for qc in q.qubits):
raise ValueError(f'Qubits on coupler not on device: {q.qubits}.')
if frozenset(q.qubits) not in qubit_pairs:
raise ValueError(f'Coupler pair is not valid on device: {q.qubits}.')
else:
raise ValueError(f'Qubit not on device: {q!r}.')

if (
len(operation.qubits) == 2
len(op_qubits) == 2
and not isinstance(operation.gate, _VARIADIC_GATE_TYPES)
and frozenset(operation.qubits) not in qubit_pairs
and frozenset(op_qubits) not in qubit_pairs
):
raise ValueError(f'Qubit pair is not valid on device: {operation.qubits!r}.')

Expand Down
28 changes: 23 additions & 5 deletions cirq-google/cirq_google/devices/grid_device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ def _create_device_spec_with_horizontal_couplings():
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
cirq.GateFamily(cirq_google.SYC),
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False),
cirq.GateFamily(cirq.SQRT_ISWAP),
cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False),
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False),
cirq.GateFamily(cirq.CZ),
cirq.GateFamily(cirq.CZ, ignore_global_phase=False),
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate),
cirq.GateFamily(cirq.ops.common_gates.XPowGate),
cirq.GateFamily(cirq.ops.common_gates.YPowGate),
cirq.GateFamily(cirq.I),
cirq.GateFamily(cirq.IdentityGate),
cirq.GateFamily(cirq.ops.SingleQubitCliffordGate),
cirq.GateFamily(cirq.ops.HPowGate),
cirq.GateFamily(cirq.ops.phased_x_gate.PhasedXPowGate),
Expand Down Expand Up @@ -133,14 +137,18 @@ def _create_device_spec_with_horizontal_couplings():
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]): base_duration * 2,
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]): base_duration * 3,
cirq.GateFamily(cirq_google.SYC): base_duration * 0,
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False): base_duration * 0,
cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1,
cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False): base_duration * 1,
cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2,
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False): base_duration * 2,
cirq.GateFamily(cirq.CZ): base_duration * 3,
cirq.GateFamily(cirq.CZ, ignore_global_phase=False): base_duration * 3,
cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
cirq.GateFamily(cirq.ops.common_gates.XPowGate): base_duration * 4,
cirq.GateFamily(cirq.ops.common_gates.YPowGate): base_duration * 4,
cirq.GateFamily(cirq.ops.common_gates.HPowGate): base_duration * 4,
cirq.GateFamily(cirq.I): base_duration * 4,
cirq.GateFamily(cirq.IdentityGate): base_duration * 4,
cirq.GateFamily(cirq.ops.SingleQubitCliffordGate): base_duration * 4,
cirq.GateFamily(cirq.ops.phased_x_gate.PhasedXPowGate): base_duration * 4,
cirq.GateFamily(
Expand Down Expand Up @@ -173,13 +181,16 @@ def _create_device_spec_with_horizontal_couplings():
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
cirq.GateFamily(cirq_google.SYC),
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False),
cirq.GateFamily(cirq.SQRT_ISWAP),
cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False),
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False),
cirq.GateFamily(cirq.CZPowGate),
cirq.ops.common_gates.XPowGate,
cirq.ops.common_gates.YPowGate,
cirq.ops.common_gates.HPowGate,
cirq.GateFamily(cirq.I),
cirq.GateFamily(cirq.IdentityGate),
cirq.ops.SingleQubitCliffordGate,
cirq.ops.phased_x_gate.PhasedXPowGate,
cirq.GateFamily(
Expand All @@ -205,13 +216,16 @@ def _create_device_spec_with_horizontal_couplings():
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
cirq.GateFamily(cirq_google.SYC),
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False),
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False),
cirq.GateFamily(cirq.CZPowGate),
cirq.GateFamily(cirq.CZ),
cirq.GateFamily(cirq.CZ, ignore_global_phase=False),
cirq.ops.common_gates.XPowGate,
cirq.ops.common_gates.YPowGate,
cirq.ops.common_gates.HPowGate,
cirq.GateFamily(cirq.I),
cirq.GateFamily(cirq.IdentityGate),
cirq.ops.SingleQubitCliffordGate,
cirq.ops.phased_x_gate.PhasedXPowGate,
cirq.GateFamily(
Expand All @@ -238,13 +252,17 @@ def _create_device_spec_with_horizontal_couplings():
cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
cirq.GateFamily(cirq_google.SYC),
cirq.GateFamily(cirq_google.SYC, ignore_global_phase=False),
cirq.GateFamily(cirq.SQRT_ISWAP),
cirq.GateFamily(cirq.SQRT_ISWAP, ignore_global_phase=False),
cirq.GateFamily(cirq.SQRT_ISWAP_INV),
cirq.GateFamily(cirq.SQRT_ISWAP_INV, ignore_global_phase=False),
cirq.GateFamily(cirq.CZ),
cirq.GateFamily(cirq.CZ, ignore_global_phase=False),
cirq.ops.common_gates.XPowGate,
cirq.ops.common_gates.YPowGate,
cirq.ops.common_gates.HPowGate,
cirq.GateFamily(cirq.I),
cirq.GateFamily(cirq.IdentityGate),
cirq.ops.SingleQubitCliffordGate,
cirq.ops.phased_x_gate.PhasedXPowGate,
cirq.GateFamily(
Expand Down