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

Unify Lightning Kokkos device and Lightning Qubit device under a Lightning Base device #876

Merged
merged 171 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
171 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
a9b253f
first verion of unify devices clases
LuisAlfredoNu Aug 21, 2024
8041450
Merge branch 'kokkosNewAPI_backend' into kokkosNewAPI_unify
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
8bff10c
Merge branch 'kokkosNewAPI_backend' into kokkosNewAPI_unify
LuisAlfredoNu Aug 26, 2024
7e57ec2
trigger CIs
LuisAlfredoNu Aug 26, 2024
956538e
Simple unification of Lightning devices LK and LQ
LuisAlfredoNu Aug 27, 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
c57f78f
Define LBase without dispatcher
LuisAlfredoNu Aug 27, 2024
d2f415f
add code style and doctrings
LuisAlfredoNu Aug 27, 2024
cca6de2
apply format
LuisAlfredoNu Aug 28, 2024
2f1d9d7
Pass local pylint
LuisAlfredoNu Aug 28, 2024
bdbc180
Apply format
LuisAlfredoNu Aug 28, 2024
73b5fcc
update docstring
LuisAlfredoNu Aug 28, 2024
7353064
Increase shots number for measurement
LuisAlfredoNu Aug 28, 2024
e967baf
add abstractmethod
LuisAlfredoNu Aug 30, 2024
d922d26
Update year of licence
LuisAlfredoNu Aug 30, 2024
c415d26
Update lightning docstring with proper device names
LuisAlfredoNu Aug 30, 2024
32dac95
Apply suggestions from code review: update doctring with correct names
LuisAlfredoNu Aug 30, 2024
cd26966
correct docstring
LuisAlfredoNu Aug 30, 2024
9c04fb0
apply format
LuisAlfredoNu Aug 30, 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
b1703e1
trigger CIs
LuisAlfredoNu 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
420544a
Merge branch 'kokkosNewAPI_backend' into kokkosNewAPI_unify
LuisAlfredoNu Sep 4, 2024
47d0af3
trigger CIs
LuisAlfredoNu Sep 4, 2024
2e9191f
solve merge conflict
LuisAlfredoNu Sep 4, 2024
31ff5da
Auto update version from '0.39.0-dev1' to '0.39.0-dev2'
ringo-but-quantum Sep 4, 2024
c5a3e4c
remove pass from abstract methods
LuisAlfredoNu Sep 4, 2024
465e0fe
solve issues with pylint
LuisAlfredoNu Sep 4, 2024
2df631a
solve issues with pylint
LuisAlfredoNu Sep 4, 2024
2730a01
solve issues with pylint
LuisAlfredoNu Sep 4, 2024
cac4474
solve issues with pylint
LuisAlfredoNu Sep 4, 2024
36e064c
solve issues with pylint
LuisAlfredoNu Sep 4, 2024
6007e80
fix bug of inheritance order
LuisAlfredoNu Sep 4, 2024
96e0a84
remove double parent class
LuisAlfredoNu Sep 4, 2024
32374ec
Merge branch 'master' into kokkosNewAPI_unify
LuisAlfredoNu Sep 4, 2024
d34b7e9
Auto update version from '0.39.0-dev2' to '0.39.0-dev3'
ringo-but-quantum Sep 4, 2024
df374eb
Merge branch 'master' into kokkosNewAPI_unify
LuisAlfredoNu Sep 5, 2024
dfd8d1d
solve conflict
LuisAlfredoNu Sep 5, 2024
634ea1d
apply format
LuisAlfredoNu Sep 5, 2024
fe4c8d9
solve issues with pylint
LuisAlfredoNu Sep 5, 2024
28fc8cb
apply format
LuisAlfredoNu Sep 5, 2024
1927480
Auto update version from '0.39.0-dev4' to '0.39.0-dev5'
ringo-but-quantum Sep 5, 2024
992e2f6
Merge branch 'master' into kokkosNewAPI_unify
LuisAlfredoNu Sep 5, 2024
20a6920
Auto update version from '0.39.0-dev5' to '0.39.0-dev6'
ringo-but-quantum Sep 5, 2024
d9488d0
Apply suggestions from Vincent's code review
LuisAlfredoNu Sep 6, 2024
d6a99e8
Merge branch 'master' into kokkosNewAPI_unify
LuisAlfredoNu Sep 6, 2024
442fed5
apply review comments
LuisAlfredoNu Sep 6, 2024
74df35d
Apply format
LuisAlfredoNu Sep 6, 2024
4d6b70f
Added raise for batch_obs in LK
LuisAlfredoNu Sep 6, 2024
6b61467
apply format
LuisAlfredoNu Sep 6, 2024
e00ae2f
remove use_mpi from docstrings
LuisAlfredoNu Sep 6, 2024
3ca8b9a
Ali's review comments
LuisAlfredoNu Sep 6, 2024
2431b35
Merge branch 'kokkosNewAPI_unify' of github.com:PennyLaneAI/pennylane…
LuisAlfredoNu Sep 6, 2024
7fd4f2f
skip test for batch_obs in LK
LuisAlfredoNu Sep 6, 2024
3a3f59d
apply format
LuisAlfredoNu Sep 6, 2024
7d523c7
Merge branch 'master' into kokkosNewAPI_unify
LuisAlfredoNu Sep 6, 2024
a1cac8e
Auto update version from '0.39.0-dev6' to '0.39.0-dev7'
ringo-but-quantum Sep 6, 2024
fbba326
revert batch_obs False
LuisAlfredoNu Sep 9, 2024
31fd852
Merge branch 'master' into kokkosNewAPI_unify
LuisAlfredoNu Sep 9, 2024
ba67cca
remove unused-import
LuisAlfredoNu Sep 9, 2024
452b8e6
Merge branch 'kokkosNewAPI_unify' of github.com:PennyLaneAI/pennylane…
LuisAlfredoNu Sep 9, 2024
efa3005
Added changelog
LuisAlfredoNu Sep 9, 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
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* Smarter defaults for the `split_obs` argument in the serializer. The serializer splits linear combinations into chunks instead of all their terms.
[(#873)](https://github.com/PennyLaneAI/pennylane-lightning/pull/873/)

* Unify Lightning Kokkos device and Lightning Qubit device under a Lightning Base device
[(#876)](https://github.com/PennyLaneAI/pennylane-lightning/pull/876)

### Documentation

### Bug fixes
Expand Down
288 changes: 288 additions & 0 deletions pennylane_lightning/core/_adjoint_jacobian_base.py
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
# 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.
"""

from abc import ABC, abstractmethod
from typing import Any, Callable, 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

from pennylane_lightning.core._serialize import QuantumScriptSerializer


class LightningBaseAdjointJacobian(ABC):
"""Lightning [Device] Adjoint Jacobian class

A class that serves as a base class for Lightning state-vector simulators.
Check and execute the adjoint Jacobian differentiation method.

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

def __init__(self, qubit_state: Any, batch_obs: bool) -> None:
self._qubit_state = qubit_state
self._batch_obs = batch_obs

Check warning on line 44 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L43-L44

Added lines #L43 - L44 were not covered by tests
maliasadi marked this conversation as resolved.
Show resolved Hide resolved

# Dummy for the C++ bindings
self._jacobian_lightning: Callable = None
self._create_ops_list_lightning: Callable = None

Check warning on line 48 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L47-L48

Added lines #L47 - L48 were not covered by tests
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved

@property
def qubit_state(self):
"""Returns a handle to the Lightning [Device] StateVector object."""
return self._qubit_state

Check warning on line 53 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L53

Added line #L53 was not covered by tests

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

Check warning on line 58 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L58

Added line #L58 was not covered by tests

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

Check warning on line 63 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L63

Added line #L63 was not covered by tests

@abstractmethod
def _adjoint_jacobian_dtype(self):
LuisAlfredoNu marked this conversation as resolved.
Show resolved Hide resolved
"""Binding to Lightning [Device] Adjoint Jacobian C++ class.

Returns: the AdjointJacobian class
"""

@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 85 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L84-L85

Added lines #L84 - L85 were not covered by tests

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

Check warning on line 88 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L87-L88

Added lines #L87 - L88 were not covered by tests

return Expectation

Check warning on line 90 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L90

Added line #L90 was not covered by tests

def _process_jacobian_tape(self, tape: QuantumTape, split_obs: bool = False):
"""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.
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._qubit_state.dtype == np.complex64

Check warning on line 103 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L103

Added line #L103 was not covered by tests

use_mpi = False
obs_serialized, obs_indices = QuantumScriptSerializer(

Check warning on line 106 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L105-L106

Added lines #L105 - L106 were 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 110 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L110

Added line #L110 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 114 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L114

Added line #L114 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 120 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L118-L120

Added lines #L118 - L120 were not covered by tests

tp_shift = []
record_tp_rows = []
all_params = 0

Check warning on line 124 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L122-L124

Added lines #L122 - L124 were not covered by tests

for op_idx, trainable_param in enumerate(trainable_params):

Check warning on line 126 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L126

Added line #L126 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 129 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L128-L129

Added lines #L128 - L129 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 135 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L133-L135

Added lines #L133 - L135 were not covered by tests

if use_sp:

Check warning on line 137 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L137

Added line #L137 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 140 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L140

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

return {

Check warning on line 142 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L142

Added line #L142 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_indices": obs_indices,
}

@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 158 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L158

Added line #L158 was not covered by tests

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

Check warning on line 161 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L160-L161

Added lines #L160 - L161 were not covered by tests

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

Check warning on line 164 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L163-L164

Added lines #L163 - L164 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 167 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L167

Added line #L167 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 173 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L172-L173

Added lines #L172 - L173 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 179 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L179

Added line #L179 was not covered by tests

if is_jacobian:
if not tape_return_type:

Check warning on line 182 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L181-L182

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

Check warning on line 184 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L184

Added line #L184 was not covered by tests

if tape_return_type is State:
raise QuantumFunctionError(

Check warning on line 187 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L186-L187

Added lines #L186 - L187 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 192 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L191-L192

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

Check warning on line 194 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.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/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L196-L197

Added lines #L196 - L197 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 202 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L201-L202

Added lines #L201 - L202 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 207 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L207

Added line #L207 was not covered by tests

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

.. code-block:: python

statevector = Lightning [Device] StateVector(num_wires=num_wires)
statevector = statevector.get_final_state(tape)
jacobian = Lightning [Device] AdjointJacobian(statevector).calculate_jacobian(tape)

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

Returns:
The Jacobian of a tape.
"""

# 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 = Lightning [Device] StateVector(num_wires=num_wires)
statevector = statevector.get_final_state(tape)
vjp = Lightning [Device] AdjointJacobian(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 253 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L253

Added line #L253 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 256 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L255-L256

Added lines #L255 - L256 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 260 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L259-L260

Added lines #L259 - L260 were not covered by tests

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

Check warning on line 263 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L262-L263

Added lines #L262 - L263 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 269 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L268-L269

Added lines #L268 - L269 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 274 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L274

Added line #L274 was not covered by tests

num_params = len(tape.trainable_params)

Check warning on line 276 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L276

Added line #L276 was not covered by tests

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

Check warning on line 279 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L278-L279

Added lines #L278 - L279 were not covered by tests

new_tape = qml.tape.QuantumScript(

Check warning on line 281 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L281

Added line #L281 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 288 in pennylane_lightning/core/_adjoint_jacobian_base.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_adjoint_jacobian_base.py#L288

Added line #L288 was not covered by tests
Loading
Loading