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, Union 

16 

17from braket.circuits.quantum_operator import QuantumOperator 

18 

19 

20class Observable(QuantumOperator): 

21 """ 

22 Class `Observable` to represent a quantum observable. 

23 

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

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

26 """ 

27 

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

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

30 

31 def to_ir(self) -> List[Union[str, List[List[List[float]]]]]: 

32 """List[Union[str, List[List[List[float]]]]]: Returns the IR 

33 representation for the observable""" 

34 raise NotImplementedError 

35 

36 @classmethod 

37 def register_observable(cls, observable: Observable): 

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

39 

40 Args: 

41 observable (Observable): Observable class to register. 

42 """ 

43 setattr(cls, observable.__name__, observable) 

44 

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

46 if isinstance(other, Observable.TensorProduct): 

47 return other.__rmatmul__(self) 

48 

49 if isinstance(other, Observable): 

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

51 

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

53 

54 def __repr__(self) -> str: 

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

56 

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

58 if isinstance(other, Observable): 

59 return self.name == other.name 

60 return NotImplemented