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 typing import Any, Dict, Union 

15 

16from braket.annealing.problem import Problem 

17from braket.aws.aws_quantum_task import AwsQuantumTask 

18from braket.aws.aws_session import AwsSession 

19from braket.circuits import Circuit 

20from braket.devices.device import Device 

21 

22 

23class AwsQuantumSimulator(Device): 

24 """ 

25 Amazon Braket implementation of a quantum simulator. 

26 Use this class to retrieve the latest metadata about the simulator, 

27 and to run a task on the simulator. 

28 """ 

29 

30 DEFAULT_SHOTS_SIMULATOR = 0 

31 DEFAULT_RESULTS_POLL_TIMEOUT_SIMULATOR = 432000 

32 DEFAULT_RESULTS_POLL_INTERVAL_SIMULATOR = 1 

33 

34 def __init__(self, arn: str, aws_session=None): 

35 """ 

36 Args: 

37 arn (str): The ARN of the simulator, for example, 

38 "arn:aws:aqx:::quantum-simulator:aqx:qs1". 

39 aws_session (AwsSession, optional) aws_session: An AWS session object. Default = None. 

40 """ 

41 super().__init__(name=None, status=None, status_reason=None) 

42 self._arn = arn 

43 self._aws_session = aws_session or AwsSession() 

44 self._properties: Dict[str, Any] = None 

45 self.refresh_metadata() 

46 

47 def run( 

48 self, 

49 task_specification: Union[Circuit, Problem], 

50 s3_destination_folder: AwsSession.S3DestinationFolder, 

51 shots: int = DEFAULT_SHOTS_SIMULATOR, 

52 poll_timeout_seconds: int = DEFAULT_RESULTS_POLL_TIMEOUT_SIMULATOR, 

53 poll_interval_seconds: int = DEFAULT_RESULTS_POLL_INTERVAL_SIMULATOR, 

54 *aws_quantum_task_args, 

55 **aws_quantum_task_kwargs, 

56 ) -> AwsQuantumTask: 

57 """ 

58 Run a task on the simulator device. 

59 

60 Args: 

61 task_specification (Union[Circuit, Problem]): Specification of task 

62 (circuit or annealing problem) to run on device. 

63 s3_destination_folder: The S3 location to save the task's results 

64 shots (int, optional): The number of times to run the circuit or annealing problem. 

65 Default is 0. 

66 For circuits, when `shots=0`, the simulator will support simulator-only 

67 result types, compute the exact results based on the task specification, 

68 and sampling is not supported. 

69 `shots>0` means that the simulator will be treated like a QPU and 

70 only support result types available for a QPU. 

71 poll_timeout_seconds (int): The polling timeout for AwsQuantumTask.result(), in seconds. 

72 Default: 432000 (5 days). 

73 poll_interval_seconds (int): The polling interval for AwsQuantumTask.result(), 

74 in seconds. Default: 1. 

75 *aws_quantum_task_args: Variable length positional arguments for 

76 `braket.aws.aws_quantum_task.AwsQuantumTask.create()`. 

77 **aws_quantum_task_kwargs: Variable length keyword arguments for 

78 `braket.aws.aws_quantum_task.AwsQuantumTask.create()`. 

79 

80 Returns: 

81 AwsQuantumTask: An AwsQuantumTask that tracks the task execution on the device. 

82 

83 Examples: 

84 >>> circuit = Circuit().h(0).cnot(0, 1) 

85 >>> device = AwsQuantumSimulator("arn:aws:aqx:::quantum-simulator:aqx:qs1") 

86 >>> device.run(circuit, ("bucket", "key"), shots=1000) 

87 

88 >>> circuit = Circuit().h(0).cnot(0, 1) 

89 >>> device = AwsQuantumSimulator("arn:aws:aqx:::quantum-simulator:aqx:qs1") 

90 >>> device.run(circuit=circuit, s3_destination_folder=("bucket", "key"), shots=1000) 

91 

92 See Also: 

93 `braket.aws.aws_quantum_task.AwsQuantumTask.create()` 

94 """ 

95 return AwsQuantumTask.create( 

96 self._aws_session, 

97 self._arn, 

98 task_specification, 

99 s3_destination_folder, 

100 shots, 

101 poll_timeout_seconds=poll_timeout_seconds, 

102 poll_interval_seconds=poll_interval_seconds, 

103 *aws_quantum_task_args, 

104 **aws_quantum_task_kwargs, 

105 ) 

106 

107 def refresh_metadata(self) -> None: 

108 """Refresh the AwsQuantumSimulator object with the most recent simulator metadata.""" 

109 simulator_metadata = self._aws_session.get_simulator_metadata(self._arn) 

110 self._name = simulator_metadata.get("name") 

111 self._status = simulator_metadata.get("status") 

112 self._status_reason = simulator_metadata.get("statusReason") 

113 self._properties = { 

114 k: simulator_metadata.get(k) for k in ["supportedQuantumOperations", "qubitCount"] 

115 } 

116 

117 @property 

118 def arn(self) -> str: 

119 """str: Returns the ARN of the simulator.""" 

120 return self._arn 

121 

122 @property 

123 # TODO: Add a link to the boto3 docs 

124 def properties(self) -> Dict[str, Any]: 

125 """Dict[str, Any]: Return the simulator properties""" 

126 return self._properties 

127 

128 def __repr__(self): 

129 return f"QuantumSimulator('name': {self.name}, 'arn': {self.arn})" 

130 

131 def __eq__(self, other): 

132 if isinstance(other, AwsQuantumSimulator): 

133 return self.arn == other.arn 

134 return NotImplemented