Example #1
0
    def _serialize_program(
        self,
        program: 'cirq.Circuit',
        gate_set: serializable_gate_set.SerializableGateSet = None
    ) -> Dict[str, Any]:
        gate_set = gate_set or gate_sets.XMON

        if self.proto_version == ProtoVersion.V1:
            if isinstance(program, circuits.Circuit):
                program.device.validate_circuit(program)
            else:
                raise TypeError(f'Unrecognized program type: {type(program)}')
            program_descriptor = v1.program_pb2.Program.DESCRIPTOR
            program_dict = {}  # type: Dict[str, Any]
            program_dict['@type'] = TYPE_PREFIX + program_descriptor.full_name
            program_dict['operations'] = [
                op for op in v1.circuit_as_schedule_to_proto_dicts(program)
            ]
            return program_dict
        elif self.proto_version == ProtoVersion.V2:
            program = gate_set.serialize(program)
            return _any_dict_from_msg(program)
        else:
            raise ValueError('invalid program proto version: {}'.format(
                self.proto_version))
Example #2
0
File: engine.py Project: yy8yy/Cirq
    def _serialize_program(
        self,
        program: TProgram,
        gate_set: serializable_gate_set.SerializableGateSet = None
    ) -> Dict[str, Any]:
        gate_set = gate_set or gate_sets.XMON

        if self.proto_version == ProtoVersion.V1:
            schedule = self.program_as_schedule(program)
            schedule.device.validate_schedule(schedule)

            program_descriptor = v1.program_pb2.Program.DESCRIPTOR
            program_dict = {}  # type: Dict[str, Any]
            program_dict['@type'] = TYPE_PREFIX + program_descriptor.full_name
            program_dict['operations'] = [
                op for op in api_v1.schedule_to_proto_dicts(schedule)
            ]
            return program_dict
        elif self.proto_version == ProtoVersion.V2:
            if isinstance(program, schedules.Schedule):
                program.device.validate_schedule(program)
            program = gate_set.serialize(program)
            return _any_dict_from_msg(program)
        else:
            raise ValueError('invalid program proto version: {}'.format(
                self.proto_version))
Example #3
0
    def _serialize_program(self, program: 'cirq.Circuit',
                           gate_set: sgs.SerializableGateSet) -> any_pb2.Any:
        if not isinstance(program, circuits.Circuit):
            raise TypeError(f'Unrecognized program type: {type(program)}')
        program.device.validate_circuit(program)

        if self.context.proto_version == ProtoVersion.V2:
            program = gate_set.serialize(program)
            return self._pack_any(program)
        else:
            raise ValueError('invalid program proto version: {}'.format(
                self.context.proto_version))
Example #4
0
File: engine.py Project: wabei/Cirq
    def _serialize_program_v2(
        self, program: Program, sweeps: List[Sweep], repetitions: int,
        gate_set: SerializableGateSet
    ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
        if isinstance(program, Schedule):
            program.device.validate_schedule(program)

        program = gate_set.serialize(program)

        run_context = v2.run_context_pb2.RunContext()
        for sweep in sweeps:
            sweep_proto = run_context.parameter_sweeps.add()
            sweep_proto.repetitions = repetitions
            api_v2.sweep_to_proto(sweep, out=sweep_proto.sweep)

        def any_dict(message: gp.message.Message) -> Dict[str, Any]:
            any_message = any_pb2.Any()
            any_message.Pack(message)
            return gp.json_format.MessageToDict(any_message)

        return any_dict(program), any_dict(run_context)
Example #5
0
    def _serialize_program(
        self,
        program: 'cirq.Circuit',
        gate_set: serializable_gate_set.SerializableGateSet = None
    ) -> qtypes.any_pb2.Any:
        gate_set = gate_set or gate_sets.XMON
        code = qtypes.any_pb2.Any()

        if not isinstance(program, circuits.Circuit):
            raise TypeError(f'Unrecognized program type: {type(program)}')
        program.device.validate_circuit(program)

        if self.context.proto_version == ProtoVersion.V1:
            code.Pack(
                v1.program_pb2.Program(operations=[
                    op for op in v1.circuit_as_schedule_to_protos(program)
                ]))
        elif self.context.proto_version == ProtoVersion.V2:
            program = gate_set.serialize(program)
            code.Pack(program)
        else:
            raise ValueError('invalid program proto version: {}'.format(
                self.context.proto_version))
        return code