-
Notifications
You must be signed in to change notification settings - Fork 278
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
Implement MatrixGate and simplify __eq__ in BasicGate to improve performance #288
Changes from 8 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 |
---|---|---|
|
@@ -203,40 +203,20 @@ 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.") | ||
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. White spaces are missing in error message |
||
|
||
def __eq__(self, other): | ||
""" Return True if equal (i.e., instance of same class). | ||
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. Maybe better "Return True if instance of the same class, unless other is an instance of :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 | ||
else: | ||
return isinstance(other, self.__class__) | ||
""" | ||
return isinstance(other, self.__class__) | ||
|
||
def __ne__(self, other): | ||
return not self.__eq__(other) | ||
|
@@ -247,6 +227,46 @@ 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. | ||
|
||
A matrix gate is defined via its matrix. | ||
""" | ||
def __init__(self, matrix=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): | ||
""" Return True if equal (i.e., instance of same class). | ||
""" | ||
if not isinstance(other, self.__class__): | ||
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): | ||
raise NotImplementedError('This gate does not implement __str__.') | ||
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. We need a |
||
|
||
def get_inverse(self): | ||
return MatrixGate(np.linalg.inv(self.matrix)) | ||
|
||
cgogolin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class SelfInverseGate(BasicGate): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ | |
* C (Creates an n-ary controlled version of an arbitrary gate) | ||
""" | ||
|
||
from ._basics import BasicGate, NotInvertible | ||
from ._basics import BasicGate, MatrixGate, NotInvertible | ||
from ._command import Command, apply_command | ||
|
||
|
||
|
@@ -39,7 +39,7 @@ class ControlQubitError(Exception): | |
pass | ||
|
||
|
||
class DaggeredGate(BasicGate): | ||
class DaggeredGate(MatrixGate): #todo: do we want this? | ||
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. No, this should still be a A |
||
""" | ||
Wrapper class allowing to execute the inverse of a gate, even when it does | ||
not define one. | ||
|
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.
Line >80 characters and alphabetic order for inputs