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

Improve the stopping condition method #386

Merged
merged 7 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

### Improvements

* Improve the stopping condition method.
[(#386)](https://github.com/PennyLaneAI/pennylane-lightning/pull/386)

### Documentation

### Bug fixes
Expand All @@ -14,6 +17,8 @@

This release contains contributions from (in alphabetical order):

Amintor Dusko

---

# Release 0.27.0
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.28.0-dev"
__version__ = "0.28.1-dev"
14 changes: 7 additions & 7 deletions pennylane_lightning/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
"""Tests the circuit Ansatz with a QChem Hamiltonian produces correct results"""

H, qubits = qml.qchem.molecular_hamiltonian(
Expand All @@ -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")
Expand Down
83 changes: 83 additions & 0 deletions tests/test_execute.py
Original file line number Diff line number Diff line change
@@ -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"))
mlxd marked this conversation as resolved.
Show resolved Hide resolved
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]],
AmintorDusko marked this conversation as resolved.
Show resolved Hide resolved
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)