Coverage for src/braket/circuits/observable.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 __future__ import annotations
16from typing import List, Sequence, Tuple, Union
18import numpy as np
19from braket.circuits.gate import Gate
20from braket.circuits.quantum_operator import QuantumOperator
21from braket.circuits.quantum_operator_helpers import get_pauli_eigenvalues
24class Observable(QuantumOperator):
25 """
26 Class `Observable` to represent a quantum observable.
28 Objects of this type can be used as input to `ResultType.Sample`, `ResultType.Variance`,
29 `ResultType.Expectation` to specify the measurement basis.
30 """
32 def __init__(self, qubit_count: int, ascii_symbols: Sequence[str]):
33 super().__init__(qubit_count=qubit_count, ascii_symbols=ascii_symbols)
35 def to_ir(self) -> List[Union[str, List[List[List[float]]]]]:
36 """List[Union[str, List[List[List[float]]]]]: Returns the IR
37 representation for the observable"""
38 raise NotImplementedError
40 @property
41 def basis_rotation_gates(self) -> Tuple[Gate]:
42 """Tuple[Gate]: Returns the basis rotation gates for this observable."""
43 raise NotImplementedError
45 @property
46 def eigenvalues(self) -> np.ndarray:
47 """np.ndarray: Returns the eigenvalues of this observable."""
48 raise NotImplementedError
50 @classmethod
51 def register_observable(cls, observable: Observable) -> None:
52 """Register an observable implementation by adding it into the Observable class.
54 Args:
55 observable (Observable): Observable class to register.
56 """
57 setattr(cls, observable.__name__, observable)
59 def __matmul__(self, other) -> Observable.TensorProduct:
60 if isinstance(other, Observable.TensorProduct):
61 return other.__rmatmul__(self)
63 if isinstance(other, Observable):
64 return Observable.TensorProduct([self, other])
66 raise ValueError("Can only perform tensor products between observables.")
68 def __repr__(self) -> str:
69 return f"{self.name}('qubit_count': {self.qubit_count})"
71 def __eq__(self, other) -> bool:
72 if isinstance(other, Observable):
73 return self.name == other.name
74 return NotImplemented
77class StandardObservable(Observable):
78 """
79 Class `StandardObservable` to represent a standard quantum observable with
80 eigenvalues of +/-1, each with a multiplicity of 1.
81 """
83 def __init__(self, qubit_count: int, ascii_symbols: Sequence[str]):
84 super().__init__(qubit_count=qubit_count, ascii_symbols=ascii_symbols)
86 @property
87 def eigenvalues(self) -> np.ndarray:
88 return get_pauli_eigenvalues(self.qubit_count)