Skip to content

Commit

Permalink
Lint tests: Directories O-Resource (#4313)
Browse files Browse the repository at this point in the history
* lint a few

* more

* more

* more

* more

* list

* more

* black

* extra lint

* fixes

* monkeypatch

* Apply suggestions from code review

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>

* lint, black

* conftest

* some default qubit

* devices

* black

* fourier partial

* math

* black

* numpy

* ops/function, some ops/qubit

* most of ops

* rest of ops

* fix

* fix

* rerun CI

* Apply suggestions from code review

* some more

* optimize, pauli..

* list

* qchem

* black

* resource, qinfo

* devices update

* bug fixes

* formatting lint

* formatting lint

* update

* fix

* update

* no self argument python version conflict

* fix

* black

* fix

* fidelity fix

* test case

* qchem of stuff

* chemist -> physicist notation

* Apply suggestions from code review

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>

---------

Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
  • Loading branch information
4 people committed Jul 6, 2023
1 parent 49d325f commit 6b6a5b3
Show file tree
Hide file tree
Showing 70 changed files with 706 additions and 801 deletions.
1 change: 1 addition & 0 deletions tests/math/test_entropies_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ def test_max_entropy_grad_tf(self, params, wires, base, check_state):
def test_max_entropy_grad_jax(self, params, wires, base, check_state, jit):
"""Test `max_entropy` differentiability with jax."""
jnp = jax.numpy
jax.config.update("jax_enable_x64", True)

params = jnp.array(params)

Expand Down
43 changes: 9 additions & 34 deletions tests/ops/functions/test_eigvals.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
"""
Unit tests for the eigvals transform
"""
# pylint: disable=too-few-public-methods
from functools import reduce

import pytest
import scipy

from gate_data import CNOT, H, I, S, X, Y, Z
import pennylane as qml
from gate_data import CNOT, H, I
from gate_data import Roty as RY
from gate_data import S, X, Y, Z
from pennylane import numpy as np
from pennylane.transforms.op_transforms import OperationTransformError

one_qubit_no_parameter = [
qml.PauliX,
Expand Down Expand Up @@ -100,7 +98,9 @@ def test_adjoint(self, op_class):
rounding_precision = 6
res = qml.eigvals(qml.adjoint(op_class))(0.54, wires=0)
expected = op_class(-0.54, wires=0).eigvals()
assert set(np.around(res, rounding_precision)) == set(np.around(res, rounding_precision))
assert set(np.around(res, rounding_precision)) == set(
np.around(expected, rounding_precision)
)

def test_ctrl(self):
"""Test that the ctrl is correctly taken into account"""
Expand All @@ -116,10 +116,10 @@ def test_tensor_product(self):

def test_hamiltonian(self):
"""Test that the matrix of a Hamiltonian is correctly returned"""
H = qml.PauliZ(0) @ qml.PauliY(1) - 0.5 * qml.PauliX(1)
ham = qml.PauliZ(0) @ qml.PauliY(1) - 0.5 * qml.PauliX(1)

with pytest.warns(UserWarning, match="the eigenvalues will be computed numerically"):
res = qml.eigvals(H)
res = qml.eigvals(ham)

expected = np.linalg.eigvalsh(reduce(np.kron, [Z, Y]) - 0.5 * reduce(np.kron, [I, X]))
assert np.allclose(res, expected)
Expand All @@ -142,7 +142,7 @@ def ansatz(x):
assert np.allclose(res, expected)

@pytest.mark.parametrize(
("row", "col", "dat", "val_ref"),
("row", "col", "dat"),
[
(
# coordinates and values of a sparse Hamiltonian computed for H2
Expand Down Expand Up @@ -173,35 +173,10 @@ def ansatz(x):
0.93441394 + 0.0j,
]
),
# eigenvalues of the same matrix computed with np.linalg.eigh(H_dense)
np.array(
[
-1.13730605,
-0.5363422,
-0.5363422,
-0.52452264,
-0.52452264,
-0.52452264,
-0.44058792,
-0.44058792,
-0.16266858,
0.0,
0.0,
0.0,
0.0,
0.2481941,
0.2481941,
0.36681148,
0.36681148,
0.49523726,
0.72004228,
0.93441394,
]
),
),
],
)
def test_sparse_hamiltonian(self, row, col, dat, val_ref):
def test_sparse_hamiltonian(self, row, col, dat):
"""Test that the eigenvalues of a sparse Hamiltonian are correctly returned"""
# N x N matrix with N = 16
h_mat = scipy.sparse.csr_matrix((dat, (row, col)), shape=(16, 16))
Expand Down
41 changes: 24 additions & 17 deletions tests/ops/functions/test_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ def test_equal_simple_op_3p1w(self, op1):
@pytest.mark.all_interfaces
def test_equal_op_remaining(self):
"""Test optional arguments are working"""
# pylint: disable=too-many-statements
wire = 0

