Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,5 @@ docs/stubs/*

# Notebook testing images
test/ipynb/mpl/*.png
test/ipynb/mpl/*.zip
test/ipynb/mpl/result_test.json
Binary file added test/ipynb/mpl/references/conditional.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/ipynb/mpl/references/empty_circut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/ipynb/mpl/references/no_barriers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/ipynb/mpl/references/plot_barriers_false.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/ipynb/mpl/references/plot_barriers_true.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 60 additions & 5 deletions test/ipynb/mpl/test_circuit_matplotlib_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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__':
Expand Down
61 changes: 56 additions & 5 deletions test/ipynb/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os
import json
from PIL import Image, ImageChops, ImageDraw
import zipfile

SWD = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -97,20 +109,58 @@ def no_reference_html(result, title):
ret += '</tr></table></details>'
return ret

def _repr_html_(self):
ret = "<div>"
def diff_images(self):
for name in self.names:
ratio = diff_name = title = None
fullpath_name = os.path.join(self.directory, name)
fullpath_reference = os.path.join(self.directory, 'references', name)

if os.path.exists(os.path.join(SWD, fullpath_reference)):
ratio, diff_name = similarity_ratio(fullpath_name, fullpath_reference)
title = '<tt><b>%s</b> | %s </tt> | ratio: %s' % (name, self.data[name], ratio)
title = '<tt><b>%s</b> | %s </tt> | ratio: %s' % (name,
self.data[name]['testname'],
ratio)
if ratio == 1:
self.exact_match.append(fullpath_name)
else:
self.mismatch.append(fullpath_name)
else:
self.missing.append(fullpath_name)

self.data[name]['ratio'] = ratio
self.data[name]['diff_name'] = diff_name
self.data[name]['title'] = title

def summary(self):
ret = ''

if len(self.mismatch) >= 2:
zipfiles(self.mismatch, 'mpl/mismatch.zip')
ret += '<div><a href="mpl/mismatch.zip">' \
'Download %s mismatch results as a zip</a></div>' % len(self.mismatch)

if len(self.mismatch) >= 2:
zipfiles(self.missing, 'mpl/missing.zip')
ret += '<div><a href="mpl/missing.zip">' \
'Download %s missing results as a zip</a></div>' % len(self.missing)

return ret

def _repr_html_(self):
ret = self.summary()
ret += "<div>"
for name in self.names:
fullpath_name = os.path.join(self.directory, name)
fullpath_reference = os.path.join(self.directory, 'references', name)
if os.path.exists(os.path.join(SWD, fullpath_reference)):
if self.data[name]['ratio'] == 1:
ret += Results.passed_result_html(fullpath_name, fullpath_reference,
diff_name, title)
self.data[name]['diff_name'],
self.data[name]['title'])
else:
ret += Results.failed_result_html(fullpath_name, fullpath_reference,
diff_name, title)
self.data[name]['diff_name'],
self.data[name]['title'])
else:
title = 'Download <a download="%s" href="%s">this image</a> to <tt>%s</tt>' \
' and add/push to the repo</td>' % (name, fullpath_name, fullpath_reference)
Expand All @@ -125,3 +175,4 @@ def _repr_html_(self):
if file.endswith(".png") and not file.endswith(".diff.png"):
result_files.append(file)
results = Results(result_files, 'mpl')
results.diff_images()
Binary file not shown.
Binary file not shown.
165 changes: 0 additions & 165 deletions test/python/visualization/test_circuit_matplotlib_drawer.py

This file was deleted.