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. 

13 

14from __future__ import annotations 

15 

16from typing import List, Sequence, Tuple, Union 

17 

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 

22 

23 

24class Observable(QuantumOperator): 

25 """ 

26 Class `Observable` to represent a quantum observable. 

27 

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

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

30 """ 

31 

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

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

34 

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 

39 

40 @property 

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

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

43 raise NotImplementedError 

44 

45 @property 

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

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

48 raise NotImplementedError 

49 

50 @classmethod 

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

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

53 

54 Args: 

55 observable (Observable): Observable class to register. 

56 """ 

57 setattr(cls, observable.__name__, observable) 

58 

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

60 if isinstance(other, Observable.TensorProduct): 

61 return other.__rmatmul__(self) 

62 

63 if isinstance(other, Observable): 

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

65 

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

67 

68 def __repr__(self) -> str: 

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

70 

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

72 if isinstance(other, Observable): 

73 return self.name == other.name 

74 return NotImplemented 

75 

76 

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

82 

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

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

85 

86 @property 

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

88 return get_pauli_eigenvalues(self.qubit_count)