Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lillian542 committed Apr 16, 2024
1 parent 1999627 commit da3bfb7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
2 changes: 2 additions & 0 deletions tests/ops/functions/test_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,7 @@ def test_s_prod_comparison_with_tolerance(self):
assert not qml.equal(op1, op2, atol=1e-5, rtol=1e-4)


@pytest.mark.usefixtures("use_new_opmath")
class TestProdComparisons:
"""Tests comparisons between Prod operators"""

Expand Down Expand Up @@ -1744,6 +1745,7 @@ def test_prod_of_prods(self):
assert qml.equal(op1, op2)


@pytest.mark.usefixtures("use_new_opmath")
class TestSumComparisons:
"""Tests comparisons between Sum operators"""

Expand Down
1 change: 1 addition & 0 deletions tests/ops/qubit/test_parametric_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3028,6 +3028,7 @@ def test_pauli_rot_generator_legacy_opmath(self):
assert gen.operands[0].name == expected.obs[0].name
assert gen.operands[1].wires == expected.obs[1].wires

@pytest.mark.usefixtures("use_new_opmath")
def test_pauli_rot_generator(self):
"""Test that the generator of the PauliRot operation
is correctly returned."""
Expand Down
8 changes: 8 additions & 0 deletions tests/pauli/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def test_observable_types_legacy_opmath(self, hamiltonian, hide_identity):
_, decomposed_obs = qml.pauli_decompose(hamiltonian, hide_identity).terms()
assert all((isinstance(o, allowed_obs) for o in decomposed_obs))

@pytest.mark.usefixtures("use_new_opmath")
@pytest.mark.parametrize("hide_identity", [True, False])
@pytest.mark.parametrize("hamiltonian", test_hamiltonians)
def test_observable_types(self, hamiltonian, hide_identity):
Expand All @@ -162,6 +163,7 @@ def test_result_length(self, hamiltonian):
tensors = filter(lambda obs: isinstance(obs, Tensor), decomposed_obs)
assert all(len(tensor.obs) == n for tensor in tensors)

# pylint: disable = consider-using-generator
@pytest.mark.parametrize("hamiltonian", test_hamiltonians)
def test_decomposition(self, hamiltonian):
"""Tests that pauli_decompose successfully decomposes Hamiltonians into a
Expand Down Expand Up @@ -266,6 +268,7 @@ def test_observable_types_legacy_opmath(self, hamiltonian, hide_identity):
).terms()
assert all((isinstance(o, allowed_obs) for o in decomposed_obs))

@pytest.mark.usefixtures("use_new_opmath")
@pytest.mark.parametrize("hide_identity", [True, False])
@pytest.mark.parametrize("hamiltonian", test_hamiltonians)
def test_observable_types(self, hamiltonian, hide_identity):
Expand All @@ -288,6 +291,7 @@ def test_result_length(self, hamiltonian):
tensors = filter(lambda obs: isinstance(obs, Tensor), decomposed_obs)
assert all(len(tensor.obs) == n for tensor in tensors)

# pylint: disable = consider-using-generator
@pytest.mark.parametrize("hamiltonian", test_hamiltonians)
def test_decomposition(self, hamiltonian):
"""Tests that pauli_decompose successfully decomposes Hamiltonians into a
Expand All @@ -314,6 +318,7 @@ def test_to_paulisentence(self, hamiltonian):
assert isinstance(ps, qml.pauli.PauliSentence)
assert np.allclose(hamiltonian, ps.to_mat(range(num_qubits)))

# pylint: disable = consider-using-generator
@pytest.mark.usefixtures("use_legacy_opmath")
@pytest.mark.parametrize("hide_identity", [True, False])
@pytest.mark.parametrize("matrix", test_general_matrix)
Expand Down Expand Up @@ -342,6 +347,8 @@ def test_observable_types_general_legacy_opmath(self, matrix, hide_identity):
tensors = filter(lambda obs: isinstance(obs, Tensor), decomposed_obs)
assert all(len(tensor.obs) == num_qubits for tensor in tensors)

# pylint: disable = consider-using-generator
@pytest.mark.usefixtures("use_new_opmath")
@pytest.mark.parametrize("hide_identity", [True, False])
@pytest.mark.parametrize("matrix", test_general_matrix)
def test_observable_types_general(self, matrix, hide_identity):
Expand Down Expand Up @@ -428,6 +435,7 @@ def test_builtins(self, matrix):
coeffs = qml.pauli_decompose(mat).coeffs
assert qml.math.get_interface(coeffs[0]) == interface

