diff --git a/.gitignore b/.gitignore index c9a349c3276c..b3d4b6bfd7a2 100644 --- a/.gitignore +++ b/.gitignore @@ -145,4 +145,5 @@ docs/stubs/* # Notebook testing images test/ipynb/mpl/*.png +test/ipynb/mpl/*.zip test/ipynb/mpl/result_test.json diff --git a/test/ipynb/mpl/references/conditional.png b/test/ipynb/mpl/references/conditional.png new file mode 100644 index 000000000000..4e247016969b Binary files /dev/null and b/test/ipynb/mpl/references/conditional.png differ diff --git a/test/ipynb/mpl/references/empty_circut.png b/test/ipynb/mpl/references/empty_circut.png new file mode 100644 index 000000000000..3bad502f0757 Binary files /dev/null and b/test/ipynb/mpl/references/empty_circut.png differ diff --git a/test/ipynb/mpl/references/no_barriers.png b/test/ipynb/mpl/references/no_barriers.png new file mode 100644 index 000000000000..d171482bf77d Binary files /dev/null and b/test/ipynb/mpl/references/no_barriers.png differ diff --git a/test/ipynb/mpl/references/plot_barriers_false.png b/test/ipynb/mpl/references/plot_barriers_false.png new file mode 100644 index 000000000000..fc57f227285e Binary files /dev/null and b/test/ipynb/mpl/references/plot_barriers_false.png differ diff --git a/test/ipynb/mpl/references/plot_barriers_true.png b/test/ipynb/mpl/references/plot_barriers_true.png new file mode 100644 index 000000000000..2b59ee72f26b Binary files /dev/null and b/test/ipynb/mpl/references/plot_barriers_true.png differ diff --git a/test/ipynb/mpl/test_circuit_matplotlib_drawer.py b/test/ipynb/mpl/test_circuit_matplotlib_drawer.py index e079ad7c2b3a..4584588b9ccf 100644 --- a/test/ipynb/mpl/test_circuit_matplotlib_drawer.py +++ b/test/ipynb/mpl/test_circuit_matplotlib_drawer.py @@ -19,8 +19,8 @@ from contextlib import contextmanager from qiskit.test import QiskitTestCase -from qiskit import QuantumCircuit, QuantumRegister -from qiskit.visualization import circuit_drawer +from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister +from qiskit.visualization.circuit_visualization import _matplotlib_circuit_drawer RESULTDIR = os.path.dirname(os.path.abspath(__file__)) @@ -33,7 +33,7 @@ def save_data(image_filename, testname): data = json.load(datafile) else: data = {} - data[image_filename] = testname + data[image_filename] = {'testname': testname} with open(datafilename, 'w') as datafile: json.dump(data, datafile) @@ -63,7 +63,13 @@ class TestMatplotlibDrawer(QiskitTestCase): """Circuit MPL visualization""" def setUp(self): - self.circuit_drawer = save_data_wrap(circuit_drawer, str(self)) + self.circuit_drawer = save_data_wrap(_matplotlib_circuit_drawer, str(self)) + + def test_empty_circuit(self): + """Test empty circuit""" + circuit = QuantumCircuit() + + self.circuit_drawer(circuit, filename='empty_circut.png') def test_long_name(self): """Test to see that long register names can be seen completely @@ -81,7 +87,56 @@ def test_long_name(self): circuit.h(qr) circuit.h(qr) - self.circuit_drawer(circuit, output='mpl', filename='long_name.png') + self.circuit_drawer(circuit, filename='long_name.png') + + def test_conditional(self): + """Test that circuits with conditionals draw correctly + """ + qr = QuantumRegister(2, 'q') + cr = ClassicalRegister(2, 'c') + circuit = QuantumCircuit(qr, cr) + + # check gates are shifted over accordingly + circuit.h(qr) + circuit.measure(qr, cr) + circuit.h(qr[0]).c_if(cr, 2) + + self.circuit_drawer(circuit, filename='conditional.png') + + def test_plot_barriers(self): + """Test to see that plotting barriers works. + If it is set to False, no blank columns are introduced""" + + # generate a circuit with barriers and other barrier like instructions in + q = QuantumRegister(2, 'q') + c = ClassicalRegister(2, 'c') + circuit = QuantumCircuit(q, c) + + # check for barriers + circuit.h(q[0]) + circuit.barrier() + + # check for other barrier like commands + circuit.h(q[1]) + + # this import appears to be unused, but is actually needed to get snapshot instruction + import qiskit.extensions.simulator # pylint: disable=unused-import + circuit.snapshot('1') + + # check the barriers plot properly when plot_barriers= True + self.circuit_drawer(circuit, filename='plot_barriers_true.png', plot_barriers=True) + self.circuit_drawer(circuit, filename='plot_barriers_false.png', plot_barriers=False) + + def test_no_barriers_false(self): + """Generate the same circuit as test_plot_barriers but without the barrier commands + as this is what the circuit should look like when displayed with plot barriers false""" + q1 = QuantumRegister(2, 'q') + c1 = ClassicalRegister(2, 'c') + circuit = QuantumCircuit(q1, c1) + circuit.h(q1[0]) + circuit.h(q1[1]) + + self.circuit_drawer(circuit, filename='no_barriers.png', plot_barriers=False) if __name__ == '__main__': diff --git a/test/ipynb/results.py b/test/ipynb/results.py index 59565f8e2364..8d920ccda3bd 100644 --- a/test/ipynb/results.py +++ b/test/ipynb/results.py @@ -15,6 +15,7 @@ import os import json from PIL import Image, ImageChops, ImageDraw +import zipfile SWD = os.path.dirname(os.path.abspath(__file__)) @@ -56,15 +57,26 @@ def black_or_b(diff_image, image, reference, opacity=0.85): shade = new_gray(size, 0) new = reference.copy() new.paste(shade, mask=mask) + if image.size != new.size: + image = image.resize(new.size) new.paste(image, mask=thresholded_diff) return new +def zipfiles(files, zipname): + with zipfile.ZipFile(zipname, "w", zipfile.ZIP_DEFLATED) as zipf: + for file in files: + zipf.write(file, arcname=os.path.basename(file)) + + class Results: def __init__(self, names, directory): self.names = names self.directory = directory self.data = {} + self.exact_match = [] + self.mismatch = [] + self.missing = [] datafilename = os.path.join(SWD, directory, 'result_test.json') if os.path.exists(datafilename): with open(datafilename, 'r') as datafile: @@ -97,20 +109,58 @@ def no_reference_html(result, title): ret += '' return ret - def _repr_html_(self): - ret = "