def test_to_proto_invalid_input(error_match, qubits, qubit_pairs, gateset, gate_durations): with pytest.raises(ValueError, match=error_match): grid_device.create_device_specification_proto( qubits=qubits, pairs=qubit_pairs, gateset=gateset, gate_durations=gate_durations)
def _create_grid_device_from_diagram( ascii_grid: str, gateset: cirq.Gateset, gate_durations: Optional[Dict['cirq.GateFamily', 'cirq.Duration']] = None, out: Optional[device_pb2.DeviceSpecification] = None, ) -> grid_device.GridDevice: """Parse ASCIIart device layout into a GridDevice instance. This function assumes that all pairs of adjacent qubits are valid targets for two-qubit gates. Args: ascii_grid: ASCII version of the grid (see _parse_device for details). gateset: The device's gate set. gate_durations: A map of durations for each gate in the gate set. out: If given, populate this proto, otherwise create a new proto. """ qubits, _ = _parse_device(ascii_grid) # Create a list of all adjacent pairs on the grid for two-qubit gates. qubit_set = frozenset(qubits) pairs: List[Tuple[cirq.GridQubit, cirq.GridQubit]] = [] for qubit in qubits: for neighbor in sorted(qubit.neighbors()): if neighbor > qubit and neighbor in qubit_set: pairs.append((qubit, cast(cirq.GridQubit, neighbor))) device_specification = grid_device.create_device_specification_proto( qubits=qubits, pairs=pairs, gateset=gateset, gate_durations=gate_durations, out=out) return grid_device.GridDevice.from_proto(device_specification)
def test_to_proto_empty(): spec = grid_device.create_device_specification_proto( # Qubits are always expected to be set qubits=[cirq.GridQubit(0, i) for i in range(5)], pairs=[], gateset=cirq.Gateset(), gate_durations=None, ) device = cirq_google.GridDevice.from_proto(spec) assert len(device.metadata.qubit_set) == 5 assert len(device.metadata.qubit_pairs) == 0 assert device.metadata.gateset == cirq.Gateset() assert device.metadata.gate_durations is None
def test_to_proto(): device_info, expected_spec = _create_device_spec_with_horizontal_couplings( ) # The set of gates in gate_durations are consistent with what's generated in # _create_device_spec_with_horizontal_couplings() base_duration = cirq.Duration(picos=1_000) gate_durations = { cirq.GateFamily(cirq_google.SYC): base_duration * 0, cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1, cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2, cirq.GateFamily(cirq.CZ): base_duration * 3, cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4, cirq.GateFamily(cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]): base_duration * 5, cirq.GateFamily(cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]): base_duration * 6, cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration * 7, cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8, cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9, } spec = grid_device.create_device_specification_proto( qubits=device_info.grid_qubits, pairs=device_info.qubit_pairs, gateset=cirq.Gateset(*gate_durations.keys()), gate_durations=gate_durations, ) assert text_format.MessageToString(spec) == text_format.MessageToString( expected_spec)
def test_to_proto_backward_compatibility(): # Deprecations: cirq_google.SerializableGateSet and # cirq_google.device.known_devices.create_device_proto_for_qubits() with cirq.testing.assert_deprecated( 'SerializableGateSet', 'create_device_specification_proto()` can be used', deadline='v0.16', count=None, ): device_info, _ = _create_device_spec_with_horizontal_couplings() # The set of gates in gate_durations are consistent with what's generated in # _create_device_spec_with_horizontal_couplings() base_duration = cirq.Duration(picos=1_000) gate_durations = { cirq.GateFamily(cirq_google.SYC): base_duration * 0, cirq.GateFamily(cirq.SQRT_ISWAP): base_duration * 1, cirq.GateFamily(cirq.SQRT_ISWAP_INV): base_duration * 2, cirq.GateFamily(cirq.CZ): base_duration * 3, cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4, cirq.GateFamily(cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]): base_duration * 5, cirq.GateFamily(cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]): base_duration * 6, cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration * 7, cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8, cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9, } # Serialize the old way spec = known_devices.create_device_proto_for_qubits( device_info.grid_qubits, device_info.qubit_pairs, [cirq_google.FSIM_GATESET], known_devices._SYCAMORE_DURATIONS_PICOS, ) # Serialize the new way grid_device.create_device_specification_proto( qubits=device_info.grid_qubits, pairs=device_info.qubit_pairs, gateset=cirq.Gateset(*gate_durations.keys()), gate_durations=gate_durations, out=spec, ) with cirq.testing.assert_deprecated('Use cirq_google.GridDevice', deadline='v0.16', count=None): # Deserialize both ways serializable_dev = cirq_google.SerializableDevice.from_proto( spec, [cirq_google.FSIM_GATESET]) grid_dev = cirq_google.GridDevice.from_proto(spec) assert serializable_dev.metadata.qubit_set == grid_dev.metadata.qubit_set assert serializable_dev.metadata.qubit_pairs == grid_dev.metadata.qubit_pairs assert serializable_dev.metadata.gateset == cirq.Gateset( cirq.FSimGate, cirq.ISwapPowGate, cirq.CZPowGate, cirq.PhasedXPowGate, cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate, cirq.PhasedXZGate, cirq.MeasurementGate, cirq.WaitGate, cirq.GlobalPhaseGate, ) assert grid_dev.metadata.gateset == device_info.expected_gateset assert (tuple(grid_dev.metadata.compilation_target_gatesets) == device_info.expected_target_gatesets) assert grid_dev.metadata.gate_durations == device_info.expected_gate_durations