# pylint: disable = superfluous-parens
# Multiple interfaces will be tested with math module
@pytest.mark.all_interfaces
@pytest.mark.parametrize(
Expand Down
1 change: 1 addition & 0 deletions tests/pauli/test_pauli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ def test_pauli_word_to_string_invalid_input(self, non_pauli_word):
with pytest.raises(TypeError):
pauli_word_to_string(non_pauli_word)

@pytest.mark.usefixtures("use_new_opmath")
@pytest.mark.parametrize(
"pauli_string,wire_map,expected_pauli",
[
Expand Down
1 change: 1 addition & 0 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ def test_check_validity_on_tensor_support_legacy_opmath(self, mock_device_suppor
with pytest.raises(DeviceError, match="Tensor observables not supported"):
dev.check_validity(queue, observables)

@pytest.mark.usefixtures("use_new_opmath")
def test_check_validity_on_prod_support(self, mock_device_supporting_paulis):
"""Tests the function Device.check_validity with prod support capability"""
dev = mock_device_supporting_paulis()
Expand Down
55 changes: 27 additions & 28 deletions tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2632,15 +2632,10 @@ def test_composed(self):
]


@pytest.mark.usefixtures("use_new_opmath")
class TestNewOpMath:
"""Tests dunder operations with new operator arithmetic enabled."""

# @pytest.fixture(autouse=True, scope="function") # this came from a push to ham-tests but I think it should not be there as it explicitly disabled new opmath after each test, so also leaving it in that state for other tests.
# def run_before_and_after_tests(self):
# qml.operation.enable_new_opmath()
# yield
# qml.operation.disable_new_opmath()

class TestAdd:
"""Test the __add__/__radd__/__sub__ dunders."""

Expand Down Expand Up @@ -2755,32 +2750,33 @@ def test_mul_does_not_auto_simplify(self):
assert qml.equal(op[0], op0 @ op1)
assert qml.equal(op[1], op2)

class TestHamiltonianLinearCombinationAlias:
"""Unit tests for using qml.Hamiltonian as an alias for LinearCombination"""

@pytest.mark.usefixtures("use_new_opmath")
def test_hamiltonian_linear_combination_alias_enabled(self):
"""Test that qml.Hamiltonian is an alias for LinearCombination with new operator
arithmetic enabled"""
op = qml.Hamiltonian([1.0], [qml.X(0)])
class TestHamiltonianLinearCombinationAlias:
"""Unit tests for using qml.Hamiltonian as an alias for LinearCombination"""

@pytest.mark.usefixtures("use_new_opmath")
def test_hamiltonian_linear_combination_alias_enabled(self):
"""Test that qml.Hamiltonian is an alias for LinearCombination with new operator
arithmetic enabled"""
op = qml.Hamiltonian([1.0], [qml.X(0)])

assert isinstance(op, qml.ops.LinearCombination)
assert isinstance(op, qml.Hamiltonian)
assert not isinstance(op, qml.ops.Hamiltonian)
assert not isinstance(op, qml.ops.qubit.Hamiltonian)
assert not isinstance(op, qml.ops.qubit.hamiltonian.Hamiltonian)
assert isinstance(op, qml.ops.LinearCombination)
assert isinstance(op, qml.Hamiltonian)
assert not isinstance(op, qml.ops.Hamiltonian)
assert not isinstance(op, qml.ops.qubit.Hamiltonian)
assert not isinstance(op, qml.ops.qubit.hamiltonian.Hamiltonian)

@pytest.mark.usefixtures("use_legacy_opmath")
def test_hamiltonian_linear_combination_alias_disabled(self):
"""Test that qml.Hamiltonian is not an alias for LinearCombination with new operator
arithmetic disabled"""
op = qml.Hamiltonian([1.0], [qml.X(0)])
@pytest.mark.usefixtures("use_legacy_opmath")
def test_hamiltonian_linear_combination_alias_disabled(self):
"""Test that qml.Hamiltonian is not an alias for LinearCombination with new operator
arithmetic disabled"""
op = qml.Hamiltonian([1.0], [qml.X(0)])

assert not isinstance(op, qml.ops.LinearCombination)
assert isinstance(op, qml.Hamiltonian)
assert isinstance(op, qml.ops.Hamiltonian)
assert isinstance(op, qml.ops.qubit.Hamiltonian)
assert isinstance(op, qml.ops.qubit.hamiltonian.Hamiltonian)
assert not isinstance(op, qml.ops.LinearCombination)
assert isinstance(op, qml.Hamiltonian)
assert isinstance(op, qml.ops.Hamiltonian)
assert isinstance(op, qml.ops.qubit.Hamiltonian)
assert isinstance(op, qml.ops.qubit.hamiltonian.Hamiltonian)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -2819,6 +2815,7 @@ def test_symmetric_matrix_early_return(op, mocker):
assert np.allclose(actual, manually_expanded)


@pytest.mark.usefixtures("use_new_opmath")
def test_op_arithmetic_toggle():
"""Tests toggling op arithmetic on and off, and that it is on by default."""
assert qml.operation.active_new_opmath()
Expand Down Expand Up @@ -2954,6 +2951,7 @@ def test_use_legacy_opmath_fixture():
]


@pytest.mark.usefixtures("use_new_opmath")
@pytest.mark.parametrize("coeffs, obs", CONVERT_HAMILTONAIN)
def test_convert_to_hamiltonian(coeffs, obs):
"""Test that arithmetic operators can be converted to Hamiltonian instances"""
Expand Down Expand Up @@ -2999,6 +2997,7 @@ def test_convert_to_hamiltonian_error(coeffs, obs):
convert_to_legacy_H(qml.dot(coeffs, obs))


@pytest.mark.usefixtures("use_new_opmath")
def test_convert_to_H():
operator = (
2 * qml.X(0)
Expand Down

0 comments on commit da3bfb7

Please sign in to comment.