From da3bfb7528a875dafbaf859ed26695f8c8eae307 Mon Sep 17 00:00:00 2001 From: lillian542 Date: Mon, 15 Apr 2024 21:56:17 -0400 Subject: [PATCH] update tests --- tests/ops/functions/test_equal.py | 2 + tests/ops/qubit/test_parametric_ops.py | 1 + tests/pauli/test_conversion.py | 8 ++++ tests/pauli/test_pauli_utils.py | 1 + tests/test_device.py | 1 + tests/test_operation.py | 55 +++++++++++++------------- 6 files changed, 40 insertions(+), 28 deletions(-) diff --git a/tests/ops/functions/test_equal.py b/tests/ops/functions/test_equal.py index f9d61178894..e0b89c53024 100644 --- a/tests/ops/functions/test_equal.py +++ b/tests/ops/functions/test_equal.py @@ -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""" @@ -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""" diff --git a/tests/ops/qubit/test_parametric_ops.py b/tests/ops/qubit/test_parametric_ops.py index 88ead3da971..fc3c49928ae 100644 --- a/tests/ops/qubit/test_parametric_ops.py +++ b/tests/ops/qubit/test_parametric_ops.py @@ -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.""" diff --git a/tests/pauli/test_conversion.py b/tests/pauli/test_conversion.py index f111c562bb5..9753b953e3e 100644 --- a/tests/pauli/test_conversion.py +++ b/tests/pauli/test_conversion.py @@ -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): @@ -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 @@ -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): @@ -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 @@ -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) @@ -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): @@ -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( diff --git a/tests/pauli/test_pauli_utils.py b/tests/pauli/test_pauli_utils.py index fa386c493d4..2f21637e06e 100644 --- a/tests/pauli/test_pauli_utils.py +++ b/tests/pauli/test_pauli_utils.py @@ -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", [ diff --git a/tests/test_device.py b/tests/test_device.py index 0422e9dfd79..96064b9d894 100644 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -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() diff --git a/tests/test_operation.py b/tests/test_operation.py index b473f4c5b39..a476200a3a7 100644 --- a/tests/test_operation.py +++ b/tests/test_operation.py @@ -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.""" @@ -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( @@ -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() @@ -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""" @@ -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)