def with_key(self, key: str | cirq.MeasurementKey) -> MeasurementGate:
        """Creates a measurement gate with a new key but otherwise identical."""
        if key == self.key:
            return self
        return MeasurementGate(
            self.num_qubits(),
            key=key,
            invert_mask=self.invert_mask,
            qid_shape=self._qid_shape,
            confusion_map=self.confusion_map,
        )
Creates a measurement gate with a new key but otherwise identical.
cirq-core/cirq/ops/measurement_gate.py