Coverage for src/braket/aws/aws_quantum_simulator.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.
14from typing import Any, Dict, Union
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
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 """
30 def __init__(self, arn: str, aws_session=None):
31 """
32 Args:
33 arn (str): The ARN of the simulator, for example,
34 "arn:aws:aqx:::quantum-simulator:aqx:qs1".
35 aws_session (AwsSession, optional) aws_session: An AWS session object. Default = None.
36 """
37 super().__init__(name=None, status=None, status_reason=None)
38 self._arn = arn
39 self._aws_session = aws_session or AwsSession()
40 self._properties: Dict[str, Any] = None
41 self.refresh_metadata()
43 def run(
44 self,
45 task_specification: Union[Circuit, Problem],
46 s3_destination_folder: AwsSession.S3DestinationFolder,
47 shots: int = 0,
48 *aws_quantum_task_args,
49 **aws_quantum_task_kwargs,
50 ) -> AwsQuantumTask:
51 """
52 Run a task on the simulator device.
54 Args:
55 task_specification (Union[Circuit, Problem]): Specification of task
56 (circuit or annealing problem) to run on device.
57 s3_destination_folder: The S3 location to save the task's results
58 shots (int, optional): The number of times to run the circuit or annealing problem.
59 Default is 0.
60 For circuits, when `shots=0`, the simulator will support simulator-only
61 result types, compute the exact results based on the task specification,
62 and sampling is not supported.
63 `shots>0` means that the simulator will be treated like a QPU and
64 only support result types available for a QPU.
65 *aws_quantum_task_args: Variable length positional arguments for
66 `braket.aws.aws_quantum_task.AwsQuantumTask.create()`.
67 **aws_quantum_task_kwargs: Variable length keyword arguments for
68 `braket.aws.aws_quantum_task.AwsQuantumTask.create()`.
70 Returns:
71 AwsQuantumTask: An AwsQuantumTask that tracks the task execution on the device.
73 Examples:
74 >>> circuit = Circuit().h(0).cnot(0, 1)
75 >>> device = AwsQuantumSimulator("arn:aws:aqx:::quantum-simulator:aqx:qs1")
76 >>> device.run(circuit, ("bucket", "key"), shots=1000)
78 >>> circuit = Circuit().h(0).cnot(0, 1)
79 >>> device = AwsQuantumSimulator("arn:aws:aqx:::quantum-simulator:aqx:qs1")
80 >>> device.run(circuit=circuit, s3_destination_folder=("bucket", "key"), shots=1000)
82 See Also:
83 `braket.aws.aws_quantum_task.AwsQuantumTask.create()`
84 """
85 return AwsQuantumTask.create(
86 self._aws_session,
87 self._arn,
88 task_specification,
89 s3_destination_folder,
90 shots,
91 *aws_quantum_task_args,
92 **aws_quantum_task_kwargs,
93 )
95 def refresh_metadata(self) -> None:
96 """Refresh the AwsQuantumSimulator object with the most recent simulator metadata."""
97 simulator_metadata = self._aws_session.get_simulator_metadata(self._arn)
98 self._name = simulator_metadata.get("name")
99 self._status = simulator_metadata.get("status")
100 self._status_reason = simulator_metadata.get("statusReason")
101 self._properties = {
102 k: simulator_metadata.get(k) for k in ["supportedQuantumOperations", "qubitCount"]
103 }
105 @property
106 def arn(self) -> str:
107 """str: Returns the ARN of the simulator."""
108 return self._arn
110 @property
111 # TODO: Add a link to the boto3 docs
112 def properties(self) -> Dict[str, Any]:
113 """Dict[str, Any]: Return the simulator properties"""
114 return self._properties
116 def __repr__(self):
117 return f"QuantumSimulator('name': {self.name}, 'arn': {self.arn})"
119 def __eq__(self, other):
120 if isinstance(other, AwsQuantumSimulator):
121 return self.arn == other.arn
122 return NotImplemented