def test_density_matrix_circle_sizes(size, show_text): matrix = cirq.testing.random_density_matrix(size) # Check that the correct title is being shown ax = plot_density_matrix(matrix, show_text=show_text, title='Test Density Matrix Plot') # Check that the radius of all the circles in the matrix is correct circles = list( filter(lambda x: isinstance(x, patches.Circle), ax.get_children())) mean_radius = np.mean([c.radius for c in circles if c.fill]) mean_value = np.mean(np.abs(matrix)) circles = np.array( sorted(circles, key=lambda x: (x.fill, x.center[0], -x.center[1]))).reshape((2, size, size)) for i in range(size): for j in range(size): assert np.isclose( np.abs(matrix[i, j]) * mean_radius / mean_value, circles[1, i, j].radius) # Check that all the rectangles are of the right height, and only on the diagonal elements rects = list( filter( lambda x: isinstance(x, patches.Rectangle) and x.get_alpha() is not None, ax.get_children(), )) assert len(rects) == size mean_size = np.mean([r.get_height() for r in rects]) mean_value = np.trace(np.abs(matrix)) / size rects = np.array(sorted(rects, key=lambda x: x.get_x())) for i in range(size): # Ensuring that the rectangle is the right height assert np.isclose( np.abs(matrix[i, i]) * mean_size / mean_value, rects[i].get_height()) rect_points = rects[i].get_bbox().get_points() # Checking for the x position of the rectangle corresponding x of the center of the circle assert np.isclose((rect_points[0, 0] + rect_points[1, 0]) / 2, circles[1, i, i].center[0]) # Asserting that only the diagonal elements are on assert (np.abs((rect_points[0, 1] + rect_points[1, 1]) / 2 - circles[1, i, i].center[1]) <= circles[0, i, i].radius * 1.5)
def test_density_matrix_sizes_upper_bounds(size, show_text): matrix = cirq.testing.random_density_matrix(size) ax = plot_density_matrix(matrix, show_text=show_text, title='Test Density Matrix Plot') circles = list(filter(lambda x: isinstance(x, patches.Circle), ax.get_children())) max_radius = np.max([c.radius for c in circles if c.fill]) rects = list( filter( lambda x: isinstance(x, patches.Rectangle) and x.get_alpha() is not None, ax.get_children(), ) ) max_height = np.max([r.get_height() for r in rects]) max_width = np.max([r.get_width() for r in rects]) assert max_height <= 1.0, "Some rectangle is exceeding out of it's cell size" assert max_width <= 1.0, "Some rectangle is exceeding out of it's cell size" assert max_radius * 2 <= 1.0, "Some circle is exceeding out of it's cell size"
def test_density_matrix_plotter(size, show_text): matrix = cirq.testing.random_density_matrix(size) # Check that the title shows back up ax = plot_density_matrix(matrix, show_text=show_text, title='Test Density Matrix Plot') assert ax.get_title() == 'Test Density Matrix Plot' # Check that the objects in the plot are only those we expect and nothing new was added for obj in ax.get_children(): assert isinstance( obj, ( patches.Circle, spines.Spine, axis.XAxis, axis.YAxis, lines.Line2D, patches.Rectangle, text.Text, ), )
def test_density_matrix_type_error(matrix): with pytest.raises(ValueError, match="Incorrect shape for density matrix:*"): plot_density_matrix(matrix)