From 16d00d97374e714d03afd73cfa362b9b9d3d8711 Mon Sep 17 00:00:00 2001 From: Ali Asadi <10773383+maliasadi@users.noreply.github.com> Date: Tue, 1 Oct 2024 08:18:36 -0400 Subject: [PATCH] Uniform Python format to adhere PL style; Always decompose qml.QFT in Lightning (#924) 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-74415] --------- Co-authored-by: ringo-but-quantum --- .github/CHANGELOG.md | 6 +++ .github/workflows/format.yml | 6 +-- Makefile | 4 +- .../core/_state_vector_base.py | 1 - pennylane_lightning/core/_version.py | 2 +- .../lightning_gpu/lightning_gpu.py | 1 - .../lightning_gpu/lightning_gpu.toml | 1 - .../lightning_kokkos/_measurements.py | 1 - .../lightning_kokkos/_state_vector.py | 2 - .../lightning_kokkos/lightning_kokkos.py | 1 - .../lightning_kokkos/lightning_kokkos.toml | 1 - .../lightning_qubit/_measurements.py | 1 - .../lightning_qubit/_state_vector.py | 1 - .../lightning_qubit/lightning_qubit.py | 1 - .../lightning_qubit/lightning_qubit.toml | 1 - .../lightning_tensor/lightning_tensor.py | 1 - requirements-dev.txt | 19 ++++---- requirements-tests.txt | 12 ++--- requirements.txt | 18 +++++--- tests/lightning_qubit/test_jacobian_method.py | 1 - tests/test_decomposition.py | 44 ------------------- tests/test_native_mcm.py | 1 - 22 files changed, 38 insertions(+), 88 deletions(-) delete mode 100644 tests/test_decomposition.py diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 8b532c26d0..6a9b55cf8f 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -34,6 +34,12 @@ ### Improvements +* Always decompose `qml.QFT` in Lightning. + [(#924)](https://github.com/PennyLaneAI/pennylane-lightning/pull/924) + +* Uniform Python format to adhere PennyLane style. + [(#924)](https://github.com/PennyLaneAI/pennylane-lightning/pull/924) + * Remove dynamic decomposition rules in Lightning. [(#926)](https://github.com/PennyLaneAI/pennylane-lightning/pull/926) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 2fd60dda5c..25c1797a78 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -17,14 +17,14 @@ jobs: - name: Set up Python uses: actions/setup-python@v5 with: - python-version: '3.11' + python-version: '3.12' - name: Checkout PennyLane-Lightning uses: actions/checkout@v4 - name: Install dependencies run: - python -m pip install -r requirements-dev.txt + python -m pip install -r requirements.txt - name: Run isort & black --check run: make format-python check=1 verbose=1 @@ -38,7 +38,7 @@ jobs: uses: actions/checkout@v4 - name: Install dependencies - run: sudo apt update && sudo apt -y install python3 python3-pip python-is-python3 && python -m pip install -r requirements-dev.txt + run: sudo apt update && sudo apt -y install python3 python3-pip python-is-python3 && python -m pip install -r requirements.txt - name: Run formatter run: PATH=$PATH:/home/ubuntu/.local/bin/:$(dirname $(which python3))/ ./bin/format --check ./pennylane_lightning/core/src diff --git a/Makefile b/Makefile index c9c454bb64..f43c9e903f 100644 --- a/Makefile +++ b/Makefile @@ -131,8 +131,8 @@ format-cpp: ./bin/format $(CHECK) ./pennylane_lightning format-python: - isort --py 311 --profile black -l 100 -p pennylane_lightning ./pennylane_lightning ./mpitests ./tests ./scripts $(ICHECK) $(VERBOSE) - black -l 100 ./pennylane_lightning ./mpitests ./tests ./scripts $(CHECK) $(VERBOSE) + isort --py 312 --profile black -l 100 -p pennylane_lightning ./pennylane_lightning ./mpitests ./tests ./scripts $(ICHECK) $(VERBOSE) + black -t py310 -t py311 -t py312 -l 100 ./pennylane_lightning ./mpitests ./tests ./scripts $(CHECK) $(VERBOSE) .PHONY: check-tidy check-tidy: diff --git a/pennylane_lightning/core/_state_vector_base.py b/pennylane_lightning/core/_state_vector_base.py index b2ba3a0669..3e08a5ab40 100644 --- a/pennylane_lightning/core/_state_vector_base.py +++ b/pennylane_lightning/core/_state_vector_base.py @@ -38,7 +38,6 @@ class LightningBaseStateVector(ABC): """ def __init__(self, num_wires: int, dtype: Union[np.complex128, np.complex64]): - if dtype not in [np.complex64, np.complex128]: raise TypeError(f"Unsupported complex type: {dtype}") diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index cb8540718b..05a5ab3841 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-dev35" +__version__ = "0.39.0-dev34" diff --git a/pennylane_lightning/lightning_gpu/lightning_gpu.py b/pennylane_lightning/lightning_gpu/lightning_gpu.py index 08d79c1473..2894b999f3 100644 --- a/pennylane_lightning/lightning_gpu/lightning_gpu.py +++ b/pennylane_lightning/lightning_gpu/lightning_gpu.py @@ -172,7 +172,6 @@ def _mebibytesToBytes(mebibytes): "QubitCarry", "QubitSum", "OrbitalRotation", - "QFT", "ECR", "BlockEncode", "C(BlockEncode)", diff --git a/pennylane_lightning/lightning_gpu/lightning_gpu.toml b/pennylane_lightning/lightning_gpu/lightning_gpu.toml index 003b248fa4..518315de09 100644 --- a/pennylane_lightning/lightning_gpu/lightning_gpu.toml +++ b/pennylane_lightning/lightning_gpu/lightning_gpu.toml @@ -50,7 +50,6 @@ GlobalPhase = { properties = [ "differentiable" ] } [operators.gates.decomp] BasisState = {} -QFT = {} QubitStateVector = {} StatePrep = {} MultiControlledX = {} diff --git a/pennylane_lightning/lightning_kokkos/_measurements.py b/pennylane_lightning/lightning_kokkos/_measurements.py index 6e706614ba..b438af350c 100644 --- a/pennylane_lightning/lightning_kokkos/_measurements.py +++ b/pennylane_lightning/lightning_kokkos/_measurements.py @@ -46,7 +46,6 @@ def __init__( self, kokkos_state, ) -> None: - super().__init__(kokkos_state) self._measurement_lightning = self._measurement_dtype()(kokkos_state.state_vector) diff --git a/pennylane_lightning/lightning_kokkos/_state_vector.py b/pennylane_lightning/lightning_kokkos/_state_vector.py index 5e76249de0..dda40ffadb 100644 --- a/pennylane_lightning/lightning_kokkos/_state_vector.py +++ b/pennylane_lightning/lightning_kokkos/_state_vector.py @@ -64,7 +64,6 @@ def __init__( kokkos_args=None, sync=True, ): # pylint: disable=too-many-arguments - super().__init__(num_wires, dtype) self._device_name = "lightning.kokkos" @@ -280,7 +279,6 @@ def _apply_lightning( elif isinstance(operation, qml.ops.Controlled) and isinstance( operation.base, qml.GlobalPhase ): # apply n-controlled gate - # Kokkos do not support the controlled gates except for GlobalPhase self._apply_lightning_controlled(operation) else: # apply gate as a matrix diff --git a/pennylane_lightning/lightning_kokkos/lightning_kokkos.py b/pennylane_lightning/lightning_kokkos/lightning_kokkos.py index 63cf446c53..51221dde92 100644 --- a/pennylane_lightning/lightning_kokkos/lightning_kokkos.py +++ b/pennylane_lightning/lightning_kokkos/lightning_kokkos.py @@ -120,7 +120,6 @@ "QubitCarry", "QubitSum", "OrbitalRotation", - "QFT", "ECR", "BlockEncode", "C(BlockEncode)", diff --git a/pennylane_lightning/lightning_kokkos/lightning_kokkos.toml b/pennylane_lightning/lightning_kokkos/lightning_kokkos.toml index 0ede05a262..3676feb996 100644 --- a/pennylane_lightning/lightning_kokkos/lightning_kokkos.toml +++ b/pennylane_lightning/lightning_kokkos/lightning_kokkos.toml @@ -50,7 +50,6 @@ T = { properties = [ "invertible", "differe BasisState = {} MultiControlledX = {} -QFT = {} QubitStateVector = {} StatePrep = {} ControlledQubitUnitary = {} diff --git a/pennylane_lightning/lightning_qubit/_measurements.py b/pennylane_lightning/lightning_qubit/_measurements.py index f762fcb7e6..c1b97a1184 100644 --- a/pennylane_lightning/lightning_qubit/_measurements.py +++ b/pennylane_lightning/lightning_qubit/_measurements.py @@ -58,7 +58,6 @@ def __init__( kernel_name: str = None, num_burnin: int = None, ) -> None: - super().__init__(qubit_state) self._mcmc = mcmc diff --git a/pennylane_lightning/lightning_qubit/_state_vector.py b/pennylane_lightning/lightning_qubit/_state_vector.py index 0a8b0b7d6d..b4b6ef5ff1 100644 --- a/pennylane_lightning/lightning_qubit/_state_vector.py +++ b/pennylane_lightning/lightning_qubit/_state_vector.py @@ -51,7 +51,6 @@ class LightningStateVector(LightningBaseStateVector): # pylint: disable=too-few """ def __init__(self, num_wires, dtype=np.complex128): - super().__init__(num_wires, dtype) self._device_name = "lightning.qubit" diff --git a/pennylane_lightning/lightning_qubit/lightning_qubit.py b/pennylane_lightning/lightning_qubit/lightning_qubit.py index 961235ae22..c317bbfbad 100644 --- a/pennylane_lightning/lightning_qubit/lightning_qubit.py +++ b/pennylane_lightning/lightning_qubit/lightning_qubit.py @@ -139,7 +139,6 @@ "QubitCarry", "QubitSum", "OrbitalRotation", - "QFT", "ECR", "BlockEncode", "C(BlockEncode)", diff --git a/pennylane_lightning/lightning_qubit/lightning_qubit.toml b/pennylane_lightning/lightning_qubit/lightning_qubit.toml index 5656934889..cedc0b3682 100644 --- a/pennylane_lightning/lightning_qubit/lightning_qubit.toml +++ b/pennylane_lightning/lightning_qubit/lightning_qubit.toml @@ -47,7 +47,6 @@ T = { properties = [ "invertible", "controllable", "differe # default, but can be useful to express a deviation from this device's regular # strategy in PennyLane. MultiControlledX = {} -QFT = {} # Gates which should be translated to QubitUnitary [operators.gates.matrix] diff --git a/pennylane_lightning/lightning_tensor/lightning_tensor.py b/pennylane_lightning/lightning_tensor/lightning_tensor.py index 333ce35d25..d70f1d3c69 100644 --- a/pennylane_lightning/lightning_tensor/lightning_tensor.py +++ b/pennylane_lightning/lightning_tensor/lightning_tensor.py @@ -140,7 +140,6 @@ "QubitCarry", "QubitSum", "OrbitalRotation", - "QFT", "ECR", "BlockEncode", "C(BlockEncode)", diff --git a/requirements-dev.txt b/requirements-dev.txt index 34e7884616..5ce2c0b9d8 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,16 +1,15 @@ -git+https://github.com/PennyLaneAI/pennylane.git@master build ninja -flaky pybind11 -pytest~=8.0.0 +pre-commit>=2.19.0 +pytest>=7.1.2 pytest-benchmark -pytest-cov -pytest-mock +pytest-cov>=3.0.0 +pytest-mock>=3.7.0 +pytest-xdist>=2.5.0 pytest-split -pytest-xdist -pre-commit>=2.19.0 -black==24.3.0 +flaky>=3.7.0 +black>=21 clang-tidy~=16.0 clang-format~=16.0 isort==5.13.2 @@ -18,5 +17,5 @@ click==8.0.4 cmake custatevec-cu12 cutensornet-cu12 -pylint -scipy +pylint==2.7.4 +git+https://github.com/PennyLaneAI/pennylane.git@master diff --git a/requirements-tests.txt b/requirements-tests.txt index 5b1b5119fa..4643390dfd 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -1,7 +1,7 @@ -pytest~=8.0.0 +pytest>=7.1.2 pytest-benchmark -pytest-cov -pytest-mock -pytest-xdist -flaky -git+https://github.com/PennyLaneAI/pennylane.git@master \ No newline at end of file +pytest-cov>=3.0.0 +pytest-mock>=3.7.0 +pytest-xdist>=2.5.0 +flaky>=3.7.0 +git+https://github.com/PennyLaneAI/pennylane.git@master diff --git a/requirements.txt b/requirements.txt index 6df7868929..a187adbabf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,13 @@ ninja -flaky -pennylane>=0.34 pybind11 -pytest~=8.0.0 -pytest-cov -pytest-mock -scipy -pytest-xdist +pytest>=7.1.2 +pytest-cov>=3.0.0 +pytest-mock>=3.7.0 +pytest-xdist>=2.5.0 +flaky>=3.7.0 +black>=21 +clang-tidy~=16.0 +clang-format~=16.0 +isort==5.13.2 +pylint==2.7.4 +pennylane>=0.37 diff --git a/tests/lightning_qubit/test_jacobian_method.py b/tests/lightning_qubit/test_jacobian_method.py index 745feee502..c636cbc77f 100644 --- a/tests/lightning_qubit/test_jacobian_method.py +++ b/tests/lightning_qubit/test_jacobian_method.py @@ -51,7 +51,6 @@ def calculate_reference(tape, execute_and_derivatives=False): @staticmethod def process_and_execute(statevector, tape, execute_and_derivatives=False): - wires = statevector.num_wires device = LightningDevice(wires) if execute_and_derivatives: diff --git a/tests/test_decomposition.py b/tests/test_decomposition.py deleted file mode 100644 index df9de740e2..0000000000 --- a/tests/test_decomposition.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2018-2023 Xanadu Quantum Technologies Inc. - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -""" -Unit tests for operation decomposition with Lightning devices. -""" -import numpy as np -import pennylane as qml -import pytest -from conftest import LightningDevice as ld - -if not ld._CPP_BINARY_AVAILABLE: - pytest.skip("No binary module found. Skipping.", allow_module_level=True) - - -@pytest.mark.skipif(ld._new_API, reason="Old API required") -class TestDenseMatrixDecompositionThreshold: - """Tests, for QFT and Grover operators, the automatic transition from full matrix to decomposition - on calculations.""" - - input = [ - (qml.QFT, 8, True), - (qml.QFT, 10, False), - (qml.QFT, 14, False), - (qml.GroverOperator, 8, True), - (qml.GroverOperator, 13, False), - ] - - @pytest.mark.parametrize("op, n_wires, condition", input) - def test_threshold(self, op, n_wires, condition): - wires = range(n_wires) - op = op(wires=wires) - dev = ld(n_wires) - assert dev.stopping_condition(op) == condition diff --git a/tests/test_native_mcm.py b/tests/test_native_mcm.py index df5ba6cb43..07281fb48a 100644 --- a/tests/test_native_mcm.py +++ b/tests/test_native_mcm.py @@ -90,7 +90,6 @@ def func(x, y): ): func(*params) if device_name == "lightning.kokkos": - with pytest.raises( qml.DeviceError, match=r"Measurement shadow\(wires=\[0\]\) not accepted with finite shots on "