def test_gateset_eq(): eq = cirq.testing.EqualsTester() eq.add_equality_group(cirq.Gateset(CustomX)) eq.add_equality_group(cirq.Gateset(CustomX ** 3)) eq.add_equality_group(cirq.Gateset(CustomX, name='Custom Gateset')) eq.add_equality_group(cirq.Gateset(CustomX, name='Custom Gateset', unroll_circuit_op=False)) eq.add_equality_group( cirq.Gateset(CustomX, name='Custom Gateset', accept_global_phase_op=False) ) eq.add_equality_group( cirq.Gateset( cirq.GateFamily(CustomX, name='custom_name', description='custom_description'), cirq.GateFamily(CustomX, name='custom_name', description='custom_description'), ), cirq.Gateset( cirq.GateFamily(CustomX ** 3, name='custom_name', description='custom_description'), cirq.GateFamily(CustomX, name='custom_name', description='custom_description'), ), ) eq.add_equality_group( cirq.Gateset(CustomX, CustomXPowGate), cirq.Gateset(CustomXPowGate, CustomX), cirq.Gateset(CustomX, CustomX, CustomXPowGate), cirq.Gateset(CustomXPowGate, CustomX, CustomXPowGate), ) eq.add_equality_group(cirq.Gateset(CustomXGateFamily())) eq.add_equality_group( cirq.Gateset( cirq.GateFamily( gate=CustomXPowGate, name='CustomXGateFamily', description='Accepts all integer powers of CustomXPowGate', ) ) )
def test_gateset_init(): assert gateset.name == 'custom gateset' assert gateset.gates == frozenset([ cirq.GateFamily(CustomX**0.5), cirq.GateFamily(cirq.testing.TwoQubitGate), CustomXGateFamily(), ])
def test_invalid_gate_family(): with pytest.raises(ValueError, match='instance or subclass of `cirq.Gate`'): _ = cirq.GateFamily(gate=cirq.Operation) with pytest.raises(ValueError, match='non-parameterized instance of `cirq.Gate`'): _ = cirq.GateFamily(gate=CustomX**sympy.Symbol('theta'))
def _build_gateset_and_gate_durations( proto: v2.device_pb2.DeviceSpecification, ) -> Tuple[cirq.Gateset, Dict[cirq.GateFamily, cirq.Duration]]: """Extracts gate set and gate duration information from the given DeviceSpecification proto.""" gates_list: List[Union[Type[cirq.Gate], cirq.Gate, cirq.GateFamily]] = [] gate_durations: Dict[cirq.GateFamily, cirq.Duration] = {} # TODO(#5050) Describe how to add/remove gates. for gate_spec in proto.valid_gates: gate_name = gate_spec.WhichOneof('gate') cirq_gates: List[Union[Type[cirq.Gate], cirq.Gate, cirq.GateFamily]] = [] if gate_name == 'syc': cirq_gates = [ops.FSimGateFamily(gates_to_accept=[ops.SYC])] elif gate_name == 'sqrt_iswap': cirq_gates = [ops.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP])] elif gate_name == 'sqrt_iswap_inv': cirq_gates = [ops.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV])] elif gate_name == 'cz': cirq_gates = [ops.FSimGateFamily(gates_to_accept=[cirq.CZ])] elif gate_name == 'phased_xz': cirq_gates = [cirq.PhasedXZGate, cirq.XPowGate, cirq.YPowGate, cirq.PhasedXPowGate] elif gate_name == 'virtual_zpow': cirq_gates = [cirq.GateFamily(cirq.ZPowGate, tags_to_ignore=[ops.PhysicalZTag()])] elif gate_name == 'physical_zpow': cirq_gates = [cirq.GateFamily(cirq.ZPowGate, tags_to_accept=[ops.PhysicalZTag()])] elif gate_name == 'coupler_pulse': cirq_gates = [experimental_ops.CouplerPulse] elif gate_name == 'meas': cirq_gates = [cirq.MeasurementGate] elif gate_name == 'wait': cirq_gates = [cirq.WaitGate] else: # coverage: ignore warnings.warn( f"The DeviceSpecification contains the gate '{gate_name}' which is not recognized" " by Cirq and will be ignored. This may be due to an out-of-date Cirq version.", UserWarning, ) continue gates_list.extend(cirq_gates) # TODO(#5050) Allow different gate representations of the same gate to be looked up in # gate_durations. for g in cirq_gates: if not isinstance(g, cirq.GateFamily): g = cirq.GateFamily(g) gate_durations[g] = cirq.Duration(picos=gate_spec.gate_duration_picos) # TODO(#4833) Add identity gate support # TODO(#5050) Add GlobalPhaseGate support return cirq.Gateset(*gates_list), gate_durations
def test_griddevice_metadata_bad_durations(): qubits = tuple(cirq.GridQubit.rect(1, 2)) gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate) invalid_duration = { cirq.GateFamily(cirq.XPowGate): cirq.Duration(nanos=1), cirq.GateFamily(cirq.ZPowGate): cirq.Duration(picos=1), } with pytest.raises(ValueError, match="ZPowGate"): cirq.GridDeviceMetadata([qubits], gateset, gate_durations=invalid_duration)
def test_invalid_gate_family(): with pytest.raises(ValueError, match='instance or subclass of `cirq.Gate`'): _ = cirq.GateFamily(gate=cirq.Operation) with pytest.raises(ValueError, match='non-parameterized instance of `cirq.Gate`'): _ = cirq.GateFamily(gate=CustomX**sympy.Symbol('theta')) with pytest.raises(ValueError, match='cannot be in both'): _ = cirq.GateFamily(gate=cirq.H, tags_to_accept={'a', 'b'}, tags_to_ignore={'b', 'c'})
def test_repr(): qubits = cirq.GridQubit.rect(2, 3) qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate) duration = { cirq.GateFamily(cirq.XPowGate): cirq.Duration(nanos=1), cirq.GateFamily(cirq.YPowGate): cirq.Duration(picos=3), cirq.GateFamily(cirq.ZPowGate): cirq.Duration(picos=2), } isolated_qubits = [cirq.GridQubit(9, 9)] metadata = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration, all_qubits=qubits + isolated_qubits) cirq.testing.assert_equivalent_repr(metadata)
def test_gate_family_init(gate): name = 'test_name' description = 'test_description' g = cirq.GateFamily(gate=gate, name=name, description=description) assert g.gate == gate assert g.name == name assert g.description == description
def test_gate_family_immutable(): g = cirq.GateFamily(CustomX) with pytest.raises(AttributeError, match="can't set attribute"): g.gate = CustomXPowGate with pytest.raises(AttributeError, match="can't set attribute"): g.name = 'new name' with pytest.raises(AttributeError, match="can't set attribute"): g.description = 'new description'
def test_griddevice_json_load(): qubits = cirq.GridQubit.rect(2, 3) qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate) duration = { cirq.GateFamily(cirq.XPowGate): cirq.Duration(nanos=1), cirq.GateFamily(cirq.YPowGate): cirq.Duration(picos=2), cirq.GateFamily(cirq.ZPowGate): cirq.Duration(picos=3), } isolated_qubits = [cirq.GridQubit(9, 9), cirq.GridQubit(10, 10)] metadata = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration, all_qubits=qubits + isolated_qubits) rep_str = cirq.to_json(metadata) assert metadata == cirq.read_json(json_text=rep_str)
def test_gateset_contains_with_tags(): tag = "PhysicalZTag" gf_accept = cirq.GateFamily(cirq.ZPowGate, tags_to_accept=[tag]) gf_ignore = cirq.GateFamily(cirq.ZPowGate, tags_to_ignore=[tag]) op = cirq.Z(q) op_with_tag = cirq.Z(q).with_tags(tag) # Only tags to ignore. assert op in cirq.Gateset(gf_ignore) assert op_with_tag not in cirq.Gateset(gf_ignore) # Only tags to accept assert op not in cirq.Gateset(gf_accept) assert op_with_tag in cirq.Gateset(gf_accept) # Both tags to accept and tags to ignore assert op in cirq.Gateset(gf_accept, gf_ignore) assert op_with_tag in cirq.Gateset(gf_accept, gf_ignore)
def test_griddevice_metadata(): qubits = cirq.GridQubit.rect(2, 3) qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] isolated_qubits = [cirq.GridQubit(9, 9), cirq.GridQubit(10, 10)] gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate, cirq.CZ) gate_durations = { cirq.GateFamily(cirq.XPowGate): 1_000, cirq.GateFamily(cirq.YPowGate): 1_000, cirq.GateFamily(cirq.ZPowGate): 1_000, # omitting cirq.CZ } target_gatesets = (cirq.CZTargetGateset(), ) metadata = cirq.GridDeviceMetadata( qubit_pairs, gateset, gate_durations=gate_durations, all_qubits=qubits + isolated_qubits, compilation_target_gatesets=target_gatesets, ) expected_pairings = frozenset({ frozenset((cirq.GridQubit(0, 0), cirq.GridQubit(0, 1))), frozenset((cirq.GridQubit(0, 1), cirq.GridQubit(0, 2))), frozenset((cirq.GridQubit(0, 1), cirq.GridQubit(1, 1))), frozenset((cirq.GridQubit(0, 2), cirq.GridQubit(1, 2))), frozenset((cirq.GridQubit(1, 0), cirq.GridQubit(1, 1))), frozenset((cirq.GridQubit(1, 1), cirq.GridQubit(1, 2))), frozenset((cirq.GridQubit(0, 0), cirq.GridQubit(1, 0))), }) assert metadata.qubit_set == frozenset(qubits + isolated_qubits) assert metadata.qubit_pairs == expected_pairings assert metadata.gateset == gateset expected_graph = nx.Graph() expected_graph.add_nodes_from(sorted(list(qubits + isolated_qubits))) expected_graph.add_edges_from(sorted(list(expected_pairings)), directed=False) assert metadata.nx_graph.edges() == expected_graph.edges() assert metadata.nx_graph.nodes() == expected_graph.nodes() assert metadata.gate_durations == gate_durations assert metadata.isolated_qubits == frozenset(isolated_qubits) assert metadata.compilation_target_gatesets == target_gatesets
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_gate_family_default_name_and_description(gate, tags_to_accept, tags_to_ignore): g = cirq.GateFamily(gate, tags_to_accept=tags_to_accept, tags_to_ignore=tags_to_ignore) assert re.match('.*GateFamily.*CustomX.*', g.name) assert re.match('Accepts.*instances.*CustomX.*', g.description) accepted_match = re.compile('.*Accepted tags.*', re.DOTALL).match(g.description) assert (accepted_match is None) == (tags_to_accept == []) ignored_match = re.compile('.*Ignored tags.*', re.DOTALL).match(g.description) assert (ignored_match is None) == (tags_to_ignore == [])
def test_gate_family_eq(): eq = cirq.testing.EqualsTester() eq.add_equality_group(cirq.GateFamily(CustomX)) eq.add_equality_group(cirq.GateFamily(CustomX ** 3)) eq.add_equality_group( cirq.GateFamily(CustomX, name='custom_name', description='custom_description'), cirq.GateFamily(CustomX ** 3, name='custom_name', description='custom_description'), ) eq.add_equality_group(cirq.GateFamily(CustomXPowGate)) eq.add_equality_group( cirq.GateFamily(CustomXPowGate, name='custom_name', description='custom_description') )
def test_griddevice_metadata_equality(): qubits = cirq.GridQubit.rect(2, 3) qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate) duration = { cirq.GateFamily(cirq.XPowGate): cirq.Duration(nanos=1), cirq.GateFamily(cirq.YPowGate): cirq.Duration(picos=3), cirq.GateFamily(cirq.ZPowGate): cirq.Duration(picos=2), } duration2 = { cirq.GateFamily(cirq.XPowGate): cirq.Duration(nanos=10), cirq.GateFamily(cirq.YPowGate): cirq.Duration(picos=13), cirq.GateFamily(cirq.ZPowGate): cirq.Duration(picos=12), } isolated_qubits = [cirq.GridQubit(9, 9)] metadata = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration) metadata2 = cirq.GridDeviceMetadata(qubit_pairs[:2], gateset, gate_durations=duration) metadata3 = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=None) metadata4 = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration2) metadata5 = cirq.GridDeviceMetadata(reversed(qubit_pairs), gateset, gate_durations=duration) metadata6 = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration, all_qubits=qubits + isolated_qubits) eq = cirq.testing.EqualsTester() eq.add_equality_group(metadata) eq.add_equality_group(metadata2) eq.add_equality_group(metadata3) eq.add_equality_group(metadata4) eq.add_equality_group(metadata6) assert metadata == metadata5
def test_gate_family_json(gate, name, description): g = cirq.GateFamily(gate, name=name, description=description) g_json = cirq.to_json(g) assert cirq.read_json(json_text=g_json) == g
eq.add_equality_group(cirq.GateFamily(CustomX ** 3)) eq.add_equality_group( cirq.GateFamily(CustomX, name='custom_name', description='custom_description'), cirq.GateFamily(CustomX ** 3, name='custom_name', description='custom_description'), ) eq.add_equality_group(cirq.GateFamily(CustomXPowGate)) eq.add_equality_group( cirq.GateFamily(CustomXPowGate, name='custom_name', description='custom_description') ) @pytest.mark.parametrize( 'gate_family, gates_to_check', [ ( cirq.GateFamily(CustomXPowGate), [ (CustomX, True), (CustomX ** 0.5, True), (CustomX ** sympy.Symbol('theta'), True), (CustomXPowGate(exponent=0.25, global_shift=0.15), True), (cirq.SingleQubitGate(), False), (cirq.X ** 0.5, False), (None, False), (cirq.GlobalPhaseOperation(1j), False), ], ), ( cirq.GateFamily(CustomX), [ (CustomX, True),
eq.add_equality_group(cirq.SqrtIswapTargetGateset(additional_gates=[cirq.XPowGate])) @pytest.mark.parametrize( 'gateset', [ cirq.SqrtIswapTargetGateset(), cirq.SqrtIswapTargetGateset( atol=1e-6, required_sqrt_iswap_count=2, use_sqrt_iswap_inv=True, additional_gates=[ cirq.CZ, cirq.XPowGate, cirq.YPowGate, cirq.GateFamily(cirq.ZPowGate, tags_to_accept=['test_tag']), ], ), cirq.SqrtIswapTargetGateset(additional_gates=()), ], ) def test_sqrt_iswap_gateset_repr(gateset): cirq.testing.assert_equivalent_repr(gateset) def test_simplifies_sqrt_iswap(): a, b = cirq.LineQubit.range(2) assert_optimizes( before=cirq.Circuit( [ # SQRT_ISWAP**8 == Identity
'xyz': 25_000, 'fsim_pi_4': 32_000, 'inv_fsim_pi_4': 32_000, 'syc': 12_000, 'z': 0, 'meas': 4_000_000, # 1000 ns for readout, 3000ns for ring_down } _SYCAMORE_GATESET = cirq.Gateset( sycamore_gate.SYC, cirq.SQRT_ISWAP, cirq.SQRT_ISWAP_INV, cirq.PhasedXZGate, # Physical Z and virtual Z gates are represented separately because they # have different gate durations. cirq.GateFamily(cirq.ZPowGate, tags_to_ignore=[physical_z_tag.PhysicalZTag()]), cirq.GateFamily(cirq.ZPowGate, tags_to_accept=[physical_z_tag.PhysicalZTag()]), coupler_pulse.CouplerPulse, cirq.MeasurementGate, cirq.WaitGate, ) _SYCAMORE_DURATIONS = { cirq.GateFamily(sycamore_gate.SYC): cirq.Duration(nanos=12), cirq.GateFamily(cirq.SQRT_ISWAP): cirq.Duration(nanos=32), cirq.GateFamily(cirq.SQRT_ISWAP_INV): cirq.Duration(nanos=32), cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate):
def test_grid_device_from_proto(): grid_qubits, spec = _create_device_spec_with_horizontal_couplings() device = cirq_google.GridDevice.from_proto(spec) assert len(device.metadata.qubit_set) == len(grid_qubits) assert device.metadata.qubit_set == frozenset(grid_qubits) assert all( frozenset((cirq.GridQubit(row, 0), cirq.GridQubit(row, 1))) in device.metadata.qubit_pairs for row in range(GRID_HEIGHT) ) assert device.metadata.gateset == cirq.Gateset( cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]), cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]), cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]), cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]), cirq.ops.phased_x_z_gate.PhasedXZGate, cirq.ops.common_gates.XPowGate, cirq.ops.common_gates.YPowGate, cirq.ops.phased_x_gate.PhasedXPowGate, cirq.GateFamily( cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()] ), cirq.GateFamily( cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()] ), cirq_google.experimental.ops.coupler_pulse.CouplerPulse, cirq.ops.measurement_gate.MeasurementGate, cirq.ops.wait_gate.WaitGate, ) assert tuple(device.metadata.compilation_target_gatesets) == ( cirq.CZTargetGateset(), cirq_google.SycamoreTargetGateset(), cirq.SqrtIswapTargetGateset(use_sqrt_iswap_inv=True), ) base_duration = cirq.Duration(picos=1_000) assert device.metadata.gate_durations == { cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]): base_duration * 0, cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]): base_duration * 1, 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.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.phased_x_gate.PhasedXPowGate): 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, }
) assert text_format.MessageToString(spec) == text_format.MessageToString( expected_spec) @pytest.mark.parametrize( 'error_match, qubits, qubit_pairs, gateset, gate_durations', [ ( 'Gate durations contain keys which are not part of the gateset', [cirq.GridQubit(0, 0)], [], cirq.Gateset(cirq.CZ), { cirq.GateFamily(cirq.SQRT_ISWAP): 1_000 }, ), ('not in the GridQubit form', [cirq.NamedQubit('q0_0') ], [], cirq.Gateset(), None), ( 'valid_targets contain .* which is not in valid_qubits', [cirq.GridQubit(0, 0)], [(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1))], cirq.Gateset(), None, ), ( 'has a target which contains repeated qubits', [cirq.GridQubit(0, 0)], [(cirq.GridQubit(0, 0), cirq.GridQubit(0, 0))],
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
def _create_device_spec_with_horizontal_couplings(): # Qubit layout: # x -- x # x -- x # x -- x # x -- x # x -- x grid_qubits = [ cirq.GridQubit(i, j) for i in range(GRID_HEIGHT) for j in range(2) ] spec = v2.device_pb2.DeviceSpecification() spec.valid_qubits.extend([v2.qubit_to_proto_id(q) for q in grid_qubits]) qubit_pairs = [] grid_targets = spec.valid_targets.add() grid_targets.name = '2_qubit_targets' grid_targets.target_ordering = v2.device_pb2.TargetSet.SYMMETRIC for row in range(int(GRID_HEIGHT / 2)): qubit_pairs.append((cirq.GridQubit(row, 0), cirq.GridQubit(row, 1))) for row in range(int(GRID_HEIGHT / 2), GRID_HEIGHT): # Flip the qubit pair order for the second half of qubits # to verify GridDevice properly handles pair symmetry. qubit_pairs.append((cirq.GridQubit(row, 1), cirq.GridQubit(row, 0))) for pair in qubit_pairs: new_target = grid_targets.targets.add() new_target.ids.extend([v2.qubit_to_proto_id(q) for q in pair]) gate_names = [ 'syc', 'sqrt_iswap', 'sqrt_iswap_inv', 'cz', 'phased_xz', 'virtual_zpow', 'physical_zpow', 'coupler_pulse', 'meas', 'wait', ] gate_durations = [(n, i * 1000) for i, n in enumerate(gate_names)] for gate_name, duration in sorted(gate_durations): gate = spec.valid_gates.add() getattr(gate, gate_name).SetInParent() gate.gate_duration_picos = duration expected_gateset = cirq.Gateset( cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]), cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]), cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]), cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]), cirq.ops.phased_x_z_gate.PhasedXZGate, cirq.ops.common_gates.XPowGate, cirq.ops.common_gates.YPowGate, cirq.ops.phased_x_gate.PhasedXPowGate, cirq.GateFamily(cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]), cirq.GateFamily(cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]), cirq_google.experimental.ops.coupler_pulse.CouplerPulse, cirq.ops.measurement_gate.MeasurementGate, cirq.ops.wait_gate.WaitGate, ) base_duration = cirq.Duration(picos=1_000) expected_gate_durations = { cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]): base_duration * 0, cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]): base_duration * 1, 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.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.phased_x_gate.PhasedXPowGate): 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, } expected_target_gatesets = ( cirq.CZTargetGateset(), cirq_google.SycamoreTargetGateset(), cirq.SqrtIswapTargetGateset(use_sqrt_iswap_inv=True), ) return ( _DeviceInfo( grid_qubits, qubit_pairs, expected_gateset, expected_gate_durations, expected_target_gatesets, ), spec, )
# See the License for the specific language governing permissions and # limitations under the License. """Device object representing Google devices with a grid qubit layout.""" from typing import Any, Collection, Dict, List, Optional, Sequence, Set, Tuple, Type, Union, cast import re import warnings import cirq from cirq_google import ops from cirq_google import transformers from cirq_google.api import v2 from cirq_google.devices import known_devices from cirq_google.experimental import ops as experimental_ops SYC_GATE_FAMILY = cirq.GateFamily(ops.SYC) SQRT_ISWAP_GATE_FAMILY = cirq.GateFamily(cirq.SQRT_ISWAP) SQRT_ISWAP_INV_GATE_FAMILY = cirq.GateFamily(cirq.SQRT_ISWAP_INV) CZ_GATE_FAMILY = cirq.GateFamily(cirq.CZ) PHASED_XZ_GATE_FAMILY = cirq.GateFamily(cirq.PhasedXZGate) VIRTUAL_ZPOW_GATE_FAMILY = cirq.GateFamily(cirq.ZPowGate, tags_to_ignore=[ops.PhysicalZTag()]) PHYSICAL_ZPOW_GATE_FAMILY = cirq.GateFamily( cirq.ZPowGate, tags_to_accept=[ops.PhysicalZTag()]) COUPLER_PULSE_GATE_FAMILY = cirq.GateFamily(experimental_ops.CouplerPulse) MEASUREMENT_GATE_FAMILY = cirq.GateFamily(cirq.MeasurementGate) WAIT_GATE_FAMILY = cirq.GateFamily(cirq.WaitGate) def _validate_device_specification( proto: v2.device_pb2.DeviceSpecification) -> None:
cirq.GateFamily(CustomX**3, name='custom_name', description='custom_description'), ) eq.add_equality_group(cirq.GateFamily(CustomXPowGate)) eq.add_equality_group( cirq.GateFamily(CustomXPowGate, name='custom_name', description='custom_description')) @pytest.mark.parametrize( 'gate_family, gates_to_check', [ ( cirq.GateFamily(CustomXPowGate), [ (CustomX, True), (CustomX**0.5, True), (CustomX**sympy.Symbol('theta'), True), (CustomXPowGate(exponent=0.25, global_shift=0.15), True), (cirq.testing.SingleQubitGate(), False), (cirq.X**0.5, False), (None, False), (cirq.global_phase_operation(1j), False), ], ), ( cirq.GateFamily(CustomX), [ (CustomX, True),
def test_gate_family_default_name_and_description(gate): g = cirq.GateFamily(gate) assert re.match('.*GateFamily.*CustomX.*', g.name) assert re.match('Accepts.*instances.*CustomX.*', g.description)
def test_griddevice_metadata_equality(): qubits = cirq.GridQubit.rect(2, 3) qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate, cirq.CZ, cirq.SQRT_ISWAP) duration = { cirq.GateFamily(cirq.XPowGate): cirq.Duration(nanos=1), cirq.GateFamily(cirq.YPowGate): cirq.Duration(picos=3), cirq.GateFamily(cirq.ZPowGate): cirq.Duration(picos=2), cirq.GateFamily(cirq.CZ): cirq.Duration(nanos=4), cirq.GateFamily(cirq.SQRT_ISWAP): cirq.Duration(nanos=5), } duration2 = { cirq.GateFamily(cirq.XPowGate): cirq.Duration(nanos=10), cirq.GateFamily(cirq.YPowGate): cirq.Duration(picos=13), cirq.GateFamily(cirq.ZPowGate): cirq.Duration(picos=12), cirq.GateFamily(cirq.CZ): cirq.Duration(nanos=14), cirq.GateFamily(cirq.SQRT_ISWAP): cirq.Duration(nanos=15), } isolated_qubits = [cirq.GridQubit(9, 9)] target_gatesets = [cirq.CZTargetGateset(), cirq.SqrtIswapTargetGateset()] metadata = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration) metadata2 = cirq.GridDeviceMetadata(qubit_pairs[:2], gateset, gate_durations=duration) metadata3 = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=None) metadata4 = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration2) metadata5 = cirq.GridDeviceMetadata(reversed(qubit_pairs), gateset, gate_durations=duration) metadata6 = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration, all_qubits=qubits + isolated_qubits) metadata7 = cirq.GridDeviceMetadata( qubit_pairs, gateset, compilation_target_gatesets=target_gatesets) metadata8 = cirq.GridDeviceMetadata( qubit_pairs, gateset, compilation_target_gatesets=target_gatesets[::-1]) metadata9 = cirq.GridDeviceMetadata( qubit_pairs, gateset, compilation_target_gatesets=tuple(target_gatesets)) metadata10 = cirq.GridDeviceMetadata( qubit_pairs, gateset, compilation_target_gatesets=set(target_gatesets)) eq = cirq.testing.EqualsTester() eq.add_equality_group(metadata) eq.add_equality_group(metadata2) eq.add_equality_group(metadata3) eq.add_equality_group(metadata4) eq.add_equality_group(metadata6) eq.add_equality_group(metadata7, metadata8, metadata9, metadata10) assert metadata == metadata5
def test_gate_family_repr_and_str(gate, name, description): g = cirq.GateFamily(gate, name=name, description=description) cirq.testing.assert_equivalent_repr(g) assert g.name in str(g) assert g.description in str(g)
cirq.GateFamily(CustomX**3, name='custom_name', description='custom_description'), ) eq.add_equality_group(cirq.GateFamily(CustomXPowGate)) eq.add_equality_group( cirq.GateFamily(CustomXPowGate, name='custom_name', description='custom_description')) @pytest.mark.parametrize( 'gate_family, gates_to_check', [ ( cirq.GateFamily(CustomXPowGate), [ (CustomX, True), (CustomX**0.5, True), (CustomX**sympy.Symbol('theta'), True), (CustomXPowGate(exponent=0.25, global_shift=0.15), True), (cirq.SingleQubitGate(), False), (cirq.X**0.5, False), (None, False), (cirq.GlobalPhaseOperation(1j), False), ], ), ( cirq.GateFamily(CustomX), [ (CustomX, True),