Skip to content

Commit

Permalink
Add LightningQubit2 class (#607)
Browse files Browse the repository at this point in the history
* lightning interface for new device api

* Auto update version

* Merge Master

* update version

* update version

* update lightning_qubit_2

* add LightningQubit2 to init

* add LightningStateVector class

* add LightningMeasurements class

* add new QuantumScriptSerializer class

* allow lightning.qubit2 to be tested within our suite

* add tests and CI workflow for lightning_qubit_2

* update CI

* update CI

* add wire mapping, black

* add tests for custom wires

* add tests for custom wires

* add review suggestions

* format

* remove python class to reverse order of PRs

* update simulate to get a LightningStateVector

* add reset state

* update simulate

* update docs

* add Result import

* Update pennylane_lightning/lightning_qubit/_measurements.py

Co-authored-by: Christina Lee <[email protected]>

* Update pennylane_lightning/lightning_qubit/_measurements.py

Co-authored-by: Christina Lee <[email protected]>

* fix reset state

* Update pennylane_lightning/lightning_qubit/_state_vector.py

Co-authored-by: Christina Lee <[email protected]>

* Update pennylane_lightning/lightning_qubit/_state_vector.py

Co-authored-by: Christina Lee <[email protected]>

* Update pennylane_lightning/lightning_qubit/_state_vector.py

Co-authored-by: Christina Lee <[email protected]>

* remove LightningQubit2 references

* remove unnecessary modules

* merging Serializer classes

* update serialize tests

* update measurements with new serialize class

* remove outdated test

* remove obsolete tests

* remove unused dtype input from simulate

* update measurements

* update state_vector

* update lightning_qubit2

* format

* pylint

* Update pennylane_lightning/lightning_qubit/_state_vector.py

Co-authored-by: Christina Lee <[email protected]>

* remove old comment

* some review suggestions

* Auto update version

* remove print

* update state vector class

* add state vector class tests

* adding measurement tests

* update state vector and tests

* move and rename test files, and format

* Auto update version

* skip measurements class for other devices and in the absence of binaries

* format

* update measurements class

* expand measurement class testing

* garbage collection

* typo

* update coverage and StateVector class

* expand measurements class coverage

* Auto update version

* add coverage for n-controlled operations

* add map to standard wires to get_final_state for safety

* update jax config import

* Auto update version

* trigger CI

* update state vector class and tests for improved coverage

* update measurement class tests

* update dev version

* remove device definition

* update dev version

* clean test_measurements_class.py

* isort+black

* review suggestion

* fix docs

* increase tolerance

* Auto update version

* isort

* update dev version

* add LightningAdjointJacobian class

* add unit tests for the LightningAdjointJacobian class

* format

* add changelog for PR #613

* update changelog

* update adjoint Jacobian

* codefactor

* fix processing_fn_expval

* make a proper new_tape

* trigger CI

* Update .github/CHANGELOG.md

Co-authored-by: Vincent Michaud-Rioux <[email protected]>

* Update pennylane_lightning/lightning_qubit/_adjoint_jacobian.py

Co-authored-by: Vincent Michaud-Rioux <[email protected]>

* Update pennylane_lightning/lightning_qubit/_adjoint_jacobian.py

Co-authored-by: Vincent Michaud-Rioux <[email protected]>

* Add probs support.

* Add double-obs tests.

* Add qml.var support.

* Add probs support.

* Add measurement tests with wires.

* pytest.skip tests

* Fix format

* update

* adding tests from add-simulate branch

* merge conflicts

* create state vector on initialization

* remove import of modifier from lightning

* Update pennylane_lightning/lightning_qubit/lightning_qubit2.py

* minor test updates

* register with setup.py, state vector fixes

* add LightningQubit2 to init and format

* add cpp binary available variable

* reduce dependency on DefaultQubit for tests

* update LightningQubit2

* Fixing rebase artifacts

* remove adjoint diff support from supports derivatives

* [skip ci] Added skeleton file for LQ2 unit tests

* Lightning qubit2 upgrade api (#628)

* update

* adding tests from add-simulate branch

* merge conflicts

* create state vector on initialization

* remove import of modifier from lightning

* Update pennylane_lightning/lightning_qubit/lightning_qubit2.py

* minor test updates

* register with setup.py, state vector fixes

* add LightningQubit2 to init and format

* add cpp binary available variable

* Auto update version

* reduce dependency on DefaultQubit for tests

* update LightningQubit2

* Introduce _new_API and fix/skip few tests.

* Fix few more tests.

* Skip shots, adjoint, vjp with new API.

* Fix no-bin interface.

* Remove duplicate class data.

* Include LQ2 in linux ests.

* --cov-append

---------

Co-authored-by: albi3ro <[email protected]>
Co-authored-by: AmintorDusko <[email protected]>
Co-authored-by: Dev version update bot <github-actions[bot]@users.noreply.github.com>

* Added init tests; Added skeleton tests for helpers

* Resolving rebase artifacts

* Added tests; integrated jacobian

* Update pennylane_lightning/lightning_qubit/lightning_qubit2.py

Co-authored-by: Amintor Dusko <[email protected]>

* Auto update version

* Small update to simulate_and_jacobian

* Added execution tests; TODO: diff tests

* Added derivative tests; TODO: tape batch tests

* Finished adding LQ2 tests; TODO: Add var, probs tests

* Minor test change

* Added var tests; TODO: add probs tests

* Auto update version

* Added helpers to probs test file

* Fixing tests

* Fixed no trainable params test

* Fixing test collection for new API

* Updated apply tests, made LQ2 ops/obs attributes instead of properties

* Fixed comparison,expval,gate tests

* Added tests; Hamiltonian diff tests are failing

* Updated tests for mode adjoint cases

* Added more observables; stopped skipping adj-jac tests

* Auto update version

* Trigger CI

* Fixed tests; added docstrings

* Linting; formatting

* Updated tests per code review

* Linting

* Fixed gate tests

* Trigger CI

* Trigger CI

* Fixed adj-jac tests

* Tidying up

* Fixed tf/jax adj-jac tests

* Apply suggestions from code review

Co-authored-by: Amintor Dusko <[email protected]>

* Adding coverage; addressing code review

* Minor updates per code review

* Auto update version

* Skipping finite shots measurements tests

* Update PL requirements; add LQ2 to test without binaries

* Apply suggestions from code review

Co-authored-by: Amintor Dusko <[email protected]>

* Upadte unnecessarily skipped tests

* Auto update version

* [skip ci] Skip CI

* Removing overlap test

* Added dtype to old and new API; updating tests

* Added Fallback device dtype property

* Updated no binary tests

* Auto update version

* [skip ci] Skip CI

* Trigger CI

* Running isort

* Trigger CI

* Fixed LQ import in MP test; changed docstrings to inline comments

* Update tests to use new API

* Add changelog entry

* Update changelog entry

* Apply suggestions from code review

Co-authored-by: Ali Asadi <[email protected]>

* Added TODOs; Updated derivative tests to use batch_obs

---------

Co-authored-by: Dev version update bot <github-actions[bot]@users.noreply.github.com>
Co-authored-by: AmintorDusko <[email protected]>
Co-authored-by: Amintor Dusko <[email protected]>
Co-authored-by: Vincent Michaud-Rioux <[email protected]>
Co-authored-by: Vincent Michaud-Rioux <[email protected]>
Co-authored-by: Mudit Pandey <[email protected]>
Co-authored-by: Ali Asadi <[email protected]>
  • Loading branch information
8 people authored Mar 15, 2024
1 parent ad04e24 commit a3071d3
Show file tree
Hide file tree
Showing 30 changed files with 2,216 additions and 143 deletions.
6 changes: 5 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* Add LightningAdjointJacobian to support `lightning.qubit2`.
[(#631)](https://github.com/PennyLaneAI/pennylane-lightning/pull/631)

* Add `lightning.qubit2` device which uses the new device API.
[(#607)](https://github.com/PennyLaneAI/pennylane-lightning/pull/607)
[(#628)](https://github.com/PennyLaneAI/pennylane-lightning/pull/628)

### Breaking changes

### Improvements
Expand All @@ -32,7 +36,7 @@

This release contains contributions from (in alphabetical order):

Ali Asadi, Amintor Dusko, Vincent Michaud-Rioux
Ali Asadi, Amintor Dusko, Christina Lee, Vincent Michaud-Rioux, Mudit Pandey

---

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ jobs:
cd main/
DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"`
PL_DEVICE=${DEVICENAME} python -m pytest tests/ $COVERAGE_FLAGS
PL_DEVICE=lightning_qubit2 python -m pytest tests/ $COVERAGE_FLAGS --cov-append
pl-device-test --device ${DEVICENAME} --skip-ops --shots=20000 $COVERAGE_FLAGS --cov-append
pl-device-test --device ${DEVICENAME} --shots=None --skip-ops $COVERAGE_FLAGS --cov-append
mv .coverage .coverage-${{ github.job }}-${{ matrix.pl_backend }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests_without_binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ jobs:
cd main/
DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"`
PL_DEVICE=${DEVICENAME} python -m pytest tests/ $COVERAGE_FLAGS
if [ ${{ matrix.pl_backend }} == "lightning_qubit" ]; then PL_DEVICE=lightning_qubit2 python -m pytest tests/ $COVERAGE_FLAGS --cov-append; fi
pl-device-test --device ${DEVICENAME} --skip-ops --shots=20000 $COVERAGE_FLAGS --cov-append
pl-device-test --device ${DEVICENAME} --shots=None --skip-ops $COVERAGE_FLAGS --cov-append
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(
self.use_csingle = use_csingle
self.device_name = device_name
self.split_obs = split_obs
if device_name == "lightning.qubit":
if device_name in ("lightning.qubit", "lightning.qubit2"):
try:
import pennylane_lightning.lightning_qubit_ops as lightning_ops
except ImportError as exception:
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-dev9"
__version__ = "0.36.0-dev10"
12 changes: 12 additions & 0 deletions pennylane_lightning/core/lightning_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class LightningBase(QubitDevice):
author = "Xanadu Inc."
short_name = "lightning.base"
_CPP_BINARY_AVAILABLE = True
_new_API = False

def __init__(
self,
Expand All @@ -80,6 +81,11 @@ def __init__(
super().__init__(wires, shots=shots, r_dtype=r_dtype, c_dtype=c_dtype)
self._batch_obs = batch_obs

@property
def dtype(self):
"""State vector complex data type."""
return self.C_DTYPE

@property
def stopping_condition(self):
""".BooleanFn: Returns the stopping condition for the device. The returned
Expand Down Expand Up @@ -396,6 +402,7 @@ class LightningBaseFallBack(DefaultQubitLegacy): # pragma: no cover
version = __version__
author = "Xanadu Inc."
_CPP_BINARY_AVAILABLE = False
_new_API = False

def __init__(self, wires, *, c_dtype=np.complex128, **kwargs):
if c_dtype is np.complex64:
Expand All @@ -410,3 +417,8 @@ def __init__(self, wires, *, c_dtype=np.complex128, **kwargs):
def state_vector(self):
"""Returns a handle to the statevector."""
return self._state

@property
def dtype(self):
"""State vector complex data type."""
return self.C_DTYPE
1 change: 1 addition & 0 deletions pennylane_lightning/lightning_qubit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
from pennylane_lightning.core import __version__

from .lightning_qubit import LightningQubit
from .lightning_qubit2 import LightningQubit2
19 changes: 11 additions & 8 deletions pennylane_lightning/lightning_qubit/_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"""
from os import getenv
from typing import List
from warnings import warn

import numpy as np
import pennylane as qml
Expand Down Expand Up @@ -193,11 +192,10 @@ def calculate_jacobian(self, tape: QuantumTape):
"""

if tape.shots:
warn(
raise QuantumFunctionError(
"Requested adjoint differentiation to be computed with finite shots. "
"The derivative is always exact when using the adjoint "
"differentiation method.",
UserWarning,
"differentiation method."
)

tape_return_type = self._get_return_type(tape.measurements)
Expand All @@ -208,6 +206,12 @@ def calculate_jacobian(self, tape: QuantumTape):
if tape_return_type is State:
raise QuantumFunctionError("This method does not support statevector return type. ")

if any(m.return_type is not Expectation for m in tape.measurements):
raise QuantumFunctionError(
"Adjoint differentiation method does not support expectation return type "
"mixed with other return types"
)

processed_data = self._process_jacobian_tape(tape)

if not processed_data: # training_params is empty
Expand Down Expand Up @@ -271,12 +275,11 @@ def calculate_vjp(self, tape: QuantumTape, grad_vec):
Returns:
The vector-Jacobian products of a tape.
"""
if tape.shots is not None:
warn(
if tape.shots:
raise QuantumFunctionError(
"Requested adjoint differentiation to be computed with finite shots. "
"The derivative is always exact when using the adjoint differentiation "
"method.",
UserWarning,
"method."
)

measurements = tape.measurements
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_qubit/_state_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ def _apply_lightning_controlled(self, operation):
method(control_wires, control_values, target_wires, inv, param)
else: # apply gate as an n-controlled matrix
method = getattr(state, "applyControlledMatrix")
target_wires = self.wires.indices(operation.target_wires)
method(
qml.matrix(operation.base),
control_wires,
Expand Down
Loading

0 comments on commit a3071d3

Please sign in to comment.