loom.visualizer.plotting_utils

Copyright (c) Entropica Labs Pte Ltd 2025.

Use, distribution and reproduction of this program in its source or compiled form is prohibited without the express written consent of Entropica Labs Pte Ltd.

loom.visualizer.plotting_utils.average_color_hex(color_list)[source]

Calculate the average color from a list of colors in HEX format.

Parameters:

color_list (list[str]) – List of hexadecimal colors

Returns:

The average color in HEX format

Return type:

str

loom.visualizer.plotting_utils.center_of_points(points)[source]

Calculate the center of a list of points.

Return type:

ndarray

loom.visualizer.plotting_utils.center_of_scatter_plot(scatter_plot)[source]

Calculate the center of the points contained in a scatter plot.

Return type:

ndarray

loom.visualizer.plotting_utils.change_color_brightness(hex_color, factor)[source]

Change the brightness of a color by a given factor.

Parameters:
  • hex_color (str) – The hexadecimal color code (e.g., “#RRGGBB”).

  • factor (float) – The factor by which the brightness of the color should be multiplied.

Returns:

The new color in HEX format.

Return type:

str

loom.visualizer.plotting_utils.convert_circuit_to_nx_graph(circ)[source]

Construct a NetworkX directed graph (DiGraph) from a circuit. The nodes of the graph are all the subcircuits contained in the circuit object. The edges are directed from every circuit to its subcircuits.

Parameters:

circ (Circuit) – Circuit from which the graph should be constructed

Return type:

tuple[DiGraph, list[str]]

Returns:

  • nx.DiGraph (Directed graph representing the circuit)

  • list[str] (List of labels for the nodes in BFS traversal order)

loom.visualizer.plotting_utils.draw_half_circle(center, r, direction=0, name='', text='', fillcolor='white', line=None, showlegend=True, legendgroup=None)[source]

Generate a scatter plot for a half circle.

Parameters:
  • center (list[float]) – Center point around which the half circle should be drawn

  • r (float) – Radius of the half circle

  • direction (float | None) –

    Direction in which the half circle should point. E.g.,

    • If direction == 0, half circle points upwards.

    • If direction == pi/2, half circle points right.

    • If direction == - pi/2, half circle points left.

  • name (str | None) – Name parameter of the scatter plot

  • text (str | None) – Text parameter of the scatter plot

  • fillcolor (str | None) – Fill color of the half circle

  • line (dict | None) – Line parameter of the scatter plot

  • showlegend (bool, optional) – If True (the default), the stabilizers are shown in the legend.

  • legendgroup (str | None) – Legend group to which the stabilizers belong.

Returns:

Scatter plot with the half circle

Return type:

go.Scatter

loom.visualizer.plotting_utils.get_angle_from_x_axis(point1, point2)[source]

Calculate the angle between the vector from point1 to point2 and the x-axis.

E.g. for point1 == [0,0] and point2 == [1,0], the angle is 0. For point1 == [0,0] and point2 == [0,1], the angle is pi/2 (90 degree).

Return type:

float

loom.visualizer.plotting_utils.get_font_color(background_color)[source]

Determine font color based on background color. The font color is white for dark backgrounds and gray or black for bright backgrounds.

Parameters:

background_color (str) – Background color in hexadecimal format (e.g., ‘#RRGGBB’)

Returns:

Font color for a good contrast with the background color

Return type:

str

loom.visualizer.plotting_utils.get_label_for_circuit(circ)[source]

Generate a label for a circuit. If the circuit has subcircuits, the label is simply the name attribute of the circuit. If the circuit has no subcircuits, the label contains the circuit’s name (specifying the gate) and the labels of the channels that are involved in the circuit.

Parameters:

circ (Circuit) – Circuit for which the label should be generated

Returns:

Label for the circuit

Return type:

str

loom.visualizer.plotting_utils.hex_to_rgb(hex_color)[source]

Converts a hexadecimal color code to a list of RGB values between 0 and 255.

Parameters:

hex_color (str) – The hexadecimal color code (e.g., “#RRGGBB”).

Returns:

RGB values between 0 and 255 representing the color.

Return type:

list[int]

loom.visualizer.plotting_utils.interpolate_values(point, interpolation_points, interpolation_values, interpolation_power=None, min_distance=0.001)[source]

Perform interpolation of the values in interpolation_values which are associated to the points in interpolation_points, based on the distances to a given point. The values in interpolation_values are either floats or lists of floats. If they are a list, the interpolation is done element- wise. E.g. every interpolation point could have a tuple (r, g, b) of three integers, representing an RGB color. Then the weighted average of these RGB tuples is calculated with weights determined by the distances to these points.

Parameters:
  • point (tuple[float, float]) – Tuple representing the coordinates of the given point

  • interpolation_points (list[tuple[float, float]]) – List of points at which the values in interpolation_values are given

  • interpolation_values (list[float] | list[list[float]]) – List of values associated to the points in interpolation_points. The values can be floats or lists of floats. If they are lists, the interpolation is done element-wise.

  • interpolation_power (float | None) – The interpolation_power parameter determines how rapidly the interpolated value changes with increasing distance. The weights with which the values of the n points are interpolated are given as 1 / distance**interpolation_power. The default value is 2.

  • min_distance (float | None) – Points which are closer to one of the given points than min_distance will get a value equal to the value of this point. This is to prevent division by zero when calculating the weights.

Returns:

Interpolated value(s) based on the distances from the given point. The length of the array is equal to the length of the values in interpolation_values.

Return type:

np.ndarray

loom.visualizer.plotting_utils.order_points_counterclockwise(points)[source]

Order the provided list of points counterclockwise around the center point.

Parameters:

points (list[tuple[float, float, list[any]]]) – List of points to be ordered. Every element in the list is a tuple which contains the x and y coordinate of the point as the first two elements. The third element is a list which can store additional metadata.

Returns:

List of points ordered counterclockwise around the center point of the provided list. The list still contains the original metadata. As an additional metadata, the angle for the every point was added to its respective list.

Return type:

list[tuple[float, float, list[any]]]

loom.visualizer.plotting_utils.point_in_polygon(x, y, polygon)[source]

Check if a point (x, y) is inside a polygon defined by its corners. This algorithm is based on the ray casting algorithm. Note that the behaviour for points on the edges or very close to them is undefined.

Parameters:
  • x (float) – x coordinate of the point to check

  • y (float) – y coordinate of the point to check

  • polygon (list[tuple[float, float]]) – List of tuples representing the corners of the polygon

Returns:

True if the point is inside the polygon, False otherwise

Return type:

bool

loom.visualizer.plotting_utils.rgb_to_hex(rgb_color)[source]

Converts a color from RGB format to HEX format.

Parameters:

rgb_color (list[int]) – RGB values between 0 and 255 representing the color.

Returns:

The hexadecimal color code (e.g., “#RRGGBB”).

Return type:

str