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

Template/expval #489

Merged
merged 56 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
2f44c1b
M pennylane_lightning/core/src/bindings/Bindings.hpp; hack `Jacobian…
vincentmr Aug 21, 2023
27b54eb
Auto update version
github-actions[bot] Aug 21, 2023
8ad1a26
Update changelog.
vincentmr Aug 21, 2023
8375e7d
Merge branch 'master' into bugfix/cuda12
vincentmr Aug 21, 2023
1098402
Merge branch 'master' into bugfix/cuda12
AmintorDusko Aug 22, 2023
68881d1
Auto update version
github-actions[bot] Aug 22, 2023
e3df23b
Merge branch 'master' into bugfix/cuda12
vincentmr Aug 22, 2023
fcc7fa3
Auto update version
github-actions[bot] Aug 22, 2023
48d9615
Add an argument to adjointJacobian to avoid syncing and copying state…
vincentmr Aug 22, 2023
3248276
Reformat
vincentmr Aug 22, 2023
504c228
trigger CI
vincentmr Aug 23, 2023
27f8e81
[skip ci] Update changelog.
vincentmr Aug 23, 2023
c45cd23
Introduce std::unordered_map<std::string, ExpValFunc> expval_funcs_.
vincentmr Aug 23, 2023
33ff620
Introduce applyExpectationValueFunctor.
vincentmr Aug 23, 2023
e0d3212
Add binding to LKokkos expval(matrix, wires). Combine expval functor …
vincentmr Aug 23, 2023
4305edc
Update changelog.
vincentmr Aug 23, 2023
5595e3c
Add test for arbitrary expval(Hermitian).
vincentmr Aug 23, 2023
22c47f4
Add getExpectationValueMultiQubitOpFunctor.
vincentmr Aug 23, 2023
1e1565d
Add typename hint for macos.
vincentmr Aug 23, 2023
614e4de
Add typename macos.
vincentmr Aug 24, 2023
b1afba8
Use Kokkos::ThreadVectorRange policy for innerloop in getExpectationV…
vincentmr Aug 24, 2023
9142b16
Merge branch 'master' into bugfix/cuda12
vincentmr Aug 24, 2023
3b3ee66
Auto update version
github-actions[bot] Aug 24, 2023
7b22095
Merge branch 'bugfix/cuda12' into accel/expval
vincentmr Aug 24, 2023
2c7cefc
Merge branch 'master' into bugfix/cuda12
vincentmr Aug 24, 2023
6dc7883
Auto update version
github-actions[bot] Aug 24, 2023
53b48d2
Couple fix for HIP.
vincentmr Aug 24, 2023
cb43f40
Merge branch 'bugfix/cuda12' into accel/expval
vincentmr Aug 24, 2023
d31f1fa
WIP
vincentmr Aug 25, 2023
51b7497
Add specialized 3-5 qubit expval functors.
vincentmr Aug 25, 2023
3ae232c
Add implementation notes.
vincentmr Aug 28, 2023
ec3660d
[skip ci] Polish getExpValMatrix and limit getExpValNQubitOpFunctor t…
vincentmr Aug 28, 2023
0077c3b
Increase expval test parameter set.
vincentmr Aug 28, 2023
079f407
Merge remote-tracking branch 'origin/master' into template/expval
vincentmr Aug 28, 2023
7a84c8a
Fix whitespace warning.
vincentmr Aug 28, 2023
0c24d28
Auto update version
github-actions[bot] Aug 28, 2023
88a29ea
Use simplified bitshifts in 1- & 2-qubit expval kernels.
vincentmr Aug 29, 2023
457f80b
Update tests.
vincentmr Aug 29, 2023
bdd3178
Update changelog.
vincentmr Aug 29, 2023
27d9c9d
Bump pennylane version.
vincentmr Aug 29, 2023
de984ce
Merge branch 'master' into template/expval
vincentmr Aug 29, 2023
edc94ea
Auto update version
github-actions[bot] Aug 29, 2023
255b971
Reimplement expval functors with macros.
vincentmr Aug 30, 2023
aac80f3
Auto update version
github-actions[bot] Aug 30, 2023
d56ec3f
Merge branch 'master' into template/expval
vincentmr Aug 30, 2023
230e94a
Update CHANGELOG.md
vincentmr Aug 31, 2023
d8cd73a
Merge remote-tracking branch 'origin/master' into template/expval
vincentmr Sep 6, 2023
3e4e6cd
Auto update version
github-actions[bot] Sep 6, 2023
4855de5
trigger CI
vincentmr Sep 6, 2023
313b5df
trigger CI
vincentmr Sep 6, 2023
bc7642b
Bump Kokkos to 4.1.00 in CI.
vincentmr Sep 6, 2023
c977466
Revert kokkos ver.
vincentmr Sep 6, 2023
3c741fb
Add tests for macroed expval functors.
vincentmr Sep 6, 2023
aef8716
Remove redundant black lines.
vincentmr Sep 6, 2023
a1f0d4f
Use matrix interface to get expval of HermitianObs in LKokkos.
vincentmr Sep 6, 2023
5123082
Cover kokkos_args error.
vincentmr Sep 6, 2023
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 @@ -12,6 +12,9 @@

### Improvements

* Refactor LKokkos `Measurements` class to use Kokkos `RangePolicy` together with special functors to obtain the expectation value of 1- to 4-wire generic unitary gates. For more than 4 wires, the general implementation using Kokkos `TeamPolicy` is employed to yield the best all-around performance.
[(#489)] (https://github.com/PennyLaneAI/pennylane-lightning/pull/489)

* Add tests to increase LKokkos coverage.
[(#485)](https://github.com/PennyLaneAI/pennylane-lightning/pull/485)

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.33.0-dev3"
__version__ = "0.33.0-dev4"
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Implementation

## Expval kernels

The general multi-qubit operator kernel requires a private variable `coeffs_in` to store state vector coefficients.
In the Kokkos framework, this variable cannot be a private member of the functor, say, because all threads will overwrite it.
One must then use `TeamPolicy`s together with `scratch_memory_space` which allows creating and manipulating thread-local variables.
This implementation however appears suboptimal compared with the straightforward `RangePolicy` with bit-injection one.

The last being more verbose, it is only implemented for 1- and 2-qubit observables.
It is however possible to generate the code automatically for higher qubit counts with the following Python script.

```python
for n_wires in range(1, 6):
name = f"getExpVal{n_wires}QubitOpFunctor"
nU = 2**n_wires

