-
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 1 commit
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): | ||
| """ | ||
|
|
@@ -203,20 +203,15 @@ def __or__(self, qubits): | |
| cmd = self.generate_command(qubits) | ||
| apply_command(cmd) | ||
|
|
||
| @property | ||
| def matrix(self): | ||
| raise AttributeError("BasicGate has no matrix property. Use MatrixGate instead.") | ||
|
|
||
| @matrix.setter | ||
| def matrix(self, matrix=None): | ||
| raise AttributeError("You cannot set the matrix property of gates derived" | ||
| "from BasicGate. Please use MatrixGate instead if" | ||
| "you need that functionality.") | ||
|
|
||
| def __eq__(self, other): | ||
| """ Return True if equal (i.e., instance of same class). | ||
| """ | ||
| return isinstance(other, self.__class__) | ||
| if isinstance(other, self.__class__): | ||
| return True | ||
| elif isinstance(other, MatrixGate): | ||
| return NotImplemented | ||
| else: | ||
| return False | ||
|
|
||
| def __ne__(self, other): | ||
| return not self.__eq__(other) | ||
|
|
@@ -227,13 +222,30 @@ def __str__(self): | |
| def __hash__(self): | ||
| return hash(str(self)) | ||
|
|
||
cgogolin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| class MatrixGate(BasicGate): | ||
| """ | ||
| Defines a base class of a matrix gate. | ||
| 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 | ||
|
|
||
| A matrix gate is defined via its matrix. | ||
| 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 | ||
|
|
||
|
|
@@ -246,9 +258,7 @@ def matrix(self, matrix): | |
| self._matrix = np.matrix(matrix) | ||
|
|
||
| def __eq__(self, other): | ||
| """ Return True if equal (i.e., instance of same class). | ||
| """ | ||
| if not isinstance(other, self.__class__): | ||
| 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)): | ||
|
|
@@ -262,12 +272,19 @@ def __eq__(self, other): | |
| return True | ||
| return False | ||
|
|
||
| def __ne__(self, other): | ||
| return not self.__eq__(other) | ||
|
|
||
|
||
| def __str__(self): | ||
| raise NotImplementedError('This gate does not implement __str__.') | ||
| 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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better "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."