Spaces:
Runtime error
Runtime error
Commit ·
41957a8
1
Parent(s): 5db11bc
feat(visualization): create visualize_circuit function
Browse files- src/utils/visualization.py +41 -0
src/utils/visualization.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pennylane as qml
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
|
| 4 |
+
def visualize_circuit(qnode_func, params, title="Quantum Circuit Visualization",
|
| 5 |
+
fig_bg_color="lightblue", ax_bg_color="whitesmoke",
|
| 6 |
+
text_color="darkblue", font_size=12, save_path=None):
|
| 7 |
+
"""
|
| 8 |
+
Visualize a quantum circuit with customizable styling options.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
qnode_func: The quantum node representing the circuit.
|
| 12 |
+
params (array): Parameters to pass to the quantum node.
|
| 13 |
+
title (str): Title for the circuit diagram.
|
| 14 |
+
fig_bg_color (str): Background color for the figure.
|
| 15 |
+
ax_bg_color (str): Background color for the axis.
|
| 16 |
+
text_color (str): Color for the text labels.
|
| 17 |
+
font_size (int): Font size for the text labels.
|
| 18 |
+
save_path (str): File path to save the visualization (optional).
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
None: Displays or saves the circuit visualization.
|
| 22 |
+
"""
|
| 23 |
+
# Generate the matplotlib figure
|
| 24 |
+
fig, ax = qml.draw_mpl(qnode_func)(params)
|
| 25 |
+
|
| 26 |
+
# Customize the figure
|
| 27 |
+
fig.patch.set_facecolor(fig_bg_color)
|
| 28 |
+
ax.set_facecolor(ax_bg_color)
|
| 29 |
+
|
| 30 |
+
# Customize text labels
|
| 31 |
+
for text in ax.texts:
|
| 32 |
+
text.set_color(text_color)
|
| 33 |
+
text.set_fontsize(font_size)
|
| 34 |
+
|
| 35 |
+
# Add title
|
| 36 |
+
ax.set_title(title, color=text_color, fontsize=font_size + 2)
|
| 37 |
+
|
| 38 |
+
if save_path:
|
| 39 |
+
fig.savefig(save_path, dpi=300, bbox_inches="tight")
|
| 40 |
+
else:
|
| 41 |
+
plt.show()
|