-
Notifications
You must be signed in to change notification settings - Fork 586
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
Remove complex warning from operator matrices #1802
Changes from 8 commits
0ef30e7
da3cdef
60d2544
7e4ace0
f4508b3
ce33ada
befe446
ee75d10
d50d01f
11c0319
d5e5bb2
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 |
---|---|---|
|
@@ -73,7 +73,9 @@ def _matrix(cls, *params): | |
|
||
js = -1j * s | ||
|
||
return qml.math.stack([qml.math.stack([c, js]), qml.math.stack([js, c])]) | ||
return qml.math.diag([c, c]) + qml.math.stack( | ||
[qml.math.stack([0, js]), qml.math.stack([js, 0])] | ||
) | ||
|
||
def adjoint(self): | ||
return RX(-self.data[0], wires=self.wires) | ||
|
@@ -122,7 +124,9 @@ def _matrix(cls, *params): | |
c = qml.math.cos(theta / 2) | ||
s = qml.math.sin(theta / 2) | ||
|
||
return qml.math.stack([qml.math.stack([c, -s]), qml.math.stack([s, c])]) | ||
return qml.math.diag([c, c]) + qml.math.stack( | ||
[qml.math.stack([0, -s]), qml.math.stack([s, 0])] | ||
) | ||
|
||
def adjoint(self): | ||
return RY(-self.data[0], wires=self.wires) | ||
|
@@ -838,18 +842,23 @@ def label(self, decimals=None, base_label=None): | |
@classmethod | ||
def _matrix(cls, *params): | ||
theta = params[0] | ||
interface = qml.math.get_interface(theta) | ||
|
||
c = qml.math.cos(theta / 2) | ||
s = qml.math.sin(theta / 2) | ||
z = qml.math.zeros([4], like=interface) | ||
|
||
if qml.math.get_interface(theta) == "tensorflow": | ||
if interface == "tensorflow": | ||
c = qml.math.cast_like(c, 1j) | ||
s = qml.math.cast_like(s, 1j) | ||
z = qml.math.cast_like(z, 1j) | ||
|
||
js = -1j * s | ||
|
||
mat = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, c, js], [0, 0, js, c]] | ||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([1, 1, c, c]) | ||
return mat + qml.math.stack( | ||
[z, z, qml.math.stack([0, 0, 0, js]), qml.math.stack([0, 0, js, 0])] | ||
) | ||
|
||
@staticmethod | ||
def decomposition(theta, wires): | ||
|
@@ -928,12 +937,16 @@ def label(self, decimals=None, base_label=None): | |
@classmethod | ||
def _matrix(cls, *params): | ||
theta = params[0] | ||
interface = qml.math.get_interface(theta) | ||
|
||
c = qml.math.cos(theta / 2) | ||
s = qml.math.sin(theta / 2) | ||
z = qml.math.zeros([4], like=interface) | ||
|
||
mat = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, c, -s], [0, 0, s, c]] | ||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([1, 1, c, c]) | ||
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. Is this necessary here, since 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. weirdly yes, for the same reason that |
||
return mat + qml.math.stack( | ||
[z, z, qml.math.stack([0, 0, 0, -s]), qml.math.stack([0, 0, s, 0])] | ||
) | ||
|
||
@staticmethod | ||
def decomposition(theta, wires): | ||
|
@@ -1396,20 +1409,15 @@ def _matrix(cls, *params): | |
|
||
c = qml.math.cos(phi / 2) | ||
s = qml.math.sin(phi / 2) | ||
Y = qml.math.convert_like(np.eye(4)[::-1].copy(), phi) | ||
|
||
if qml.math.get_interface(phi) == "tensorflow": | ||
c = qml.math.cast_like(c, 1j) | ||
s = qml.math.cast_like(s, 1j) | ||
Y = qml.math.cast_like(Y, 1j) | ||
|
||
js = -1j * s | ||
|
||
mat = [ | ||
[c, 0, 0, js], | ||
[0, c, js, 0], | ||
[0, js, c, 0], | ||
[js, 0, 0, c], | ||
] | ||
|
||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([c, c, c, c]) - 1j * s * Y | ||
return mat | ||
|
||
@staticmethod | ||
def decomposition(phi, wires): | ||
|
@@ -1471,21 +1479,14 @@ def _matrix(cls, *params): | |
|
||
c = qml.math.cos(phi / 2) | ||
s = qml.math.sin(phi / 2) | ||
Y = qml.math.convert_like(np.diag([1, -1, -1, 1])[::-1].copy(), phi) | ||
|
||
if qml.math.get_interface(phi) == "tensorflow": | ||
c = qml.math.cast_like(c, 1j) | ||
s = qml.math.cast_like(s, 1j) | ||
Y = qml.math.cast_like(Y, 1j) | ||
|
||
js = 1j * s | ||
|
||
mat = [ | ||
[c, 0.0, 0.0, js], | ||
[0.0, c, -js, 0.0], | ||
[0.0, -js, c, 0.0], | ||
[js, 0.0, 0.0, c], | ||
] | ||
|
||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
return qml.math.diag([c, c, c, c]) + 1j * s * Y | ||
|
||
def adjoint(self): | ||
(phi,) = self.parameters | ||
|
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.
I tracked down another complex warning here. @glassnotes, I fear more are hiding still 😆
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.
I'm very confused about this one, is it not doing the exact same thing but in two lines? Or does doing the casting in the other frameworks spit out a warning while tensorflow does not? 😕
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.
Yep! during the backwards pass, the reverse will happen - a complex value will be cast to real. Which... generates the warning in PyTorch and Autograd 🤦
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.
🤦♀️ indeed