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. 

13from __future__ import annotations 

14 

15from typing import Any, Dict 

16 

17from braket.circuits.qubit import QubitInput 

18from braket.circuits.qubit_set import QubitSetInput 

19 

20 

21class Result: 

22 """ 

23 Class `Result` represents a requested result for the circuit. 

24 This class is considered the result definition containing 

25 the metadata that defines what a requested result is and what it does. 

26 """ 

27 

28 def __init__(self, ascii_symbol: str): 

29 """ 

30 Args: 

31 ascii_symbol (str): ASCII string symbol for the result. This is used when 

32 printing a diagram of circuits. 

33 

34 Raises: 

35 ValueError: `ascii_symbol` is None 

36 """ 

37 

38 if ascii_symbol is None: 

39 raise ValueError(f"ascii_symbol must not be None") 

40 

41 self._ascii_symbol = ascii_symbol 

42 

43 @property 

44 def ascii_symbol(self) -> str: 

45 """Tuple[str]: Returns the ascii symbol for the requested result.""" 

46 return self._ascii_symbol 

47 

48 @property 

49 def name(self) -> str: 

50 """ 

51 Returns the name of the result 

52 

53 Returns: 

54 The name of the result as a string 

55 """ 

56 return self.__class__.__name__ 

57 

58 def to_ir(self, *args, **kwargs) -> Any: 

59 """Returns IR object of the requested result 

60 

61 Args: 

62 *args: Positional arguments 

63 **kwargs: Keyword arguments 

64 

65 Returns: 

66 IR object of the requested result 

67 """ 

68 raise NotImplementedError("to_ir has not been implemented yet.") 

69 

70 def copy(self, target_mapping: Dict[QubitInput, QubitInput] = {}, target: QubitSetInput = None): 

71 """ 

72 Return a shallow copy of the requested result. 

73 

74 Note: 

75 If `target_mapping` is specified, then `self.target` is mapped to the specified 

76 qubits. This is useful apply an instruction to a circuit and change the target qubits. 

77 

78 Args: 

79 target_mapping (dictionary[int or Qubit, int or Qubit], optional): A dictionary of 

80 qubit mappings to apply to the target. Key is the qubit in this `target` and the 

81 value is what the key is changed to. Default = {}. 

82 target (int, Qubit, or iterable of int / Qubit, optional): Target qubits for the new 

83 instruction. 

84 

85 Returns: 

86 Result: A shallow copy of the result. 

87 

88 Raises: 

89 TypeError: If both `target_mapping` and `target` are supplied. 

90 

91 Examples: 

92 >>> result = Result.Probabilities(targets=[0]) 

93 >>> new_result = result.copy() 

94 >>> new_result.targets 

95 QubitSet(Qubit(0)) 

96 >>> new_result = result.copy(target_mapping={0: 5}) 

97 >>> new_result.target 

98 QubitSet(Qubit(5)) 

99 >>> new_result = result.copy(target=[5]) 

100 >>> new_result.target 

101 QubitSet(Qubit(5)) 

102 """ 

103 copy = self.__copy__() 

104 if target_mapping and target is not None: 

105 raise TypeError("Only 'target_mapping' or 'target' can be supplied, but not both.") 

106 elif target is not None: 

107 if hasattr(copy, "target"): 

108 copy.target = target 

109 else: 

110 if hasattr(copy, "target"): 

111 copy.target = self._target.map(target_mapping) 

112 return copy 

113 

114 @classmethod 

115 def register_result(cls, result: "Result"): 

116 """Register a result implementation by adding it into the Result class. 

117 

118 Args: 

119 result (Result): Result instance to register. 

120 """ 

121 setattr(cls, result.__name__, result) 

122 

123 def __repr__(self) -> str: 

124 return f"{self.name}()"