Coverage for src/braket/aws/aws_quantum_task_result.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"). You
4# may not use this file except in compliance with the License. A copy of
5# the License is located at
6#
7# http://aws.amazon.com/apache2.0/
8#
9# or in the "license" file accompanying this file. This file is
10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11# ANY KIND, either express or implied. See the License for the specific
12# language governing permissions and limitations under the License.
14import json
15from dataclasses import dataclass
16from typing import Any, Dict
18import numpy as np
19from braket.tasks.quantum_task_result import QuantumTaskResult
22@dataclass
23class AwsQuantumTaskResult(QuantumTaskResult):
24 """
25 Result of an AWS quantum task execution. This class is intended
26 to be initialized by a QuantumTask class.
28 Args:
29 measurements (numpy.ndarray): 2d array - row is shot, column is qubit.
30 measurement_counts (Counter): A Counter of measurements. Key is the measurements
31 in a big endian binary string. Value is the number of times that measurement occurred.
32 measurement_probabilities (Dict[str, float]): A dictionary of probabilistic results.
33 Key is the measurements in a big endian binary string.
34 Value is the probability the measurement occurred.
35 measurements_copied_from_device (bool): flag whether `measurements` were copied from device.
36 If false, `measurements` are calculated from device data.
37 measurement_counts_copied_from_device (bool): flag whether `measurement_counts` were copied
38 from device. If false, `measurement_counts` are calculated from device data.
39 measurement_probabilities_copied_from_device (bool): flag whether
40 `measurement_probabilities` were copied from device. If false,
41 `measurement_probabilities` are calculated from device data.
42 task_metadata (Dict[str, Any]): Dictionary of task metadata. TODO: Link boto3 docs.
43 state_vector (Dict[str, complex]): Dictionary where key is state and value is amplitude.
44 """
46 task_metadata: Dict[str, Any]
47 state_vector: Dict[str, complex] = None
49 @staticmethod
50 def from_string(result: str) -> "AwsQuantumTaskResult":
51 """
52 Create AwsQuantumTaskResult from string with the S3 format defined by the Amazon Braket
53 Service. TODO: Link Amazon Braket S3 format docs.
55 Args:
56 result (str): JSON object string, whose keys are AwsQuantumTaskResult attributes.
58 Returns:
59 AwsQuantumTaskResult: A AwsQuantumTaskResult based on a string loaded from S3.
60 """
61 json_obj = json.loads(result)
62 state_vector = json_obj.get("StateVector", None)
63 task_metadata = json_obj["TaskMetadata"]
64 if "Measurements" in json_obj:
65 measurements = np.asarray(json_obj["Measurements"], dtype=int)
66 m_counts = QuantumTaskResult.measurement_counts_from_measurements(measurements)
67 m_probabilities = QuantumTaskResult.measurement_probabilities_from_measurement_counts(
68 m_counts
69 )
70 measurements_copied_from_device = True
71 m_counts_copied_from_device = False
72 m_probabilities_copied_from_device = False
73 if "MeasurementProbabilities" in json_obj:
74 shots = task_metadata["Shots"]
75 m_probabilities = json_obj["MeasurementProbabilities"]
76 measurements = QuantumTaskResult.measurements_from_measurement_probabilities(
77 m_probabilities, shots
78 )
79 m_counts = QuantumTaskResult.measurement_counts_from_measurements(measurements)
80 measurements_copied_from_device = False
81 m_counts_copied_from_device = False
82 m_probabilities_copied_from_device = True
83 if "StateVector" in json_obj:
84 state_vector = json_obj.get("StateVector", None)
85 for state in state_vector:
86 state_vector[state] = complex(*state_vector[state])
87 return AwsQuantumTaskResult(
88 state_vector=state_vector,
89 task_metadata=task_metadata,
90 measurements=measurements,
91 measurement_counts=m_counts,
92 measurement_probabilities=m_probabilities,
93 measurements_copied_from_device=measurements_copied_from_device,
94 measurement_counts_copied_from_device=m_counts_copied_from_device,
95 measurement_probabilities_copied_from_device=m_probabilities_copied_from_device,
96 )
98 def __eq__(self, other) -> bool:
99 if isinstance(other, AwsQuantumTaskResult):
100 self_fields = (self.task_metadata, self.state_vector)
101 other_fields = (other.task_metadata, other.state_vector)
102 return self_fields == other_fields and super().__eq__(other)
103 return NotImplemented