Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a base LKokkos new device API backend. #810

Merged
merged 113 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
93a52bf
Create a base with mock simulate, jacobian, simulate_and_jacobian, v…
LuisAlfredoNu Jul 22, 2024
feca914
Auto update version from '0.38.0-dev12' to '0.38.0-dev13'
ringo-but-quantum Jul 22, 2024
5ccf20c
Apply format
LuisAlfredoNu Jul 22, 2024
bcf961e
Solve issue with measurenment class
LuisAlfredoNu Jul 22, 2024
5701cc5
Merge branch 'kokkosNewAPI_backend' of github.com:PennyLaneAI/pennyla…
LuisAlfredoNu Jul 22, 2024
2e4e668
solve warnings from Pylint
LuisAlfredoNu Jul 22, 2024
3ee9669
solve warnings from Pylint
LuisAlfredoNu Jul 22, 2024
0e717f6
Apply format
LuisAlfredoNu Jul 22, 2024
42b6714
Skip test native mcm for Kokkos because has a bug with reference to o…
LuisAlfredoNu Jul 22, 2024
27c7ebc
Set the base class for LightningDevice and skip the tests
LuisAlfredoNu Jul 23, 2024
fd02214
Apply format
LuisAlfredoNu Jul 23, 2024
14b4964
apply format
LuisAlfredoNu Jul 24, 2024
369ac0e
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Jul 24, 2024
04283f9
Auto update version from '0.38.0-dev13' to '0.38.0-dev14'
ringo-but-quantum Jul 24, 2024
8d40c14
Apply suggestions from code review
LuisAlfredoNu Jul 24, 2024
44751ee
Fix spelling error
LuisAlfredoNu Jul 24, 2024
d17059d
Auto update version from '0.38.0-dev14' to '0.38.0-dev17'
ringo-but-quantum Jul 24, 2024
e8de485
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Jul 24, 2024
79b6bbe
Auto update version from '0.38.0-dev16' to '0.38.0-dev17'
ringo-but-quantum Jul 24, 2024
f34100b
Delete the unnecessary code for the backend
LuisAlfredoNu Jul 24, 2024
bcd323e
Delete the unnecessary functions up to now
LuisAlfredoNu Jul 24, 2024
a54ab3f
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Jul 26, 2024
39fb65f
Auto update version from '0.38.0-dev18' to '0.38.0-dev19'
ringo-but-quantum Jul 26, 2024
8dc5004
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Jul 29, 2024
90d1d6e
Auto update version from '0.38.0-dev19' to '0.38.0-dev20'
ringo-but-quantum Jul 29, 2024
80277d9
Add the simulate class for the new device (#817)
LuisAlfredoNu Jul 30, 2024
a15b018
Auto update version from '0.38.0-dev20' to '0.38.0-dev21'
ringo-but-quantum Jul 30, 2024
09d02dc
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Jul 30, 2024
06a8d2b
Auto update version from '0.38.0-dev20' to '0.38.0-dev21'
ringo-but-quantum Jul 30, 2024
aa052ad
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 1, 2024
180a065
Auto update version from '0.38.0-dev21' to '0.38.0-dev22'
ringo-but-quantum Aug 1, 2024
6eb6eba
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 2, 2024
319cacc
Auto update version from '0.38.0-dev22' to '0.38.0-dev23'
ringo-but-quantum Aug 2, 2024
5b99da7
Add the support class for the Adjoint Jacobian to the new device (#829)
LuisAlfredoNu Aug 2, 2024
3eea0d1
Auto update version from '0.38.0-dev23' to '0.38.0-dev25'
ringo-but-quantum Aug 2, 2024
35bcab6
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 5, 2024
3588b9d
Auto update version from '0.38.0-dev25' to '0.38.0-dev26'
ringo-but-quantum Aug 5, 2024
f9ab86f
Add (check) full support for sampling in full parity with Lightning (…
LuisAlfredoNu Aug 9, 2024
0d1dd71
Auto update version from '0.38.0-dev26' to '0.38.0-dev27'
ringo-but-quantum Aug 9, 2024
ac9ed86
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 9, 2024
9a20ee1
Auto update version from '0.38.0-dev26' to '0.38.0-dev27'
ringo-but-quantum Aug 9, 2024
244e0a1
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 12, 2024
873656c
Auto update version from '0.38.0-dev28' to '0.38.0-dev29'
ringo-but-quantum Aug 12, 2024
fe0954a
Update unit/integration tests for the new device (#840)
LuisAlfredoNu Aug 13, 2024
286ea30
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 13, 2024
3cb37be
Auto update version from '0.38.0-dev32' to '0.38.0-dev33'
ringo-but-quantum Aug 13, 2024
1bd9adc
Solve conflict with master
LuisAlfredoNu Aug 13, 2024
9ebbefc
check pylint
LuisAlfredoNu Aug 13, 2024
86316bc
trigger CIs
LuisAlfredoNu Aug 13, 2024
5c6b97d
trigger CIs
LuisAlfredoNu Aug 14, 2024
0293b29
remove verbose from CI kcuda
LuisAlfredoNu Aug 14, 2024
e310fef
Update CHANGELOG
LuisAlfredoNu Aug 14, 2024
9478c50
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 14, 2024
bbc6448
Auto update version from '0.38.0-dev33' to '0.38.0-dev34'
ringo-but-quantum Aug 14, 2024
b586e21
Apply suggestions from code review - remove debug comments
LuisAlfredoNu Aug 15, 2024
00b6eb6
Auto update version from '0.38.0-dev34' to '0.38.0-dev35'
ringo-but-quantum Aug 15, 2024
3f3a48d
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 15, 2024
84795f5
Auto update version from '0.38.0-dev34' to '0.38.0-dev35'
ringo-but-quantum Aug 15, 2024
6dbb9fe
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 19, 2024
620a021
Auto update version from '0.38.0-dev37' to '0.38.0-dev38'
ringo-but-quantum Aug 19, 2024
7ae3360
Apply Amintor's commests
LuisAlfredoNu Aug 19, 2024
4a91c6f
Apply format
LuisAlfredoNu Aug 19, 2024
ebc503c
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 20, 2024
c93f1c5
Auto update version from '0.38.0-dev38' to '0.38.0-dev39'
ringo-but-quantum Aug 20, 2024
519ce4a
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 21, 2024
794c166
Auto update version from '0.38.0-dev39' to '0.38.0-dev40'
ringo-but-quantum Aug 21, 2024
168a406
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 21, 2024
63ee6fd
fix issues with the unitary tests
LuisAlfredoNu Aug 21, 2024
3608945
Auto update version from '0.38.0-dev40' to '0.38.0-dev41'
ringo-but-quantum Aug 21, 2024
2ce52c3
Apply format
LuisAlfredoNu Aug 21, 2024
9bcd0fc
Merge branch 'kokkosNewAPI_backend' of github.com:PennyLaneAI/pennyla…
LuisAlfredoNu Aug 21, 2024
a8f4ec2
Using the attribute new_API
LuisAlfredoNu Aug 21, 2024
cc67c16
Auto update version from '0.38.0-dev41' to '0.38.0-dev42'
ringo-but-quantum Aug 21, 2024
81fd864
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 21, 2024
c0d6e11
Auto update version from '0.38.0-dev41' to '0.38.0-dev42'
ringo-but-quantum Aug 21, 2024
330cc9c
Using the attribute new_API
LuisAlfredoNu Aug 21, 2024
e59baa1
Using the attribute new_API
LuisAlfredoNu Aug 21, 2024
c41b69a
Delete commented line
LuisAlfredoNu Aug 22, 2024
ee2c01a
Auto update version from '0.38.0-dev42' to '0.38.0-dev43'
ringo-but-quantum Aug 22, 2024
b1d4cad
Using the attribute new_API
LuisAlfredoNu Aug 22, 2024
32cf4a6
Using the attribute new_API
LuisAlfredoNu Aug 22, 2024
57dfbca
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 22, 2024
d046ad9
Auto update version from '0.38.0-dev42' to '0.38.0-dev43'
ringo-but-quantum Aug 22, 2024
c2079f9
Changes from PR review
LuisAlfredoNu Aug 22, 2024
2a7e58a
Merge branch 'kokkosNewAPI_backend' of github.com:PennyLaneAI/pennyla…
LuisAlfredoNu Aug 22, 2024
3402e10
apply format
LuisAlfredoNu Aug 22, 2024
cc0f209
reduce the number of shots for testing and add class definitons on co…
LuisAlfredoNu Aug 23, 2024
8d5e198
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 23, 2024
aa98b93
Auto update version from '0.38.0-dev47' to '0.38.0-dev48'
ringo-but-quantum Aug 23, 2024
e706192
Merge branch 'kokkosNewAPI_backend' of github.com:PennyLaneAI/pennyla…
LuisAlfredoNu Aug 23, 2024
5aced15
apply format
LuisAlfredoNu Aug 23, 2024
4ed563a
solve complains about CodeFactor
LuisAlfredoNu Aug 23, 2024
ea412bb
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 23, 2024
5363361
Fix issue with the Hamiltonians
LuisAlfredoNu Aug 23, 2024
37fdbe2
Auto update version from '0.38.0-dev49' to '0.38.0-dev50'
ringo-but-quantum Aug 23, 2024
818093e
Fix error of lgpu importError
LuisAlfredoNu Aug 26, 2024
bd5ebf8
fix ImportError tensor
LuisAlfredoNu Aug 26, 2024
f64edf0
apply format
LuisAlfredoNu Aug 26, 2024
bef41b6
fix importError in test for lTensor
LuisAlfredoNu Aug 26, 2024
3e60948
Auto update version from '0.38.0-dev50' to '0.38.0-dev51'
ringo-but-quantum Aug 26, 2024
4b8b9a7
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 26, 2024
6e9a6a3
Auto update version from '0.38.0-dev50' to '0.38.0-dev51'
ringo-but-quantum Aug 26, 2024
2dc1bc8
check flaky on CI
LuisAlfredoNu Aug 26, 2024
7e57ec2
trigger CIs
LuisAlfredoNu Aug 26, 2024
ab56233
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Aug 27, 2024
43b3724
Auto update version from '0.38.0-dev51' to '0.38.0-dev52'
ringo-but-quantum Aug 27, 2024
00d01c8
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Sep 3, 2024
4af7af9
Auto update version from '0.38.0-dev53' to '0.38.0-dev54'
ringo-but-quantum Sep 3, 2024
47b4860
remove temporary change
LuisAlfredoNu Sep 3, 2024
cf9302b
Update .github/CHANGELOG.md
LuisAlfredoNu Sep 3, 2024
f84cdcf
Merge branch 'master' into kokkosNewAPI_backend
LuisAlfredoNu Sep 4, 2024
0252f8a
Auto update version from '0.39.0-dev0' to '0.39.0-dev1'
ringo-but-quantum Sep 4, 2024
5561277
Increase the number of shots for measurenment test
LuisAlfredoNu Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### New features since last release

* Lightning-Kokkos migrated to the new device API.
[(#810)](https://github.com/PennyLaneAI/pennylane-lightning/pull/810)

### Breaking changes

### Improvements
Expand All @@ -17,7 +20,7 @@

This release contains contributions from (in alphabetical order):

Shuli Shu
Luis Alfredo Nuñez Meneses, Shuli Shu

---

Expand Down Expand Up @@ -57,6 +60,9 @@ Shuli Shu
* Do not run GPU tests and Docker workflows on release.
[(#788)](https://github.com/PennyLaneAI/pennylane-lightning/pull/788)

* Update python packaging to follow PEP 517/518/621/660 standards.
[(#832)](https://github.com/PennyLaneAI/pennylane-lightning/pull/832)

### Improvements

* Updated calls of ``size_t`` to ``std::size_t`` everywhere.
Expand Down Expand Up @@ -198,7 +204,7 @@ Shuli Shu

This release contains contributions from (in alphabetical order):

Ali Asadi, Astral Cai, Ahmed Darwish, Amintor Dusko, Vincent Michaud-Rioux, Erick Ochoa Lopez, Lee J. O'Riordan, Mudit Pandey, Shuli Shu, Raul Torres, Paul Haochen Wang
Ali Asadi, Astral Cai, Ahmed Darwish, Amintor Dusko, Vincent Michaud-Rioux, Luis Alfredo Nuñez Meneses, Erick Ochoa Lopez, Lee J. O'Riordan, Mudit Pandey, Shuli Shu, Raul Torres, Paul Haochen Wang

---

Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.39.0-dev0"
__version__ = "0.39.0-dev1"
339 changes: 339 additions & 0 deletions pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,339 @@
# Copyright 2018-2024 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.
r"""
Internal methods for adjoint Jacobian differentiation method.
"""

try:
from pennylane_lightning.lightning_kokkos_ops.algorithms import (
AdjointJacobianC64,
AdjointJacobianC128,
create_ops_listC64,
create_ops_listC128,
)
except ImportError:
pass

from os import getenv
from typing import List

import numpy as np
import pennylane as qml
from pennylane import BasisState, QuantumFunctionError, StatePrep
from pennylane.measurements import Expectation, MeasurementProcess, State
from pennylane.operation import Operation
from pennylane.tape import QuantumTape

# pylint: disable=import-error, no-name-in-module, ungrouped-imports
from pennylane_lightning.core._serialize import QuantumScriptSerializer
from pennylane_lightning.core.lightning_base import _chunk_iterable

from ._state_vector import LightningKokkosStateVector


class LightningKokkosAdjointJacobian:
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
"""Check and execute the adjoint Jacobian differentiation method.

Args:
qubit_state(LightningKokkosStateVector): State Vector to calculate the adjoint Jacobian with.
batch_obs(bool): If serialized tape is to be batched or not.
"""

def __init__(self, kokkos_state: LightningKokkosStateVector, batch_obs: bool = False) -> None:
self._qubit_state = kokkos_state
self._state = kokkos_state.state_vector
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
self._dtype = kokkos_state.dtype
self._jacobian_lightning = (

Check warning on line 57 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L54-L57

Added lines #L54 - L57 were not covered by tests
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
AdjointJacobianC64() if self._dtype == np.complex64 else AdjointJacobianC128()
)
self._create_ops_list_lightning = (

Check warning on line 60 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L60

Added line #L60 was not covered by tests
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
create_ops_listC64 if self._dtype == np.complex64 else create_ops_listC128
)
self._batch_obs = batch_obs

Check warning on line 63 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L63

Added line #L63 was not covered by tests
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved

@property
def qubit_state(self):
"""Returns a handle to the LightningKokkosStateVector object."""
return self._qubit_state

Check warning on line 68 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L68

Added line #L68 was not covered by tests
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved

@property
def state(self):
"""Returns a handle to the Lightning internal data object."""
return self._state

Check warning on line 73 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L73

Added line #L73 was not covered by tests
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved

@property
def dtype(self):
"""Returns the simulation data type."""
return self._dtype

Check warning on line 78 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L78

Added line #L78 was not covered by tests
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def _get_return_type(
measurements: List[MeasurementProcess],
):
"""Get the measurement return type.

Args:
measurements (List[MeasurementProcess]): a list of measurement processes to check.

Returns:
None, Expectation or State: a common return type of measurements.
"""
if not measurements:
return None

Check warning on line 93 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L92-L93

Added lines #L92 - L93 were not covered by tests

if len(measurements) == 1 and measurements[0].return_type is State:
return State

Check warning on line 96 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L95-L96

Added lines #L95 - L96 were not covered by tests

return Expectation

Check warning on line 98 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L98

Added line #L98 was not covered by tests

def _process_jacobian_tape(
self, tape: QuantumTape, use_mpi: bool = False, split_obs: bool = False
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
):
"""Process a tape, serializing and building a dictionary proper for
the adjoint Jacobian calculation in the C++ layer.

Args:
tape (QuantumTape): Operations and measurements that represent instructions for execution on Lightning.
use_mpi (bool, optional): If using MPI to accelerate calculation. Defaults to False.
split_obs (bool, optional): If splitting the observables in a list. Defaults to False.

Returns:
dictionary: dictionary providing serialized data for Jacobian calculation.
"""
use_csingle = self._dtype == np.complex64

Check warning on line 114 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L114

Added line #L114 was not covered by tests

obs_serialized, obs_idx_offsets = QuantumScriptSerializer(

Check warning on line 116 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L116

Added line #L116 was not covered by tests
self._qubit_state.device_name, use_csingle, use_mpi, split_obs
).serialize_observables(tape)

ops_serialized, use_sp = QuantumScriptSerializer(

Check warning on line 120 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L120

Added line #L120 was not covered by tests
self._qubit_state.device_name, use_csingle, use_mpi, split_obs
).serialize_ops(tape)

ops_serialized = self._create_ops_list_lightning(*ops_serialized)

Check warning on line 124 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L124

Added line #L124 was not covered by tests

# We need to filter out indices in trainable_params which do not
# correspond to operators.
trainable_params = sorted(tape.trainable_params)
if len(trainable_params) == 0:
return None

Check warning on line 130 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L128-L130

Added lines #L128 - L130 were not covered by tests

tp_shift = []
record_tp_rows = []
all_params = 0

Check warning on line 134 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L132-L134

Added lines #L132 - L134 were not covered by tests

for op_idx, trainable_param in enumerate(trainable_params):

Check warning on line 136 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L136

Added line #L136 was not covered by tests
# get op_idx-th operator among differentiable operators
operation, _, _ = tape.get_operation(op_idx)
if isinstance(operation, Operation) and not isinstance(

Check warning on line 139 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L138-L139

Added lines #L138 - L139 were not covered by tests
operation, (BasisState, StatePrep)
):
# We now just ignore non-op or state preps
tp_shift.append(trainable_param)
record_tp_rows.append(all_params)
all_params += 1

Check warning on line 145 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L143-L145

Added lines #L143 - L145 were not covered by tests

if use_sp:

Check warning on line 147 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L147

Added line #L147 was not covered by tests
# When the first element of the tape is state preparation. Still, I am not sure
# whether there must be only one state preparation...
tp_shift = [i - 1 for i in tp_shift]

Check warning on line 150 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L150

Added line #L150 was not covered by tests

return {

Check warning on line 152 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L152

Added line #L152 was not covered by tests
"state_vector": self.state,
"obs_serialized": obs_serialized,
"ops_serialized": ops_serialized,
"tp_shift": tp_shift,
"record_tp_rows": record_tp_rows,
"all_params": all_params,
"obs_idx_offsets": obs_idx_offsets,
}

@staticmethod
def _adjoint_jacobian_processing(jac):
"""
Post-process the Jacobian matrix returned by ``adjoint_jacobian`` for
the new return type system.
"""
jac = np.squeeze(jac)

Check warning on line 168 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L168

Added line #L168 was not covered by tests

if jac.ndim == 0:
return np.array(jac)

Check warning on line 171 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L170-L171

Added lines #L170 - L171 were not covered by tests

if jac.ndim == 1:
return tuple(np.array(j) for j in jac)

Check warning on line 174 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L173-L174

Added lines #L173 - L174 were not covered by tests

# must be 2-dimensional
return tuple(tuple(np.array(j_) for j_ in j) for j in jac)

Check warning on line 177 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L177

Added line #L177 was not covered by tests

def _handle_raises(self, tape: QuantumTape, is_jacobian: bool, grad_vec=None):
"""Handle the raises related with the tape for computing the Jacobian with the adjoint method or the vector-Jacobian products."""

if tape.shots:
raise QuantumFunctionError(

Check warning on line 183 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L182-L183

Added lines #L182 - L183 were not covered by tests
"Requested adjoint differentiation to be computed with finite shots. "
"The derivative is always exact when using the adjoint "
"differentiation method."
)

tape_return_type = self._get_return_type(tape.measurements)

Check warning on line 189 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L189

Added line #L189 was not covered by tests

if is_jacobian:
if not tape_return_type:

Check warning on line 192 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L191-L192

Added lines #L191 - L192 were not covered by tests
# the tape does not have measurements
return True

Check warning on line 194 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L194

Added line #L194 was not covered by tests

if tape_return_type is State:
raise QuantumFunctionError(

Check warning on line 197 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L196-L197

Added lines #L196 - L197 were not covered by tests
"Adjoint differentiation method does not support measurement StateMP."
)

if not is_jacobian:
if qml.math.allclose(grad_vec, 0.0) or not tape_return_type:

Check warning on line 202 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L201-L202

Added lines #L201 - L202 were not covered by tests
# the tape does not have measurements or the gradient is 0.0
return True

Check warning on line 204 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L204

Added line #L204 was not covered by tests

if tape_return_type is State:
raise QuantumFunctionError(

Check warning on line 207 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L206-L207

Added lines #L206 - L207 were not covered by tests
"Adjoint differentiation does not support State measurements."
)

if any(m.return_type is not Expectation for m in tape.measurements):
raise QuantumFunctionError(

Check warning on line 212 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L211-L212

Added lines #L211 - L212 were not covered by tests
"Adjoint differentiation method does not support expectation return type "
"mixed with other return types"
)

return False

Check warning on line 217 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L217

Added line #L217 was not covered by tests

def calculate_jacobian(self, tape: QuantumTape):
"""Computes the Jacobian with the adjoint method.

.. code-block:: python

statevector = LightningKokkosStateVector(num_wires=num_wires)
statevector = statevector.get_final_state(tape)
jacobian = LightningKokkosAdjointJacobian(statevector).calculate_jacobian(tape)

Args:
tape (QuantumTape): Operations and measurements that represent instructions for execution on Lightning.

Returns:
The Jacobian of a tape.
"""

empty_array = self._handle_raises(tape, is_jacobian=True)

Check warning on line 235 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L235

Added line #L235 was not covered by tests

if empty_array:
return np.array([], dtype=self._dtype)

Check warning on line 238 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L237-L238

Added lines #L237 - L238 were not covered by tests

processed_data = self._process_jacobian_tape(tape)

Check warning on line 240 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L240

Added line #L240 was not covered by tests

if not processed_data: # training_params is empty
return np.array([], dtype=self._dtype)

Check warning on line 243 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L242-L243

Added lines #L242 - L243 were not covered by tests

trainable_params = processed_data["tp_shift"]

Check warning on line 245 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L245

Added line #L245 was not covered by tests

# If requested batching over observables, chunk into OMP_NUM_THREADS sized chunks.
# This will allow use of Lightning with adjoint for large-qubit numbers AND large
# numbers of observables, enabling choice between compute time and memory use.
requested_threads = int(getenv("OMP_NUM_THREADS", "1"))

Check warning on line 250 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L250

Added line #L250 was not covered by tests

if self._batch_obs and requested_threads > 1:
obs_partitions = _chunk_iterable(processed_data["obs_serialized"], requested_threads)
jac = []
for obs_chunk in obs_partitions:
jac_local = self._jacobian_lightning(

Check warning on line 256 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L252-L256

Added lines #L252 - L256 were not covered by tests
processed_data["state_vector"],
obs_chunk,
processed_data["ops_serialized"],
trainable_params,
)
jac.extend(jac_local)

Check warning on line 262 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L262

Added line #L262 was not covered by tests
else:
jac = self._jacobian_lightning(

Check warning on line 264 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L264

Added line #L264 was not covered by tests
processed_data["state_vector"],
processed_data["obs_serialized"],
processed_data["ops_serialized"],
trainable_params,
)
jac = np.array(jac)
jac = jac.reshape(-1, len(trainable_params)) if len(jac) else jac
jac_r = np.zeros((jac.shape[0], processed_data["all_params"]))
jac_r[:, processed_data["record_tp_rows"]] = jac

Check warning on line 273 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L270-L273

Added lines #L270 - L273 were not covered by tests

return self._adjoint_jacobian_processing(jac_r)

Check warning on line 275 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L275

Added line #L275 was not covered by tests

# pylint: disable=inconsistent-return-statements
def calculate_vjp(self, tape: QuantumTape, grad_vec):
"""Compute the vector-Jacobian products of a tape.

.. code-block:: python

statevector = LightningKokkosStateVector(num_wires=num_wires)
statevector = statevector.get_final_state(tape)
vjp = LightningKokkosAdjointJacobian(statevector).calculate_vjp(tape, grad_vec)

computes :math:`\\pmb{w} = (w_1,\\cdots,w_m)` where

.. math::

w_k = dy_k \\cdot J_{k,j}

Here, :math:`dy` is the workflow cotangent (grad_vec), and :math:`J` the Jacobian.

Args:
tape (QuantumTape): Operations and measurements that represent instructions for execution on Lightning.
grad_vec (tensor_like): Gradient-output vector, also called `dy` or cotangent. Must have shape matching the output
shape of the corresponding tape, i.e. number of measurements if the return type is expectation.

Returns:
The vector-Jacobian products of a tape.
"""

empty_array = self._handle_raises(tape, is_jacobian=False, grad_vec=grad_vec)

Check warning on line 304 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L304

Added line #L304 was not covered by tests

if empty_array:
return qml.math.convert_like(np.zeros(len(tape.trainable_params)), grad_vec)

Check warning on line 307 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L306-L307

Added lines #L306 - L307 were not covered by tests

# Proceed, because tape_return_type is Expectation.
if qml.math.ndim(grad_vec) == 0:
grad_vec = (grad_vec,)

Check warning on line 311 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L310-L311

Added lines #L310 - L311 were not covered by tests

if len(grad_vec) != len(tape.measurements):
raise ValueError(

Check warning on line 314 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L313-L314

Added lines #L313 - L314 were not covered by tests
"Number of observables in the tape must be the same as the "
"length of grad_vec in the vjp method"
)

if np.iscomplexobj(grad_vec):
raise ValueError(

Check warning on line 320 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L319-L320

Added lines #L319 - L320 were not covered by tests
"The vjp method only works with a real-valued grad_vec when the "
"tape is returning an expectation value"
)

ham = qml.simplify(qml.dot(grad_vec, [m.obs for m in tape.measurements]))

Check warning on line 325 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L325

Added line #L325 was not covered by tests

num_params = len(tape.trainable_params)

Check warning on line 327 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L327

Added line #L327 was not covered by tests

if num_params == 0:
return np.array([], dtype=self.qubit_state.dtype)

Check warning on line 330 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L329-L330

Added lines #L329 - L330 were not covered by tests

new_tape = qml.tape.QuantumScript(

Check warning on line 332 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L332

Added line #L332 was not covered by tests
tape.operations,
[qml.expval(ham)],
shots=tape.shots,
trainable_params=tape.trainable_params,
)

return self.calculate_jacobian(new_tape)

Check warning on line 339 in pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_kokkos/_adjoint_jacobian.py#L339

Added line #L339 was not covered by tests
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading