def parse_result(self, result: CalibrationResult) -> PhasedFSimCalibrationResult: decoded: Dict[int, Dict[str, Any]] = collections.defaultdict(lambda: {}) for keys, values in result.metrics['angles'].items(): for key, value in zip(keys, values): match = re.match(r'(\d+)_(.+)', str(key)) if not match: raise ValueError(f'Unknown metric name {key}') index = int(match[1]) name = match[2] decoded[index][name] = value parsed = {} for data in decoded.values(): a = v2.qubit_from_proto_id(data['qubit_a']) b = v2.qubit_from_proto_id(data['qubit_b']) parsed[(a, b)] = PhasedFSimCharacterization( theta=data.get('theta_est', None), zeta=data.get('zeta_est', None), chi=data.get('chi_est', None), gamma=data.get('gamma_est', None), phi=data.get('phi_est', None), ) return PhasedFSimCalibrationResult(parameters=parsed, gate=self.gate, options=self.options)
def from_proto( self, proto: v2.program_pb2.Operation, *, arg_function_language: str = '', constants: List[v2.program_pb2.Constant] = None, ) -> 'cirq.Operation': """Turns a cirq.google.api.v2.Operation proto into a GateOperation.""" qubits = [v2.qubit_from_proto_id(q.id) for q in proto.qubits] args = self._args_from_proto( proto, arg_function_language=arg_function_language) if self.num_qubits_param is not None: args[self.num_qubits_param] = len(qubits) gate = self.gate_constructor(**args) op = self.op_wrapper(gate.on(*qubits), proto) if self.deserialize_tokens: which = proto.WhichOneof('token') if which == 'token_constant_index': if not constants: raise ValueError('Proto has references to constants table ' 'but none was passed in, value =' f'{proto}') op = op.with_tags( CalibrationTag( constants[proto.token_constant_index].string_value)) elif which == 'token_value': op = op.with_tags(CalibrationTag(proto.token_value)) return op
def _optional_control_promote(gate, qubits_message, values_message): """Optionally promote to controlled gate based on serialized control msg.""" if qubits_message == '' and values_message == '': return gate qbs = [v2.qubit_from_proto_id(qb) for qb in qubits_message.split(',')] vals = [int(cv) for cv in values_message.split(',')] return DelayedAssignmentGate(gate, qbs, vals)
def from_proto(self, proto: v2.program_pb2.Operation, *, arg_function_language: str = '') -> 'cirq.Operation': """Turns a cirq.google.api.v2.Operation proto into a GateOperation.""" qubits = [v2.qubit_from_proto_id(q.id) for q in proto.qubits] args = self._args_from_proto( proto, arg_function_language=arg_function_language) if self.num_qubits_param is not None: args[self.num_qubits_param] = len(qubits) gate = self.gate_constructor(**args) return self.op_wrapper(gate.on(*qubits), proto)
def test_generic_qubit_from_proto_id(): assert v2.qubit_from_proto_id('1_2') == cirq.GridQubit(1, 2) assert v2.qubit_from_proto_id('1') == cirq.LineQubit(1) assert v2.qubit_from_proto_id('a') == cirq.NamedQubit('a') # Despite the fact that int(1_2_3) = 123, only pure numbers are parsed into # LineQubits. assert v2.qubit_from_proto_id('1_2_3') == cirq.NamedQubit('1_2_3') # All non-int-parseable names are converted to NamedQubits. assert v2.qubit_from_proto_id('a') == cirq.NamedQubit('a') assert v2.qubit_from_proto_id('1_b') == cirq.NamedQubit('1_b')