Hide keyboard shortcuts

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 

14 

15from typing import List, Sequence, Tuple, Union 

16 

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 

21 

22 

23class Observable(QuantumOperator): 

24 """ 

25 Class `Observable` to represent a quantum observable. 

26 

27 Objects of this type can be used as input to `ResultType.Sample`, `ResultType.Variance`, 

28 `ResultType.Expectation` to specify the measurement basis. 

29 """ 

30 

31 def __init__(self, qubit_count: int, ascii_symbols: Sequence[str]): 

32 super().__init__(qubit_count=qubit_count, ascii_symbols=ascii_symbols) 

33 

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 

38 

39 @property 

40 def basis_rotation_gates(self) -> Tuple[Gate]: 

41 """Tuple[Gate]: Returns the basis rotation gates for this observable.""" 

42 raise NotImplementedError 

43 

44 @property 

45 def eigenvalues(self) -> np.ndarray: 

46 """np.ndarray: Returns the eigenvalues of this observable.""" 

47 raise NotImplementedError 

48 

49 @classmethod 

50 def register_observable(cls, observable: Observable) -> None: 

51 """Register an observable implementation by adding it into the Observable class. 

52 

53 Args: 

54 observable (Observable): Observable class to register. 

55 """ 

56 setattr(cls, observable.__name__, observable) 

57 

58 def __matmul__(self, other) -> Observable.TensorProduct: 

59 if isinstance(other, Observable.TensorProduct): 

60 return other.__rmatmul__(self) 

61 

62 if isinstance(other, Observable): 

63 return Observable.TensorProduct([self, other]) 

64 

65 raise ValueError("Can only perform tensor products between observables.") 

66 

67 def __repr__(self) -> str: 

68 return f"{self.name}('qubit_count': {self.qubit_count})" 

69 

70 def __eq__(self, other) -> bool: 

71 if isinstance(other, Observable): 

72 return self.name == other.name 

73 return NotImplemented 

74 

75 

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 """ 

81 

82 def __init__(self, qubit_count: int, ascii_symbols: Sequence[str]): 

83 super().__init__(qubit_count=qubit_count, ascii_symbols=ascii_symbols) 

84 

85 @property 

86 def eigenvalues(self) -> np.ndarray: 

87 return get_pauli_eigenvalues(self.qubit_count)