Exemplo n.º 1
0
    def create_calibration_program(
        self,
        layers: List['cirq_google.CalibrationLayer'],
        program_id: Optional[str] = None,
        gate_set: Optional[Serializer] = None,
        description: Optional[str] = None,
        labels: Optional[Dict[str, str]] = None,
    ) -> engine_program.EngineProgram:
        """Wraps a list of calibration layers into an Any for Quantum Engine.

        Args:
            layers: The calibration routines to execute.  All layers will be
                executed within the same API call in the order specified,
                though some layers may be interleaved together using
                hardware-specific batching.
            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
                'calibration-################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 circuits in each
                layer.  The gate set must be supported by the 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.

        Raises:
            ValueError: If not gate set is given.
        """
        if not gate_set:
            raise ValueError('Gate set must be specified.')
        if not program_id:
            program_id = _make_random_id('calibration-')

        calibration = v2.calibration_pb2.FocusedCalibration()
        for layer in layers:
            new_layer = calibration.layers.add()
            new_layer.calibration_type = layer.calibration_type
            for arg in layer.args:
                arg_to_proto(layer.args[arg], out=new_layer.args[arg])
            gate_set.serialize(layer.program, msg=new_layer.layer)

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

        return engine_program.EngineProgram(
            self.project_id,
            new_program_id,
            self.context,
            new_program,
            result_type=ResultType.Calibration,
        )
Exemplo n.º 2
0
    def get_program(self, program_id: str) -> engine_program.EngineProgram:
        """Returns an EngineProgram for an existing Quantum Engine program.

        Args:
            program_id: Unique ID of the program within the parent project.

        Returns:
            A EngineProgram for the program.
        """
        return engine_program.EngineProgram(self.project_id, program_id, self.context)
Exemplo n.º 3
0
    def create_batch_program(
        self,
        programs: Sequence[cirq.AbstractCircuit],
        program_id: Optional[str] = None,
        gate_set: Optional[Serializer] = 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.

        Raises:
            ValueError: If no gate set is provided.
        """
        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)
Exemplo n.º 4
0
    def create_program(
        self,
        program: cirq.AbstractCircuit,
        program_id: Optional[str] = None,
        gate_set: Optional[Serializer] = 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.

        Raises:
            ValueError: If no gate set is provided.
        """
        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)
Exemplo n.º 5
0
    def list_programs(
        self,
        created_before: Optional[Union[datetime.datetime,
                                       datetime.date]] = None,
        created_after: Optional[Union[datetime.datetime,
                                      datetime.date]] = None,
        has_labels: Optional[Dict[str, str]] = None,
    ) -> List[engine_program.EngineProgram]:
        """Returns a list of previously executed quantum programs.

        Args:
            created_after: retrieve programs that were created after this date
                or time.
            created_before: retrieve programs that were created after this date
                or time.
            has_labels: retrieve programs that have labels on them specified by
                this dict. If the value is set to `*`, filters having the label
                regardless of the label value will be filtered. For example, to
                query programs that have the shape label and have the color
                label with value red can be queried using
                `{'color: red', 'shape:*'}`
        """

        client = self.context.client
        response = client.list_programs(
            self.project_id,
            created_before=created_before,
            created_after=created_after,
            has_labels=has_labels,
        )
        return [
            engine_program.EngineProgram(
                project_id=client._ids_from_program_name(p.name)[0],
                program_id=client._ids_from_program_name(p.name)[1],
                _program=p,
                context=self.context,
            ) for p in response
        ]