Skip to content

Commit

Permalink
Fix grover operator work wires (#4668)
Browse files Browse the repository at this point in the history
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 PennyLaneAI/pennylane-qiskit#339

---------

Co-authored-by: Matthew Silverman <[email protected]>
  • Loading branch information
albi3ro and timmysilv authored Oct 13, 2023
1 parent 7ce1d4f commit eafabe4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@

<h3>Bug fixes 🐛</h3>

* 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)

Expand Down
5 changes: 4 additions & 1 deletion pennylane/templates/subroutines/grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions tests/templates/test_subroutines/test_grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit eafabe4

Please sign in to comment.