Hide keyboard shortcuts

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. 

13 

14from dataclasses import dataclass 

15from typing import Counter, Dict 

16 

17import numpy as np 

18 

19 

20@dataclass 

21class QuantumTaskResult: 

22 """ 

23 Result of a quantum task execution. This class is intended 

24 to be initialized by a QuantumTask class. 

25 

26 Args: 

27 measurements (numpy.ndarray): 2d array - row is shot, column is qubit. 

28 measurement_counts (Counter): A Counter of measurements. Key is the measurements 

29 in a big endian binary string. Value is the number of times that measurement occurred. 

30 measurement_probabilities (Dict[str, float]): A dictionary of probabilistic results. 

31 Key is the measurements in a big endian binary string. 

32 Value is the probability the measurement occurred. 

33 measurements_copied_from_device (bool): flag whether `measurements` were copied from device. 

34 If false, `measurements` are calculated from device data. 

35 measurement_counts_copied_from_device (bool): flag whether `measurement_counts` were copied 

36 from device. If false, `measurement_counts` are calculated from device data. 

37 measurement_probabilities_copied_from_device (bool): flag whether 

38 `measurement_probabilities` were copied from device. If false, 

39 `measurement_probabilities` are calculated from device data. 

40 """ 

41 

42 measurements: np.ndarray 

43 measurement_counts: Counter 

44 measurement_probabilities: Dict[str, float] 

45 measurements_copied_from_device: bool 

46 measurement_counts_copied_from_device: bool 

47 measurement_probabilities_copied_from_device: bool 

48 

49 def __eq__(self, other) -> bool: 

50 if isinstance(other, QuantumTaskResult): 

51 # __eq__ on numpy arrays results in an array of booleans and therefore can't use 

52 # the default dataclass __eq__ implementation. Override equals to check if all 

53 # elements in the array are equal. 

54 return (self.measurements == other.measurements).all() 

55 return NotImplemented 

56 

57 @staticmethod 

58 def measurement_counts_from_measurements(measurements: np.ndarray) -> Counter: 

59 """ 

60 Creates measurement counts from measurements 

61 

62 Args: 

63 measurements (numpy.ndarray): 2d array - row is shot, column is qubit. 

64 

65 Returns: 

66 Counter: A Counter of measurements. Key is the measurements in a big endian binary 

67 string. Value is the number of times that measurement occurred. 

68 """ 

69 bitstrings = [] 

70 for j in range(len(measurements)): 

71 bitstrings.append("".join([str(element) for element in measurements[j]])) 

72 return Counter(bitstrings) 

73 

74 @staticmethod 

75 def measurement_probabilities_from_measurement_counts( 

76 measurement_counts: Counter, 

77 ) -> Dict[str, float]: 

78 """ 

79 Creates measurement probabilities from measurement counts 

80 

81 Args: 

82 measurement_counts (Counter): A Counter of measurements. Key is the measurements 

83 in a big endian binary string. Value is the number of times that measurement 

84 occurred. 

85 

86 Returns: 

87 Dict[str, float]: A dictionary of probabilistic results. Key is the measurements 

88 in a big endian binary string. Value is the probability the measurement occurred. 

89 """ 

90 measurement_probabilities = {} 

91 shots = sum(measurement_counts.values()) 

92 

93 for key, count in measurement_counts.items(): 

94 measurement_probabilities[key] = count / shots 

95 return measurement_probabilities 

96 

97 @staticmethod 

98 def measurements_from_measurement_probabilities( 

99 measurement_probabilities: Dict[str, float], shots: int 

100 ) -> np.ndarray: 

101 """ 

102 Creates measurements from measurement probabilities 

103 

104 Args: 

105 measurement_probabilities (Dict[str, float]): A dictionary of probabilistic results. 

106 Key is the measurements in a big endian binary string. 

107 Value is the probability the measurement occurred. 

108 shots (int): number of iterations on device 

109 

110 Returns: 

111 Dict[str, float]: A dictionary of probabilistic results. 

112 Key is the measurements in a big endian binary string. 

113 Value is the probability the measurement occurred. 

114 """ 

115 measurements_list = [] 

116 for bitstring in measurement_probabilities: 

117 measurement = list(bitstring) 

118 individual_measurement_list = [measurement] * int( 

119 round(measurement_probabilities[bitstring] * shots) 

120 ) 

121 measurements_list.extend(individual_measurement_list) 

122 return np.asarray(measurements_list, dtype=int)