Skip to content

Commit

Permalink
Improved code coverage (#2537)
Browse files Browse the repository at this point in the history
* improve code coverage

* Update doc/releases/changelog-dev.md

* Update tests/ops/functions/test_generator.py

Co-authored-by: antalszava <antalszava@gmail.com>

Co-authored-by: antalszava <antalszava@gmail.com>
  • Loading branch information
albi3ro and antalszava authored May 3, 2022
1 parent 705bfd5 commit e326486
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
* `Operator.decomposition()` is now an instance method, and no longer accepts parameters.
[(#2498)](https://github.com/PennyLaneAI/pennylane/pull/2498)

* Adds tests, adds no-coverage directives, and removes inaccessible logic to improve code coverage.
[(#2537)](https://github.com/PennyLaneAI/pennylane/pull/2537)

<h3>Bug fixes</h3>

* Fixed a bug where `QNGOptimizer` did not work with operators
Expand Down
4 changes: 2 additions & 2 deletions pennylane/drawer/mpldrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
try:
import matplotlib.pyplot as plt
from matplotlib import patches
except (ModuleNotFoundError, ImportError) as e:
except (ModuleNotFoundError, ImportError) as e: # pragma: no cover
has_mpl = False


Expand Down Expand Up @@ -247,7 +247,7 @@ class MPLDrawer:

def __init__(self, n_layers, n_wires, wire_options=None, figsize=None):

if not has_mpl:
if not has_mpl: # pragma: no cover
raise ImportError(
"Module matplotlib is required for ``MPLDrawer`` class. "
"You can install matplotlib via \n\n pip install matplotlib"
Expand Down
2 changes: 1 addition & 1 deletion pennylane/fourier/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# a hard requirement for everything in this module.
try:
from matplotlib.colors import to_rgb
except (ModuleNotFoundError, ImportError) as e:
except (ModuleNotFoundError, ImportError) as e: # pragma: no cover
raise ImportError(
"Module matplotlib is required for visualization in the Fourier module. "
"You can install matplolib via \n\n pip install matplotlib"
Expand Down
2 changes: 0 additions & 2 deletions pennylane/ops/qubit/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,6 @@ def queue(self, context=qml.QueuingContext):
except QueuingError:
o.queue(context=context)
context.update_info(o, owner=self)
except NotImplementedError:
pass

context.append(self, owns=tuple(self.ops))
return self
2 changes: 1 addition & 1 deletion tests/fourier/test_coefficients.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def test_anti_aliasing(self, circuit, inpt, degree):
"""Test that the coefficients obtained through anti-aliasing are the
same as the ones when we don't anti-alias at the correct degree."""
coeffs_regular = coefficients(circuit, len(inpt), degree, lowpass_filter=False)
coeffs_anti_aliased = coefficients(circuit, len(inpt), degree)
coeffs_anti_aliased = coefficients(circuit, len(inpt), degree, lowpass_filter=True)

assert np.allclose(coeffs_regular, coeffs_anti_aliased)

Expand Down
12 changes: 12 additions & 0 deletions tests/ops/functions/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ class DeprecatedClassOp(CustomOp):
assert gen.name == "Hermitian"
assert gen.wires.tolist() == ["a"]

def test_generator_property_old_default(self):
"""Test that if the old-style generator property is the default,
a GeneratorUndefinedError is raised and a warning is raised about the old syntax."""

class DeprecatedClassOp(CustomOp):
generator = [None, 1]

op = DeprecatedClassOp(0.5, wires="a")
with pytest.warns(UserWarning, match=r"The Operator\.generator property is deprecated"):
with pytest.raises(qml.operation.GeneratorUndefinedError):
qml.generator(op)


class TestPrefactorReturn:
"""Tests for format="prefactor". This format attempts to isolate a prefactor
Expand Down
9 changes: 7 additions & 2 deletions tests/ops/qubit/test_matrix_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,16 @@ def test_matrix_representation(self, tol):
assert np.allclose(res_static, expected, atol=tol)
assert np.allclose(res_dynamic, expected, atol=tol)

def test_error_not_unitary(self):
"""Tests that error is raised if diagonal does not lead to a unitary"""
def test_error_matrix_not_unitary(self):
"""Tests that error is raised if diagonal by `compute_matrix` does not lead to a unitary"""
with pytest.raises(ValueError, match="Operator must be unitary"):
qml.DiagonalQubitUnitary.compute_matrix(np.array([1, 2]))

def test_error_eigvals_not_unitary(self):
"""Tests that error is raised by `compute_eigvals` if diagonal does not lead to a unitary"""
with pytest.raises(ValueError, match="Operator must be unitary"):
qml.DiagonalQubitUnitary.compute_eigvals(np.array([1, 2]))

def test_jax_jit(self):
"""Test that the diagonal matrix unitary operation works
within a QNode that uses the JAX JIT"""
Expand Down

0 comments on commit e326486

Please sign in to comment.