Skip to content

Commit

Permalink
Replace assert in tests with unittest methods (Qiskit#10136)
Browse files Browse the repository at this point in the history
* Replace `assert` with unittest methods

* Update test/python/algorithms/optimizers/test_umda.py

Co-authored-by: Jake Lishman <[email protected]>

---------

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
2 people authored and king-p3nguin committed May 22, 2023
1 parent 0c9ed9b commit 9315c93
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
20 changes: 9 additions & 11 deletions test/python/algorithms/optimizers/test_umda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.assertEqual(umda.alpha, 0.6)
self.assertEqual(umda.maxiter, 100)

def test_settings(self):
"""Test if the settings display works well"""
Expand All @@ -50,23 +52,19 @@ 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

optimizer = UMDA(maxiter=1000, size_gen=100)
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):
Expand Down
3 changes: 2 additions & 1 deletion test/python/circuit/library/test_nlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions test/python/result/test_quasi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 9315c93

Please sign in to comment.