Dvirmor100 commited on
Commit
ded0705
·
1 Parent(s): a0918a5

Add quantum circuit visualization and execution functionality

Browse files

Introduced a `run_selected_circuit` function and integrated it into a Gradio interface for running and visualizing quantum circuits. Added helper functions `visualize_circuit` and `build_quantum_circuit` to construct and display circuits using PennyLane. This commits sets up the foundation for interactive quantum circuit exploration.

src/app.py CHANGED
@@ -1,6 +1,53 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return f"Hello {name}!"
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from circuit_builder import CircuitManager, circuits
3
+ from circuit_builder.circuits_helpers import visualize_circuit, build_quantum_circuit
4
 
5
+ def run_selected_circuit(circuit_index, randomize=False):
6
+ """
7
+ Run and visualize a selected circuit from the module.
8
 
9
+ Args:
10
+ circuit_index (int): Index of the circuit to run.
11
+
12
+ Returns:
13
+ tuple: Circuit information and visualization image.
14
+ """
15
+ try:
16
+ circuit_func, params, circuit_name, wires = circuits[circuit_index]
17
+ # Build the quantum circuit
18
+ constructed_circuit = build_quantum_circuit((circuit_func, params, circuit_name, wires))
19
+ # Visualize the circuit
20
+ visualize_circuit(constructed_circuit, *params, title=circuit_name,
21
+ save_path="output.png")
22
+ return "success", "output.png"
23
+
24
+ except IndexError:
25
+ return "Invalid circuit selection", None
26
+
27
+
28
+ # Gradio app interface
29
+ def run_app():
30
+ iface = gr.Interface(
31
+ fn=lambda circuit_choice, randomize: run_selected_circuit(
32
+ int(circuit_choice)-1, randomize),
33
+ inputs=[
34
+ gr.Radio(
35
+ choices=[str(i+1) for i in range(len(circuits))],
36
+ label="Choose Circuit Type"
37
+ ),
38
+ gr.Checkbox(label="Randomize Circuit", visible=False)
39
+ ],
40
+ outputs=[
41
+ gr.Textbox(label="Circuit Info"),
42
+ gr.Image(type="filepath", label="Circuit Image"),
43
+ ],
44
+ title="Quantum Circuit Visualizer",
45
+ description="Visualize circuits from the module or generate a randomized quantum circuit.",
46
+ )
47
+
48
+ # Launch interface
49
+ iface.launch()
50
+
51
+
52
+ if __name__ == "__main__":
53
+ run_app()
src/circuit_builder/circuits_helpers.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pennylane as qml
2
+ from pennylane import numpy as np
3
+ import matplotlib.pyplot as plt
4
+
5
+
6
+ def visualize_circuit(qnode, params, title="Quantum Circuit Visualization",
7
+ fig_bg_color="lightblue", ax_bg_color="whitesmoke",
8
+ text_color="darkblue", font_size=12, save_path=None):
9
+ """
10
+ Visualize a quantum circuit with customizable styling options.
11
+
12
+ Args:
13
+ qnode (qml.QNode): The quantum node representing the circuit.
14
+ params (array): Parameters to pass to the quantum node.
15
+ title (str): Title for the circuit diagram.
16
+ fig_bg_color (str): Background color for the figure.
17
+ ax_bg_color (str): Background color for the axis.
18
+ text_color (str): Color for the text labels.
19
+ font_size (int): Font size for the text labels.
20
+ save_path (str): File path to save the visualization (optional).
21
+
22
+ Returns:
23
+ None: Displays or saves the circuit visualization.
24
+ """
25
+ # Generate the matplotlib figure
26
+ fig, ax = qml.draw_mpl(qnode)(params)
27
+
28
+ # Customize the figure
29
+ fig.patch.set_facecolor(fig_bg_color)
30
+ ax.set_facecolor(ax_bg_color)
31
+
32
+ # Customize text labels
33
+ for text in ax.texts:
34
+ text.set_color(text_color)
35
+ text.set_fontsize(font_size)
36
+
37
+ # Add title
38
+ ax.set_title(title, color=text_color, fontsize=font_size + 2)
39
+
40
+ if save_path:
41
+ fig.savefig(save_path, dpi=300, bbox_inches="tight")
42
+ else:
43
+ plt.show()
44
+
45
+
46
+ def build_quantum_circuit(circuit_info, device_name="default.qubit"):
47
+ """
48
+ Build and execute a quantum circuit.
49
+
50
+ Parameters:
51
+ circuit_info (tuple): A tuple containing (circuit_function, params, name, wires).
52
+ device_name (str): Name of the device to use.
53
+
54
+ Returns:
55
+ function: A QNode function that evaluates the expectation values.
56
+ """
57
+ circuit_func, params, name, wires = circuit_info
58
+
59
+ # Create a device
60
+ dev = qml.device(device_name, wires=len(wires))
61
+
62
+ # Define a QNode
63
+ @qml.qnode(dev)
64
+ def quantum_circuit(*params):
65
+ # Call the circuit function and apply the gates
66
+ circuit_func(*params)()
67
+ # Return the expectation values of PauliZ for all wires
68
+ return [qml.expval(qml.PauliZ(w)) for w in wires]
69
+
70
+ return quantum_circuit
71
+
72
+
73
+ from circuit_builder.circuit_manager import circuits
74
+
75
+ if __name__ == "__main__":
76
+ circuit_func, params, name, wires = circuits[0]
77
+
78
+ # Build the quantum circuit
79
+ constructed_circuit = build_quantum_circuit((circuit_func, params, name, wires))
80
+
81
+ # Execute the circuit
82
+ result = constructed_circuit(*params)
83
+ print("Expectation values:", result)
84
+
85
+ # Visualize the circuit
86
+ visualize_circuit(constructed_circuit, *params, title="My Quantum Circuit",
87
+ save_path=None)