File size: 1,434 Bytes
41957a8
 
 
e0d7f28
 
 
 
 
 
 
 
 
 
 
41957a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pennylane as qml
import matplotlib.pyplot as plt


def visualize_circuit(
    qnode_func,
    params,
    title="Quantum Circuit Visualization",
    fig_bg_color="lightblue",
    ax_bg_color="whitesmoke",
    text_color="darkblue",
    font_size=12,
    save_path=None,
):
    """
    Visualize a quantum circuit with customizable styling options.

    Args:
        qnode_func: The quantum node representing the circuit.
        params (array): Parameters to pass to the quantum node.
        title (str): Title for the circuit diagram.
        fig_bg_color (str): Background color for the figure.
        ax_bg_color (str): Background color for the axis.
        text_color (str): Color for the text labels.
        font_size (int): Font size for the text labels.
        save_path (str): File path to save the visualization (optional).

    Returns:
        None: Displays or saves the circuit visualization.
    """
    # Generate the matplotlib figure
    fig, ax = qml.draw_mpl(qnode_func)(params)

    # Customize the figure
    fig.patch.set_facecolor(fig_bg_color)
    ax.set_facecolor(ax_bg_color)

    # Customize text labels
    for text in ax.texts:
        text.set_color(text_color)
        text.set_fontsize(font_size)

    # Add title
    ax.set_title(title, color=text_color, fontsize=font_size + 2)

    if save_path:
        fig.savefig(save_path, dpi=300, bbox_inches="tight")
    else:
        plt.show()