Path: blob/main/quick-start-to-quantum/interactive_widget/bloch_sphere_rotation.py
1127 views
# Interactive widget to visualize the final qubit state on the Bloch sphere after rotating about the X, Y, and Z axes with varying angles (θx, θy, θz in radians)12def create_rotation_widget():3# Clear any existing widgets4import IPython.display5IPython.display.clear_output()67import cudaq8import ipywidgets as widgets9from ipywidgets import interactive_output, HBox, VBox10from IPython.display import display, clear_output11import numpy as np1213# Kernel to initialize a qubit in the zero ket state and rotate it about the x, y, and z axis by given angles14@cudaq.kernel15def rotation_kernel(theta_x: float, theta_y: float, theta_z: float):16qubit = cudaq.qubit()17rx(theta_x, qubit)18ry(theta_y, qubit)19rz(theta_z, qubit)2021# Function to update the Bloch sphere plot based on the slider values22def update_bloch_sphere(theta_x, theta_y, theta_z):23state = cudaq.get_state(rotation_kernel, theta_x, theta_y, theta_z)24bloch_sphere = cudaq.add_to_bloch_sphere(state)25cudaq.show(bloch_sphere)2627# Function to update the circuit diagram based on the slider values28def update_circuit_diagram(theta_x, theta_y, theta_z):29# Draw the circuit diagram30print(cudaq.draw(rotation_kernel, theta_x, theta_y, theta_z))3132# Create interactive sliders for angles33slider_x = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='θx:', continuous_update=False)34slider_y = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='θy:', continuous_update=False)35slider_z = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='θz:', continuous_update=False)3637# Add a new title to the widget38title = widgets.HTML(value="<h3>Visualize the Final Qubit State on the Bloch Sphere after Rotating about the X, Y and Z Axis with Varying Angles (θx, θy, θz in radians)</h3>")3940# Create interactive outputs41output_bloch = widgets.Output()42output_circuit = widgets.Output()4344def update_all(theta_x, theta_y, theta_z):45with output_bloch:46output_bloch.clear_output(wait=True)47update_bloch_sphere(theta_x, theta_y, theta_z)48with output_circuit:49output_circuit.clear_output(wait=True)50update_circuit_diagram(theta_x, theta_y, theta_z)5152# Observe changes in sliders and update the outputs53slider_x.observe(lambda _: update_all(slider_x.value, slider_y.value, slider_z.value), 'value')54slider_y.observe(lambda _: update_all(slider_x.value, slider_y.value, slider_z.value), 'value')55slider_z.observe(lambda _: update_all(slider_x.value, slider_y.value, slider_z.value), 'value')5657# Arrange sliders horizontally58slider_box = HBox([slider_x, slider_y, slider_z])5960# Single display statement for the entire widget61display(VBox([62title,63slider_box,64HBox([output_bloch, output_circuit])65]))6667# Initial update68update_all(slider_x.value, slider_y.value, slider_z.value)697071# When imported, this will create and display a new widget72if __name__ == '__main__':73widget = create_rotation_widget()74display(widget)757677