From f535a7e3c0341ba3cc706dfb71c5819e810d2ea0 Mon Sep 17 00:00:00 2001 From: Julien Gacon Date: Mon, 22 May 2023 09:43:48 +0200 Subject: [PATCH 1/2] Replace `assert` with unittest methods --- .../python/algorithms/optimizers/test_umda.py | 20 +++++++++---------- test/python/circuit/library/test_nlocal.py | 3 ++- .../operators/symplectic/test_pauli_list.py | 2 +- test/python/result/test_quasi.py | 5 +++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/test/python/algorithms/optimizers/test_umda.py b/test/python/algorithms/optimizers/test_umda.py index 8d05c1cd2454..1b3c8f995923 100644 --- a/test/python/algorithms/optimizers/test_umda.py +++ b/test/python/algorithms/optimizers/test_umda.py @@ -15,8 +15,10 @@ from test.python.algorithms import QiskitAlgorithmsTestCase import numpy as np +from scipy.optimize import rosen from qiskit.algorithms.optimizers.umda import UMDA +from qiskit.utils import algorithm_globals class TestUMDA(QiskitAlgorithmsTestCase): @@ -30,10 +32,10 @@ def test_get_set(self): umda.alpha = 0.6 umda.maxiter = 100 - assert umda.disp is True - assert umda.size_gen == 30 - assert umda.alpha == 0.6 - assert umda.maxiter == 100 + self.assertTrue(umda.disp) + self.assertEqual(umda.size_gen, 30) + self.assertAlmostEqual(umda.alpha, 0.6) + self.assertEqual(umda.maxiter, 100) def test_settings(self): """Test if the settings display works well""" @@ -50,13 +52,10 @@ def test_settings(self): "callback": None, } - assert umda.settings == set_ + self.assertEqual(umda.settings, set_) def test_minimize(self): """optimize function test""" - from scipy.optimize import rosen - from qiskit.utils import algorithm_globals - # UMDA is volatile so we need to set the seeds for the execution algorithm_globals.random_seed = 52 @@ -64,9 +63,8 @@ def test_minimize(self): x_0 = [1.3, 0.7, 1.5] res = optimizer.minimize(rosen, x_0) - assert res.fun is not None - assert len(res.x) == len(x_0) - + self.assertIsNotNone(res.fun) + self.assertEqual(len(res.x), len(x_0)) np.testing.assert_array_almost_equal(res.x, [1.0] * len(x_0), decimal=2) def test_callback(self): diff --git a/test/python/circuit/library/test_nlocal.py b/test/python/circuit/library/test_nlocal.py index 5c77bac358aa..f20377368ed4 100644 --- a/test/python/circuit/library/test_nlocal.py +++ b/test/python/circuit/library/test_nlocal.py @@ -913,7 +913,8 @@ def test_full_vs_reverse_linear(self, num_qubits): reverse = RealAmplitudes(num_qubits=num_qubits, entanglement="reverse_linear", reps=reps) full.assign_parameters(params, inplace=True) reverse.assign_parameters(params, inplace=True) - assert Operator(full) == Operator(reverse) + + self.assertEqual(Operator(full), Operator(reverse)) if __name__ == "__main__": diff --git a/test/python/quantum_info/operators/symplectic/test_pauli_list.py b/test/python/quantum_info/operators/symplectic/test_pauli_list.py index 28e4d30b215f..379111df5eaa 100644 --- a/test/python/quantum_info/operators/symplectic/test_pauli_list.py +++ b/test/python/quantum_info/operators/symplectic/test_pauli_list.py @@ -2106,8 +2106,8 @@ def qubitwise_commutes(left: Pauli, right: Pauli) -> bool: # checking that every input Pauli in pauli_list is in a group in the ouput output_labels = [pauli.to_label() for group in groups for pauli in group] - # assert sorted(output_labels) == sorted(input_labels) self.assertListEqual(sorted(output_labels), sorted(input_labels)) + # Within each group, every operator qubit-wise commutes with every other operator. for group in groups: self.assertTrue( diff --git a/test/python/result/test_quasi.py b/test/python/result/test_quasi.py index 3c9b7d4c59a8..e124c569a81f 100644 --- a/test/python/result/test_quasi.py +++ b/test/python/result/test_quasi.py @@ -191,9 +191,10 @@ def test_known_quasi_conversion(self): ans = {0: 9 / 20, 1: 7 / 20, 2: 1 / 5} # Check probs are correct for key, val in closest.items(): - assert abs(ans[key] - val) < 1e-14 + self.assertAlmostEqual(ans[key], val, places=14) + # Check if distance calculation is correct - assert abs(dist - sqrt(0.38)) < 1e-14 + self.assertAlmostEqual(dist, sqrt(0.38), places=14) def test_marginal_distribution(self): """Test marginal_distribution with float value.""" From 267a68242c4f3873a848abb472e6a835f79e683d Mon Sep 17 00:00:00 2001 From: Julien Gacon Date: Mon, 22 May 2023 14:32:33 +0200 Subject: [PATCH 2/2] Update test/python/algorithms/optimizers/test_umda.py Co-authored-by: Jake Lishman --- test/python/algorithms/optimizers/test_umda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/python/algorithms/optimizers/test_umda.py b/test/python/algorithms/optimizers/test_umda.py index 1b3c8f995923..e33e11bda60d 100644 --- a/test/python/algorithms/optimizers/test_umda.py +++ b/test/python/algorithms/optimizers/test_umda.py @@ -34,7 +34,7 @@ def test_get_set(self): self.assertTrue(umda.disp) self.assertEqual(umda.size_gen, 30) - self.assertAlmostEqual(umda.alpha, 0.6) + self.assertEqual(umda.alpha, 0.6) self.assertEqual(umda.maxiter, 100) def test_settings(self):