From eafabe418a4fd83e36333abab762c119ef5b80a1 Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Fri, 13 Oct 2023 11:45:47 -0400 Subject: [PATCH] Fix grover operator work wires (#4668) The default to `GroverOperator` for work wires was `None`. Unfortunately this was being interpreted as `work_wires = Wires([None,])`. This PR makes it so that `work_wires=None` is interpreted as `work_wires = Wires([])`. Fixes https://github.com/PennyLaneAI/pennylane-qiskit/issues/339 --------- Co-authored-by: Matthew Silverman --- doc/releases/changelog-dev.md | 3 +++ pennylane/templates/subroutines/grover.py | 5 ++++- tests/templates/test_subroutines/test_grover.py | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 3d7a8d7fc24..d500a55edca 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -340,6 +340,9 @@

Bug fixes 🐛

+* Providing `work_wires=None` to `qml.GroverOperator` no longer interprets `None` as a wire. + [(#4668)](https://github.com/PennyLaneAI/pennylane/pull/4668) + * Fixed issue where `__copy__` method of the `qml.Select()` operator attempted to access un-initialized data. [(#4551)](https://github.com/PennyLaneAI/pennylane/pull/4551) diff --git a/pennylane/templates/subroutines/grover.py b/pennylane/templates/subroutines/grover.py index b6178e0e701..5f3cbf09461 100644 --- a/pennylane/templates/subroutines/grover.py +++ b/pennylane/templates/subroutines/grover.py @@ -113,7 +113,10 @@ def __init__(self, wires=None, work_wires=None, id=None): if (not hasattr(wires, "__len__")) or (len(wires) < 2): raise ValueError("GroverOperator must have at least two wires provided.") - self._hyperparameters = {"n_wires": len(wires), "work_wires": Wires(work_wires)} + self._hyperparameters = { + "n_wires": len(wires), + "work_wires": Wires(work_wires) if work_wires is not None else Wires([]), + } super().__init__(wires=wires, id=id) diff --git a/tests/templates/test_subroutines/test_grover.py b/tests/templates/test_subroutines/test_grover.py index bc2f0dce0fd..987ef2d5daf 100644 --- a/tests/templates/test_subroutines/test_grover.py +++ b/tests/templates/test_subroutines/test_grover.py @@ -62,6 +62,12 @@ def test_work_wires(): assert ops[2].hyperparameters["work_wires"] == work_wire +def test_work_wires_None(): + """Test that work wires of None are not inpreted as work wires.""" + op = qml.GroverOperator(wires=(0, 1, 2, 3), work_wires=None) + assert op.hyperparameters["work_wires"] == qml.wires.Wires([]) + + @pytest.mark.parametrize("bad_wires", [0, (0,), tuple()]) def test_single_wire_error(bad_wires): """Assert error raised when called with only a single wire"""