Example #1
0
    def create_program(
        self,
        program: TProgram,
        program_id: Optional[str] = None,
        gate_set: serializable_gate_set.SerializableGateSet = gate_sets.XMON
    ) -> engine_program.EngineProgram:
        """Wraps a Circuit or Scheduler for use with the Quantum Engine.

        Args:
            program: The Circuit or Schedule to execute. If a circuit is
                provided, a moment by moment schedule will be used.
            program_id: A user-provided identifier for the program. This must be
                unique within the Google Cloud project being used. If this
                parameter is not provided, a random id of the format
                'prog-######' will be generated.
            gate_set: The gate set used to serialize the circuit. The gate set
                must be supported by the selected processor
        """
        if not program_id:
            program_id = _make_random_id('prog-')

        parent_name = 'projects/%s' % self.project_id
        program_name = '%s/programs/%s' % (parent_name, program_id)
        # Create program.
        request = {
            'name': program_name,
            'code': self._serialize_program(program, gate_set),
        }
        result = self.service.projects().programs().create(
            parent=parent_name, body=request).execute()

        return engine_program.EngineProgram(result['name'], self)
Example #2
0
    def create_batch_program(
        self,
        programs: List['cirq.Circuit'],
        program_id: Optional[str] = None,
        gate_set: Optional[sgs.SerializableGateSet] = None,
        description: Optional[str] = None,
        labels: Optional[Dict[str, str]] = None,
    ) -> engine_program.EngineProgram:
        """Wraps a list of Circuits into a BatchProgram for the Quantum Engine.

        Args:
            programs: The Circuits to execute within a batch.
            program_id: A user-provided identifier for the program. This must be
                unique within the Google Cloud project being used. If this
                parameter is not provided, a random id of the format
                'prog-################YYMMDD' will be generated, where # is
                alphanumeric and YYMMDD is the current year, month, and day.
            gate_set: The gate set used to serialize the circuit. The gate set
                must be supported by the selected processor
            description: An optional description to set on the program.
            labels: Optional set of labels to set on the program.

        Returns:
            A EngineProgram for the newly created program.
        """
        if not gate_set:
            raise ValueError('Gate set must be specified.')
        if not program_id:
            program_id = _make_random_id('prog-')

        batch = v2.batch_pb2.BatchProgram()
        for program in programs:
            gate_set.serialize(program, msg=batch.programs.add())

        new_program_id, new_program = self.context.client.create_program(
            self.project_id,
            program_id,
            code=self._pack_any(batch),
            description=description,
            labels=labels,
        )

        return engine_program.EngineProgram(self.project_id,
                                            new_program_id,
                                            self.context,
                                            new_program,
                                            result_type=ResultType.Batch)
Example #3
0
    def create_program(
        self,
        program: 'cirq.Circuit',
        program_id: Optional[str] = None,
        gate_set: Optional[sgs.SerializableGateSet] = None,
        description: Optional[str] = None,
        labels: Optional[Dict[str, str]] = None,
    ) -> engine_program.EngineProgram:
        """Wraps a Circuit for use with the Quantum Engine.

        Args:
            program: The Circuit to execute.
            program_id: A user-provided identifier for the program. This must be
                unique within the Google Cloud project being used. If this
                parameter is not provided, a random id of the format
                'prog-################YYMMDD' will be generated, where # is
                alphanumeric and YYMMDD is the current year, month, and day.
            gate_set: The gate set used to serialize the circuit. The gate set
                must be supported by the selected processor
            description: An optional description to set on the program.
            labels: Optional set of labels to set on the program.

        Returns:
            A EngineProgram for the newly created program.
        """
        if not gate_set:
            raise ValueError('No gate set provided')

        if not program_id:
            program_id = _make_random_id('prog-')

        new_program_id, new_program = self.context.client.create_program(
            self.project_id,
            program_id,
            code=self._serialize_program(program, gate_set),
            description=description,
            labels=labels,
        )

        return engine_program.EngineProgram(
            self.project_id, new_program_id, self.context, new_program
        )