Coverage for src/braket/circuits/result_type.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.
13from __future__ import annotations
15from typing import Any, Dict
17from braket.circuits.qubit import QubitInput
18from braket.circuits.qubit_set import QubitSetInput
21class ResultType:
22 """
23 Class `ResultType` represents a requested result type for the circuit.
24 This class is considered the result type definition containing
25 the metadata that defines what a requested result type is and what it does.
26 """
28 def __init__(self, ascii_symbol: str):
29 """
30 Args:
31 ascii_symbol (str): ASCII string symbol for the result type. This is used when
32 printing a diagram of circuits.
34 Raises:
35 ValueError: `ascii_symbol` is None
36 """
38 if ascii_symbol is None:
39 raise ValueError(f"ascii_symbol must not be None")
41 self._ascii_symbol = ascii_symbol
43 @property
44 def ascii_symbol(self) -> str:
45 """str: Returns the ascii symbol for the requested result type."""
46 return self._ascii_symbol
48 @property
49 def name(self) -> str:
50 """
51 Returns the name of the result type
53 Returns:
54 The name of the result type as a string
55 """
56 return self.__class__.__name__
58 def to_ir(self, *args, **kwargs) -> Any:
59 """Returns IR object of the result type
61 Args:
62 *args: Positional arguments
63 **kwargs: Keyword arguments
65 Returns:
66 IR object of the result type
67 """
68 raise NotImplementedError("to_ir has not been implemented yet.")
70 def copy(self, target_mapping: Dict[QubitInput, QubitInput] = {}, target: QubitSetInput = None):
71 """
72 Return a shallow copy of the result type.
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.
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.
85 Returns:
86 ResultType: A shallow copy of the result type.
88 Raises:
89 TypeError: If both `target_mapping` and `target` are supplied.
91 Examples:
92 >>> result_type = ResultType.Probabilities(targets=[0])
93 >>> new_result_type = result_type.copy()
94 >>> new_result_type.targets
95 QubitSet(Qubit(0))
96 >>> new_result = result_type.copy(target_mapping={0: 5})
97 >>> new_result_type.target
98 QubitSet(Qubit(5))
99 >>> new_result = result_type.copy(target=[5])
100 >>> new_result_type.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
114 @classmethod
115 def register_result_type(cls, result_type: "ResultType"):
116 """Register a result type implementation by adding it into the ResultType class.
118 Args:
119 result_type (ResultType): ResultType instance to register.
120 """
121 setattr(cls, result_type.__name__, result_type)
123 def __repr__(self) -> str:
124 return f"{self.name}()"