コード例 #1
0
    def result(
        self,
        poll_timeout_seconds: float = QuantumJob.DEFAULT_RESULTS_POLL_TIMEOUT,
        poll_interval_seconds: float = QuantumJob.
        DEFAULT_RESULTS_POLL_INTERVAL,
    ) -> Dict[str, Any]:
        """Retrieves the job result persisted using save_job_result() function.

        Args:
            poll_timeout_seconds (float): The polling timeout, in seconds, for `result()`.
                Default: 10 days.
            poll_interval_seconds (float): The polling interval, in seconds, for `result()`.
                Default: 5 seconds.

        Returns:
            Dict[str, Any]: Dict specifying the job results.
        """
        try:
            with open(os.path.join(self.name, "results.json"), "r") as f:
                persisted_data = PersistedJobData.parse_raw(f.read())
                deserialized_data = deserialize_values(
                    persisted_data.dataDictionary, persisted_data.dataFormat)
                return deserialized_data
        except FileNotFoundError:
            raise ValueError(
                f"Unable to find results in the local job directory {self.name}."
            )
コード例 #2
0
 def _read_and_deserialize_results(temp_dir, job_name):
     try:
         with open(
                 f"{temp_dir}/{job_name}/{AwsQuantumJob.RESULTS_FILENAME}",
                 "r") as f:
             persisted_data = PersistedJobData.parse_raw(f.read())
             deserialized_data = deserialize_values(
                 persisted_data.dataDictionary, persisted_data.dataFormat)
             return deserialized_data
     except FileNotFoundError:
         return {}
コード例 #3
0
def load_job_checkpoint(job_name: str,
                        checkpoint_file_suffix: str = "") -> Dict[str, Any]:
    """
    Loads the job checkpoint data stored for the job named 'job_name', with the checkpoint
    file that ends with the `checkpoint_file_suffix`. The `job_name` can refer to any job whose
    checkpoint data you expect to be available in the file path specified by the `CHECKPOINT_DIR`
    container environment variable.

    Note: This function for loading job checkpoints is only for use inside the job container
          as it writes data to directories and references env variables set in the containers.


    Args:
        job_name (str): str that specifies the name of the job whose checkpoints
            are to be loaded.
        checkpoint_file_suffix (str): str specifying the file suffix that is used to
            locate the checkpoint file to load. The resulting file name
            `f"{job_name}(_{checkpoint_file_suffix}).json"` is used to locate the
            checkpoint file. Default: ""

    Returns:
        Dict[str, Any]: Dict that contains the checkpoint data persisted in the checkpoint file.

    Raises:
        FileNotFoundError: If the file `f"{job_name}(_{checkpoint_file_suffix})"` could not be found
            in the directory specified by the container environment variable `CHECKPOINT_DIR`.
        ValueError: If the data stored in the checkpoint file can't be deserialized (possibly due to
            corruption).
    """
    checkpoint_directory = os.environ["AMZN_BRAKET_CHECKPOINT_DIR"]
    checkpoint_file_path = (
        f"{checkpoint_directory}/{job_name}_{checkpoint_file_suffix}.json" if
        checkpoint_file_suffix else f"{checkpoint_directory}/{job_name}.json")
    with open(checkpoint_file_path, "r") as f:
        persisted_data = PersistedJobData.parse_raw(f.read())
        deserialized_data = deserialize_values(persisted_data.dataDictionary,
                                               persisted_data.dataFormat)
        return deserialized_data
コード例 #4
0
def test_job_deserialize_data(data_format, submitted_data,
                              expected_deserialized_data):
    deserialized_data = deserialize_values(submitted_data, data_format)
    assert deserialized_data == expected_deserialized_data