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 __future__ import annotations 

15 

16from typing import List 

17 

18import braket.ir.jaqcd as ir 

19from braket.circuits import circuit 

20from braket.circuits.qubit_set import QubitSet, QubitSetInput 

21from braket.circuits.result import Result 

22 

23 

24""" 

25To add a new result: 

26 1. Implement the class and extend `Result` 

27 2. Add a method with the `@circuit.subroutine(register=True)` decorator. Method name 

28 will be added into the `Circuit` class. This method is the default way 

29 clients add this result to a circuit. 

30 3. Register the class with the `Result` class via `Result.register_result()`. 

31""" 

32 

33 

34class StateVector(Result): 

35 """StateVector as a requested result.""" 

36 

37 def __init__(self): 

38 super().__init__(ascii_symbol=["StateVector"]) 

39 

40 def to_ir(self) -> ir.StateVector: 

41 return ir.StateVector() 

42 

43 @staticmethod 

44 @circuit.subroutine(register=True) 

45 def state_vector() -> Result: 

46 """Registers this function into the circuit class. 

47 

48 Returns: 

49 Result: state vector as a requested result 

50 

51 Examples: 

52 >>> circ = Circuit().state_vector() 

53 """ 

54 return Result.StateVector() 

55 

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

57 if isinstance(other, StateVector): 

58 return True 

59 return False 

60 

61 def __copy__(self) -> StateVector: 

62 return type(self)() 

63 

64 

65Result.register_result(StateVector) 

66 

67 

68class Amplitude(Result): 

69 """Amplitude as a requested result.""" 

70 

71 def __init__(self, state: List[str]): 

72 """ 

73 Args: 

74 state (List[str]): list of quantum states as strings with "0" and "1" 

75 

76 Raises: 

77 ValueError: If state is None or an empty list 

78 

79 Examples: 

80 >>> Result.Amplitude(state=['01', '10']) 

81 """ 

82 super().__init__(ascii_symbol=["Amplitude"]) 

83 if not state: 

84 raise ValueError("A non-empty list of state must be specified e.g. ['01', '10']") 

85 self._state = state 

86 

87 @property 

88 def state(self) -> List[str]: 

89 return self._state 

90 

91 def to_ir(self) -> ir.Amplitude: 

92 return ir.Amplitude(states=self.state) 

93 

94 @staticmethod 

95 @circuit.subroutine(register=True) 

96 def amplitude(state: List[str]) -> Result: 

97 """Registers this function into the circuit class. 

98 

99 Args: 

100 state (List[str]): list of quantum states as strings with "0" and "1" 

101 

102 Returns: 

103 Result: state vector as a requested result 

104 

105 Examples: 

106 >>> circ = Circuit().amplitude(state=["01", "10"]) 

107 """ 

108 return Result.Amplitude(state=state) 

109 

110 def __eq__(self, other): 

111 if isinstance(other, Amplitude): 

112 return self.state == other.state 

113 return False 

114 

115 def __repr__(self): 

116 return f"Amplitude(state={self.state})" 

117 

118 def __copy__(self): 

119 return type(self)(state=self.state) 

120 

121 

122Result.register_result(Amplitude) 

123 

124 

125class Probability(Result): 

126 """Probability as a requested result.""" 

127 

128 def __init__(self, target: QubitSetInput = []): 

129 """ 

130 Args: 

131 target (int, Qubit, or iterable of int / Qubit): Target qubits that the result 

132 is requested for. Default is [], which means all qubits for the circuit. 

133 

134 Examples: 

135 >>> Result.Probability(target=[0, 1]) 

136 """ 

137 super().__init__(ascii_symbol=["Prob"]) 

138 self._target = QubitSet(target) 

139 

140 @property 

141 def target(self) -> QubitSet: 

142 return self._target 

143 

144 @target.setter 

145 def target(self, target: QubitSetInput = []) -> None: 

146 self._target = QubitSet(target) 

147 

148 def to_ir(self) -> ir.Probability: 

149 return ir.Probability(targets=list(self.target)) 

150 

151 @staticmethod 

152 @circuit.subroutine(register=True) 

153 def probability(target: QubitSetInput) -> Result: 

154 """Registers this function into the circuit class. 

155 

156 Args: 

157 target (int, Qubit, or iterable of int / Qubit): Target qubits that the result 

158 is requested for. 

159 

160 Returns: 

161 Result: probability as a requested result 

162 

163 Examples: 

164 >>> circ = Circuit().probability(target=[0, 1]) 

165 """ 

166 return Result.Probability(target=target) 

167 

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

169 if isinstance(other, Probability): 

170 return self.target == other.target 

171 return False 

172 

173 def __repr__(self) -> str: 

174 return f"Probability(target={self.target})" 

175 

176 def __copy__(self) -> Probability: 

177 return type(self)(target=self.target) 

178 

179 

180Result.register_result(Probability)