def test_grid_device_validate_operation_coupler_for_horizontal_couplings():
    """Tests coupler device on a device spec that only
    has horizontal couplings."""
    _, spec = _create_device_spec_with_horizontal_couplings()
    device = cirq_google.GridDevice.from_proto(spec)

    g = cirq_google.InternalGate(
        gate_name="DetuneCoupler", gate_module='internal_module', num_qubits=1, freq=5.5
    )
    for y in range(GRID_HEIGHT):
        # Valid couplers
        coupler = Coupler(cirq.GridQubit(y, 0), cirq.GridQubit(y, 1))
        device.validate_operation(g(coupler))
        coupler = Coupler(cirq.GridQubit(y, 1), cirq.GridQubit(y, 0))
        device.validate_operation(g(coupler))
        # One coupler off grid
        coupler = Coupler(cirq.GridQubit(y, 1), cirq.GridQubit(y, 2))
        with pytest.raises(ValueError, match="Qubits on coupler not on device"):
            device.validate_operation(g(coupler))
        # Both couplers off grid
        coupler = Coupler(cirq.GridQubit(y, 2), cirq.GridQubit(y, 3))
        with pytest.raises(ValueError, match="Qubits on coupler not on device"):
            device.validate_operation(g(coupler))
        # Vertical Coupler (not on device)
        coupler = Coupler(cirq.GridQubit(y, 0), cirq.GridQubit((y + 1) % GRID_HEIGHT, 0))
        with pytest.raises(ValueError, match="Coupler pair is not valid on device"):
            device.validate_operation(g(coupler))
Tests coupler device on a device spec that only
    has horizontal couplings.
cirq-google/cirq_google/devices/grid_device_test.py