Skip to content

Commit

Permalink
revert changes in tests because utils
Browse files Browse the repository at this point in the history
  • Loading branch information
1ucian0 committed Jul 25, 2024
1 parent 4ed0e22 commit df44306
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 69 deletions.
90 changes: 35 additions & 55 deletions test/python/primitives/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,21 @@ def test_estimator_run(self):
# since the corresponding indices need to be searched.
# User can append a circuit and observable.
# calculate [ <psi2(theta2)|H1|psi2(theta2)> ]
with self.assertWarns(DeprecationWarning):
result2 = estimator.run([psi2], [hamiltonian1], [theta2]).result()
result2 = estimator.run([psi2], [hamiltonian1], [theta2]).result()
np.testing.assert_allclose(result2.values, [2.97797666])

# calculate [ <psi1(theta1)|H2|psi1(theta1)>, <psi1(theta1)|H3|psi1(theta1)> ]
with self.assertWarns(DeprecationWarning):
result3 = estimator.run(
[psi1, psi1], [hamiltonian2, hamiltonian3], [theta1] * 2
).result()
result3 = estimator.run([psi1, psi1], [hamiltonian2, hamiltonian3], [theta1] * 2).result()
np.testing.assert_allclose(result3.values, [-0.551653, 0.07535239])

# calculate [ <psi1(theta1)|H1|psi1(theta1)>,
# <psi2(theta2)|H2|psi2(theta2)>,
# <psi1(theta3)|H3|psi1(theta3)> ]
with self.assertWarns(DeprecationWarning):
result4 = estimator.run(
[psi1, psi2, psi1],
[hamiltonian1, hamiltonian2, hamiltonian3],
[theta1, theta2, theta3],
).result()
result4 = estimator.run(
[psi1, psi2, psi1],
[hamiltonian1, hamiltonian2, hamiltonian3],
[theta1, theta2, theta3],
).result()
np.testing.assert_allclose(result4.values, [1.55555728, 0.17849238, -1.08766318])

def test_estiamtor_run_no_params(self):
Expand All @@ -121,8 +116,7 @@ def test_run_single_circuit_observable(self):
target = [-1]
for val in param_vals:
self.subTest(f"{val}")
with self.assertWarns(DeprecationWarning):
result = est.run(qc, op, val).result()
result = est.run(qc, op, val).result()
np.testing.assert_allclose(result.values, target)
self.assertEqual(len(result.metadata), 1)

Expand All @@ -141,8 +135,7 @@ def test_run_single_circuit_observable(self):
target = [-1]
for val in param_vals:
self.subTest(f"{val}")
with self.assertWarns(DeprecationWarning):
result = est.run(qc, op, val).result()
result = est.run(qc, op, val).result()
np.testing.assert_allclose(result.values, target)
self.assertEqual(len(result.metadata), 1)

Expand All @@ -159,8 +152,7 @@ def test_run_single_circuit_observable(self):
target = [1.5555572817900956]
for val in param_vals:
self.subTest(f"{val}")
with self.assertWarns(DeprecationWarning):
result = est.run(qc, op, val).result()
result = est.run(qc, op, val).result()
np.testing.assert_allclose(result.values, target)
self.assertEqual(len(result.metadata), 1)

Expand All @@ -179,19 +171,16 @@ def test_run_1qubit(self):
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1])

with self.assertWarns(DeprecationWarning):
result = est.run([qc], [op2], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
result = est.run([qc], [op2], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1])

with self.assertWarns(DeprecationWarning):
result = est.run([qc2], [op], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
result = est.run([qc2], [op], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1])

with self.assertWarns(DeprecationWarning):
result = est.run([qc2], [op2], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
result = est.run([qc2], [op2], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [-1])

def test_run_2qubits(self):
Expand All @@ -210,29 +199,24 @@ def test_run_2qubits(self):
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1])

with self.assertWarns(DeprecationWarning):
result = est.run([qc2], [op], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
result = est.run([qc2], [op], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1])

with self.assertWarns(DeprecationWarning):
result = est.run([qc], [op2], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
result = est.run([qc], [op2], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1])

with self.assertWarns(DeprecationWarning):
result = est.run([qc2], [op2], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
result = est.run([qc2], [op2], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1])

