Skip to content

Commit

Permalink
Merge branch 'master' into remove-qubit-state-vector
Browse files Browse the repository at this point in the history
  • Loading branch information
andrijapau committed Nov 14, 2024
2 parents d5989f4 + 94577a8 commit 7e96803
Show file tree
Hide file tree
Showing 49 changed files with 6,358 additions and 205 deletions.
14 changes: 13 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

### New features since last release

* Add native N-controlled generators and adjoint support to `lightning.gpu`'s single-GPU backend.
[(#970)](https://github.com/PennyLaneAI/pennylane-lightning/pull/970)

* Add a Catalyst-specific wrapping class for Lightning Qubit.
[(#960)](https://github.com/PennyLaneAI/pennylane-lightning/pull/960)

* Add native N-controlled gates support to `lightning.gpu`'s single-GPU backend.
[(#938)](https://github.com/PennyLaneAI/pennylane-lightning/pull/938)

Expand All @@ -10,6 +16,9 @@
* Removed all instances of `qml.QubitStateVector`.
[(#985)](https://github.com/PennyLaneAI/pennylane-lightning/pull/985)

* Handling for the legacy operator arithmetic (the `Hamiltonian` and `Tensor` classes in PennyLane) is removed.
[(#994)](https://github.com/PennyLaneAI/pennylane-lightning/pull/994)

### Improvements

* Unify excitation gates memory layout to row-major for both LGPU and LT.
Expand All @@ -22,6 +31,9 @@

### Bug fixes

* Fix the `upload-pypi` token issues for Linux and MacOS (x86_64 & AARCH64) wheels.
[(#989)](https://github.com/PennyLaneAI/pennylane-lightning/pull/989)

* Fix Pennylane dependency branch (`v0.39_rc0` to `master`).
[(#984)](https://github.com/PennyLaneAI/pennylane-lightning/pull/984)

Expand All @@ -32,7 +44,7 @@

This release contains contributions from (in alphabetical order):

Ali Asadi, Luis Alfredo Nuñez Meneses, Andrija Paurevic, Shuli Shu
Ali Asadi, Joseph Lee, Luis Alfredo Nuñez Meneses, Andrija Paurevic, Shuli Shu, Raul Torres, Haochen Paul Wang

---

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/tests_lqcpu_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ jobs:
- name: Run PennyLane-Lightning unit tests
run: |
DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"`
OMP_NUM_THREADS=1 PL_DEVICE=${DEVICENAME} python -m pytest -n auto tests/ -k "not unitary_correct" \
# Remove `python -m` to avoid running tests with relative modules
OMP_NUM_THREADS=1 PL_DEVICE=${DEVICENAME} pytest -n auto tests/ -k "not unitary_correct" \
$COVERAGE_FLAGS --splits 4 --group ${{ matrix.group }} \
--durations-path='.github/workflows/python_lightning_qubit_test_durations.json' --splitting-algorithm=least_duration
PL_DEVICE=${DEVICENAME} python -m pytest tests/ -k "unitary_correct" $COVERAGE_FLAGS --cov-append
PL_DEVICE=${DEVICENAME} pytest tests/ -k "unitary_correct" $COVERAGE_FLAGS --cov-append
pl-device-test --device ${DEVICENAME} --skip-ops --shots=20000 $COVERAGE_FLAGS --cov-append
pl-device-test --device ${DEVICENAME} --shots=None --skip-ops $COVERAGE_FLAGS --cov-append
mv .coverage ${{ github.workspace }}/.coverage-${{ github.job }}-${{ matrix.pl_backend }}-${{ matrix.blas }}-${{ matrix.group }}
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/wheel_linux_aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ jobs:
pl_backend: ["lightning_qubit", "lightning_kokkos"]
cibw_build: ${{ fromJson(needs.set_wheel_build_matrix.outputs.python_version) }}
runs-on: ubuntu-latest
permissions:
id-token: write
if: |
github.event_name == 'release' ||
github.ref == 'refs/heads/master'
Expand All @@ -211,9 +213,8 @@ jobs:
name: ${{ runner.os }}-wheels-${{ matrix.pl_backend }}-${{ fromJson('{ "cp310-*":"py310","cp311-*":"py311","cp312-*":"py312" }')[matrix.cibw_build] }}-${{ matrix.arch }}.zip
path: dist

- name: Upload wheels to PyPI
- name: Upload wheels to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ ( matrix.pl_backend == 'lightning_qubit' ) && secrets.TEST_PYPI_API_TOKEN || secrets.TEST_PYPI_LKOKKOS_TOKEN }}
verbose: true
repository-url: https://test.pypi.org/legacy/
3 changes: 2 additions & 1 deletion .github/workflows/wheel_linux_aarch64_cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ jobs:
- name: Upload wheels to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository_url: https://test.pypi.org/legacy/
verbose: true
repository-url: https://test.pypi.org/legacy/
7 changes: 4 additions & 3 deletions .github/workflows/wheel_linux_x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ jobs:
pl_backend: ["lightning_qubit", "lightning_kokkos"]
cibw_build: ${{ fromJson(needs.set_wheel_build_matrix.outputs.python_version) }}
runs-on: ubuntu-latest
permissions:
id-token: write
if: |
github.event_name == 'release' ||
github.ref == 'refs/heads/master'
Expand All @@ -219,9 +221,8 @@ jobs:
name: ${{ runner.os }}-wheels-${{ matrix.pl_backend }}-${{ fromJson('{ "cp310-*":"py310","cp311-*":"py311","cp312-*":"py312" }')[matrix.cibw_build] }}-${{ matrix.arch }}.zip
path: dist

- name: Upload wheels to PyPI
- name: Upload wheels to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ ( matrix.pl_backend == 'lightning_qubit' ) && secrets.TEST_PYPI_API_TOKEN || secrets.TEST_PYPI_LKOKKOS_TOKEN }}
verbose: true
repository-url: https://test.pypi.org/legacy/
1 change: 1 addition & 0 deletions .github/workflows/wheel_linux_x86_64_cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,5 @@ jobs:
- name: Upload wheels to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
verbose: true
repository-url: https://test.pypi.org/legacy/
7 changes: 4 additions & 3 deletions .github/workflows/wheel_macos_arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ jobs:
pl_backend: ["lightning_qubit", "lightning_kokkos"]
cibw_build: ${{ fromJson(needs.mac-set-matrix-arm.outputs.python_version) }}
runs-on: ubuntu-latest
permissions:
id-token: write
if: |
github.event_name == 'release' ||
github.ref == 'refs/heads/master'
Expand All @@ -153,9 +155,8 @@ jobs:
name: macOS-wheels-${{ matrix.pl_backend }}-${{ fromJson('{ "cp310-*":"py310","cp311-*":"py311","cp312-*":"py312" }')[matrix.cibw_build] }}-${{ matrix.arch }}.zip
path: dist

- name: Upload wheels to PyPI
- name: Upload wheels to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ ( matrix.pl_backend == 'lightning_qubit' ) && secrets.TEST_PYPI_API_TOKEN || secrets.TEST_PYPI_LKOKKOS_TOKEN }}
verbose: true
repository-url: https://test.pypi.org/legacy/
7 changes: 4 additions & 3 deletions .github/workflows/wheel_macos_x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ jobs:
pl_backend: ["lightning_qubit", "lightning_kokkos"]
cibw_build: ${{ fromJson(needs.set_wheel_build_matrix.outputs.python_version) }}
runs-on: ubuntu-latest
permissions:
id-token: write
if: |
github.event_name == 'release' ||
github.ref == 'refs/heads/master'
Expand All @@ -212,9 +214,8 @@ jobs:
name: macOS-wheels-${{ matrix.pl_backend }}-${{ fromJson('{ "cp310-*":"py310","cp311-*":"py311","cp312-*":"py312" }')[matrix.cibw_build] }}-${{ matrix.arch }}.zip
path: dist

- name: Upload wheels to PyPI
- name: Upload wheels to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ ( matrix.pl_backend == 'lightning_qubit' ) && secrets.TEST_PYPI_API_TOKEN || secrets.TEST_PYPI_LKOKKOS_TOKEN }}
verbose: true
repository-url: https://test.pypi.org/legacy/
6 changes: 3 additions & 3 deletions cmake/support_catalyst.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ macro(FindCatalyst target_name)
${HEADER_NAME}
URL https://raw.githubusercontent.com/PennyLaneAI/catalyst/${CATALYST_GIT_TAG}/runtime/lib/backend/common/${HEADER}
DOWNLOAD_NO_EXTRACT True
SOURCE_DIR include
SOURCE_DIR ../../include
)

FetchContent_MakeAvailable(${HEADER_NAME})
Expand All @@ -62,13 +62,13 @@ macro(FindCatalyst target_name)
${HEADER_NAME}
URL https://raw.githubusercontent.com/PennyLaneAI/catalyst/${CATALYST_GIT_TAG}/runtime/include/${HEADER}
DOWNLOAD_NO_EXTRACT True
SOURCE_DIR include
SOURCE_DIR ../../include
)

FetchContent_MakeAvailable(${HEADER_NAME})
endforeach()

target_include_directories(${target_name} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include)
target_include_directories(${target_name} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/../../include)

endif()
endmacro()
34 changes: 6 additions & 28 deletions pennylane_lightning/core/_measurements_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
StateMeasurement,
VarianceMP,
)
from pennylane.ops import Hamiltonian, SparseHamiltonian, Sum
from pennylane.ops import SparseHamiltonian, Sum
from pennylane.tape import QuantumScript
from pennylane.typing import Result, TensorLike
from pennylane.wires import Wires
Expand Down Expand Up @@ -117,7 +117,7 @@ def expval(self, measurementprocess: MeasurementProcess):
)

if (
isinstance(measurementprocess.obs, (qml.ops.Hamiltonian, qml.Hermitian))
isinstance(measurementprocess.obs, qml.Hermitian)
or (measurementprocess.obs.arithmetic_depth > 0)
or isinstance(measurementprocess.obs.name, List)
):
Expand Down Expand Up @@ -176,7 +176,7 @@ def var(self, measurementprocess: MeasurementProcess):
)

if (
isinstance(measurementprocess.obs, (qml.ops.Hamiltonian, qml.Hermitian))
isinstance(measurementprocess.obs, qml.Hermitian)
or (measurementprocess.obs.arithmetic_depth > 0)
or isinstance(measurementprocess.obs.name, List)
):
Expand Down Expand Up @@ -307,15 +307,13 @@ def measure_with_samples(
raise TypeError(
"ExpectationMP/VarianceMP(SparseHamiltonian) cannot be computed with samples."
)
if isinstance(group[0], VarianceMP) and isinstance(group[0].obs, (Hamiltonian, Sum)):
raise TypeError("VarianceMP(Hamiltonian/Sum) cannot be computed with samples.")
if isinstance(group[0], VarianceMP) and isinstance(group[0].obs, Sum):
raise TypeError("VarianceMP(Sum) cannot be computed with samples.")
if isinstance(group[0], (ClassicalShadowMP, ShadowExpvalMP)):
raise TypeError(
"ExpectationMP(ClassicalShadowMP, ShadowExpvalMP) cannot be computed with samples."
)
if isinstance(group[0], ExpectationMP) and isinstance(group[0].obs, Hamiltonian):
all_res.extend(self._measure_hamiltonian_with_samples(group, shots))
elif isinstance(group[0], ExpectationMP) and isinstance(group[0].obs, Sum):
if isinstance(group[0], ExpectationMP) and isinstance(group[0].obs, Sum):
all_res.extend(self._measure_sum_with_samples(group, shots))
else:
all_res.extend(self._measure_with_samples_diagonalizing_gates(group, shots))
Expand Down Expand Up @@ -372,26 +370,6 @@ def _measure_with_samples_diagonalizing_gates(
TensorLike[Any]: Sample measurement results
"""

def _measure_hamiltonian_with_samples(
self,
mp: List[SampleMeasurement],
shots: Shots,
):
# the list contains only one element based on how we group measurements
mp = mp[0]

# if the measurement process involves a Hamiltonian, measure each
# of the terms separately and sum
def _sum_for_single_shot(s):
results = self.measure_with_samples(
[ExpectationMP(t) for t in mp.obs.terms()[1]],
s,
)
return sum(c * res for c, res in zip(mp.obs.terms()[0], results))

unsqueezed_results = tuple(_sum_for_single_shot(type(shots)(s)) for s in shots)
return [unsqueezed_results] if shots.has_partitioned_shots else [unsqueezed_results[0]]

def _measure_sum_with_samples(
self,
mp: List[SampleMeasurement],
Expand Down
12 changes: 4 additions & 8 deletions pennylane_lightning/core/_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
matrix,
)
from pennylane.math import unwrap
from pennylane.operation import Tensor
from pennylane.ops import Hamiltonian, LinearCombination, Prod, SProd, Sum
from pennylane.ops import LinearCombination, Prod, SProd, Sum
from pennylane.tape import QuantumTape

NAMED_OBS = (Identity, PauliX, PauliY, PauliZ, Hadamard)
Expand Down Expand Up @@ -211,8 +210,7 @@ def _hermitian_ob(self, observable, wires_map: dict = None):

def _tensor_ob(self, observable, wires_map: dict = None):
"""Serialize a tensor observable"""
obs = observable.obs if isinstance(observable, Tensor) else observable.operands
return self.tensor_obs([self._ob(o, wires_map) for o in obs])
return self.tensor_obs([self._ob(o, wires_map) for o in observable.operands])

def _chunk_ham_terms(self, coeffs, ops, split_num: int = 1) -> List:
"Create split_num sub-Hamiltonians from a single high term-count Hamiltonian"
Expand Down Expand Up @@ -262,7 +260,7 @@ def _sparse_hamiltonian(self, observable, wires_map: dict = None):
"""

if self._use_mpi:
Hmat = Hamiltonian([1.0], [Identity(0)]).sparse_matrix()
Hmat = Identity(0).sparse_matrix()
H_sparse = SparseHamiltonian(Hmat, wires=range(1))
spm = H_sparse.sparse_matrix()
# Only root 0 needs the overall sparse matrix data
Expand Down Expand Up @@ -323,11 +321,9 @@ def _ob(self, observable, wires_map: dict = None):
"""Serialize a :class:`pennylane.operation.Observable` into an Observable."""
if isinstance(observable, NAMED_OBS):
return self._named_obs(observable, wires_map)
if isinstance(observable, Hamiltonian):
return self._hamiltonian(observable, wires_map)
if observable.pauli_rep is not None:
return self._pauli_sentence(observable.pauli_rep, wires_map)
if isinstance(observable, (Tensor, Prod)):
if isinstance(observable, Prod):
if isinstance(observable, Prod) and observable.has_overlapping_wires:
return self._hermitian_ob(observable, wires_map)
return self._tensor_ob(observable, wires_map)
Expand Down
3 changes: 2 additions & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.40.0-dev4"

__version__ = "0.40.0-dev8"
16 changes: 4 additions & 12 deletions pennylane_lightning/core/lightning_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pennylane import BasisState, StatePrep
from pennylane.devices import QubitDevice
from pennylane.measurements import Expectation, MeasurementProcess, State
from pennylane.operation import Operation, Tensor
from pennylane.operation import Operation
from pennylane.ops import Prod, Projector, SProd, Sum
from pennylane.wires import Wires

Expand Down Expand Up @@ -178,9 +178,7 @@ def probability(self, wires=None, shot_range=None, bin_size=None):
def _get_diagonalizing_gates(self, circuit: qml.tape.QuantumTape) -> List[Operation]:
# pylint: disable=no-member, protected-access
def skip_diagonalizing(obs):
return isinstance(obs, qml.Hamiltonian) or (
isinstance(obs, qml.ops.Sum) and obs._pauli_rep is not None
)
return isinstance(obs, qml.ops.Sum) and obs._pauli_rep is not None

meas_filtered = list(
filter(lambda m: m.obs is None or not skip_diagonalizing(m.obs), circuit.measurements)
Expand Down Expand Up @@ -319,18 +317,12 @@ def _assert_adjdiff_no_projectors(observable):
Raises:
~pennylane.QuantumFunctionError: if a ``Projector`` is found.
"""
if isinstance(observable, Tensor):
if any(isinstance(o, Projector) for o in observable.non_identity_obs):
raise qml.QuantumFunctionError(
"Adjoint differentiation method does not support the Projector observable"
)

elif isinstance(observable, Projector):
if isinstance(observable, Projector):
raise qml.QuantumFunctionError(
"Adjoint differentiation method does not support the Projector observable"
)

elif isinstance(observable, SProd):
if isinstance(observable, SProd):
LightningBase._assert_adjdiff_no_projectors(observable.base)

elif isinstance(observable, (Sum, Prod)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ using namespace Pennylane::LightningKokkos::Observables;

#elif _ENABLE_PLGPU == 1
constexpr bool BACKEND_FOUND = true;
constexpr bool SUPPORTS_CTRL = false;
constexpr bool SUPPORTS_CTRL = true;
#include "AdjointJacobianGPU.hpp"
#include "ObservablesGPU.hpp"
#include "TestHelpersStateVectors.hpp"
Expand Down
Loading

0 comments on commit 7e96803

Please sign in to comment.