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