From 06d3fd28460d0c9c4dc20894ece4035b34f171c1 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Wed, 9 Oct 2024 12:53:57 -0400 Subject: [PATCH 1/4] remove allwires validation + tests --- pennylane/workflow/qnode.py | 10 ------- tests/test_qnode.py | 55 ------------------------------------- 2 files changed, 65 deletions(-) diff --git a/pennylane/workflow/qnode.py b/pennylane/workflow/qnode.py index ab68a9ad147..140ce8c66a0 100644 --- a/pennylane/workflow/qnode.py +++ b/pennylane/workflow/qnode.py @@ -861,16 +861,6 @@ def construct(self, args, kwargs): # pylint: disable=too-many-branches "All measurements must be returned in the order they are measured." ) - num_wires = len(self.tape.wires) if not self.device.wires else len(self.device.wires) - for obj in self.tape.operations + self.tape.observables: - if ( - getattr(obj, "num_wires", None) is qml.operation.WiresEnum.AllWires - and obj.wires - and len(obj.wires) != num_wires - ): - # check here only if enough wires - raise qml.QuantumFunctionError(f"Operator {obj.name} must act on all wires") - def _execution_component(self, args: tuple, kwargs: dict) -> qml.typing.Result: """Construct the transform program and execute the tapes. Helper function for ``__call__`` diff --git a/tests/test_qnode.py b/tests/test_qnode.py index b0b381a1806..e0266a720e5 100644 --- a/tests/test_qnode.py +++ b/tests/test_qnode.py @@ -645,61 +645,6 @@ def func(x, y): assert qn.qtape.operations == contents[0:3] assert qn.qtape.measurements == contents[3:] - def test_operator_all_device_wires(self, monkeypatch, tol): - """Test that an operator that must act on all wires raises an error - if the operator wires are not the device wires (when device wires - are defined).""" - monkeypatch.setattr(qml.RX, "num_wires", qml.operation.AllWires) - - def circuit(x): - qml.RX(x, wires=0) - return qml.expval(qml.PauliZ(0)) - - dev = qml.device("default.qubit", wires=2) - qn = QNode(circuit, dev) - - with pytest.raises(qml.QuantumFunctionError, match="Operator RX must act on all wires"): - qn(0.5) - - dev = qml.device("default.qubit", wires=1) - qn = QNode(circuit, dev) - assert np.allclose(qn(0.5), np.cos(0.5), atol=tol, rtol=0) - - def test_all_wires_new_device(self): - """Test that an operator on AllWires must act on all device wires if they - are specified, and otherwise all tape wires, with the new device API.""" - - assert qml.GlobalPhase.num_wires == qml.operation.AllWires - - dev = qml.device("default.qubit") - dev_with_wires = qml.device("default.qubit", wires=3) - - @qml.qnode(dev) - def circuit1(x): - qml.GlobalPhase(x, wires=0) - return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)) - - # fails when GlobalPhase is a strict subset of all tape wires - with pytest.raises(qml.QuantumFunctionError, match="GlobalPhase must act on all wires"): - circuit1(0.5) - - @qml.qnode(dev) - def circuit2(x): - qml.GlobalPhase(x, wires=[0, 1]) - return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)) - - # passes here, does not care for device.wires because it has none - assert circuit2(0.5) == 1 - - @qml.qnode(dev_with_wires) - def circuit3(x): - qml.GlobalPhase(x, wires=[0, 1]) - return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)) - - # fails when GlobalPhase is a subset of device wires, even if it acts on all tape wires - with pytest.raises(qml.QuantumFunctionError, match="GlobalPhase must act on all wires"): - circuit3(0.5) - @pytest.mark.jax def test_jit_counts_raises_error(self): """Test that returning counts in a quantum function with trainable parameters while From 35fa04f2d95363da950395ee466edd104c89f45d Mon Sep 17 00:00:00 2001 From: andrijapau Date: Wed, 9 Oct 2024 13:00:54 -0400 Subject: [PATCH 2/4] update changelog --- doc/releases/changelog-dev.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 496cba2d23f..739699df84a 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -137,6 +137,9 @@

