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 typing import Any, List, Sequence 

14 

15import numpy as np 

16from braket.circuits.operator import Operator 

17 

18 

19class QuantumOperator(Operator): 

20 """A quantum operator is the definition of a quantum operation for a quantum device.""" 

21 

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

23 """ 

24 Args: 

25 qubit_count (int): Number of qubits this quantum operator interacts with. 

26 ascii_symbols (Sequence[str]): ASCII string symbols for the quantum operator. 

27 These are used when printing a diagram of circuits. 

28 Length must be the same as `qubit_count`, and index ordering is expected 

29 to correlate with target ordering on the instruction. 

30 For instance, if CNOT instruction has the control qubit on the first index and 

31 target qubit on the second index. Then ASCII symbols would have ["C", "X"] to 

32 correlate a symbol with that index. 

33 

34 Raises: 

35 ValueError: `qubit_count` is less than 1, `ascii_symbols` are None, or 

36 `ascii_symbols` length != `qubit_count` 

37 """ 

38 

39 if qubit_count < 1: 

40 raise ValueError(f"qubit_count, {qubit_count}, must be greater than zero") 

41 self._qubit_count = qubit_count 

42 

43 if ascii_symbols is None: 

44 raise ValueError("ascii_symbols must not be None") 

45 

46 if len(ascii_symbols) != qubit_count: 

47 msg = f"ascii_symbols, {ascii_symbols}, length must equal qubit_count, {qubit_count}" 

48 raise ValueError(msg) 

49 self._ascii_symbols = tuple(ascii_symbols) 

50 

51 @property 

52 def qubit_count(self) -> int: 

53 """int: Returns number of qubits this quantum operator interacts with.""" 

54 return self._qubit_count 

55 

56 @property 

57 def ascii_symbols(self) -> List[str]: 

58 """List[str]: Returns the ascii symbols for the quantum operator.""" 

59 return self._ascii_symbols 

60 

61 @property 

62 def name(self) -> str: 

63 """ 

64 Returns the name of the quantum operator 

65 

66 Returns: 

67 The name of the quantum operator as a string 

68 """ 

69 return self.__class__.__name__ 

70 

71 def to_ir(self, *args, **kwargs) -> Any: 

72 """Returns IR representation of quantum operator 

73 

74 Args: 

75 *args: Positional arguments 

76 **kwargs: Keyword arguments 

77 """ 

78 raise NotImplementedError("to_ir has not been implemented yet.") 

79 

80 def to_matrix(self, *args, **kwargs) -> Any: 

81 """Returns a matrix representation of the quantum operator 

82 

83 Returns: 

84 np.ndarray: A matrix representation of the quantum operator 

85 """ 

86 raise NotImplementedError("to_matrix has not been implemented yet.") 

87 

88 def matrix_equivalence(self, other): 

89 """ 

90 Whether the matrix form of two gates are equivalent 

91 

92 Args: 

93 other (Gate): Gate instance to compare this quantum operator to 

94 

95 Returns: 

96 bool: If matrix forms of this quantum operator and the other quantum operator 

97 are equivalent 

98 """ 

99 if not isinstance(other, QuantumOperator): 

100 return NotImplemented 

101 try: 

102 return np.allclose(self.to_matrix(), other.to_matrix()) 

103 except ValueError: 

104 return False 

105 

106 def __repr__(self): 

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