- 
                Notifications
    You must be signed in to change notification settings 
- Fork 282
Implement MatrixGate and simplify __eq__ in BasicGate to improve performance #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4da06f8
              380a08c
              b9a1e2a
              248b959
              dc46ca7
              b00c07b
              48df21a
              a986845
              ca98f6c
              2356246
              63c65e4
              c1dd0d6
              c958219
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -64,7 +64,7 @@ class NotInvertible(Exception): | |
|  | ||
| class BasicGate(object): | ||
| """ | ||
| Base class of all gates. | ||
| Base class of all gates. (Don't use it directly but derive from it) | ||
| """ | ||
| def __init__(self): | ||
| """ | ||
|  | @@ -204,39 +204,19 @@ def __or__(self, qubits): | |
| apply_command(cmd) | ||
|  | ||
| def __eq__(self, other): | ||
| """ Return True if equal (i.e., instance of same class). | ||
|  | ||
| Unless both have a matrix attribute in which case we also check | ||
| that the matrices are identical as people might want to do the | ||
| following: | ||
|  | ||
| Example: | ||
| .. code-block:: python | ||
|  | ||
| gate = BasicGate() | ||
| gate.matrix = numpy.matrix([[1,0],[0, -1]]) | ||
| """ | ||
| if hasattr(self, 'matrix'): | ||
| if not hasattr(other, 'matrix'): | ||
| return False | ||
| if hasattr(other, 'matrix'): | ||
| if not hasattr(self, 'matrix'): | ||
| return False | ||
| if hasattr(self, 'matrix') and hasattr(other, 'matrix'): | ||
| if (not isinstance(self.matrix, np.matrix) or | ||
| not isinstance(other.matrix, np.matrix)): | ||
| raise TypeError("One of the gates doesn't have the correct " | ||
| "type (numpy.matrix) for the matrix " | ||
| "attribute.") | ||
| if (self.matrix.shape == other.matrix.shape and | ||
| np.allclose(self.matrix, other.matrix, | ||
| rtol=RTOL, atol=ATOL, | ||
| equal_nan=False)): | ||
| return True | ||
| else: | ||
| return False | ||
| """ | ||
| Equality comparision | ||
|  | ||
| Return True if instance of the same class, unless other is an instance | ||
| of :class:MatrixGate, in which case equality is to be checked by testing | ||
| for existence and (approximate) equality of matrix representations. | ||
| """ | ||
| if isinstance(other, self.__class__): | ||
| return True | ||
| elif isinstance(other, MatrixGate): | ||
| return NotImplemented | ||
| else: | ||
| return isinstance(other, self.__class__) | ||
| return False | ||
|  | ||
| def __ne__(self, other): | ||
| return not self.__eq__(other) | ||
|  | @@ -248,6 +228,71 @@ def __hash__(self): | |
| return hash(str(self)) | ||
|  | ||
|  | ||
| class MatrixGate(BasicGate): | ||
| """ | ||
| Defines a gate class whose instances are defined by a matrix. | ||
|  | ||
| Note: | ||
| Use this gate class only for gates acting on a small numbers of qubits. | ||
| In general, consider instead using one of the provided ProjectQ gates | ||
| or define a new class as this allows the compiler to work symbolically. | ||
|  | ||
| Example: | ||
|  | ||
| .. code-block:: python | ||
|  | ||
| gate = MatrixGate([[0, 1], [1, 0]]) | ||
| gate | qubit | ||
| """ | ||
| def __init__(self, matrix=None): | ||
| """ | ||
| Initialize MatrixGate | ||
|  | ||
| Args: | ||
| matrix(numpy.matrix): matrix which defines the gate. Default: None | ||
| """ | ||
| BasicGate.__init__(self) | ||
| self._matrix = np.matrix(matrix) if matrix is not None else None | ||
|  | ||
| @property | ||
| def matrix(self): | ||
| return self._matrix | ||
|  | ||
| @matrix.setter | ||
| def matrix(self, matrix): | ||
| self._matrix = np.matrix(matrix) | ||
|  | ||
| def __eq__(self, other): | ||
| """ | ||
| Equality comparision | ||
|  | ||
| Return True only if both gates have a matrix respresentation and the | ||
| matrices are (approximately) equal. Otherwise return False. | ||
| """ | ||
| if not hasattr(other, 'matrix'): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I achnowledge that the docstring was not good, but do you really want no docstring at all? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 | ||
| return False | ||
| if (not isinstance(self.matrix, np.matrix) or | ||
| not isinstance(other.matrix, np.matrix)): | ||
| raise TypeError("One of the gates doesn't have the correct " | ||
| "type (numpy.matrix) for the matrix " | ||
| "attribute.") | ||
| if (self.matrix.shape == other.matrix.shape and | ||
| np.allclose(self.matrix, other.matrix, | ||
| rtol=RTOL, atol=ATOL, | ||
| equal_nan=False)): | ||
| return True | ||
| return False | ||
|  | ||
| def __str__(self): | ||
| return("MatrixGate(" + str(self.matrix.tolist()) + ")") | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you convert to list here? Isn't it better if it is visible in the output that self.matrix is a numpy array (i.e., no commas between elements vs. commas between elements for  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just so that the  We can use numpy arrays for this string once we switch from numpy matrix to numpy arrays #287 | ||
|  | ||
| def __hash__(self): | ||
| return hash(str(self)) | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this seems superflous because  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uups. Python3 seems to remove hash if eq is redefined... | ||
|  | ||
| def get_inverse(self): | ||
| return MatrixGate(np.linalg.inv(self.matrix)) | ||
|  | ||
|         
                  cgogolin marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| class SelfInverseGate(BasicGate): | ||
| """ | ||
| Self-inverse basic gate class. | ||
|  | ||
Uh oh!
There was an error while loading. Please reload this page.