Breaking changes 💔

+* `AllWires` validation in `QNode.construct` has been removed. + [(#6373)](https://github.com/PennyLaneAI/pennylane/pull/6373) + * The `simplify` argument in `qml.Hamiltonian` and `qml.ops.LinearCombination` has been removed. Instead, `qml.simplify()` can be called on the constructed operator. [(#6279)](https://github.com/PennyLaneAI/pennylane/pull/6279) From 61b893bd576c6754bdd76ebca3cb83e292e5fc6d Mon Sep 17 00:00:00 2001 From: andrijapau Date: Wed, 9 Oct 2024 17:17:26 -0400 Subject: [PATCH 3/4] retrigger ci From 4e5f69633b297ec4762f73ab9bbf9c6fc8ea9295 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 10 Oct 2024 09:06:37 -0400 Subject: [PATCH 4/4] remove related tests that were missed --- tests/test_operation.py | 22 -------------------- tests/test_qnode_legacy.py | 41 -------------------------------------- 2 files changed, 63 deletions(-) diff --git a/tests/test_operation.py b/tests/test_operation.py index 25334ad1e89..cdcdc48fb4d 100644 --- a/tests/test_operation.py +++ b/tests/test_operation.py @@ -1070,28 +1070,6 @@ class DummyObserv(qml.operation.Observable): class TestOperatorIntegration: """Integration tests for the Operator class""" - def test_all_wires_defined_but_init_with_one(self): - """Test that an exception is raised if the class is defined with ALL wires, - but then instantiated with only one""" - - dev1 = qml.device("default.qubit", wires=2) - - class DummyOp(qml.operation.Operation): - r"""Dummy custom operator""" - - num_wires = qml.operation.WiresEnum.AllWires - - @qml.qnode(dev1) - def circuit(): - DummyOp(wires=[0]) - return qml.expval(qml.PauliZ(0)) - - with pytest.raises( - qml.QuantumFunctionError, - match=f"Operator {DummyOp.__name__} must act on all wires", - ): - circuit() - def test_pow_method_with_non_numeric_power_raises_error(self): """Test that when raising an Operator to a power that is not a number raises a ValueError.""" diff --git a/tests/test_qnode_legacy.py b/tests/test_qnode_legacy.py index 76a5b5f485d..aa9c8e75f72 100644 --- a/tests/test_qnode_legacy.py +++ b/tests/test_qnode_legacy.py @@ -529,47 +529,6 @@ def func(x, y): assert qn.qtape.operations == contents[0:3] assert qn.qtape.measurements == contents[3:] - def test_operator_all_wires(self, monkeypatch, tol): - """Test that an operator that must act on all wires - does, or raises an error.""" - monkeypatch.setattr(qml.RX, "num_wires", qml.operation.AllWires) - - def circuit(x): - qml.RX(x, wires=0) - return qml.expval(qml.PauliZ(0)) - - dev = qml.device("default.mixed", wires=2) - qn = QNode(circuit, dev) - - with pytest.raises(qml.QuantumFunctionError, match="Operator RX must act on all wires"): - qn(0.5) - - dev = qml.device("default.mixed", wires=1) - qn = QNode(circuit, dev) - assert np.allclose(qn(0.5), np.cos(0.5), atol=tol, rtol=0) - - def test_all_wires_new_device(self): - """Test that an operator must act on all tape wires with the new device API.""" - - def circuit1(x): - qml.GlobalPhase(x, wires=0) - return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)) - - dev = qml.devices.DefaultQubit() # TODO: add wires, change comment below - qn = QNode(circuit1, dev) - - # fails when GlobalPhase is a strict subset of all tape wires - with pytest.raises(qml.QuantumFunctionError, match="GlobalPhase must act on all wires"): - qn(0.5) - - @qml.qnode(dev) - def circuit2(x): - qml.GlobalPhase(x, wires=[0, 1]) - return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)) - - # passes here, does not care for device.wires because it has none - assert circuit2(0.5) == 1 - @pytest.mark.jax def test_jit_counts_raises_error(self): """Test that returning counts in a quantum function with trainable parameters while