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

Update QuantumScriptSerializer.serialize_observables to better handle new operator arithmetic #670

Merged
merged 41 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
b15aeaf
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane-lig…
AmintorDusko Mar 18, 2024
65c8853
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane-lig…
AmintorDusko Mar 19, 2024
ebeb417
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane-lig…
AmintorDusko Mar 19, 2024
d664fd2
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane-lig…
AmintorDusko Mar 21, 2024
c942497
port PR changes so far, after traumatic rebase
AmintorDusko Mar 21, 2024
e503442
update vjp tests
AmintorDusko Mar 21, 2024
473c030
format
AmintorDusko Mar 21, 2024
e3ff0f7
update vjp tests
AmintorDusko Mar 21, 2024
87ed3ff
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane-lig…
AmintorDusko Mar 22, 2024
690402c
Merge branch 'master' into lq2-vjp
AmintorDusko Mar 22, 2024
016388b
update dev version
AmintorDusko Mar 22, 2024
3e8e2cd
format
AmintorDusko Mar 22, 2024
3032b65
add tests for vjp
AmintorDusko Mar 22, 2024
fc7b22a
Merge branch 'master' into lq2-vjp
AmintorDusko Mar 22, 2024
9e7c6ed
Auto update version
github-actions[bot] Mar 22, 2024
8dc40b1
Merge branch 'lq2-vjp' of https://github.com/PennyLaneAI/pennylane-li…
AmintorDusko Mar 22, 2024
a085bca
Merge branch 'master' into lq2-vjp
mudit2812 Apr 2, 2024
e597ada
Auto update version
github-actions[bot] Apr 2, 2024
27b00a6
Updated tests
mudit2812 Apr 2, 2024
47fa871
Updated serialization
mudit2812 Apr 2, 2024
3fbeea1
Unpinned cmake
mudit2812 Apr 2, 2024
424fb83
Update changelog
mudit2812 Apr 2, 2024
17c9b0f
Update .github/CHANGELOG.md
mudit2812 Apr 3, 2024
2bd881f
isort
mudit2812 Apr 3, 2024
08bea1c
black
mudit2812 Apr 3, 2024
bed5404
Run CI with updated PL branch
mudit2812 Apr 3, 2024
5a0823e
Merge branch 'master' into op-math-serialize
mudit2812 Apr 4, 2024
858dc24
Auto update version
github-actions[bot] Apr 4, 2024
57807f3
trigger ci
vincentmr Apr 5, 2024
7691b04
Added changes per review
mudit2812 Apr 8, 2024
1ce158b
Auto update version
github-actions[bot] Apr 8, 2024
3c198c3
Update requirements-dev.txt
mudit2812 Apr 8, 2024
ec55c7c
Merge branch 'master' into op-math-serialize
mudit2812 Apr 8, 2024
e44e9dc
Update pennylane_lightning/core/_serialize.py
mudit2812 Apr 9, 2024
45f3342
Merge branch 'master' into op-math-serialize
mudit2812 Apr 9, 2024
cdc2d9c
Auto update version
github-actions[bot] Apr 9, 2024
98c77ee
Trigger CI
mudit2812 Apr 9, 2024
d125a3c
Update overlapping wires prod serialization test
mudit2812 Apr 9, 2024
ce6da5a
Merge branch 'master' into op-math-serialize
mudit2812 Apr 10, 2024
fa5e577
Auto update version
github-actions[bot] Apr 10, 2024
8f770d9
Trigger CI
mudit2812 Apr 10, 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 @@ -49,6 +49,9 @@
* Add `isort` to `requirements-dev.txt` and run before `black` upon `make format` to sort Python imports.
[(#623)](https://github.com/PennyLaneAI/pennylane-lightning/pull/623)

* Improve support for new operator arithmetic with `QuantumScriptSerializer.serialize_observables`.
maliasadi marked this conversation as resolved.
Show resolved Hide resolved
[(#670)](https://github.com/PennyLaneAI/pennylane-lightning/pull/670)

### Documentation

### Bug fixes
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/tests_gpu_cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ jobs:

- name: Install required packages
run: |
# Omitting the installation of cmake v3.29.0 due to
# https://github.com/scikit-build/cmake-python-distributions/pull/474
python -m pip install ninja "cmake!=3.29.0" custatevec-cu${{ matrix.cuda_version }}
python -m pip install ninja cmake custatevec-cu${{ matrix.cuda_version }}
sudo apt-get -y -q install liblapack-dev

- name: Build and run unit tests
Expand Down Expand Up @@ -243,9 +241,7 @@ jobs:
run: |
cd main
python -m pip install -r requirements-dev.txt
# Omitting the installation of cmake v3.29.0 due to
# https://github.com/scikit-build/cmake-python-distributions/pull/474
python -m pip install "cmake!=3.29.0" custatevec-cu${{ matrix.cuda_version }} openfermionpyscf
python -m pip install cmake custatevec-cu${{ matrix.cuda_version }} openfermionpyscf

- name: Checkout PennyLane for release build
if: inputs.pennylane-version == 'release'
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/tests_linux_x86_mpi_gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ jobs:
- name: Install required packages
run: |
python -m pip install -r requirements-dev.txt
# Omitting the installation of cmake v3.29.0 due to
# https://github.com/scikit-build/cmake-python-distributions/pull/474
python -m pip install "cmake!=3.29.0" custatevec-cu12
python -m pip install cmake custatevec-cu12
sudo apt-get -y -q install liblapack-dev

- name: Validate GPU version and installed compiler and modules
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/wheel_macos_x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ jobs:
run: |
mkdir -p ${{ github.workspace}}/Kokkos_install/${{ matrix.exec_model }}
cd kokkos
# Omitting the installation of cmake v3.29.0 due to
# https://github.com/scikit-build/cmake-python-distributions/pull/474
python -m pip install "cmake!=3.29.0" ninja
python -m pip install cmake ninja

cmake -BBuild . -DCMAKE_INSTALL_PREFIX=${{ github.workspace}}/Kokkos_install/${{ matrix.exec_model }} \
-DKokkos_ENABLE_COMPLEX_ALIGN=OFF \
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/wheel_noarch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ jobs:

- name: Install CMake and ninja
run: |
# Omitting the installation of cmake v3.29.0 due to
# https://github.com/scikit-build/cmake-python-distributions/pull/474
python -m pip install --upgrade "cmake!=3.29.0" ninja
python -m pip install --upgrade cmake ninja

- name: Build wheels
if: ${{ matrix.pl_backend == 'lightning_qubit'}}
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/wheel_win_x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ jobs:
- name: Install dependencies
if: steps.kokkos-cache.outputs.cache-hit != 'true'
run: |
# Omitting the installation of cmake v3.29.0 due to
# https://github.com/scikit-build/cmake-python-distributions/pull/474
python -m pip install "cmake!=3.29.0" build
python -m pip install cmake build

- name: Build Kokkos core library
if: steps.kokkos-cache.outputs.cache-hit != 'true'
Expand Down
38 changes: 18 additions & 20 deletions pennylane_lightning/core/_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
BasisState,
DeviceError,
Hadamard,
Hamiltonian,
Identity,
PauliX,
PauliY,
Expand All @@ -34,10 +33,12 @@
)
from pennylane.math import unwrap
from pennylane.operation import Tensor
from pennylane.ops import Prod, SProd, Sum
from pennylane.ops import Hamiltonian, LinearCombination, Prod, SProd, Sum
from pennylane.tape import QuantumTape

mudit2812 marked this conversation as resolved.
Show resolved Hide resolved
pauli_name_map = {
NAMED_OBS = (Identity, PauliX, PauliY, PauliZ, Hadamard)
OP_MATH_OBS = (Prod, SProd, Sum, LinearCombination)
PAULI_NAME_MAP = {
"I": "Identity",
"X": "PauliX",
"Y": "PauliY",
Expand Down Expand Up @@ -243,11 +244,11 @@ def map_wire(wire: int):

if len(observable) == 1:
wire, pauli = list(observable.items())[0]
return self.named_obs(pauli_name_map[pauli], [map_wire(wire)])
return self.named_obs(PAULI_NAME_MAP[pauli], [map_wire(wire)])

return self.tensor_obs(
[
self.named_obs(pauli_name_map[pauli], [map_wire(wire)])
self.named_obs(PAULI_NAME_MAP[pauli], [map_wire(wire)])
for wire, pauli in observable.items()
]
)
Expand All @@ -258,9 +259,8 @@ def _pauli_sentence(self, observable, wires_map: dict = None):
terms = [self._pauli_word(pw, wires_map) for pw in pwords]
coeffs = np.array(coeffs).astype(self.rtype)

# TODO: Add this
# if len(terms) == 1 and coeffs[0] == 1.0:
# return terms[0]
if len(terms) == 1 and coeffs[0] == 1.0:
mudit2812 marked this conversation as resolved.
Show resolved Hide resolved
return terms[0]

if self.split_obs:
return [self.hamiltonian_obs([c], [t]) for (c, t) in zip(coeffs, terms)]
Expand All @@ -269,22 +269,20 @@ def _pauli_sentence(self, observable, wires_map: dict = None):
# pylint: disable=protected-access, too-many-return-statements
def _ob(self, observable, wires_map: dict = None):
"""Serialize a :class:`pennylane.operation.Observable` into an Observable."""
if isinstance(observable, (Prod, Sum, SProd)) and observable.pauli_rep is not None:
if isinstance(observable, NAMED_OBS):
return self._named_obs(observable, wires_map)
if isinstance(observable, Hamiltonian):
return self._hamiltonian(observable, wires_map)
if observable.pauli_rep is not None:
vincentmr marked this conversation as resolved.
Show resolved Hide resolved
return self._pauli_sentence(observable.pauli_rep, wires_map)
if isinstance(observable, Tensor) or (
isinstance(observable, Prod) and not observable.has_overlapping_wires
):
if isinstance(observable, (Tensor, Prod)):
if isinstance(observable, Prod) and observable.has_overlapping_wires:
return self._hermitian_ob(observable, wires_map)
return self._tensor_ob(observable, wires_map)
if observable.name in ("Hamiltonian", "LinearCombination"):
if isinstance(observable, OP_MATH_OBS):
return self._hamiltonian(observable, wires_map)
if observable.name == "SparseHamiltonian":
if isinstance(observable, SparseHamiltonian):
return self._sparse_hamiltonian(observable, wires_map)
if isinstance(observable, (PauliX, PauliY, PauliZ, Identity, Hadamard)):
return self._named_obs(observable, wires_map)
if observable.pauli_rep is not None:
return self._pauli_sentence(observable.pauli_rep, wires_map)
# if isinstance(observable, (Prod, Sum)):
# return self._hamiltonian(observable, wires_map)
return self._hermitian_ob(observable, wires_map)

def serialize_observables(self, tape: QuantumTape, wires_map: dict = None) -> List:
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.36.0-dev23"
__version__ = "0.36.0-dev24"
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ clang-tidy~=16.0
clang-format~=16.0
isort==5.13.2
click==8.0.4
cmake==3.28.4
cmake
custatevec-cu12
pylint
10 changes: 2 additions & 8 deletions tests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,14 +1008,8 @@ def f(params1, params2):
qml.RY(tf.cos(params2), wires=[0])
return qml.expval(qml.PauliZ(0))

if dev._new_API:
# tf expects float32 with new device API
tf_r_dtype = tf.float32
h = 2e-3
tol = 1e-3
else:
tf_r_dtype = tf.float32 if dev.dtype == np.complex64 else tf.float64
tol, h = get_tolerance_and_stepsize(dev, step_size=True)
tf_r_dtype = tf.float32 if dev.dtype == np.complex64 else tf.float64
tol, h = get_tolerance_and_stepsize(dev, step_size=True)

params1 = tf.Variable(0.3, dtype=tf_r_dtype)
params2 = tf.Variable(0.4, dtype=tf_r_dtype)
Expand Down
Loading
Loading