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_state_visualization.py
1122 views
1
# Interactive widget to visualize the state |ψ⟩ = cos(θ/2)|0⟩ + sin(θ/2)e^{iφ}|1⟩ on the Bloch sphere
2
3
def create_state_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
15
# Kernel to initialize a qubit and set it to the state |ψ⟩ = cos(θ/2)|0⟩ + sin(θ/2)e^{iφ}|1⟩
16
@cudaq.kernel
17
def state_kernel(theta: float, phi: float):
18
qubit = cudaq.qubit()
19
ry(theta, qubit)
20
rz(phi, qubit)
21
22
# Function to update the Bloch sphere plot based on the slider values
23
def update_bloch_sphere(theta, phi):
24
with output_bloch:
25
clear_output(wait=True) # Clear previous output
26
state = cudaq.get_state(state_kernel, theta, phi)
27
bloch_sphere = cudaq.add_to_bloch_sphere(state)
28
cudaq.show(bloch_sphere)
29
30
# Function to update the circuit diagram based on the slider values
31
def update_circuit_diagram(theta, phi):
32
with output_circuit:
33
clear_output(wait=True) # Clear previous output
34
print(cudaq.draw(state_kernel, theta, phi))
35
36
# Function to display the state in plain text with rounded angles
37
def update_state_output(theta, phi):
38
with output_state:
39
clear_output(wait=True) # Clear previous output
40
theta_half = round(theta / 2, 3)
41
phi = round(phi, 3)
42
expression = f"|ψ⟩ = cos({theta_half})|0⟩ + sin({theta_half})e^(i{phi})|1⟩"
43
print('|ψ⟩ = cos(θ/2)|0⟩ + sin(θ/2)e^(iφ)|1⟩')
44
print(expression)
45
46
# Create interactive sliders for angles
47
slider_theta = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='θ:', continuous_update=False)
48
slider_phi = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='φ:', continuous_update=False)
49
50
# Add a new title to the widget
51
title = widgets.HTML(value="<h3>Visualize the State |ψ⟩ with Varying Angles θ and φ (in radians)</h3>")
52
53
# Display the title and sliders
54
display(title)
55
56
# Create output widgets
57
output_bloch = widgets.Output()
58
output_circuit = widgets.Output()
59
output_state = widgets.Output()
60
61
def update_all(theta, phi):
62
update_bloch_sphere(theta, phi)
63
update_circuit_diagram(theta, phi)
64
update_state_output(theta, phi)
65
66
interactive_plot = interactive_output(update_all, {'theta': slider_theta, 'phi': slider_phi})
67
68
# Arrange sliders horizontally
69
slider_box = HBox([slider_theta, slider_phi])
70
71
# Display the state output above the sliders and the interactive plots side by side
72
display(VBox([output_state, slider_box, HBox([output_bloch, output_circuit])]))
73
74
# When imported, this will create and display a new widget
75
if __name__ == '__main__':
76
widget = create_state_widget()
77
display(widget)
78