From 1c1f36e26bea4da00204a9cdfada7903c0d076d1 Mon Sep 17 00:00:00 2001 From: Ali Asadi <10773383+maliasadi@users.noreply.github.com> Date: Mon, 30 Sep 2024 09:31:15 -0400 Subject: [PATCH] Remove magic numbers in Lightning stopping_conditions (#926) ### Before submitting Please complete the following checklist when submitting a PR: - [ ] 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 [`tests`](../tests) directory! - [ ] 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`. - [ ] Ensure that the test suite passes, by running `make test`. - [ ] Add a new entry to the `.github/CHANGELOG.md` file, summarizing the change, and including a link back to the PR. - [ ] Ensure that code is properly formatted by running `make format`. When all the above are checked, delete everything above the dashed line and fill in the pull request template. ------------------------------------------------------------------------------------------------------------ **Context:** **Description of the Change:** **Benefits:** **Possible Drawbacks:** **Related GitHub Issues:** [sc-74414] --------- Co-authored-by: ringo-but-quantum --- .github/CHANGELOG.md | 3 +++ README.rst | 1 + pennylane_lightning/core/_version.py | 2 +- pennylane_lightning/core/lightning_base.py | 4 ---- .../lightning_kokkos/lightning_kokkos.py | 6 ------ .../lightning_qubit/lightning_qubit.py | 7 ------- .../lightning_tensor/lightning_tensor.py | 7 ------- tests/test_templates.py | 13 ++++++------- 8 files changed, 11 insertions(+), 32 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index e8e5540ba..8b532c26d 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -34,6 +34,9 @@ ### Improvements +* Remove dynamic decomposition rules in Lightning. + [(#926)](https://github.com/PennyLaneAI/pennylane-lightning/pull/926) + * Add the `ci:use-gpu-runner` GitHub label to `lightning.kokkos` GPU Testing CIs. [(#916)](https://github.com/PennyLaneAI/pennylane-lightning/pull/916) diff --git a/README.rst b/README.rst index 6649f184d..a762da889 100644 --- a/README.rst +++ b/README.rst @@ -50,6 +50,7 @@ PennyLane-Lightning high performance simulators include the following backends: * ``lightning.gpu``: is a state-vector simulator based on the `NVIDIA cuQuantum SDK `_. It notably implements a distributed state-vector simulator based on MPI. * ``lightning.kokkos``: is a state-vector simulator written with `Kokkos `_. It can exploit the inherent parallelism of modern processing units supporting the `OpenMP `_, `CUDA `_ or `HIP `_ programming models. * ``lightning.tensor``: is a tensor network simulator based on the `NVIDIA cuQuantum SDK `_ (requires NVIDIA GPUs with SM 7.0 or greater). The supported method is Matrix Product State (MPS). + .. header-end-inclusion-marker-do-not-remove The following table summarizes the supported platforms and the primary installation mode: diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index 017bfccf8..4b8d31ae9 100644 --- a/pennylane_lightning/core/_version.py +++ b/pennylane_lightning/core/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.39.0-dev32" +__version__ = "0.39.0-dev33" diff --git a/pennylane_lightning/core/lightning_base.py b/pennylane_lightning/core/lightning_base.py index 016b7a476..dd355e6a0 100644 --- a/pennylane_lightning/core/lightning_base.py +++ b/pennylane_lightning/core/lightning_base.py @@ -94,10 +94,6 @@ def stopping_condition(self): and observable) and returns ``True`` if supported by the device.""" def accepts_obj(obj): - if isinstance(obj, qml.QFT): - return len(obj.wires) < 10 - if isinstance(obj, qml.GroverOperator): - return len(obj.wires) < 13 is_not_tape = not isinstance(obj, qml.tape.QuantumTape) is_supported = getattr(self, "supports_operation", lambda name: False)(obj.name) return is_not_tape and is_supported diff --git a/pennylane_lightning/lightning_kokkos/lightning_kokkos.py b/pennylane_lightning/lightning_kokkos/lightning_kokkos.py index 4089b3aca..63cf446c5 100644 --- a/pennylane_lightning/lightning_kokkos/lightning_kokkos.py +++ b/pennylane_lightning/lightning_kokkos/lightning_kokkos.py @@ -151,12 +151,6 @@ def stopping_condition(op: Operator) -> bool: """A function that determines whether or not an operation is supported by ``lightning.kokkos``.""" - # To avoid building matrices beyond the given thresholds. - # This should reduce runtime overheads for larger systems. - if isinstance(op, qml.QFT): - return len(op.wires) < 10 - if isinstance(op, qml.GroverOperator): - return len(op.wires) < 13 if isinstance(op, qml.PauliRot): word = op._hyperparameters["pauli_word"] # pylint: disable=protected-access # decomposes to IsingXX, etc. for n <= 2 diff --git a/pennylane_lightning/lightning_qubit/lightning_qubit.py b/pennylane_lightning/lightning_qubit/lightning_qubit.py index 5340d1bad..961235ae2 100644 --- a/pennylane_lightning/lightning_qubit/lightning_qubit.py +++ b/pennylane_lightning/lightning_qubit/lightning_qubit.py @@ -170,13 +170,6 @@ def stopping_condition(op: Operator) -> bool: """A function that determines whether or not an operation is supported by ``lightning.qubit``.""" - # To avoid building matrices beyond the given thresholds. - # This should reduce runtime overheads for larger systems. - if isinstance(op, qml.QFT): - return len(op.wires) < 10 - if isinstance(op, qml.GroverOperator): - return len(op.wires) < 13 - # As ControlledQubitUnitary == C(QubitUnitrary), # it can be removed from `_operations` to keep # consistency with `lightning_qubit.toml` diff --git a/pennylane_lightning/lightning_tensor/lightning_tensor.py b/pennylane_lightning/lightning_tensor/lightning_tensor.py index ff53d41ed..333ce35d2 100644 --- a/pennylane_lightning/lightning_tensor/lightning_tensor.py +++ b/pennylane_lightning/lightning_tensor/lightning_tensor.py @@ -168,13 +168,6 @@ def stopping_condition(op: Operator) -> bool: """A function that determines whether or not an operation is supported by the ``mps`` method of ``lightning.tensor``.""" - # TODOs: These thresholds are from ``lightning.qubit`` and should be adjuested based on the benchmarking tests for the MPS - # simulator (against both max_mps_bond_dim and number of qubits). - if isinstance(op, qml.QFT): - return len(op.wires) < 10 - if isinstance(op, qml.GroverOperator): - return len(op.wires) < 13 - if isinstance(op, qml.ControlledQubitUnitary): return True diff --git a/tests/test_templates.py b/tests/test_templates.py index a0b6913b6..93e2c83e0 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -726,7 +726,7 @@ def circuit(): class TestQFT: """Test the QFT algorithm.""" - @pytest.mark.parametrize("n_qubits", range(2, 20, 2)) + @pytest.mark.parametrize("n_qubits", range(2, 15, 2)) def test_qft(self, n_qubits): lightning_tensor_check(n_qubits) dev = qml.device(device_name, wires=n_qubits) @@ -745,9 +745,9 @@ def circuit(basis_state): assert np.allclose(res, ref) @pytest.mark.skipif(not LightningDevice._new_API, reason="New API required") - @pytest.mark.parametrize("wires", [5, 9, 10, 13]) + @pytest.mark.parametrize("wires", [5, 13]) def test_preprocess_qft_decomposition(self, wires): - """Test that qml.QFT is not decomposed for less than 10 wires.""" + """Test that qml.QFT is always decomposed for any wires.""" tape = qml.tape.QuantumScript( [qml.QFT(wires=list(range(wires)))], [qml.expval(qml.PauliZ(0))] ) @@ -756,10 +756,9 @@ def test_preprocess_qft_decomposition(self, wires): program, _ = dev.preprocess() [new_tape], _ = program([tape]) - if wires >= 10: - assert all(not isinstance(op, qml.QFT) for op in new_tape.operations) - else: - assert tape.operations == [qml.QFT(wires=list(range(wires)))] + # assert all(not isinstance(op, qml.QFT) for op in new_tape.operations) + # else: + assert tape.operations == [qml.QFT(wires=list(range(wires)))] class TestAQFT: