Skip to content

Commit

Permalink
Update LightningBaseFallBack to raise an error instead of falling b…
Browse files Browse the repository at this point in the history
…ack to default.qubit (#689)

* Initial commit.

* Update changelog

* Auto update version

* trigger ci

* Unused import.

* Auto update version

* trigger ci

* Fix imports.

* Auto update version

* Fix LightningException

* make format

* Auto update version from '0.36.0-dev34' to '0.36.0-dev35'

* Auto update version from '0.36.0-dev35' to '0.36.0-dev36'

* Auto update version from '0.36.0-dev36' to '0.36.0-dev37'

* Make python -m pip install consistent.

* Fix ImportError

* Auto update version from '0.36.0-dev37' to '0.36.0-dev38'

* --force-reinstall

* Auto update version from '0.36.0-dev38' to '0.36.0-dev39'

* Remove --force-reinstall

* Auto update version from '0.36.0-dev39' to '0.36.0-dev40'

* Auto update version from '0.36.0-dev42' to '0.36.0-dev43'

* Auto update version from '0.36.0-dev43' to '0.36.0-dev44'

* Spec CMAKE_CXX_COMPILER

* Fix quotes.

* Remove quotes

* Hardcode g++-11

* use env.COMPILER

* which g++

* Revert

* Downgrade pip

* Pin libomp.

* Revert wheel_linux

* Upload artifacts.

* Comment upload trigger

* Install pip==23

* Revert libomp

* try testing outside cibuildwheel

* yum install devtools

* yum install devtools

* Do not install python

* Revert changes

* Don't editable install.

* Not edit install

* Not edit install

* Fix cov path

* Explicit mcm tests path

* Fix qml.QuantumFunctionError error messages.

---------

Co-authored-by: Dev version update bot <github-actions[bot]@users.noreply.github.com>
Co-authored-by: ringo-but-quantum <github-ringo-but-quantum@xanadu.ai>
Co-authored-by: AmintorDusko <amintor_dusko@hotmail.com>
  • Loading branch information
4 people authored Apr 30, 2024
1 parent b5402dd commit 34877ff
Show file tree
Hide file tree
Showing 28 changed files with 1,599 additions and 1,732 deletions.
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

### Breaking changes

* Update `lightning.gpu` and `lightning.kokkos` to raise an error instead of falling back to `default.qubit`.
[(#689)](https://github.com/PennyLaneAI/pennylane-lightning/pull/689)

* Add `paths` directives to test workflows to avoid running tests that cannot be impacted by changes.
[(#699)](https://github.com/PennyLaneAI/pennylane-lightning/pull/699)
[(#695)](https://github.com/PennyLaneAI/pennylane-lightning/pull/695)
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests_linux_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ jobs:
mv .coverage .coverage-${{ github.job }}-${{ matrix.pl_backend }}
# TODO: Remove this if-cond with release v0.36.0
if [ -f tests/test_native_mcm.py ]; then
OMP_NUM_THREADS=1 PL_DEVICE=${DEVICENAME} python -m pytest -n auto tests/ -k "test_native_mcm" $COVERAGE_FLAGS --cov-append
OMP_NUM_THREADS=1 PL_DEVICE=${DEVICENAME} python -m pytest -n auto tests/test_native_mcm.py $COVERAGE_FLAGS --cov-append
fi
- name: Install all backend devices
Expand Down Expand Up @@ -418,7 +418,7 @@ jobs:
- name: Combine coverage files
run: |
python -m pip install coverage
python -m coverage combine .coverage-python*
python -m coverage combine .coverage*
# Added cov xml -i to ignore "No source for code" random errors
# https://stackoverflow.com/questions/2386975/no-source-for-code-message-in-coverage-py
python -m coverage xml -i -o coverage-${{ github.job }}.xml
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheel_linux_aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ jobs:

CIBW_BEFORE_TEST: |
python -m pip install -r requirements-tests.txt
if ${{ matrix.pl_backend == 'lightning_kokkos'}}; then SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" pip install -e . -vv; fi
if ${{ matrix.pl_backend == 'lightning_kokkos'}}; then SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" python -m pip install . -vv; fi
CIBW_TEST_COMMAND: |
DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"`
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheel_linux_x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ jobs:
CIBW_BEFORE_TEST: |
python -m pip install -r requirements-tests.txt
if ${{ matrix.pl_backend == 'lightning_kokkos'}}; then SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" pip install -e . -vv; fi
if ${{ matrix.pl_backend == 'lightning_kokkos'}}; then SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" python -m pip install . -vv; fi
CIBW_TEST_COMMAND: |
DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"`
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheel_macos_arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
CIBW_BEFORE_TEST: |
python -m pip install -r requirements-tests.txt
if ${{ matrix.pl_backend == 'lightning_kokkos'}}; then SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" pip install -e . -vv; fi
if ${{ matrix.pl_backend == 'lightning_kokkos'}}; then SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" python -m pip install . -vv; fi
CIBW_TEST_COMMAND: |
DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"`
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheel_macos_x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ jobs:

CIBW_BEFORE_TEST: |
python -m pip install -r requirements-tests.txt
if ${{ matrix.pl_backend == 'lightning_kokkos'}}; then SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" pip install -e . -vv; fi
if ${{ matrix.pl_backend == 'lightning_kokkos'}}; then SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" python -m pip install . -vv; fi
CIBW_TEST_COMMAND: |
DEVICENAME=`echo ${{ matrix.pl_backend }} | sed "s/_/./g"`
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheel_win_x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ jobs:
python -m pip install setuptools
python -m pip install -r requirements-tests.txt
if (${{ matrix.pl_backend == 'lightning_kokkos'}}) {
SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" python -m pip install -e . -vv
SKIP_COMPILATION=True PL_BACKEND="lightning_qubit" python -m pip install . -vv
}
pushd wheelhouse
$wheels = Get-ChildItem "./" -Filter *.whl
Expand Down
55 changes: 28 additions & 27 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
venv/
kokkos/
prototypes/
doc/_build/
doc/code/api/
PennyLane_Lightning.egg-info/
PennyLane_Lightning_Kokkos.egg-info/
build/
build_lightning_*/
Build/
BuildCov/
BuildGBench/
BuildTests/
BuildTidy/
dist/
tests/__pycache__/
.idea
*.ipynp
*.ipynb_checkpoints
__pycache__
.pytest_cache/
coverage_html_report/
.cache/*
.coverage
*.so
cpptests
*.o
.DS_Store
.cache/*
.idea
.pytest_cache/
.vscode/
.ycm_extra_conf.py
*.ipynb_checkpoints
*.ipynp
*.o
*.so
/.vs
/PennyLane_Lightning*
/pennylane_lightning/.vs
/pennylane_lightning/*.pyd
/pennylane_lightning/src/Kokkos/
/pennylane_lightning/src/GBenchmarks/
/PennyLane_Lightning*
/pennylane_lightning/src/Kokkos/
build_lightning_*/
build/
Build/
BuildCov/
BuildGBench/
BuildTests/
BuildTidy/
coverage_html_report/
cpptests
dist/
doc/_build/
doc/code/api/
kokkos/
PennyLane_Lightning_Kokkos.egg-info/
PennyLane_Lightning.egg-info/
pennylane_lightning/core/src/utils/config.h
prototypes/
tests/__pycache__/
venv/
19 changes: 6 additions & 13 deletions mpitests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
from pennylane import qnode
from scipy.stats import unitary_group

if not ld._CPP_BINARY_AVAILABLE:
pytest.skip("No binary module found. Skipping.", allow_module_level=True)

I, X, Y, Z = (
np.eye(2),
qml.PauliX.compute_matrix(),
Expand Down Expand Up @@ -109,12 +112,10 @@ def test_not_expval(self, dev):
qml.RX(0.1, wires=0)
qml.state()

if device_name == "lightning.gpu" and ld._CPP_BINARY_AVAILABLE:
if device_name == "lightning.gpu":
message = "Adjoint differentiation does not support State measurements."
elif ld._CPP_BINARY_AVAILABLE:
message = "This method does not support statevector return type."
else:
message = "Adjoint differentiation method does not support measurement StateMP"
message = "Adjoint differentiation method does not support measurement StateMP."
with pytest.raises(
qml.QuantumFunctionError,
match=message,
Expand Down Expand Up @@ -144,7 +145,6 @@ def test_empty_measurements(self, dev):
jac = dev.adjoint_jacobian(tape)
assert len(jac) == 0

@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
def test_unsupported_op(self, dev):
"""Test if a QuantumFunctionError is raised for an unsupported operation, i.e.,
multi-parameter operations that are not qml.Rot"""
Expand All @@ -159,7 +159,6 @@ def test_unsupported_op(self, dev):
):
dev.adjoint_jacobian(tape)

@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
def test_proj_unsupported(self, dev):
"""Test if a QuantumFunctionError is raised for a Projector observable"""
with qml.tape.QuantumTape() as tape:
Expand Down Expand Up @@ -332,7 +331,6 @@ def test_multiple_rx_gradient_expval_hermitian(self, tol, dev):
qubit_ops = [getattr(qml, name) for name in qml.ops._qubit__ops__] # pylint: disable=no-member
ops = {qml.RX, qml.RY, qml.RZ, qml.PhaseShift, qml.CRX, qml.CRY, qml.CRZ, qml.Rot}

@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
def test_multiple_rx_gradient_expval_hamiltonian(self, tol, dev):
"""Tests that the gradient of multiple RX gates in a circuit yields the correct result
with Hermitian observable
Expand Down Expand Up @@ -522,7 +520,6 @@ def test_gradient_gate_with_multiple_parameters_hermitian(self, dev):
# the different methods agree
assert np.allclose(grad_D, grad_F, atol=tol, rtol=0)

@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
def test_gradient_gate_with_multiple_parameters_hamiltonian(self, dev):
"""Tests that gates with multiple free parameters yield correct gradients."""
x, y, z = [0.5, 0.3, -0.7]
Expand Down Expand Up @@ -601,7 +598,6 @@ def test_provide_starting_state(self, tol, dev):
dM2 = dev.adjoint_jacobian(tape, starting_state=state_vector)
assert np.allclose(dM1, dM2, atol=tol, rtol=0)

@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
def test_provide_wrong_starting_state(self, dev):
"""Tests raise an exception when provided starting state mismatches."""
x, y, z = [0.5, 0.3, -0.7]
Expand All @@ -624,7 +620,6 @@ def test_provide_wrong_starting_state(self, dev):
device_name == "lightning.gpu",
reason="Adjoint differentiation does not support State measurements.",
)
@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
def test_state_return_type(self, dev):
"""Tests raise an exception when the return type is State"""
with qml.tape.QuantumTape() as tape:
Expand All @@ -635,7 +630,7 @@ def test_state_return_type(self, dev):

with pytest.raises(
qml.QuantumFunctionError,
match="This method does not support statevector return type.",
match="Adjoint differentiation method does not support measurement StateMP.",
):
dev.adjoint_jacobian(tape)

Expand Down Expand Up @@ -675,7 +670,6 @@ def circ(x):
):
qml.grad(circ)(0.1)

@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
def test_qnode(self, mocker, dev):
"""Test that specifying diff_method allows the adjoint method to be selected"""
args = np.array([0.54, 0.1, 0.5], requires_grad=True)
Expand Down Expand Up @@ -766,7 +760,6 @@ def cost(p1, p2):

assert np.allclose(grad_D[0], expected, atol=tol, rtol=0)

@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
def test_gradient_repeated_gate_parameters(self, mocker, dev):
"""Tests that repeated use of a free parameter in a multi-parameter gate yields correct
gradients."""
Expand Down
35 changes: 6 additions & 29 deletions pennylane_lightning/core/lightning_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import numpy as np
import pennylane as qml
from pennylane import BasisState, QubitDevice, StatePrep
from pennylane.devices import DefaultQubitLegacy
from pennylane.measurements import Expectation, MeasurementProcess, State
from pennylane.operation import Operation, Tensor
from pennylane.ops import Prod, Projector, SProd, Sum
Expand Down Expand Up @@ -71,6 +70,12 @@ def __init__(
shots=None,
batch_obs=False,
):
if not self._CPP_BINARY_AVAILABLE:
raise ImportError(
f"Pre-compiled binaries for {self.short_name} are not available. "
"To manually compile from source, follow the instructions at "
"https://pennylane-lightning.readthedocs.io/en/latest/installation.html."
)
if c_dtype is np.complex64:
r_dtype = np.float32
self.use_csingle = True
Expand Down Expand Up @@ -446,31 +451,3 @@ def processing_fns(tapes):
return vjps

return processing_fns


class LightningBaseFallBack(DefaultQubitLegacy): # pragma: no cover
# pylint: disable=missing-class-docstring, too-few-public-methods
pennylane_requires = ">=0.34"
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:
r_dtype = np.float32
elif c_dtype is np.complex128:
r_dtype = np.float64
else:
raise TypeError(f"Unsupported complex type: {c_dtype}")
super().__init__(wires, r_dtype=r_dtype, c_dtype=c_dtype, **kwargs)

@property
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
Loading

0 comments on commit 34877ff

Please sign in to comment.