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

Statevector vjp and support Hamiltonian in adjoint_jacobian #294

Merged
merged 147 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 141 commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
a4ef444
Adding
chaeyeunpark Mar 16, 2022
e0f8c79
Refactoring Algorithms
chaeyeunpark Mar 17, 2022
7135538
Add more test
chaeyeunpark Mar 17, 2022
cc040af
Refactor bindings
chaeyeunpark Mar 18, 2022
6a08d62
Merge remote-tracking branch 'origin/master' into statevec_jacobian
chaeyeunpark Mar 18, 2022
9494249
Exploring more
chaeyeunpark Mar 19, 2022
b7321eb
Refactoring JacobianTape...
chaeyeunpark Mar 19, 2022
b0b3ba4
Merge remote-tracking branch 'origin/master' into statevec_jacobian
chaeyeunpark Mar 25, 2022
51d77ce
C++ test works
chaeyeunpark Mar 27, 2022
bc88868
Hamiltonian works in Python
chaeyeunpark Mar 27, 2022
469b2f6
Refactoring vjp
chaeyeunpark Mar 28, 2022
8d9b3b6
Fixing some errors from tests
chaeyeunpark Mar 28, 2022
e5b15f0
Merge remote-tracking branch 'origin/master' into statevec_jacobian
chaeyeunpark Mar 28, 2022
0c6270b
Fixing some tests
chaeyeunpark Mar 30, 2022
db86c2a
Merge remote-tracking branch 'origin/master' into statevec_jacobian
chaeyeunpark Apr 21, 2022
15950ef
Merge remote-tracking branch 'origin/master' into statevec_jacobian
chaeyeunpark May 6, 2022
98fe571
Merge remote-tracking branch 'origin/master' into statevec_jacobian
chaeyeunpark May 6, 2022
2fe2e0c
New branch from master
chaeyeunpark May 7, 2022
13a9f29
Add tests
chaeyeunpark May 7, 2022
c81bc30
Fix Python test
chaeyeunpark May 8, 2022
133499e
Fix test
chaeyeunpark May 8, 2022
f1d89a1
Update coverage
chaeyeunpark May 9, 2022
7ad725d
Fix binding dependency
chaeyeunpark May 9, 2022
e5c62cf
Fix GBenchmark
chaeyeunpark May 9, 2022
8667186
Revert StateVector tests to origin
chaeyeunpark May 9, 2022
063cd34
Add more tests
chaeyeunpark May 10, 2022
4302124
Auto update version
chaeyeunpark May 10, 2022
f51e904
Merge remote-tracking branch 'origin/master' into new_kernel_dispatch
chaeyeunpark May 10, 2022
5cbb9a0
Fix for tidy
chaeyeunpark May 10, 2022
f3cdbac
Update KernelMap
chaeyeunpark May 10, 2022
4faf279
Fix for codefactor
chaeyeunpark May 10, 2022
61726c3
Merge remote-tracking branch 'origin/master' into new_kernel_dispatch
chaeyeunpark May 10, 2022
ebdf601
Auto update version
chaeyeunpark May 10, 2022
09d52fa
Trigger CI
chaeyeunpark May 10, 2022
1428195
Update Memory
chaeyeunpark May 10, 2022
39aebb9
Fix
chaeyeunpark May 10, 2022
bd6b704
Revert single file
chaeyeunpark May 10, 2022
063b379
Fix for tidy
chaeyeunpark May 10, 2022
0b208cd
Merge remote-tracking branch 'origin/master' into statevec_jacobian
chaeyeunpark May 10, 2022
9f15dfe
Fixing
chaeyeunpark May 11, 2022
ff73bcb
Works
chaeyeunpark May 11, 2022
0110edb
Works for python tests
chaeyeunpark May 12, 2022
e16fede
Remove unused file
chaeyeunpark May 12, 2022
d48d88f
Fix some more
chaeyeunpark May 12, 2022
e6d68ba
Auto update version
chaeyeunpark May 12, 2022
db197a6
Trigger CI
chaeyeunpark May 12, 2022
3dab791
Fix
chaeyeunpark May 12, 2022
2dcc242
Fix for tidy
chaeyeunpark May 12, 2022
fbb4233
Fix for MSVC
chaeyeunpark May 12, 2022
1e5bab3
Fix tape trainable_param interface
chaeyeunpark May 14, 2022
e32b1de
Add statevec diff test
chaeyeunpark May 14, 2022
c24816b
Fixing serialization tests
chaeyeunpark May 14, 2022
18de985
Apply suggestions from code reviews
chaeyeunpark May 14, 2022
c43d20e
Update macos target version
chaeyeunpark May 14, 2022
f3643cf
Update MacOS minimum version in CMake
chaeyeunpark May 14, 2022
2d44742
Update for MacOS
chaeyeunpark May 14, 2022
67107d3
Revert aligned_alloc for MacOS
chaeyeunpark May 14, 2022
b734125
Fix
chaeyeunpark May 14, 2022
12ccb44
Apply suggestions from code review
chaeyeunpark May 18, 2022
ed2e955
Apply suggestions from code review
chaeyeunpark May 18, 2022
483b687
Compiles
chaeyeunpark May 31, 2022
bb83f54
Test passes
chaeyeunpark May 31, 2022
f57e95e
Update
chaeyeunpark May 31, 2022
9846903
Fix
chaeyeunpark May 31, 2022
931c0ba
Fix docstring
chaeyeunpark May 31, 2022
d84deb9
Remove unused dir
chaeyeunpark May 31, 2022
97f8733
Update
chaeyeunpark May 31, 2022
669db14
Fix doc
chaeyeunpark May 31, 2022
5d70afa
Remove BUILD_EXAMPLES option from CMake
chaeyeunpark May 31, 2022
733c6eb
Fix
chaeyeunpark Jun 1, 2022
9f6c557
Update memory
chaeyeunpark Jun 1, 2022
8bbb8b0
Memory works
chaeyeunpark Jun 1, 2022
b2b0035
Try to use std::aligned_alloc for MacOS
chaeyeunpark Jun 1, 2022
efe2a95
Revert "Try to use std::aligned_alloc for MacOS"
chaeyeunpark Jun 1, 2022
8be6179
Use C aligned_alloc for MacOs
chaeyeunpark Jun 1, 2022
8d894a2
Fix for tidy
chaeyeunpark Jun 1, 2022
b740869
Revert to posix_malloc in MacOS
chaeyeunpark Jun 1, 2022
59a5613
Update pennylane_lightning/src/simulator/KernelMap.hpp
chaeyeunpark Jun 1, 2022
b745ba9
Update docstring
chaeyeunpark Jun 1, 2022
bb7c64d
Merge branch 'new_kernel_dispatch' of github.com:PennyLaneAI/pennylan…
chaeyeunpark Jun 1, 2022
8080d66
Update pennylane_lightning/src/tests/Test_Measures_Sparse.cpp
chaeyeunpark Jun 1, 2022
fb3094e
Apply suggestions from review
chaeyeunpark Jun 1, 2022
e582e22
Merge remote-tracking branch 'origin/master' into new_kernel_dispatch
chaeyeunpark Jun 1, 2022
3ef6832
Auto update version
chaeyeunpark Jun 1, 2022
c68585c
Update KernelMap
chaeyeunpark Jun 2, 2022
1bc2aec
Merge branch 'new_kernel_dispatch' of github.com:PennyLaneAI/pennylan…
chaeyeunpark Jun 2, 2022
b21911d
Update pennylane_lightning/src/tests/TestKernels.hpp
chaeyeunpark Jun 2, 2022
65fa05e
Fix tests
chaeyeunpark Jun 2, 2022
8078f36
Merge branch 'new_kernel_dispatch' of github.com:PennyLaneAI/pennylan…
chaeyeunpark Jun 2, 2022
5720f16
Add Changelog
chaeyeunpark Jun 3, 2022
8f4b97c
Merge branch 'new_kernel_dispatch' into statevec_jacobian
chaeyeunpark Jun 3, 2022
726e626
Split and add tests for observables
chaeyeunpark Jun 6, 2022
9533db1
Fix all
chaeyeunpark Jun 7, 2022
02dda9a
Auto update version
chaeyeunpark Jun 7, 2022
fb67265
Trigger CI
chaeyeunpark Jun 7, 2022
f692391
Trigger CI
chaeyeunpark Jun 7, 2022
72f96af
Fix for tidy
chaeyeunpark Jun 7, 2022
fe0ce08
Fix non-const error from clang
chaeyeunpark Jun 7, 2022
bfe4da6
Fix for tidy
chaeyeunpark Jun 7, 2022
8eff6b1
Apply suggestions from code review
chaeyeunpark Jun 7, 2022
e0f2321
Apply suggestions from a code review
chaeyeunpark Jun 7, 2022
7f81192
Merge branch 'statevec_jacobian' of github.com:PennyLaneAI/pennylane-…
chaeyeunpark Jun 7, 2022
0ecf1a1
Apply suggestions from code review
chaeyeunpark Jun 8, 2022
f6e0e0d
Increase coverage a bit
chaeyeunpark Jun 9, 2022
3cf90b2
Merge branch 'statevec_jacobian' of github.com:PennyLaneAI/pennylane-…
chaeyeunpark Jun 9, 2022
0e27afc
Fix
chaeyeunpark Jun 9, 2022
9a9dcfe
Fix
chaeyeunpark Jun 9, 2022
7122a06
Format
chaeyeunpark Jun 9, 2022
9c8b271
Add C++ tests for LinearAlgebra
chaeyeunpark Jun 9, 2022
59aa554
Format
chaeyeunpark Jun 9, 2022
7d16a8d
Use span for statevector
chaeyeunpark Jun 9, 2022
5a5cabc
Add tests for getObsName
chaeyeunpark Jun 9, 2022
2c76b39
Format
chaeyeunpark Jun 9, 2022
c3911c5
More format
chaeyeunpark Jun 9, 2022
a83535a
Trigger CI
chaeyeunpark Jun 9, 2022
8f05df6
Fix
chaeyeunpark Jun 9, 2022
d572cfe
Fix for tidy
chaeyeunpark Jun 9, 2022
e46f327
Add more tests for covergage
chaeyeunpark Jun 9, 2022
1605ef7
More fix for tidy
chaeyeunpark Jun 9, 2022
ddf1a2e
Add more tests for observables
chaeyeunpark Jun 10, 2022
627002f
Fix for tidy
chaeyeunpark Jun 10, 2022
c545581
Add python tests; Use std::span
chaeyeunpark Jun 10, 2022
bf82a31
Add more tests for codecov
chaeyeunpark Jun 10, 2022
1e7f6a5
Add more tests for coverage; Update Makefile
chaeyeunpark Jun 10, 2022
4628846
Fix for tidy
chaeyeunpark Jun 10, 2022
cf220d0
Fix for tidy
chaeyeunpark Jun 10, 2022
7983772
Clarify documentation on statevector diff; Add tests for complex dy f…
chaeyeunpark Jun 12, 2022
c13808b
Apply suggestions from code review; Add some tests
chaeyeunpark Jun 13, 2022
2883f96
Update tests/test_adjoint_jacobian.py
chaeyeunpark Jun 13, 2022
a86c03f
Fix docstring
chaeyeunpark Jun 13, 2022
a169907
Merge branch 'statevec_jacobian' of github.com:PennyLaneAI/pennylane-…
chaeyeunpark Jun 13, 2022
f258898
Fix doc
chaeyeunpark Jun 13, 2022
1d8f1cb
Merge branch 'master' into statevec_jacobian
chaeyeunpark Jun 14, 2022
fd65dc0
Auto update version
chaeyeunpark Jun 14, 2022
cbc34ad
Update pennylane_lightning/src/algorithms/StateVecAdjDiff.hpp
chaeyeunpark Jun 14, 2022
59cb8fa
Fix for invalid trainable_params
chaeyeunpark Jun 16, 2022
e218a50
Format
chaeyeunpark Jun 16, 2022
c174376
Fix multithreading hamiltonian expval
chaeyeunpark Jun 16, 2022
ee5a46e
Merge branch 'statevec_jacobian' of github.com:PennyLaneAI/pennylane-…
chaeyeunpark Jun 16, 2022
504f11f
Fix format
chaeyeunpark Jun 16, 2022
386fe80
Fix for MacOS?
chaeyeunpark Jun 16, 2022
3ceeb84
Format
chaeyeunpark Jun 16, 2022
fdf2be1
Fix more openmp parallel Hamiltonian computation
chaeyeunpark Jun 16, 2022
cb2d43d
Add complicated Hamiltonian test
chaeyeunpark Jun 16, 2022
54e7844
Add changelog
chaeyeunpark Jun 16, 2022
bbbae97
Apply suggestions from code review
chaeyeunpark Jun 16, 2022
f1347b0
Fix for tidy
chaeyeunpark Jun 16, 2022
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ doc/_build/
PennyLane_Lightning.egg-info/
build/
Build/
BuildBench/
BuildGBench/
BuildTests/
BuildTidy/
dist/
tests/__pycache__/
.idea
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ docs:
clean-docs:
$(MAKE) -C doc clean

