Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
NVIDIA
GitHub Repository: NVIDIA/cuda-q-academic
Path: blob/main/quick-start-to-quantum/interactive_widget/bloch_sphere_rotation.py
1127 views
1
# 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)
2
3
def create_rotation_widget():
4
# Clear any existing widgets
5
import IPython.display
6
IPython.display.clear_output()
7
8
import cudaq
9
import ipywidgets as widgets
10
from ipywidgets import interactive_output, HBox, VBox
11
from IPython.display import display, clear_output
12
import numpy as np
13
14
# Kernel to initialize a qubit in the zero ket state and rotate it about the x, y, and z axis by given angles
15
@cudaq.kernel
16
def rotation_kernel(theta_x: float, theta_y: float, theta_z: float):
17
qubit = cudaq.qubit()
18
rx(theta_x, qubit)
19
ry(theta_y, qubit)
20
rz(theta_z, qubit)
21
22
# Function to update the Bloch sphere plot based on the slider values
23
def update_bloch_sphere(theta_x, theta_y, theta_z):
24
state = cudaq.get_state(rotation_kernel, theta_x, theta_y, theta_z)
25
bloch_sphere = cudaq.add_to_bloch_sphere(state)
26
cudaq.show(bloch_sphere)
27
28
# Function to update the circuit diagram based on the slider values
29
def update_circuit_diagram(theta_x, theta_y, theta_z):
30
# Draw the circuit diagram
31
print(cudaq.draw(rotation_kernel, theta_x, theta_y, theta_z))
32
33
# Create interactive sliders for angles
34
slider_x = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='θx:', continuous_update=False)
35
slider_y = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='θy:', continuous_update=False)
36
slider_z = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='θz:', continuous_update=False)
37
38
# Add a new title to the widget
39
title = 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>")
40
41
# Create interactive outputs
42
output_bloch = widgets.Output()
43
output_circuit = widgets.Output()
44
45
def update_all(theta_x, theta_y, theta_z):
46
with output_bloch:
47
output_bloch.clear_output(wait=True)
48
update_bloch_sphere(theta_x, theta_y, theta_z)
49
with output_circuit:
50
output_circuit.clear_output(wait=True)
51
update_circuit_diagram(theta_x, theta_y, theta_z)
52
53
# Observe changes in sliders and update the outputs
54
slider_x.observe(lambda _: update_all(slider_x.value, slider_y.value, slider_z.value), 'value')
55
slider_y.observe(lambda _: update_all(slider_x.value, slider_y.value, slider_z.value), 'value')
56
slider_z.observe(lambda _: update_all(slider_x.value, slider_y.value, slider_z.value), 'value')
57
58
# Arrange sliders horizontally
59
slider_box = HBox([slider_x, slider_y, slider_z])
60
61
# Single display statement for the entire widget
62
display(VBox([
63
title,
64
slider_box,
65
HBox([output_bloch, output_circuit])
66
]))
67
68
# Initial update
69
update_all(slider_x.value, slider_y.value, slider_z.value)
70
71
72
# When imported, this will create and display a new widget
73
if __name__ == '__main__':
74
widget = create_rotation_widget()
75
display(widget)
76
77