Skip to content

Commit 96f38b3

Browse files
committed
Ignore expected warnings during testing (quantumlib#1065)
A few test cases produce `RuntimeWarning`'s. These turn out to be expected. We can indicate to pytest that they can be ignored.
1 parent ee72eac commit 96f38b3

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

src/openfermion/linalg/davidson_test.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import logging
1515
import unittest
1616

17+
import pytest
1718
import numpy
1819
import numpy.linalg
1920
import scipy.linalg
@@ -406,9 +407,10 @@ def test_get_lowest_z_real(self):
406407
initial_guess = 1.0j * numpy.random.rand(dimension, n_lowest)
407408
numpy.random.seed(dimension * 2)
408409
initial_guess += numpy.random.rand(dimension, n_lowest)
409-
success, eigen_values, eigen_vectors = davidson.get_lowest_n(
410-
n_lowest, initial_guess, max_iterations=10
411-
)
410+
with pytest.warns(RuntimeWarning):
411+
success, eigen_values, eigen_vectors = davidson.get_lowest_n(
412+
n_lowest, initial_guess, max_iterations=10
413+
)
412414

413415
# one half of the eigenvalues is -1 and the other half is +1, together
414416
# with the coefficient.
@@ -436,9 +438,13 @@ def test_get_lowest_y_real_fail(self):
436438
initial_guess = 1.0j * numpy.random.rand(dimension, n_lowest)
437439
numpy.random.seed(dimension * 2)
438440
initial_guess += numpy.random.rand(dimension, n_lowest)
439-
success, _, eigen_vectors = davidson.get_lowest_n(
440-
n_lowest, initial_guess, max_iterations=10
441-
)
441+
with pytest.warns(
442+
RuntimeWarning,
443+
match=("Unable to get real only eigenvectors|Initial guess is not real only!"),
444+
):
445+
success, _, eigen_vectors = davidson.get_lowest_n(
446+
n_lowest, initial_guess, max_iterations=10
447+
)
442448

443449
self.assertFalse(success)
444450

@@ -458,9 +464,13 @@ def test_get_lowest_y_real(self):
458464
initial_guess = 1.0j * numpy.random.rand(dimension, n_lowest)
459465
numpy.random.seed(dimension * 2)
460466
initial_guess += numpy.random.rand(dimension, n_lowest)
461-
success, eigen_values, eigen_vectors = davidson.get_lowest_n(
462-
n_lowest, initial_guess, max_iterations=10
463-
)
467+
with pytest.warns(
468+
RuntimeWarning,
469+
match=("Unable to get real only eigenvectors|Initial guess is not real only!"),
470+
):
471+
success, eigen_values, eigen_vectors = davidson.get_lowest_n(
472+
n_lowest, initial_guess, max_iterations=10
473+
)
464474

465475
# one half of the eigenvalues is -1 and the other half is +1, together
466476
# with the coefficient.
@@ -487,9 +497,13 @@ def test_get_lowest_y_complex(self):
487497
initial_guess = 1.0j * numpy.random.rand(dimension, n_lowest)
488498
numpy.random.seed(dimension * 2)
489499
initial_guess += numpy.random.rand(dimension, n_lowest)
490-
success, eigen_values, eigen_vectors = davidson.get_lowest_n(
491-
n_lowest, initial_guess, max_iterations=10
492-
)
500+
with pytest.warns(
501+
RuntimeWarning,
502+
match=("Unable to get real only eigenvectors|Initial guess is not real only!"),
503+
):
504+
success, eigen_values, eigen_vectors = davidson.get_lowest_n(
505+
n_lowest, initial_guess, max_iterations=10
506+
)
493507

494508
# one half of the eigenvalues is -1 and the other half is +1, together
495509
# with the coefficient.
@@ -658,7 +672,8 @@ def test_append_vectors_big_col(self):
658672
"""Test append_random_vectors() with too many failed trial."""
659673
row = 10
660674
vectors = numpy.eye(row, row)
661-
new_vectors = append_random_vectors(vectors, 1)
675+
with pytest.warns(RuntimeWarning):
676+
new_vectors = append_random_vectors(vectors, 1)
662677

663678
self.assertTrue(numpy.allclose(new_vectors, vectors))
664679

src/openfermion/ops/operators/qubit_operator_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ def check_sum(operators, operator):
231231
def test_get_operator_groups_zero():
232232
"""Tests get_operator_groups() with one group."""
233233
operator = generate_operator(0, 20)
234-
operator_groups = list(operator.get_operator_groups(0))
234+
with pytest.warns(RuntimeWarning, match="Invalid num_groups 0 < 1"):
235+
# Min. is 1; get_operator_groups() should issue warning and use 1.
236+
operator_groups = list(operator.get_operator_groups(0))
235237

236238
# Using 1 group instead.
237239
assert check_length(operator_groups, [20])

0 commit comments

Comments
 (0)