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, Optional, 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: Optional[int] = None,
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 (Optional[int]): The number of times to run the circuit or annealing problem
59 *aws_quantum_task_args: Variable length positional arguments for
60 `braket.aws.aws_quantum_task.AwsQuantumTask.create()`.
61 **aws_quantum_task_kwargs: Variable length keyword arguments for
62 `braket.aws.aws_quantum_task.AwsQuantumTask.create()`.
64 Returns:
65 AwsQuantumTask: An AwsQuantumTask that tracks the task execution on the device.
67 Examples:
68 >>> circuit = Circuit().h(0).cnot(0, 1)
69 >>> device = AwsQuantumSimulator("arn:aws:aqx:::quantum-simulator:aqx:qs1")
70 >>> device.run(circuit, ("bucket", "key"), shots=1000)
72 >>> circuit = Circuit().h(0).cnot(0, 1)
73 >>> device = AwsQuantumSimulator("arn:aws:aqx:::quantum-simulator:aqx:qs1")
74 >>> device.run(circuit=circuit, s3_destination_folder=("bucket", "key"), shots=1000)
76 See Also:
77 `braket.aws.aws_quantum_task.AwsQuantumTask.create()`
78 """
79 return AwsQuantumTask.create(
80 self._aws_session,
81 self._arn,
82 task_specification,
83 s3_destination_folder,
84 shots,
85 *aws_quantum_task_args,
86 **aws_quantum_task_kwargs,
87 )
89 def refresh_metadata(self) -> None:
90 """Refresh the AwsQuantumSimulator object with the most recent simulator metadata."""
91 simulator_metadata = self._aws_session.get_simulator_metadata(self._arn)
92 self._name = simulator_metadata.get("name")
93 self._status = simulator_metadata.get("status")
94 self._status_reason = simulator_metadata.get("statusReason")
95 self._properties = {
96 k: simulator_metadata.get(k) for k in ["supportedQuantumOperations", "qubitCount"]
97 }
99 @property
100 def arn(self) -> str:
101 """str: Returns the ARN of the simulator."""
102 return self._arn
104 @property
105 # TODO: Add a link to the boto3 docs
106 def properties(self) -> Dict[str, Any]:
107 """Dict[str, Any]: Return the simulator properties"""
108 return self._properties
110 def __repr__(self):
111 return f"QuantumSimulator('name': {self.name}, 'arn': {self.arn})"
113 def __eq__(self, other):
114 if isinstance(other, AwsQuantumSimulator):
115 return self.arn == other.arn
116 return NotImplemented