Skip to content

Commit 1634589

Browse files
authored
Close matplotlib figures after testing (#4810)
Add test fixture that cleans up any open matplotlib figures and plug it in the tests that open new figures. Fix RuntimeWarning: More than 20 figures have been opened.
1 parent bbd965e commit 1634589

13 files changed

+35
-0
lines changed

cirq-core/cirq/experiments/cross_entropy_benchmarking_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from cirq.experiments.cross_entropy_benchmarking import CrossEntropyPair, SpecklePurityPair
2727

2828

29+
@pytest.mark.usefixtures('closefigures')
2930
def test_cross_entropy_benchmarking():
3031
# Check that the fidelities returned from a four-qubit XEB simulation are
3132
# close to 1 (deviations from 1 is expected due to finite number of

cirq-core/cirq/experiments/qubit_characterizations_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def test_two_qubit_state_tomography():
188188
np.testing.assert_almost_equal(act_rho_yx, tar_rho_yx, decimal=1)
189189

190190

191+
@pytest.mark.usefixtures('closefigures')
191192
def test_tomography_plot_raises_for_incorrect_number_of_axes():
192193
simulator = sim.Simulator()
193194
qubit = GridQubit(0, 0)

cirq-core/cirq/experiments/t1_decay_experiment_test.py

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def test_init_result():
3434
assert result.data is data
3535

3636

37+
@pytest.mark.usefixtures('closefigures')
3738
def test_plot_does_not_raise_error():
3839
class _TimeDependentDecay(cirq.NoiseModel):
3940
def noisy_moment(self, moment, system_qubits):
@@ -167,6 +168,7 @@ def test_all_off_results():
167168
)
168169

169170

