diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 2590633a76..1c6b89eac9 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -9,15 +9,21 @@ * Improve Lightning package structure for external use as a C++ library. [(#369)](https://github.com/PennyLaneAI/pennylane-lightning/pull/369) +* Improve the stopping condition method. +[(#386)](https://github.com/PennyLaneAI/pennylane-lightning/pull/386) + ### Documentation ### Bug fixes +- Pin CMake to 3.24.x in wheel-builder to avoid Python not found error in CMake 3.25, when building wheels for PennyLane-Lightning-GPU. +[(#387)](https://github.com/PennyLaneAI/pennylane-lightning/pull/387) + ### Contributors This release contains contributions from (in alphabetical order): -Lee J. O'Riordan +Amintor Dusko, Lee J. O'Riordan --- diff --git a/.github/workflows/wheel_linux_aarch64.yml b/.github/workflows/wheel_linux_aarch64.yml index 2cf56d426d..7055c018cc 100644 --- a/.github/workflows/wheel_linux_aarch64.yml +++ b/.github/workflows/wheel_linux_aarch64.yml @@ -159,7 +159,7 @@ jobs: # Python build settings CIBW_BEFORE_BUILD: | cat /etc/yum.conf | sed "s/\[main\]/\[main\]\ntimeout=5/g" > /etc/yum.conf - pip install ninja cmake + pip install ninja cmake~=3.24.0 CIBW_MANYLINUX_AARCH64_IMAGE: manylinux2014 diff --git a/.github/workflows/wheel_linux_ppc64le.yml b/.github/workflows/wheel_linux_ppc64le.yml index 5c206c8bfc..7f5bd5dc36 100644 --- a/.github/workflows/wheel_linux_ppc64le.yml +++ b/.github/workflows/wheel_linux_ppc64le.yml @@ -159,7 +159,7 @@ jobs: # Python build settings CIBW_BEFORE_BUILD: | cat /etc/yum.conf | sed "s/\[main\]/\[main\]\ntimeout=5/g" > /etc/yum.conf - pip install ninja cmake + pip install ninja cmake~=3.24.0 CIBW_MANYLINUX_PPC64LE_IMAGE: manylinux2014 diff --git a/.github/workflows/wheel_linux_x86_64.yml b/.github/workflows/wheel_linux_x86_64.yml index f98dce0396..73e196f6cb 100644 --- a/.github/workflows/wheel_linux_x86_64.yml +++ b/.github/workflows/wheel_linux_x86_64.yml @@ -164,7 +164,7 @@ jobs: # Python build settings CIBW_BEFORE_BUILD: | cat /etc/yum.conf | sed "s/\[main\]/\[main\]\ntimeout=5/g" > /etc/yum.conf - pip install ninja cmake + pip install ninja cmake~=3.24.0 yum clean all -y yum install centos-release-scl-rh -y yum install devtoolset-11-gcc-c++ -y diff --git a/.github/workflows/wheel_macos_arm64.yml b/.github/workflows/wheel_macos_arm64.yml index c5f0d7a445..eca5bb1177 100644 --- a/.github/workflows/wheel_macos_arm64.yml +++ b/.github/workflows/wheel_macos_arm64.yml @@ -88,7 +88,7 @@ jobs: # Python build settings CIBW_BEFORE_BUILD: | - pip install pybind11 ninja cmake + pip install pybind11 ninja cmake~=3.24.0 # Testing of built wheels CIBW_TEST_REQUIRES: numpy~=1.21 scipy pytest pytest-cov pytest-mock flaky diff --git a/.github/workflows/wheel_macos_x86_64.yml b/.github/workflows/wheel_macos_x86_64.yml index d610abdf0a..35e4964aa1 100644 --- a/.github/workflows/wheel_macos_x86_64.yml +++ b/.github/workflows/wheel_macos_x86_64.yml @@ -160,7 +160,7 @@ jobs: # Python build settings CIBW_BEFORE_BUILD: | - pip install pybind11 ninja cmake + pip install pybind11 ninja cmake~=3.24.0 # Testing of built wheels CIBW_TEST_REQUIRES: numpy~=1.21 scipy pytest pytest-cov pytest-mock flaky diff --git a/.github/workflows/wheel_win_x86_64.yml b/.github/workflows/wheel_win_x86_64.yml index fdf36aa9db..bb3f13e071 100644 --- a/.github/workflows/wheel_win_x86_64.yml +++ b/.github/workflows/wheel_win_x86_64.yml @@ -145,7 +145,7 @@ jobs: # Python build settings CIBW_BEFORE_BUILD: | - pip install pybind11 cmake + pip install pybind11 cmake~=3.24.0 # Testing of built wheels CIBW_TEST_REQUIRES: numpy~=1.21 scipy pytest pytest-cov pytest-mock flaky diff --git a/pennylane_lightning/_version.py b/pennylane_lightning/_version.py index fb1f393a13..a4fe7dca8f 100644 --- a/pennylane_lightning/_version.py +++ b/pennylane_lightning/_version.py @@ -17,4 +17,4 @@ """ -__version__ = "0.28.0-dev2" +__version__ = "0.28.0-dev4" diff --git a/pennylane_lightning/lightning_qubit.py b/pennylane_lightning/lightning_qubit.py index 6ee8da254f..04f69d2b45 100644 --- a/pennylane_lightning/lightning_qubit.py +++ b/pennylane_lightning/lightning_qubit.py @@ -197,13 +197,13 @@ def stopping_condition(self): and observable) and returns ``True`` if supported by the device.""" def accepts_obj(obj): - if obj.name == "QFT" and len(obj.wires) >= 10: - return False - if obj.name == "GroverOperator" and len(obj.wires) >= 13: - return False - if getattr(obj, "has_matrix", False): - return not (qml.operation.is_trainable(obj)) - return obj.name in self.observables.union(self.operations) + if obj.name == "QFT" and len(obj.wires) < 10: + return True + if obj.name == "GroverOperator" and len(obj.wires) < 13: + return True + return (not isinstance(obj, qml.tape.QuantumTape)) and getattr( + self, "supports_operation", lambda name: False + )(obj.name) return qml.BooleanFn(accepts_obj) diff --git a/tests/test_adjoint_jacobian.py b/tests/test_adjoint_jacobian.py index cab9326b2f..65d59ebac4 100644 --- a/tests/test_adjoint_jacobian.py +++ b/tests/test_adjoint_jacobian.py @@ -855,7 +855,7 @@ def circuit_ansatz(params, wires): @pytest.mark.skipif(not lq._CPP_BINARY_AVAILABLE, reason="Lightning binary required") -def test__tape_qchem(tol): +def test_tape_qchem(tol): """Tests the circuit Ansatz with a QChem Hamiltonian produces correct results""" H, qubits = qml.qchem.molecular_hamiltonian( @@ -866,10 +866,10 @@ def circuit(params): circuit_ansatz(params, wires=range(4)) return qml.expval(H) - params = np.arange(30) * 0.111 + params = np.linspace(0, 29, 30) * 0.111 - dev_lq = qml.device("lightning.qubit", wires=4) - dev_dq = qml.device("default.qubit", wires=4) + dev_lq = qml.device("lightning.qubit", wires=qubits) + dev_dq = qml.device("default.qubit", wires=qubits) circuit_lq = qml.QNode(circuit, dev_lq, diff_method="adjoint") circuit_dq = qml.QNode(circuit, dev_dq, diff_method="parameter-shift") diff --git a/tests/test_execute.py b/tests/test_execute.py new file mode 100644 index 0000000000..47889e5b0f --- /dev/null +++ b/tests/test_execute.py @@ -0,0 +1,83 @@ +# Copyright 2022 Xanadu Quantum Technologies Inc. + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Integration tests for the ``execute`` method of LightningQubit. +""" +import pytest + +import pennylane as qml +from pennylane import numpy as np + + +@pytest.mark.parametrize("diff_method", ("param_shift", "finite_diff")) +class TestQChem: + """Test tapes returning the expectation values of a Hamiltonian, with a qchem workflow.""" + + def test_VQE_gradients(self, diff_method, tol): + """Test if the VQE procedure returns the expected gradients.""" + + symbols = ["H", "H"] + + geometry = np.array( + [[-0.676411907, 0.000000000, 0.000000000], [0.676411907, 0.000000000, 0.000000000]], + requires_grad=False, + ) + + mol = qml.qchem.Molecule(symbols, geometry, basis_name="STO-3G") + + H, qubits = qml.qchem.molecular_hamiltonian( + symbols, + geometry, + basis="STO-3G", + ) + + singles, doubles = qml.qchem.excitations(mol.n_electrons, len(H.wires)) + + excitations = singles + doubles + + num_params = len(singles + doubles) + params = np.zeros(num_params, requires_grad=True) + + hf_state = qml.qchem.hf_state(mol.n_electrons, qubits) + + with qml.tape.QuantumTape() as tape: + qml.BasisState(hf_state, wires=range(qubits)) + + for i, excitation in enumerate(excitations): + if len(excitation) == 4: + qml.DoubleExcitation(params[i], wires=excitation) + elif len(excitation) == 2: + qml.SingleExcitation(params[i], wires=excitation) + + qml.expval(H) + + num_params = len(excitations) + tape.trainable_params = np.linspace(1, num_params, num_params, dtype=int).tolist() + + gradient_tapes, fn_grad = getattr(qml.gradients, diff_method)(tape) + + dev_l = qml.device("lightning.qubit", wires=qubits) + dev_d = qml.device("default.qubit", wires=qubits) + + def dev_l_execute(t): + dev = qml.device("lightning.qubit", wires=qubits) + return dev.execute(t) + + grad_dev_l = fn_grad([dev_l_execute(t) for t in gradient_tapes]) + grad_qml_l = fn_grad(qml.execute(gradient_tapes, dev_l)) + + grad_qml_d = fn_grad(qml.execute(gradient_tapes, dev_d)) + + assert np.allclose(grad_dev_l, grad_qml_l, tol) + assert np.allclose(grad_dev_l, grad_qml_d, tol)