Skip to content

Commit

Permalink
Improve QROM decomposition if no work wires provided (#6967)
Browse files Browse the repository at this point in the history
### Before submitting

Please complete the following checklist when submitting a PR:

- [x] All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to
the
      test directory!

- [x] All new functions and code must be clearly commented and
documented.
If you do make documentation changes, make sure that the docs build and
      render correctly by running `make docs`.

- [x] Ensure that the test suite passes, by running `make test`.

- [x] Add a new entry to the `doc/releases/changelog-dev.md` file,
summarizing the
      change, and including a link back to the PR.

- [x] The PennyLane source code conforms to
      [PEP8 standards](https://www.python.org/dev/peps/pep-0008/).
We check all of our code against [Pylint](https://www.pylint.org/).
      To lint modified files, simply `pip install pylint`, and then
      run `pylint pennylane/path/to/file.py`.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


------------------------------------------------------------------------------------------------------------

**Context:**

Fix this bug [#6968]
[sc-84591]
**Description of the Change:**

Check if work wires are used and choose the correct decomposition based
on that.

**Benefits:**

More efficient decomposition

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: Jay Soni <[email protected]>
Co-authored-by: Soran Jahangiri <[email protected]>
  • Loading branch information
3 people authored Feb 25, 2025
1 parent d21992b commit 329965e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,14 @@
passed to the `QNode`, instead of assuming the method is always `deferred`.
[(#6903)](https://github.com/PennyLaneAI/pennylane/pull/6903)

* The `QROM` template is upgraded to decompose more efficiently when `work_wires` are not used.
[#6967)](https://github.com/PennyLaneAI/pennylane/pull/6967)

<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):

Guillermo Alonso,
Utkarsh Azad,
Henry Chang,
Yushao Chen,
Expand Down
2 changes: 1 addition & 1 deletion pennylane/templates/subroutines/qrom.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def compute_decomposition(
)
swap_ops.insert(0, qml.ctrl(new_op, control=control_swap_wires[-ind - 1]))

if not clean:
if not clean or depth == 1:
# Based on this paper (Fig 1.c): https://arxiv.org/abs/1812.00954
decomp_ops = select_ops + swap_ops

Expand Down
25 changes: 25 additions & 0 deletions tests/templates/test_subroutines/test_qrom.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ class TestQROM:
@pytest.mark.parametrize(
("bitstrings", "target_wires", "control_wires", "work_wires", "clean"),
[
(
["111", "101", "100", "110"],
[0, 1, 2],
[3, 4],
None,
False,
),
(
["111", "101", "100", "110"],
[0, 1, 2],
[3, 4],
None,
True,
),
(
["11", "01", "00", "10"],
[0, 1],
Expand Down Expand Up @@ -258,3 +272,14 @@ def test_wrong_wires_error(bitstrings, control_wires, target_wires, msg_match):
"""Test that error is raised if more ops are requested than can fit in control wires"""
with pytest.raises(ValueError, match=msg_match):
qml.QROM(bitstrings, control_wires, target_wires, work_wires=None)


def test_none_work_wires_case():
"""Test that clean version is not applied if work wires are not used"""

gates_clean = qml.QROM.compute_decomposition(["1", "0", "0", "1"], [0, 1], [2], [], clean=True)
expected_gates = qml.QROM.compute_decomposition(
["1", "0", "0", "1"], [0, 1], [2], [], clean=False
)

assert gates_clean == expected_gates

0 comments on commit 329965e

Please sign in to comment.