.PHONY : test-builtin test-suite test-python coverage test-cpp
.PHONY : test-builtin test-suite test-python coverage test-cpp test-cpp-no-omp test-cpp-blas test-cpp-kokkos
test-builtin:
$(PYTHON) -I $(TESTRUNNER)

Expand Down Expand Up @@ -86,9 +86,9 @@ test-cpp-blas:
cmake --build ./BuildTests --target runner
cmake --build ./BuildTests --target test

test-cpp-omp:
test-cpp-no-omp:
rm -rf ./BuildTests
cmake $(LIGHTNING_CPP_DIR) -BBuildTests -DBUILD_TESTS=ON -DENABLE_OPENMP=ON
cmake $(LIGHTNING_CPP_DIR) -BBuildTests -DBUILD_TESTS=ON -DENABLE_OPENMP=OFF
chaeyeunpark marked this conversation as resolved.
Show resolved Hide resolved
cmake --build ./BuildTests --target runner
cmake --build ./BuildTests --target test

Expand Down
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __getattr__(cls, name):
# TIP: if using the sphinx-bootstrap-theme, you need
# "treeViewIsBootstrap": True,
"exhaleExecutesDoxygen": True,
"exhaleDoxygenStdin": ("INPUT = " + " ".join(CPP_FILES) + " " "EXCLUDE_SYMBOLS = std::* "),
"exhaleDoxygenStdin": ("INPUT = " + " ".join(CPP_FILES) + "\nEXCLUDE_SYMBOLS = std::* "),
"afterTitleDescription": inspect.cleandoc(
"""
The Pennylane Lightning C++ API is intended to be called from Python through Pybind11. Direct use of the C++ API is currently unsupported and is provided for reference only.
Expand Down
120 changes: 72 additions & 48 deletions pennylane_lightning/_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,85 +35,109 @@
try:
from .lightning_qubit_ops import (
StateVectorC64,
ObsStructC64,
StateVectorC128,
ObsStructC128,
)
from .lightning_qubit_ops.adjoint_diff import (
NamedObsC64,
NamedObsC128,
HermitianObsC64,
HermitianObsC128,
TensorProdObsC64,
TensorProdObsC128,
HamiltonianC64,
HamiltonianC128,
OpsStructC64,
OpsStructC128,
)
except ImportError:
pass


def _obs_has_kernel(obs: Observable) -> bool:
def _obs_has_kernel(ob: Observable) -> bool:
"""Returns True if the input observable has a supported kernel in the C++ backend.

Args:
obs (Observable): the input observable
ob (Observable): the input observable

Returns:
bool: indicating whether ``obs`` has a dedicated kernel in the backend
"""
if is_pauli_word(obs):
if is_pauli_word(ob):
return True
if isinstance(obs, (Hadamard, Projector)):
if isinstance(ob, (Hadamard, Projector)):
return True
if isinstance(obs, Tensor):
return all(_obs_has_kernel(o) for o in obs.obs)
if isinstance(ob, Tensor):
return all(_obs_has_kernel(o) for o in ob.obs)
return False


def _serialize_obs(tape: QuantumTape, wires_map: dict, use_csingle: bool = False) -> List:
"""Serializes the observables of an input tape.

Args:
tape (QuantumTape): the input quantum tape
wires_map (dict): a dictionary mapping input wires to the device's backend wires
use_csingle (bool): whether to use np.complex64 instead of np.complex128

Returns:
list(ObsStructC128 or ObsStructC64): A list of observable objects compatible with the C++ backend
"""
obs = []
def _serialize_named_hermitian_ob(o, wires_map: dict, use_csingle: bool):
"""Serializes an observable (Named or Hermitian)"""
assert not isinstance(o, Tensor)

if use_csingle:
ctype = np.complex64
obs_py = ObsStructC64
named_obs = NamedObsC64
hermitian_obs = HermitianObsC64
else:
ctype = np.complex128
obs_py = ObsStructC128
named_obs = NamedObsC128
hermitian_obs = HermitianObsC128

for o in tape.observables:
is_tensor = isinstance(o, Tensor)
wires_list = o.wires.tolist()
wires = [wires_map[w] for w in wires_list]
if _obs_has_kernel(o):
return named_obs(o.name, wires)
return hermitian_obs(qml.matrix(o).ravel().astype(ctype), wires)

wires = []

if is_tensor:
for o_ in o.obs:
wires_list = o_.wires.tolist()
w = [wires_map[w] for w in wires_list]
wires.append(w)
else:
wires_list = o.wires.tolist()
w = [wires_map[w] for w in wires_list]
wires.append(w)
def _serialize_tensor_ob(ob, wires_map: dict, use_csingle: bool):
"""Serialize a tensor observable"""
assert isinstance(ob, Tensor)

name = o.name if is_tensor else [o.name]
if use_csingle:
tensor_obs = TensorProdObsC64
else:
tensor_obs = TensorProdObsC128

params = []
return tensor_obs([_serialize_ob(o, wires_map, use_csingle) for o in ob.obs])

if not _obs_has_kernel(o):
if is_tensor:
for o_ in o.obs:
if not _obs_has_kernel(o_):
params.append(qml.matrix(o_).ravel().astype(ctype))
else:
params.append([])
else:
params.append(qml.matrix(o).ravel().astype(ctype))

ob = obs_py(name, params, wires)
obs.append(ob)
def _serialize_hamiltonian(ob, wires_map: dict, use_csingle: bool):
if use_csingle:
rtype = np.float32
hamiltonian_obs = HamiltonianC64
else:
rtype = np.float64
hamiltonian_obs = HamiltonianC128

coeffs = np.array(ob.coeffs).astype(rtype)
terms = [_serialize_ob(t, wires_map, use_csingle) for t in ob.ops]
return hamiltonian_obs(coeffs, terms)


def _serialize_ob(ob, wires_map, use_csingle):
if isinstance(ob, Tensor):
return _serialize_tensor_ob(ob, wires_map, use_csingle)
elif ob.name == "Hamiltonian":
return _serialize_hamiltonian(ob, wires_map, use_csingle)
else:
return _serialize_named_hermitian_ob(ob, wires_map, use_csingle)


def _serialize_observables(tape: QuantumTape, wires_map: dict, use_csingle: bool = False) -> List:
"""Serializes the observables of an input tape.

Args:
tape (QuantumTape): the input quantum tape
wires_map (dict): a dictionary mapping input wires to the device's backend wires
use_csingle (bool): whether to use np.complex64 instead of np.complex128

Returns:
list(ObsStructC128 or ObsStructC64): A list of observable objects compatible with the C++ backend
"""

return obs
return [_serialize_ob(ob, wires_map, use_csingle) for ob in tape.observables]


def _serialize_ops(
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/_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.24.0-dev15"
__version__ = "0.24.0-dev16"
Loading