with self.assertWarns(DeprecationWarning):
result = est.run([qc], [op3], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
result = est.run([qc], [op3], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [1])

with self.assertWarns(DeprecationWarning):
result = est.run([qc2], [op3], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
result = est.run([qc2], [op3], [[]]).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [-1])

def test_run_errors(self):
Expand Down Expand Up @@ -270,14 +254,12 @@ def test_run_numpy_params(self):
target = estimator.run([qc] * k, [op] * k, params_list).result()

with self.subTest("ndarrary"):
with self.assertWarns(DeprecationWarning):
result = estimator.run([qc] * k, [op] * k, params_array).result()
result = estimator.run([qc] * k, [op] * k, params_array).result()
self.assertEqual(len(result.metadata), k)
np.testing.assert_allclose(result.values, target.values)

with self.subTest("list of ndarray"):
with self.assertWarns(DeprecationWarning):
result = estimator.run([qc] * k, [op] * k, params_list_array).result()
result = estimator.run([qc] * k, [op] * k, params_list_array).result()
self.assertEqual(len(result.metadata), k)
np.testing.assert_allclose(result.values, target.values)

Expand Down Expand Up @@ -326,13 +308,12 @@ def test_options(self):
self.assertEqual(estimator.options.get("shots"), 1024)
self.assertEqual(estimator.options.get("seed"), 15)
with self.subTest("run"):
with self.assertWarns(DeprecationWarning):
result = estimator.run(
[self.ansatz],
[self.observable],
parameter_values=[[0, 1, 1, 2, 3, 5]],
).result()
self.assertIsInstance(result, EstimatorResult)
result = estimator.run(
[self.ansatz],
[self.observable],
parameter_values=[[0, 1, 1, 2, 3, 5]],
).result()
self.assertIsInstance(result, EstimatorResult)
np.testing.assert_allclose(result.values, [-1.307397243478641])

def test_negative_variance(self):
Expand Down Expand Up @@ -380,8 +361,7 @@ class TestObservableValidation(QiskitTestCase):
@unpack
def test_validate_observables(self, observables, expected):
"""Test observables standardization."""
with self.assertWarns(DeprecationWarning):
self.assertEqual(validation._validate_observables(observables), expected)
self.assertEqual(validation._validate_observables(observables), expected)

@data(None, "ERROR")
def test_qiskit_error(self, observables):
Expand Down
15 changes: 5 additions & 10 deletions test/python/primitives/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ def test_run_single_circuit(self):
with self.subTest(f"{circuit.name} w/ {val}"):
with self.assertWarns(DeprecationWarning):
sampler = Sampler()
with self.assertWarns(DeprecationWarning):
result = sampler.run(circuit, val).result()
result = sampler.run(circuit, val).result()
self._compare_probs(result.quasi_dists, target)
self.assertEqual(len(result.metadata), 1)

Expand All @@ -222,8 +221,7 @@ def test_run_single_circuit(self):
with self.subTest(f"{circuit.name} w/ {val}"):
with self.assertWarns(DeprecationWarning):
sampler = Sampler()
with self.assertWarns(DeprecationWarning):
result = sampler.run(circuit, val).result()
result = sampler.run(circuit, val).result()
self._compare_probs(result.quasi_dists, target)
self.assertEqual(len(result.metadata), 1)

Expand All @@ -241,8 +239,7 @@ def test_run_single_circuit(self):
with self.subTest(f"{circuit.name} w/ {val}"):
with self.assertWarns(DeprecationWarning):
sampler = Sampler()
with self.assertWarns(DeprecationWarning):
result = sampler.run(circuit, val).result()
result = sampler.run(circuit, val).result()
self._compare_probs(result.quasi_dists, target)
self.assertEqual(len(result.metadata), 1)

Expand Down Expand Up @@ -319,8 +316,7 @@ def test_run_empty_parameter(self):
with self.assertWarns(DeprecationWarning):
sampler = Sampler()
with self.subTest("one circuit"):
with self.assertWarns(DeprecationWarning):
result = sampler.run([qc], shots=1000).result()
result = sampler.run([qc], shots=1000).result()
self.assertEqual(len(result.quasi_dists), 1)
for q_d in result.quasi_dists:
quasi_dist = {k: v for k, v in q_d.items() if v != 0.0}
Expand Down Expand Up @@ -417,8 +413,7 @@ def test_options(self):
self.assertEqual(sampler.options.get("seed"), 15)
with self.subTest("run"):
params, target = self._generate_params_target([1])
with self.assertWarns(DeprecationWarning):
result = sampler.run([self._pqc], parameter_values=params).result()
result = sampler.run([self._pqc], parameter_values=params).result()
self._compare_probs(result.quasi_dists, target)
self.assertEqual(result.quasi_dists[0].shots, 1024)

Expand Down
6 changes: 2 additions & 4 deletions test/python/primitives/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class TestMapping(QiskitTestCase):
def test_empty_circ(self):
"""Empty circuit has no mapping"""
qc = QuantumCircuit()
with self.assertWarns(DeprecationWarning):
self.assertDictEqual(final_measurement_mapping(qc), {})
self.assertDictEqual(final_measurement_mapping(qc), {})

def test_sime_circ(self):
"""Just measures"""
Expand All @@ -42,8 +41,7 @@ def test_simple2_circ(self):
qc = QuantumCircuit(5)
qc.measure_all()
qc.h(range(5))
with self.assertWarns(DeprecationWarning):
self.assertDictEqual(final_measurement_mapping(qc), {})
self.assertDictEqual(final_measurement_mapping(qc), {})

def test_multi_qreg(self):
"""Test multiple qregs"""
Expand Down

0 comments on commit df44306

Please sign in to comment.