Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace assert in tests with unittest methods #10136

Merged
merged 2 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.assertAlmostEqual(umda.alpha, 0.6)
Cryoris marked this conversation as resolved.
Show resolved Hide resolved
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
Comment on lines -57 to -58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These local imports looked strange to me too.


# 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