Skip to content

Commit

Permalink
Update VQE and NumpyMinimumEigensolver unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
CisterMoke committed Aug 31, 2021
1 parent ccf1dc5 commit 6824351
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions qiskit/algorithms/eigen_solvers/numpy_eigen_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,16 @@ def compute_eigenvalues(
raise AlgorithmError("Operator was never provided")

self._check_set_k(operator)
zero_op = I.tensorpower(operator.num_qubits) * 0.0
if isinstance(aux_operators, list) and len(aux_operators) > 0:
zero_op = I.tensorpower(operator.num_qubits) * 0.0
# For some reason Chemistry passes aux_ops with 0 qubits and paulis sometimes.
aux_operators = [zero_op if op == 0 else op for op in aux_operators]
elif isinstance(aux_operators, dict) and len(aux_operators) > 0:
aux_operators = {key: op for key, op in aux_operators.items() if op}
aux_operators = {
key: zero_op if op == 0 else op # Convert zero values to zero operators
for key, op in aux_operators.items()
if op is not None # Discard None values
}
else:
aux_operators = None

Expand Down
3 changes: 2 additions & 1 deletion test/python/algorithms/test_numpy_minimum_eigen_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ def test_cme_aux_ops_dict(self):
extra_ops = {"None_op": None, "zero_op": 0, **self.aux_ops_dict}
result = algo.compute_minimum_eigenvalue(operator=self.qubit_op, aux_operators=extra_ops)
self.assertAlmostEqual(result.eigenvalue, -1.85727503 + 0j)
self.assertEqual(len(result.aux_operator_eigenvalues), 2)
self.assertEqual(len(result.aux_operator_eigenvalues), 3)
np.testing.assert_array_almost_equal(result.aux_operator_eigenvalues["aux_op1"], [2, 0])
np.testing.assert_array_almost_equal(result.aux_operator_eigenvalues["aux_op2"], [0, 0])
self.assertEqual(result.aux_operator_eigenvalues["zero_op"], (0.0, 0))


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion test/python/algorithms/test_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,10 @@ def test_aux_operators_dict(self):
extra_ops = {**aux_ops, "None_operator": None, "zero_operator": 0}
result = vqe.compute_minimum_eigenvalue(self.h2_op, aux_operators=extra_ops)
self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=6)
self.assertEqual(len(result.aux_operator_eigenvalues), 2)
self.assertEqual(len(result.aux_operator_eigenvalues), 3)
self.assertAlmostEqual(result.aux_operator_eigenvalues["aux_op1"], 2, places=6)
self.assertAlmostEqual(result.aux_operator_eigenvalues["aux_op2"], 0, places=6)
self.assertEqual(result.aux_operator_eigenvalues["zero_operator"], 0.0)


if __name__ == "__main__":
Expand Down

0 comments on commit 6824351

Please sign in to comment.