import jax
Expand Down Expand Up @@ -1055,7 +1056,7 @@ def jax_assertion_func(x, other_tensor):
operation1 = op1(jax.numpy.array(x), wires=1)
operation2 = op1(other_tensor, wires=1)
if isinstance(other_tensor, tf.Variable):
with tf.GradientTape() as tape:
with tf.GradientTape():
assert qml.equal(
operation1, operation2, check_interface=False, check_trainability=True
)
Expand All @@ -1071,7 +1072,7 @@ def jax_assertion_func(x, other_tensor):

# TF and Autograd
# ------------------
with tf.GradientTape() as tape:
with tf.GradientTape():
assert qml.equal(
op1(tf_tensor, wires=wire),
op1(pl_tensor, wires=wire),
Expand All @@ -1081,7 +1082,7 @@ def jax_assertion_func(x, other_tensor):

# TF and Torch
# ------------------
with tf.GradientTape() as tape:
with tf.GradientTape():
assert qml.equal(
op1(tf_tensor, wires=wire),
op1(torch_tensor, wires=wire),
Expand Down Expand Up @@ -1247,15 +1248,15 @@ def test_hamiltonian_and_operation_not_equal(self):
"""Tests that comparing a Hamiltonian with an Operator that is not an Observable returns False"""
op1 = qml.Hamiltonian([1, 1], [qml.PauliX(0), qml.PauliY(0)])
op2 = qml.RX(1.2, 0)
assert qml.equal(op1, op2) == False
assert qml.equal(op2, op1) == False
assert qml.equal(op1, op2) is False
assert qml.equal(op2, op1) is False

def test_tensor_and_operation_not_equal(self):
"""Tests that comparing a Tensor with an Operator that is not an Observable returns False"""
op1 = qml.PauliX(0) @ qml.PauliY(1)
op2 = qml.RX(1.2, 0)
assert qml.equal(op1, op2) == False
assert qml.equal(op2, op1) == False
assert qml.equal(op1, op2) is False
assert qml.equal(op2, op1) is False

def test_tensor_and_unsupported_observable_not_implemented(self):
"""Tests that trying to compare a Tensor to something other than another Tensor or a Hamiltonian raises a NotImplmenetedError"""
Expand Down Expand Up @@ -1298,7 +1299,7 @@ def test_mismatched_arithmetic_depth(self):

assert op1.arithmetic_depth == 1
assert op2.arithmetic_depth == 2
assert qml.equal(op1, op2) == False
assert qml.equal(op1, op2) is False

def test_comparison_of_base_not_implemented_error(self):
"""Test that comparing SymbolicOps of base operators whose comparison is not yet implemented raises an error"""
Expand Down Expand Up @@ -1379,26 +1380,32 @@ def test_adjoint_comparison(self, base):
assert qml.equal(op1, op2)
assert not qml.equal(op1, op3)

@pytest.mark.parametrize(("base1", "base2", "bases_match"), BASES)
@pytest.mark.parametrize(("param1", "param2", "params_match"), PARAMS)
def test_pow_comparison(self, base1, base2, bases_match, param1, param2, params_match):
@pytest.mark.parametrize("bases_bases_match", BASES)
@pytest.mark.parametrize("params_params_match", PARAMS)
def test_pow_comparison(self, bases_bases_match, params_params_match):
"""Test that equal compares two objects of the Pow class"""
base1, base2, bases_match = bases_bases_match
param1, param2, params_match = params_params_match
op1 = qml.pow(base1, param1)
op2 = qml.pow(base2, param2)
assert qml.equal(op1, op2) == (bases_match and params_match)

@pytest.mark.parametrize(("base1", "base2", "bases_match"), BASES)
@pytest.mark.parametrize(("param1", "param2", "params_match"), PARAMS)
def test_exp_comparison(self, base1, base2, bases_match, param1, param2, params_match):
@pytest.mark.parametrize("bases_bases_match", BASES)
@pytest.mark.parametrize("params_params_match", PARAMS)
def test_exp_comparison(self, bases_bases_match, params_params_match):
"""Test that equal compares two objects of the Exp class"""
base1, base2, bases_match = bases_bases_match
param1, param2, params_match = params_params_match
op1 = qml.exp(base1, param1)
op2 = qml.exp(base2, param2)
assert qml.equal(op1, op2) == (bases_match and params_match)

@pytest.mark.parametrize(("base1", "base2", "bases_match"), BASES)
@pytest.mark.parametrize(("param1", "param2", "params_match"), PARAMS)
def test_s_prod_comparison(self, base1, base2, bases_match, param1, param2, params_match):
@pytest.mark.parametrize("bases_bases_match", BASES)
@pytest.mark.parametrize("params_params_match", PARAMS)
def test_s_prod_comparison(self, bases_bases_match, params_params_match):
"""Test that equal compares two objects of the SProd class"""
base1, base2, bases_match = bases_bases_match
param1, param2, params_match = params_params_match
op1 = qml.s_prod(param1, base1)
op2 = qml.s_prod(param2, base2)
assert qml.equal(op1, op2) == (bases_match and params_match)
Expand Down
11 changes: 6 additions & 5 deletions tests/ops/functions/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
Unit tests for the qml.generator transform
"""
# pylint: disable=too-few-public-methods
from functools import reduce
from operator import matmul

Expand All @@ -22,7 +23,7 @@

import pennylane as qml
from pennylane import numpy as np
from pennylane.ops import Hamiltonian, Prod, SProd, Sum
from pennylane.ops import Prod, SProd, Sum

###################################################################
# Test operations
Expand Down Expand Up @@ -438,7 +439,7 @@ def test_tensor_observable(self):
gen = qml.generator(TensorOp, format="arithmetic")(0.5, wires=[0, 1])
result = qml.s_prod(0.5, qml.PauliX(0) @ qml.PauliY(1))

assert not isinstance(gen, Hamiltonian)
assert not isinstance(gen, qml.Hamiltonian)
assert np.allclose(
qml.matrix(gen, wire_order=[0, 1]),
qml.matrix(result, wire_order=[0, 1]),
Expand All @@ -451,7 +452,7 @@ def test_hamiltonian(self):
qml.PauliX(0) @ qml.Identity(1),
qml.s_prod(0.5, qml.PauliX(0) @ qml.PauliY(1)),
)
assert not isinstance(gen, Hamiltonian)
assert not isinstance(gen, qml.Hamiltonian)
assert np.allclose(
qml.matrix(gen, wire_order=[0, 1]),
qml.matrix(result, wire_order=[0, 1]),
Expand All @@ -463,7 +464,7 @@ def test_hermitian(self):
gen = qml.generator(HermitianOp, format="arithmetic")(0.5, wires=0)
expected = qml.pauli_decompose(HermitianOp.H, hide_identity=True, pauli=True).operation()

assert not isinstance(gen, Hamiltonian)
assert not isinstance(gen, qml.Hamiltonian)
assert np.allclose(
qml.matrix(gen),
qml.matrix(expected),
Expand All @@ -477,7 +478,7 @@ def test_sparse_hamiltonian(self):
SparseOp.H.toarray(), hide_identity=True, pauli=True
).operation()

assert not isinstance(gen, Hamiltonian)
assert not isinstance(gen, qml.Hamiltonian)
assert np.allclose(
qml.matrix(gen),
qml.matrix(expected),
Expand Down
3 changes: 2 additions & 1 deletion tests/ops/functions/test_is_commuting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
Unittests for is_commuting
"""
# pylint: disable=too-many-public-methods
import pytest
import pennylane.numpy as np
import pennylane as qml
Expand Down Expand Up @@ -847,4 +848,4 @@ def test_non_commuting(self):
"""Test the function with an operator from the non-commuting list."""

res = qml.is_commuting(qml.PauliX(wires=0), qml.QFT(wires=[1, 0]))
assert res == False
assert res is False
5 changes: 3 additions & 2 deletions tests/ops/functions/test_map_wires.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
"""
Unit tests for the qml.map_wires function
"""
# pylint: disable=too-few-public-methods
from functools import partial

import pytest

import pennylane as qml
from pennylane.ops import Prod, Sum
from pennylane.ops import Prod
from pennylane.tape import QuantumScript
from pennylane.wires import Wires

Expand Down Expand Up @@ -210,7 +211,7 @@ def test_jitting_simplified_qfunc(self):
@qml.qnode(qml.device("default.qubit", wires=5))
def circuit(x):
qml.adjoint(qml.RX(x, wires=0))
qml.PauliX(0) ** 2
_ = qml.PauliX(0) ** 2
return qml.expval(qml.PauliY(0))

x = jax.numpy.array(4 * jax.numpy.pi + 0.1)
Expand Down
Loading

0 comments on commit 6b6a5b3

Please sign in to comment.