print(
f"""template <class PrecisionT, std::size_t n_wires> struct {name} {{

using ComplexT = Kokkos::complex<PrecisionT>;
using KokkosComplexVector = Kokkos::View<ComplexT *>;
using KokkosIntVector = Kokkos::View<std::size_t *>;

KokkosComplexVector arr;
KokkosComplexVector matrix;
KokkosIntVector wires;
std::size_t dim;
std::size_t num_qubits;

{name}(const KokkosComplexVector &arr_,
const std::size_t num_qubits_,
const KokkosComplexVector &matrix_,
const KokkosIntVector &wires_) {{
wires = wires_;
arr = arr_;
matrix = matrix_;
num_qubits = num_qubits_;
dim = 1U << wires.size();
}}

KOKKOS_INLINE_FUNCTION
void operator()(const std::size_t k, PrecisionT &expval) const {{
const std::size_t kdim = k * dim;
"""
)

for k in range(nU):
print(
f"""
std::size_t i{k:0{n_wires}b} = kdim | {k};
for (std::size_t pos = 0; pos < n_wires; pos++) {{
std::size_t x =
((i{k:0{n_wires}b} >> (n_wires - pos - 1)) ^
(i{k:0{n_wires}b} >> (num_qubits - wires(pos) - 1))) &
1U;
i{k:0{n_wires}b} = i{k:0{n_wires}b} ^ ((x << (n_wires - pos - 1)) |
(x << (num_qubits - wires(pos) - 1)));
}}
"""
)

# print("expval += real(")
for k in range(nU):
tmp = f"expval += real(conj(arr(i{k:0{n_wires}b})) * ("
tmp += f"matrix(0B{k:0{n_wires}b}{0:0{n_wires}b}) * arr(i{0:0{n_wires}b})"
for j in range(1, nU):
tmp += (
f" + matrix(0B{k:0{n_wires}b}{j:0{n_wires}b}) * arr(i{j:0{n_wires}b})"
)
print(tmp, end="")
print("));")
print("}")
print("};")
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ target_link_libraries(${PL_BACKEND}_measurements INTERFACE lightning_compile_op
lightning_observables
lightning_utils
${PL_BACKEND}
${PL_BACKEND}_observables
${PL_BACKEND}_utils
)

Expand Down
Loading
Loading