From a9b18022a853f0e287b1145f50547730ae3306a3 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Tue, 24 Aug 2021 16:20:28 +0100 Subject: [PATCH 01/11] bugfix: warn when backprop with finite-shots --- pennylane/qnode.py | 4 +++ tests/devices/test_default_qubit_tf.py | 44 +++++--------------------- tests/tape/test_qnode.py | 8 +++++ 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/pennylane/qnode.py b/pennylane/qnode.py index f6542056b98..aec1253a1e1 100644 --- a/pennylane/qnode.py +++ b/pennylane/qnode.py @@ -315,6 +315,10 @@ def _validate_backprop_method(device, interface): qml.QuantumFunctionError: if the device does not support backpropagation, or the interface provided is not compatible with the device """ + if device.shots is not None: + raise qml.QuantumFunctionError("Devices with finite shots are incompatible with backpropogation. " + "Please set ``shots=None`` or chose a different diff_method.") + # determine if the device supports backpropagation backprop_interface = device.capabilities().get("passthru_interface", None) diff --git a/tests/devices/test_default_qubit_tf.py b/tests/devices/test_default_qubit_tf.py index 6ea373aa4e7..c4f33b071ce 100644 --- a/tests/devices/test_default_qubit_tf.py +++ b/tests/devices/test_default_qubit_tf.py @@ -1328,23 +1328,17 @@ def test_sample_observables_non_differentiable(self): shots = 100 dev = qml.device("default.qubit.tf", wires=2, shots=shots) - @qml.qnode(dev, diff_method="backprop", interface="tf") - def circuit(a): - qml.RX(a, wires=0) - return qml.sample(qml.PauliZ(0)) - - a = tf.Variable(0.54) - - with tf.GradientTape() as tape: - res = circuit(a) - - assert tape.gradient(res, a) is None + with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): + @qml.qnode(dev, diff_method="backprop", interface="tf") + def circuit(a): + qml.RX(a, wires=0) + return qml.sample(qml.PauliZ(0)) def test_estimating_marginal_probability(self, tol): """Test that the probability of a subset of wires is accurately estimated.""" dev = qml.device("default.qubit.tf", wires=2, shots=1000) - @qml.qnode(dev, diff_method="backprop", interface="tf") + @qml.qnode(dev, diff_method="parameter-shift", interface="tf") def circuit(): qml.PauliX(0) return qml.probs(wires=[0]) @@ -1360,7 +1354,7 @@ def test_estimating_full_probability(self, tol): """Test that the probability of a subset of wires is accurately estimated.""" dev = qml.device("default.qubit.tf", wires=2, shots=1000) - @qml.qnode(dev, diff_method="backprop", interface="tf") + @qml.qnode(dev, diff_method="parameter-shift", interface="tf") def circuit(): qml.PauliX(0) qml.PauliX(1) @@ -1378,7 +1372,7 @@ def test_estimating_expectation_values(self, tol): of shots produces a numeric tensor""" dev = qml.device("default.qubit.tf", wires=3, shots=1000) - @qml.qnode(dev, diff_method="backprop", interface="tf") + @qml.qnode(dev, diff_method="parameter-shift", interface="tf") def circuit(a, b): qml.RX(a, wires=[0]) qml.RX(b, wires=[1]) @@ -1396,28 +1390,6 @@ def circuit(a, b): # expected = [tf.cos(a), tf.cos(a) * tf.cos(b)] # assert np.allclose(res, expected, atol=tol, rtol=0) - def test_estimating_expectation_values_not_differentiable(self, tol): - """Test that finite shots results in non-differentiable QNodes""" - - dev = qml.device("default.qubit.tf", wires=3, shots=1000) - - @qml.qnode(dev, diff_method="backprop", interface="tf") - def circuit(a, b): - qml.RX(a, wires=[0]) - qml.RX(b, wires=[1]) - qml.CNOT(wires=[0, 1]) - return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)) - - a = tf.Variable(0.543) - b = tf.Variable(0.43) - - with tf.GradientTape() as tape: - res = circuit(a, b) - - assert isinstance(res, tf.Tensor) - grad = tape.gradient(res, [a, b]) - assert grad == [None, None] - class TestHighLevelIntegration: """Tests for integration with higher level components of PennyLane.""" diff --git a/tests/tape/test_qnode.py b/tests/tape/test_qnode.py index 4504236dff8..6e65aadd01c 100644 --- a/tests/tape/test_qnode.py +++ b/tests/tape/test_qnode.py @@ -70,6 +70,14 @@ def test_validate_device_method(self, monkeypatch): assert interface == "interface" assert device is dev + def test_validate_backprop_method_finite_shots(self): + """Tests an error is raised for backpropogation with finite shots.""" + + dev = qml.device('default.qubit', wires=1, shots=3) + + with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): + QNode._validate_backprop_method(dev, "doesnt matter") + def test_validate_backprop_method_invalid_device(self): """Test that the method for validating the backprop diff method tape raises an exception if the device does not support backprop.""" From cbeec00a94f71804f63d94efbe817b9801851832 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Tue, 24 Aug 2021 16:35:07 +0100 Subject: [PATCH 02/11] add changelog --- .github/CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 6208f72a2c7..a34dcd5cec9 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -245,6 +245,10 @@ and requirements-ci.txt (unpinned). This latter would be used by the CI.

