-
Notifications
You must be signed in to change notification settings - Fork 603
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 10 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,8 +95,9 @@ def _matrix(cls, *params): | |
c = qml.math.cos(theta / 2) | ||
s = qml.math.sin(theta / 2) | ||
|
||
mat = [[1, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, 1]] | ||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([1, c, c, 1]) | ||
off_diag = qml.math.convert_like(np.diag([0, 1, -1, 0])[::-1].copy(), theta) | ||
return mat + s * qml.math.cast_like(off_diag, s) | ||
|
||
@staticmethod | ||
def decomposition(theta, wires): | ||
|
@@ -163,9 +164,9 @@ def _matrix(cls, *params): | |
s = qml.math.cast_like(s, 1j) | ||
|
||
e = qml.math.exp(-1j * theta / 2) | ||
|
||
mat = [[e, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, e]] | ||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([e, 0, 0, e]) + qml.math.diag([0, c, c, 0]) | ||
off_diag = qml.math.convert_like(np.diag([0, 1, -1, 0])[::-1].copy(), theta) | ||
return mat + s * qml.math.cast_like(off_diag, s) | ||
|
||
@staticmethod | ||
def decomposition(theta, wires): | ||
|
@@ -238,9 +239,9 @@ def _matrix(cls, *params): | |
s = qml.math.cast_like(s, 1j) | ||
|
||
e = qml.math.exp(1j * theta / 2) | ||
|
||
mat = [[e, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, e]] | ||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([e, 0, 0, e]) + qml.math.diag([0, c, c, 0]) | ||
off_diag = qml.math.convert_like(np.diag([0, 1, -1, 0])[::-1].copy(), theta) | ||
return mat + s * qml.math.cast_like(off_diag, s) | ||
|
||
@staticmethod | ||
def decomposition(theta, wires): | ||
|
@@ -332,26 +333,10 @@ def _matrix(cls, *params): | |
c = qml.math.cos(theta / 2) | ||
s = qml.math.sin(theta / 2) | ||
|
||
mat = [ | ||
[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, c, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -s, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, s, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, c, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], | ||
] | ||
|
||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([1.0] * 3 + [c] + [1.0] * 8 + [c] + [1.0] * 3) | ||
mat = qml.math.scatter_element_add(mat, (3, 12), -s) | ||
mat = qml.math.scatter_element_add(mat, (12, 3), s) | ||
return mat | ||
|
||
@staticmethod | ||
def decomposition(theta, wires): | ||
|
@@ -454,26 +439,12 @@ def _matrix(cls, *params): | |
|
||
e = qml.math.exp(1j * theta / 2) | ||
|
||
mat = [ | ||
[e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, c, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -s, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, s, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, c, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e], | ||
] | ||
|
||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([e] * 3 + [0] + [e] * 8 + [0] + [e] * 3) | ||
mat = qml.math.scatter_element_add(mat, (3, 3), c) | ||
mat = qml.math.scatter_element_add(mat, (3, 12), -s) | ||
mat = qml.math.scatter_element_add(mat, (12, 3), s) | ||
mat = qml.math.scatter_element_add(mat, (12, 12), c) | ||
return mat | ||
|
||
def adjoint(self): | ||
(theta,) = self.parameters | ||
|
@@ -539,27 +510,12 @@ def _matrix(cls, *params): | |
s = qml.math.cast_like(s, 1j) | ||
|
||
e = qml.math.exp(-1j * theta / 2) | ||
|
||
mat = [ | ||
[e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, c, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -s, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, s, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, c, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, e], | ||
] | ||
|
||
return qml.math.stack([qml.math.stack(row) for row in mat]) | ||
mat = qml.math.diag([e] * 3 + [0] + [e] * 8 + [0] + [e] * 3) | ||
mat = qml.math.scatter_element_add(mat, (3, 3), c) | ||
mat = qml.math.scatter_element_add(mat, (3, 12), -s) | ||
mat = qml.math.scatter_element_add(mat, (12, 3), s) | ||
mat = qml.math.scatter_element_add(mat, (12, 12), c) | ||
return mat | ||
|
||
def adjoint(self): | ||
(theta,) = self.parameters | ||
|
@@ -657,30 +613,33 @@ def _matrix(cls, *params): | |
# Additionally, there was a typo in the sign of a matrix element "s" at [2, 8], which is fixed here. | ||
|
||
phi = params[0] | ||
interface = qml.math.get_interface(phi) | ||
c = qml.math.cos(phi / 2) | ||
s = qml.math.sin(phi / 2) | ||
z = qml.math.zeros([16], like=interface) | ||
|
||
matrix = [ | ||
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, c, 0, 0, -s, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, c, 0, 0, 0, 0, 0, -s, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, c ** 2, 0, 0, -c * s, 0, 0, -c * s, 0, 0, s ** 2, 0, 0, 0], | ||
[0, s, 0, 0, c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, c * s, 0, 0, c ** 2, 0, 0, -(s ** 2), 0, 0, -c * s, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, c, 0, 0, 0, 0, 0, -s, 0, 0], | ||
[0, 0, s, 0, 0, 0, 0, 0, c, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, c * s, 0, 0, -(s ** 2), 0, 0, c ** 2, 0, 0, -c * s, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, c, 0, 0, -s, 0], | ||
[0, 0, 0, s ** 2, 0, 0, c * s, 0, 0, c * s, 0, 0, c ** 2, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, s, 0, 0, 0, 0, 0, c, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, s, 0, 0, c, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], | ||
] | ||
diag = qml.math.diag([1, c, c, c ** 2, c, 1, c ** 2, c, c, c ** 2, 1, c, c ** 2, c, c, 1]) | ||
|
||
# first stack each row and then stack all the rows | ||
U = qml.math.stack([qml.math.stack(row) for row in matrix], axis=0) | ||
U = diag + qml.math.stack( | ||
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. wait why does this fix it 🤯 what makes the diagonal part different??????????? This PR has made me so confused 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. at some point in the future other devs are going to look at this and just be like why is the matrix coded like this hahahaha 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 know, I'm just totally baffled! It's the exact same matrix! We can always leave a humorous warning for the next dev who looks at it. 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. ... which, realistically, is going to be us and @mariaschuld while doing the operator refactor. 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. @josh146 Hahaha, having a laugh reading through this, and finding myself in this statement:
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. In retrospect we should have added comments to the code, linking back to this PR for context to future developers 😆 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. wait @dwierichs how did you find this PR 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 was wondering the same thing! 😂 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 am going to implement batching for parametrized operations and therefore need to modify this code :D @staticmethod
def compute_matrix(theta):
c = qml.math.cos(theta / 2)
s = qml.math.sin(theta / 2)
if qml.math.get_interface(theta)=="tensorflow":
c = qml.math.cast_like(c, 1j)
s = qml.math.cast_like(s, 1j)
mat = qml.math.stack([qml.math.stack([c, s]), qml.math.stack([s, c])])
return mat * qml.math.array([[1+0j, -1j], [-1j, 1+0j]], like=mat) which is batch-compatible (i.e. |
||
[ | ||
z, | ||
qml.math.stack([0, 0, 0, 0, -s, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), | ||
qml.math.stack([0, 0, 0, 0, 0, 0, 0, 0, -s, 0, 0, 0, 0, 0, 0, 0]), | ||
qml.math.stack([0, 0, 0, 0, 0, 0, -c * s, 0, 0, -c * s, 0, 0, s * s, 0, 0, 0]), | ||
qml.math.stack([0, s, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), | ||
z, | ||
qml.math.stack([0, 0, 0, c * s, 0, 0, 0, 0, 0, -s * s, 0, 0, -c * s, 0, 0, 0]), | ||
qml.math.stack([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -s, 0, 0]), | ||
qml.math.stack([0, 0, s, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), | ||
qml.math.stack([0, 0, 0, c * s, 0, 0, -s * s, 0, 0, 0, 0, 0, -c * s, 0, 0, 0]), | ||
z, | ||
qml.math.stack([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -s, 0]), | ||
qml.math.stack([0, 0, 0, s * s, 0, 0, c * s, 0, 0, c * s, 0, 0, 0, 0, 0, 0]), | ||
qml.math.stack([0, 0, 0, 0, 0, 0, 0, s, 0, 0, 0, 0, 0, 0, 0, 0]), | ||
qml.math.stack([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, s, 0, 0, 0, 0]), | ||
z, | ||
] | ||
) | ||
|
||
return U | ||
|
||
|
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