171+
@pytest.mark.usefixtures('closefigures')
170172
def test_curve_fit_plot_works():
171173
good_fit = cirq.experiments.T1DecayResult(
172174
data=pd.DataFrame(
@@ -184,6 +186,7 @@ def test_curve_fit_plot_works():
184186
good_fit.plot(include_fit=True)
185187

186188

189+
@pytest.mark.usefixtures('closefigures')
187190
def test_curve_fit_plot_warning():
188191
bad_fit = cirq.experiments.T1DecayResult(
189192
data=pd.DataFrame(

cirq-core/cirq/experiments/t2_decay_experiment_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_init_t2_decay_result():
5555
cirq.experiments.T2DecayResult(x_data, bad_data)
5656

5757

58+
@pytest.mark.usefixtures('closefigures')
5859
def test_plot_does_not_raise_error():
5960
class _TimeDependentDecay(cirq.NoiseModel):
6061
def noisy_moment(self, moment, system_qubits):

cirq-core/cirq/linalg/decompositions_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ def test_kak_vector_empty():
252252
assert len(cirq.kak_vector([])) == 0
253253

254254

255+
@pytest.mark.usefixtures('closefigures')
255256
def test_kak_plot_empty():
256257
cirq.scatter_plot_normalized_kak_interaction_coefficients([])
257258

@@ -553,6 +554,7 @@ def test_axis_angle_init():
553554
cirq.AxisAngleDecomposition(angle=1, axis=(0, 0.5, 0), global_phase=1)
554555

555556

557+
@pytest.mark.usefixtures('closefigures')
556558
def test_scatter_plot_normalized_kak_interaction_coefficients():
557559
a, b = cirq.LineQubit.range(2)
558560
data = [

cirq-core/cirq/vis/density_matrix_test.py

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from cirq.vis.density_matrix import _plot_element_of_density_matrix
2525

2626

27+
@pytest.mark.usefixtures('closefigures')
2728
@pytest.mark.parametrize('show_text', [True, False])
2829
@pytest.mark.parametrize('size', [2, 4, 8, 16])
2930
def test_density_matrix_plotter(size, show_text):
@@ -47,6 +48,7 @@ def test_density_matrix_plotter(size, show_text):
4748
)
4849

4950

51+
@pytest.mark.usefixtures('closefigures')
5052
@pytest.mark.parametrize('show_text', [True, False])
5153
@pytest.mark.parametrize('size', [2, 4, 8, 16])
5254
def test_density_matrix_circle_rectangle_sizes(size, show_text):
@@ -89,6 +91,7 @@ def test_density_matrix_circle_rectangle_sizes(size, show_text):
8991
)
9092

9193

94+
@pytest.mark.usefixtures('closefigures')
9295
@pytest.mark.parametrize('show_text', [True, False])
9396
@pytest.mark.parametrize('size', [2, 4, 8, 16])
9497
def test_density_matrix_sizes_upper_bounds(size, show_text):
@@ -112,6 +115,7 @@ def test_density_matrix_sizes_upper_bounds(size, show_text):
112115
assert max_radius * 2 <= 1.0, "Some circle is exceeding out of it's cell size"
113116

114117

118+
@pytest.mark.usefixtures('closefigures')
115119
@pytest.mark.parametrize('show_rect', [True, False])
116120
@pytest.mark.parametrize('value', [0.0, 1.0, 0.5 + 0.3j, 0.2 + 0.1j, 0.5 + 0.5j])
117121
def test_density_element_plot(value, show_rect):

cirq-core/cirq/vis/heatmap_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ def test_colorbar(ax, position, size, pad):
311311
plt.close(fig2)
312312

313313

314+
@pytest.mark.usefixtures('closefigures')
314315
def test_plot_updates_local_config():
315316
value_map_2d = {
316317
(grid_qubit.GridQubit(3, 2), grid_qubit.GridQubit(4, 2)): 0.004619111460557768,

cirq-core/cirq/vis/histogram_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from cirq.vis import integrated_histogram
2222

2323

24+
@pytest.mark.usefixtures('closefigures')
2425
@pytest.mark.parametrize('data', [range(10), {f'key_{i}': i for i in range(10)}])
2526
def test_integrated_histogram(data):
2627
ax = integrated_histogram(
@@ -39,6 +40,7 @@ def test_integrated_histogram(data):
3940
assert line.get_color() == 'r'
4041

4142

43+
@pytest.mark.usefixtures('closefigures')
4244
def test_multiple_plots():
4345
_, ax = plt.subplots(1, 1)
4446
n = 53

cirq-core/cirq/vis/state_histogram_test.py

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import numpy as np
1818
from matplotlib import pyplot as plt
1919
import matplotlib as mpl
20+
import pytest
2021

2122
import cirq
2223
from cirq.devices import GridQubit
@@ -64,6 +65,7 @@ def test_get_state_histogram_multi_2():
6465
np.testing.assert_equal(values_to_plot, expected_values)
6566

6667

68+
@pytest.mark.usefixtures('closefigures')
6769
def test_plot_state_histogram_result():
6870
qubits = cirq.LineQubit.range(4)
6971
c = cirq.Circuit(
@@ -80,6 +82,7 @@ def test_plot_state_histogram_result():
8082
assert str(r1) == str(r2)
8183

8284

85+
@pytest.mark.usefixtures('closefigures')
8386
def test_plot_state_histogram_collection():
8487
qubits = cirq.LineQubit.range(4)
8588
c = cirq.Circuit(

cirq-google/cirq_google/engine/calibration_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ def test_calibration_heatmap():
191191
cg.Calibration(multi_qubit_data).heatmap('multi_value')
192192

193193

194+
@pytest.mark.usefixtures('closefigures')
194195
def test_calibration_plot_histograms():
195196
calibration = cg.Calibration(_CALIBRATION_DATA)
196197
_, ax = mpl.pyplot.subplots(1, 1)
@@ -208,6 +209,7 @@ def test_calibration_plot_histograms():
208209
cg.Calibration(multi_qubit_data).plot_histograms('multi_value')
209210

210211

212+
@pytest.mark.usefixtures('closefigures')
211213
def test_calibration_plot():
212214
calibration = cg.Calibration(_CALIBRATION_DATA)
213215
_, axs = calibration.plot('two_qubit_xeb')
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# pylint: disable=wrong-or-nonexistent-copyright-notice
22
from matplotlib import pyplot as plt
3+
import pytest
34

45
from cirq_google.optimizers.two_qubit_gates import example
56

67

8+
@pytest.mark.usefixtures('closefigures')
79
def test_gate_compilation_example():
810
plt.switch_backend('agg')
911
example.main(samples=10, max_infidelity=0.3)

conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import os
1616
import random
1717

18+
import pytest
19+
1820
# allow CI execution of isolated_packages_test.py without numpy
1921
try:
2022
import numpy
@@ -47,6 +49,14 @@ def pytest_addoption(parser):
4749
)
4850

4951

52+
@pytest.fixture
53+
def closefigures():
54+
import matplotlib.pyplot as plt
55+
56+
yield
57+
plt.close('all')
58+
59+
5060
# skip seeding for unset or empty CIRQ_TESTING_RANDOM_SEED
5161
if numpy is not None and os.environ.get('CIRQ_TESTING_RANDOM_SEED'):
5262
rngseed = int(os.environ['CIRQ_TESTING_RANDOM_SEED'])

examples/examples_test.py

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def test_example_runs_phase_estimator():
9191
examples.phase_estimator.main(qnums=(2,), repetitions=2)
9292

9393

94+
@pytest.mark.usefixtures('closefigures')
9495
def test_example_heatmaps():
9596
pytest.importorskip("cirq_google")
9697
plt.switch_backend('agg')
@@ -114,6 +115,7 @@ def test_example_runs_hhl():
114115
examples.hhl.main()
115116

116117

118+
@pytest.mark.usefixtures('closefigures')
117119
def test_example_runs_qubit_characterizations():
118120
examples.qubit_characterizations_example.main(
119121
minimum_cliffords=2, maximum_cliffords=6, cliffords_step=2
@@ -124,6 +126,7 @@ def test_example_swap_networks():
124126
examples.swap_networks.main()
125127

126128

129+
@pytest.mark.usefixtures('closefigures')
127130
def test_example_cross_entropy_benchmarking():
128131
examples.cross_entropy_benchmarking_example.main(
129132
repetitions=10, num_circuits=2, cycles=[2, 3, 4]

0 commit comments

Comments
 (0)