From 0ef30e783bf0fc29a879436022551d4f109d0fb5 Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Mon, 25 Oct 2021 16:26:18 +0800 Subject: [PATCH 1/6] Update operator matrices to remove complex casting warning --- pennylane/ops/qubit/parametric_ops.py | 55 ++++++++++++++------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/pennylane/ops/qubit/parametric_ops.py b/pennylane/ops/qubit/parametric_ops.py index 86f3ad69974..64fb4b79bd6 100644 --- a/pennylane/ops/qubit/parametric_ops.py +++ b/pennylane/ops/qubit/parametric_ops.py @@ -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]) + 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 From da3cdef5d243542577b5fd189d0d802bce4fae64 Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Mon, 25 Oct 2021 17:07:28 +0800 Subject: [PATCH 2/6] more operators --- pennylane/ops/qubit/qchem_ops.py | 94 +++++++++----------------------- 1 file changed, 25 insertions(+), 69 deletions(-) diff --git a/pennylane/ops/qubit/qchem_ops.py b/pennylane/ops/qubit/qchem_ops.py index a427b574024..8bdc566c33a 100644 --- a/pennylane/ops/qubit/qchem_ops.py +++ b/pennylane/ops/qubit/qchem_ops.py @@ -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 @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, 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 =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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 From 60d2544cd6f9f92160025f03f90afeea010c2d41 Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Mon, 25 Oct 2021 17:46:49 +0800 Subject: [PATCH 3/6] more operators --- pennylane/devices/default_qubit.py | 7 ++++++- tests/ops/qubit/test_hamiltonian.py | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pennylane/devices/default_qubit.py b/pennylane/devices/default_qubit.py index 818d13e3b4b..fe1e232170b 100644 --- a/pennylane/devices/default_qubit.py +++ b/pennylane/devices/default_qubit.py @@ -487,6 +487,7 @@ def expval(self, observable, shot_range=None, bin_size=None): # Compute via sum_i coeff_i * using a sparse # representation of the Pauliword res = qml.math.cast(qml.math.convert_like(0.0, observable.data), dtype=complex) + interface = qml.math.get_interface(self.state) # Note: it is important that we use the Hamiltonian's data and not the coeffs attribute. # This is because the .data attribute may be 'unwrapped' as required by the interfaces, @@ -502,7 +503,11 @@ def expval(self, observable, shot_range=None, bin_size=None): * Hmat * qml.math.gather(self.state, coo.col) ) - c = qml.math.cast(qml.math.convert_like(coeff, product), "complex128") + c = qml.math.convert_like(coeff, product) + + if interface == "tensorflow": + c = qml.math.cast(c, "complex128") + res = qml.math.convert_like(res, product) + qml.math.sum(c * product) else: diff --git a/tests/ops/qubit/test_hamiltonian.py b/tests/ops/qubit/test_hamiltonian.py index 62603c0719b..2b81ff226d4 100644 --- a/tests/ops/qubit/test_hamiltonian.py +++ b/tests/ops/qubit/test_hamiltonian.py @@ -46,7 +46,7 @@ try: import torch - COEFFS_PARAM_INTERFACE.append((torch.tensor([-0.05, 0.17]), torch.tensor([1.7]), "torch")) + COEFFS_PARAM_INTERFACE.append((torch.tensor([-0.05, 0.17]), torch.tensor(1.7), "torch")) except ImportError: pass @@ -1189,6 +1189,8 @@ def test_vqe_forward_different_coeff_types(self, coeffs, param, interface): dev = qml.device("default.qubit", wires=2) H = qml.Hamiltonian(coeffs, [qml.PauliX(0), qml.PauliZ(0)]) + print(param) + @qml.qnode(dev, interface=interface) def circuit(): qml.RX(param, wires=0) From 7e4ace0cbeba8a57f14347ef2e73133fac562000 Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Mon, 25 Oct 2021 18:41:39 +0800 Subject: [PATCH 4/6] Update tests/ops/qubit/test_hamiltonian.py --- tests/ops/qubit/test_hamiltonian.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/ops/qubit/test_hamiltonian.py b/tests/ops/qubit/test_hamiltonian.py index 2b81ff226d4..ab6ac8f1703 100644 --- a/tests/ops/qubit/test_hamiltonian.py +++ b/tests/ops/qubit/test_hamiltonian.py @@ -1189,8 +1189,6 @@ def test_vqe_forward_different_coeff_types(self, coeffs, param, interface): dev = qml.device("default.qubit", wires=2) H = qml.Hamiltonian(coeffs, [qml.PauliX(0), qml.PauliZ(0)]) - print(param) - @qml.qnode(dev, interface=interface) def circuit(): qml.RX(param, wires=0) From d50d01f1c228632a2d0c77841cc52f5041c8d195 Mon Sep 17 00:00:00 2001 From: glassnotes Date: Tue, 26 Oct 2021 13:37:39 -0400 Subject: [PATCH 5/6] Fix autograd warning in orbital rotation. --- pennylane/ops/qubit/qchem_ops.py | 43 +++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/pennylane/ops/qubit/qchem_ops.py b/pennylane/ops/qubit/qchem_ops.py index 8bdc566c33a..d64115c0bda 100644 --- a/pennylane/ops/qubit/qchem_ops.py +++ b/pennylane/ops/qubit/qchem_ops.py @@ -613,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( + [ + 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 From 11c0319998f884681b5a660006006d5e9e24b302 Mon Sep 17 00:00:00 2001 From: glassnotes Date: Tue, 26 Oct 2021 13:54:16 -0400 Subject: [PATCH 6/6] Update variable dtype in torch test to remove warning. --- tests/ops/qubit/test_qchem_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ops/qubit/test_qchem_ops.py b/tests/ops/qubit/test_qchem_ops.py index 31bc536c6a2..d21cdac1396 100644 --- a/tests/ops/qubit/test_qchem_ops.py +++ b/tests/ops/qubit/test_qchem_ops.py @@ -910,7 +910,7 @@ def circuit(phi): qml.OrbitalRotation(phi, wires=[0, 1, 2, 3]) return qml.expval(qml.PauliZ(0)) - phi_t = torch.tensor(phi, dtype=torch.float64, requires_grad=True) + phi_t = torch.tensor(phi, dtype=torch.complex128, requires_grad=True) result = circuit(phi_t) result.backward()