Breaking changes

+* And error is raised during QNode creation when a user requests backpropogation on + a device with finite-shots. + [(#1588)](https://github.com/PennyLaneAI/pennylane/pull/1588) + * The class `qml.Interferometer` is deprecated and will be renamed `qml.InterferometerUnitary` after one release cycle. [(#1546)](https://github.com/PennyLaneAI/pennylane/pull/1546) @@ -272,7 +276,7 @@ operation is easier to locate. This release contains contributions from (in alphabetical order): -Vishnu Ajith, Akash Narayanan B, Thomas Bromley, Tanya Garg, Josh Izaac, Prateek Jain, Johannes Jakob Meyer, Pratul Saini, Maria Schuld, +Vishnu Ajith, Akash Narayanan B, Thomas Bromley, Tanya Garg, Josh Izaac, Prateek Jain, Christina Lee, Johannes Jakob Meyer, Pratul Saini, Maria Schuld, Ingrid Strandberg, David Wierichs, Vincent Wong. From bfd9c24a9f86cb02406d4629c422c7f3fb310bfc Mon Sep 17 00:00:00 2001 From: albi3ro Date: Thu, 26 Aug 2021 11:55:15 +0100 Subject: [PATCH 03/11] get tests to pass, black --- pennylane/qnode.py | 6 ++++-- tests/devices/test_default_qubit_jax.py | 14 +++++++++----- tests/devices/test_default_qubit_tf.py | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pennylane/qnode.py b/pennylane/qnode.py index aec1253a1e1..c6508f88c41 100644 --- a/pennylane/qnode.py +++ b/pennylane/qnode.py @@ -316,8 +316,10 @@ def _validate_backprop_method(device, interface): interface provided is not compatible with the device """ if device.shots is not None: - raise qml.QuantumFunctionError("Devices with finite shots are incompatible with backpropogation. " - "Please set ``shots=None`` or chose a different diff_method.") + raise qml.QuantumFunctionError( + "Devices with finite shots are incompatible with backpropogation. " + "Please set ``shots=None`` or chose a different diff_method." + ) # determine if the device supports backpropagation backprop_interface = device.capabilities().get("passthru_interface", None) diff --git a/tests/devices/test_default_qubit_jax.py b/tests/devices/test_default_qubit_jax.py index 0571ecd27a1..417c0c5131c 100644 --- a/tests/devices/test_default_qubit_jax.py +++ b/tests/devices/test_default_qubit_jax.py @@ -151,6 +151,7 @@ def circuit(): expected = jnp.array([amplitude, 0, jnp.conj(amplitude), 0]) assert jnp.allclose(state, expected, atol=tol, rtol=0) + @pytest.mark.skip(reason="sampling doesnt work in backprop, fails with parameter-shift") def test_sampling_with_jit(self): """Test that sampling works with a jax.jit""" @@ -158,7 +159,7 @@ def test_sampling_with_jit(self): def circuit(key): dev = qml.device("default.qubit.jax", wires=1, shots=1000, prng_key=key) - @qml.qnode(dev, interface="jax", diff_method="backprop") + @qml.qnode(dev, interface="jax", diff_method="best") def inner_circuit(): qml.Hadamard(0) return qml.sample(qml.PauliZ(wires=0)) @@ -171,11 +172,12 @@ def inner_circuit(): np.testing.assert_array_equal(a, b) assert not np.all(a == c) + @pytest.mark.skip(reason="sampling doesnt work in backprop, fails with parameter-shift") def test_sampling_op_by_op(self): """Test that op-by-op sampling works as a new user would expect""" dev = qml.device("default.qubit.jax", wires=1, shots=1000) - @qml.qnode(dev, interface="jax", diff_method="backprop") + @qml.qnode(dev, interface="jax", diff_method="best") def circuit(): qml.Hadamard(0) return qml.sample(qml.PauliZ(wires=0)) @@ -190,7 +192,7 @@ def test_sampling_analytic_mode(self): """ dev = qml.device("default.qubit.jax", wires=1, shots=None) - @qml.qnode(dev, interface="jax", diff_method="backprop") + @qml.qnode(dev, interface="jax", diff_method="best") def circuit(): return qml.sample(qml.PauliZ(wires=0)) @@ -201,11 +203,12 @@ def circuit(): assert len(res) == 1000 + @pytest.mark.skip(reason="sampling doesnt work in backprop, fails with parameter-shift") def test_gates_dont_crash(self): """Test for gates that weren't covered by other tests.""" dev = qml.device("default.qubit.jax", wires=2, shots=1000) - @qml.qnode(dev, interface="jax", diff_method="backprop") + @qml.qnode(dev, interface="jax", diff_method="best") def circuit(): qml.CRZ(0.0, wires=[0, 1]) qml.CRX(0.0, wires=[0, 1]) @@ -217,11 +220,12 @@ def circuit(): circuit() # Just don't crash. + @pytest.mark.skip(reason="sampling doesnt work in backprop, fails with parameter-shift") def test_diagonal_doesnt_crash(self): """Test that diagonal gates can be used.""" dev = qml.device("default.qubit.jax", wires=1, shots=1000) - @qml.qnode(dev, interface="jax", diff_method="backprop") + @qml.qnode(dev, interface="jax", diff_method="best") def circuit(): qml.DiagonalQubitUnitary(np.array([1.0, 1.0]), wires=0) return qml.sample(qml.PauliZ(wires=0)) diff --git a/tests/devices/test_default_qubit_tf.py b/tests/devices/test_default_qubit_tf.py index c4f33b071ce..f366be7955e 100644 --- a/tests/devices/test_default_qubit_tf.py +++ b/tests/devices/test_default_qubit_tf.py @@ -1311,7 +1311,7 @@ def test_sample_observables(self): shots = 100 dev = qml.device("default.qubit.tf", wires=2, shots=shots) - @qml.qnode(dev, diff_method="backprop", interface="tf") + @qml.qnode(dev, diff_method="best", interface="tf") def circuit(a): qml.RX(a, wires=0) return qml.sample(qml.PauliZ(0)) From ef4f6c84ac94236e8b2c43be16697c0826043dec Mon Sep 17 00:00:00 2001 From: albi3ro Date: Thu, 26 Aug 2021 12:07:07 +0100 Subject: [PATCH 04/11] black --- tests/devices/test_default_qubit_tf.py | 1 + tests/tape/test_qnode.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/devices/test_default_qubit_tf.py b/tests/devices/test_default_qubit_tf.py index f366be7955e..61c05c06daa 100644 --- a/tests/devices/test_default_qubit_tf.py +++ b/tests/devices/test_default_qubit_tf.py @@ -1329,6 +1329,7 @@ def test_sample_observables_non_differentiable(self): dev = qml.device("default.qubit.tf", wires=2, shots=shots) with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): + @qml.qnode(dev, diff_method="backprop", interface="tf") def circuit(a): qml.RX(a, wires=0) diff --git a/tests/tape/test_qnode.py b/tests/tape/test_qnode.py index 6e65aadd01c..8cb2147286e 100644 --- a/tests/tape/test_qnode.py +++ b/tests/tape/test_qnode.py @@ -73,7 +73,7 @@ def test_validate_device_method(self, monkeypatch): def test_validate_backprop_method_finite_shots(self): """Tests an error is raised for backpropogation with finite shots.""" - dev = qml.device('default.qubit', wires=1, shots=3) + dev = qml.device("default.qubit", wires=1, shots=3) with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): QNode._validate_backprop_method(dev, "doesnt matter") From 77ff97ca07db6d792b6a6375154403ba42d2e4b2 Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Thu, 26 Aug 2021 15:25:08 +0100 Subject: [PATCH 05/11] Apply suggestions from code review Co-authored-by: antalszava --- .github/CHANGELOG.md | 2 +- pennylane/qnode.py | 2 +- tests/tape/test_qnode.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 2f7a2c05c52..08279d052dd 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -325,7 +325,7 @@ and requirements-ci.txt (unpinned). This latter would be used by the CI.

Breaking changes

-* And error is raised during QNode creation when a user requests backpropogation on +* An error is raised during QNode creation when a user requests backpropagation on a device with finite-shots. [(#1588)](https://github.com/PennyLaneAI/pennylane/pull/1588) diff --git a/pennylane/qnode.py b/pennylane/qnode.py index 504629f8ad9..f75f12da496 100644 --- a/pennylane/qnode.py +++ b/pennylane/qnode.py @@ -318,7 +318,7 @@ def _validate_backprop_method(device, interface): if device.shots is not None: raise qml.QuantumFunctionError( "Devices with finite shots are incompatible with backpropogation. " - "Please set ``shots=None`` or chose a different diff_method." + "Please set shots=None or chose a different diff_method." ) # determine if the device supports backpropagation diff --git a/tests/tape/test_qnode.py b/tests/tape/test_qnode.py index 8cb2147286e..057cc6f79de 100644 --- a/tests/tape/test_qnode.py +++ b/tests/tape/test_qnode.py @@ -71,7 +71,7 @@ def test_validate_device_method(self, monkeypatch): assert device is dev def test_validate_backprop_method_finite_shots(self): - """Tests an error is raised for backpropogation with finite shots.""" + """Tests that an error is raised for backpropagation with finite shots.""" dev = qml.device("default.qubit", wires=1, shots=3) From 9bcc23b451d9ee312c0b7f2c7163d7728d47fb3e Mon Sep 17 00:00:00 2001 From: albi3ro Date: Fri, 27 Aug 2021 10:01:00 +0100 Subject: [PATCH 06/11] test over interfaces --- tests/tape/test_qnode.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/tape/test_qnode.py b/tests/tape/test_qnode.py index 057cc6f79de..751df3c20ec 100644 --- a/tests/tape/test_qnode.py +++ b/tests/tape/test_qnode.py @@ -70,13 +70,14 @@ def test_validate_device_method(self, monkeypatch): assert interface == "interface" assert device is dev - def test_validate_backprop_method_finite_shots(self): + @pytest.mark.parametrize("interface", ("autograd", "torch","tensorflow", "jax")) + def test_validate_backprop_method_finite_shots(self, interface): """Tests that an error is raised for backpropagation with finite shots.""" dev = qml.device("default.qubit", wires=1, shots=3) with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): - QNode._validate_backprop_method(dev, "doesnt matter") + QNode._validate_backprop_method(dev, interface) def test_validate_backprop_method_invalid_device(self): """Test that the method for validating the backprop diff method From 9d860ed3fe2c14796198ed9fc7363f0607d54c9b Mon Sep 17 00:00:00 2001 From: albi3ro Date: Thu, 9 Sep 2021 12:22:53 +0100 Subject: [PATCH 07/11] black --- tests/tape/test_qnode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tape/test_qnode.py b/tests/tape/test_qnode.py index b68e80ca835..04053d0137e 100644 --- a/tests/tape/test_qnode.py +++ b/tests/tape/test_qnode.py @@ -70,7 +70,7 @@ def test_validate_device_method(self, monkeypatch): assert interface == "interface" assert device is dev - @pytest.mark.parametrize("interface", ("autograd", "torch","tensorflow", "jax")) + @pytest.mark.parametrize("interface", ("autograd", "torch", "tensorflow", "jax")) def test_validate_backprop_method_finite_shots(self, interface): """Tests that an error is raised for backpropagation with finite shots.""" From 6b5c677e5aaffac828b25bb66399aae3db291211 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Thu, 9 Sep 2021 18:40:40 +0100 Subject: [PATCH 08/11] use diff_method=None in tests --- tests/devices/test_default_qubit_jax.py | 13 ++++------ tests/devices/test_default_qubit_torch.py | 29 ++++------------------- 2 files changed, 9 insertions(+), 33 deletions(-) diff --git a/tests/devices/test_default_qubit_jax.py b/tests/devices/test_default_qubit_jax.py index 5908d41dcee..724fe7d08a3 100644 --- a/tests/devices/test_default_qubit_jax.py +++ b/tests/devices/test_default_qubit_jax.py @@ -160,7 +160,7 @@ def test_sampling_with_jit(self): def circuit(key): dev = qml.device("default.qubit.jax", wires=1, shots=1000, prng_key=key) - @qml.qnode(dev, interface="jax", diff_method="best") + @qml.qnode(dev, interface="jax", diff_method=None) def inner_circuit(): qml.Hadamard(0) return qml.sample(qml.PauliZ(wires=0)) @@ -173,12 +173,11 @@ def inner_circuit(): np.testing.assert_array_equal(a, b) assert not np.all(a == c) - @pytest.mark.skip(reason="sampling doesnt work in backprop, fails with parameter-shift") def test_sampling_op_by_op(self): """Test that op-by-op sampling works as a new user would expect""" dev = qml.device("default.qubit.jax", wires=1, shots=1000) - @qml.qnode(dev, interface="jax", diff_method="best") + @qml.qnode(dev, interface="jax", diff_method=None) def circuit(): qml.Hadamard(0) return qml.sample(qml.PauliZ(wires=0)) @@ -193,7 +192,7 @@ def test_sampling_analytic_mode(self): """ dev = qml.device("default.qubit.jax", wires=1, shots=None) - @qml.qnode(dev, interface="jax", diff_method="best") + @qml.qnode(dev, interface="jax", diff_method=None) def circuit(): return qml.sample(qml.PauliZ(wires=0)) @@ -204,12 +203,11 @@ def circuit(): assert len(res) == 1000 - @pytest.mark.skip(reason="sampling doesnt work in backprop, fails with parameter-shift") def test_gates_dont_crash(self): """Test for gates that weren't covered by other tests.""" dev = qml.device("default.qubit.jax", wires=2, shots=1000) - @qml.qnode(dev, interface="jax", diff_method="best") + @qml.qnode(dev, interface="jax", diff_method=None) def circuit(): qml.CRZ(0.0, wires=[0, 1]) qml.CRX(0.0, wires=[0, 1]) @@ -221,12 +219,11 @@ def circuit(): circuit() # Just don't crash. - @pytest.mark.skip(reason="sampling doesnt work in backprop, fails with parameter-shift") def test_diagonal_doesnt_crash(self): """Test that diagonal gates can be used.""" dev = qml.device("default.qubit.jax", wires=1, shots=1000) - @qml.qnode(dev, interface="jax", diff_method="best") + @qml.qnode(dev, interface="jax", diff_method=None) def circuit(): qml.DiagonalQubitUnitary(np.array([1.0, 1.0]), wires=0) return qml.sample(qml.PauliZ(wires=0)) diff --git a/tests/devices/test_default_qubit_torch.py b/tests/devices/test_default_qubit_torch.py index 851f202518a..c0b432c04e9 100644 --- a/tests/devices/test_default_qubit_torch.py +++ b/tests/devices/test_default_qubit_torch.py @@ -1435,7 +1435,7 @@ def test_sample_observables(self): shots = 100 dev = qml.device("default.qubit.torch", wires=2, shots=shots) - @qml.qnode(dev, diff_method="backprop", interface="torch") + @qml.qnode(dev, diff_method=None, interface="torch") def circuit(a): qml.RX(a, wires=0) return qml.sample(qml.PauliZ(0)) @@ -1451,7 +1451,7 @@ def test_estimating_marginal_probability(self, tol): """Test that the probability of a subset of wires is accurately estimated.""" dev = qml.device("default.qubit.torch", wires=2, shots=1000) - @qml.qnode(dev, diff_method="backprop", interface="torch") + @qml.qnode(dev, diff_method=None, interface="torch") def circuit(): qml.PauliX(0) return qml.probs(wires=[0]) @@ -1467,7 +1467,7 @@ def test_estimating_full_probability(self, tol): """Test that the probability of a subset of wires is accurately estimated.""" dev = qml.device("default.qubit.torch", wires=2, shots=1000) - @qml.qnode(dev, diff_method="backprop", interface="torch") + @qml.qnode(dev, diff_method=None, interface="torch") def circuit(): qml.PauliX(0) qml.PauliX(1) @@ -1485,7 +1485,7 @@ def test_estimating_expectation_values(self, tol): of shots produces a numeric tensor""" dev = qml.device("default.qubit.torch", wires=3, shots=1000) - @qml.qnode(dev, diff_method="backprop", interface="torch") + @qml.qnode(dev, diff_method=None, interface="torch") def circuit(a, b): qml.RX(a, wires=[0]) qml.RX(b, wires=[1]) @@ -1503,27 +1503,6 @@ def circuit(a, b): # expected = [torch.cos(a), torch.cos(a) * torch.cos(b)] # assert np.allclose(res, expected, atol=tol, rtol=0) - def test_estimating_expectation_values_not_differentiable(self, tol): - """Test that finite shots results in non-differentiable QNodes""" - - dev = qml.device("default.qubit.torch", wires=3, shots=1000) - - @qml.qnode(dev, diff_method="backprop", interface="torch") - def circuit(a, b): - qml.RX(a, wires=[0]) - qml.RX(b, wires=[1]) - qml.CNOT(wires=[0, 1]) - return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)) - - a = torch.tensor(0.543) - b = torch.tensor(0.43) - - res = circuit(a, b) - - with pytest.raises(RuntimeError): - res.backward() - - class TestHighLevelIntegration: """Tests for integration with higher level components of PennyLane.""" From 2c95ad5b4947b413b86d59e4ddf0e1753aa9d0c8 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Thu, 9 Sep 2021 18:44:07 +0100 Subject: [PATCH 09/11] black --- tests/devices/test_default_qubit_tf.py | 6 +++--- tests/devices/test_default_qubit_torch.py | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/devices/test_default_qubit_tf.py b/tests/devices/test_default_qubit_tf.py index 48661a07e48..db67e87ec41 100644 --- a/tests/devices/test_default_qubit_tf.py +++ b/tests/devices/test_default_qubit_tf.py @@ -1340,7 +1340,7 @@ def test_estimating_marginal_probability(self, tol): """Test that the probability of a subset of wires is accurately estimated.""" dev = qml.device("default.qubit.tf", wires=2, shots=1000) - @qml.qnode(dev, diff_method="parameter-shift", interface="tf") + @qml.qnode(dev, diff_method=None, interface="tf") def circuit(): qml.PauliX(0) return qml.probs(wires=[0]) @@ -1356,7 +1356,7 @@ def test_estimating_full_probability(self, tol): """Test that the probability of a subset of wires is accurately estimated.""" dev = qml.device("default.qubit.tf", wires=2, shots=1000) - @qml.qnode(dev, diff_method="parameter-shift", interface="tf") + @qml.qnode(dev, diff_method=None, interface="tf") def circuit(): qml.PauliX(0) qml.PauliX(1) @@ -1374,7 +1374,7 @@ def test_estimating_expectation_values(self, tol): of shots produces a numeric tensor""" dev = qml.device("default.qubit.tf", wires=3, shots=1000) - @qml.qnode(dev, diff_method="parameter-shift", interface="tf") + @qml.qnode(dev, diff_method=None, interface="tf") def circuit(a, b): qml.RX(a, wires=[0]) qml.RX(b, wires=[1]) diff --git a/tests/devices/test_default_qubit_torch.py b/tests/devices/test_default_qubit_torch.py index c0b432c04e9..66485376e68 100644 --- a/tests/devices/test_default_qubit_torch.py +++ b/tests/devices/test_default_qubit_torch.py @@ -1503,6 +1503,7 @@ def circuit(a, b): # expected = [torch.cos(a), torch.cos(a) * torch.cos(b)] # assert np.allclose(res, expected, atol=tol, rtol=0) + class TestHighLevelIntegration: """Tests for integration with higher level components of PennyLane.""" From 959bbbbac23bb2e82cc8eef7c99812fb19b7df37 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Fri, 10 Sep 2021 10:46:39 +0100 Subject: [PATCH 10/11] remove unnecessary test --- tests/devices/test_default_qubit_tf.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/devices/test_default_qubit_tf.py b/tests/devices/test_default_qubit_tf.py index db67e87ec41..6e549ce8396 100644 --- a/tests/devices/test_default_qubit_tf.py +++ b/tests/devices/test_default_qubit_tf.py @@ -1324,18 +1324,6 @@ def circuit(a): assert res.shape == (shots,) assert set(res.numpy()) == {-1, 1} - def test_sample_observables_non_differentiable(self): - """Test that sampled observables cannot be differentiated.""" - shots = 100 - dev = qml.device("default.qubit.tf", wires=2, shots=shots) - - with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): - - @qml.qnode(dev, diff_method="backprop", interface="tf") - def circuit(a): - qml.RX(a, wires=0) - return qml.sample(qml.PauliZ(0)) - def test_estimating_marginal_probability(self, tol): """Test that the probability of a subset of wires is accurately estimated.""" dev = qml.device("default.qubit.tf", wires=2, shots=1000) From 51593a6c03655ff039d2729dbd2ef28248934f47 Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Fri, 10 Sep 2021 14:18:05 +0100 Subject: [PATCH 11/11] Update tests/devices/test_default_qubit_jax.py --- tests/devices/test_default_qubit_jax.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/devices/test_default_qubit_jax.py b/tests/devices/test_default_qubit_jax.py index 724fe7d08a3..5593618cfa4 100644 --- a/tests/devices/test_default_qubit_jax.py +++ b/tests/devices/test_default_qubit_jax.py @@ -152,7 +152,6 @@ def circuit(): expected = jnp.array([amplitude, 0, jnp.conj(amplitude), 0]) assert jnp.allclose(state, expected, atol=tol, rtol=0) - @pytest.mark.skip(reason="sampling doesnt work in backprop, fails with parameter-shift") def test_sampling_with_jit(self): """Test that sampling works with a jax.jit"""