def to_proto( self, op, msg=None, *, arg_function_language='', ): """Returns the cirq.google.api.v2.Operation message as a proto dict.""" gate = op.gate if not isinstance(gate, self.gate_type): raise ValueError( 'Gate of type {} but serializer expected type {}'.format( type(gate), self.gate_type)) if not self.can_serialize_predicate(op): return None if msg is None: msg = program_pb2.Operation() msg.gate.id = self.serialized_gate_id for qubit in op.qubits: msg.qubits.add().id = '{}_{}'.format(qubit.row, qubit.col) for arg in self.args: value = self._value_from_gate(op, arg) if value is not None and (not arg.default or value != arg.default): _arg_to_proto(value, out=msg.args[arg.serialized_name], arg_function_language=arg_function_language) return msg
def test_deserialize_bad_operation_id(self): """Ensure error is raised when deserializing bad operation.""" proto = program_pb2.Program( language=program_pb2.Language(arg_function_language='', gate_set='my_gate_set'), circuit=program_pb2.Circuit( scheduling_strategy=program_pb2.Circuit.MOMENT_BY_MOMENT, moments=[ program_pb2.Moment(operations=[]), program_pb2.Moment(operations=[ program_pb2.Operation( gate=program_pb2.Gate(id='UNKNOWN_GATE'), args={ 'half_turns': program_pb2.Arg( arg_value=program_pb2.ArgValue( float_value=1.0)) }, qubits=[program_pb2.Qubit(id='1_1')]) ]), ])) with self.assertRaisesRegex( ValueError, expected_regex='problem in moment 1 handling an ' 'operation with the following'): MY_GATE_SET.deserialize(proto)
def op_proto(json): """Json to proto.""" op = program_pb2.Operation() json_format.ParseDict(json, op) return op
def _build_op_proto(gate_id, arg_names, arg_vals, qubit_ids): """Helper function to generate proto for a given circuit spec. Understand how it works from this example: _build_op_proto("HP", ['exponent', 'global_shift'], ['alpha', 0.0], ['0_0']) would produce the following: language { gate_set: "tfq_gate_set" } circuit { scheduling_strategy: MOMENT_BY_MOMENT moments { operations { gate { id: "HP" } args { key: "global_shift" value { arg_value { float_value: 0.0 } } } args { key: "exponent" value { symbol: "alpha" } } args { key: "control_qubits" value { arg_value: "" } } args { key: "control_values" value { arg_value: "" } } qubits { id: "0_0" } } } } """ program_proto = program_pb2.Program() program_proto.language.gate_set = 'tfq_gate_set' circuit_proto = program_proto.circuit circuit_proto.scheduling_strategy = circuit_proto.MOMENT_BY_MOMENT circuit_proto.moments.add(operations=[program_pb2.Operation( gate = program_pb2.Gate(id=gate_id), args = {arg_names[i]: (program_pb2.Arg(symbol=arg_vals[i]) \ if isinstance(arg_vals[i], str) else \ program_pb2.Arg( arg_value=program_pb2.ArgValue( float_value=np.round(float(arg_vals[i]), 6)))) \ for i in range(len(arg_vals))}, qubits=[program_pb2.Qubit( id=q_id) for q_id in qubit_ids])]) # Add in empty control information t = program_proto.circuit.moments[0].operations[0] t.args['control_qubits'].arg_value.string_value = '' t.args['control_values'].arg_value.string_value = '' return program_proto