Path: blob/main/quick-start-to-quantum/interactive_widget/borns_rule.py
1127 views
def create_borns_rule_widget():1# Clear any existing widgets2import IPython.display3IPython.display.clear_output()4# Widget to visualize Born's Rule for the state |ψ⟩ = cos(θ/2)|0⟩ + sin(θ/2)e^{iφ}|1⟩ along with its Bloch sphere representation5import cudaq6import ipywidgets as widgets7from ipywidgets import interactive_output, HBox, VBox8from IPython.display import display, clear_output9import numpy as np10import matplotlib.pyplot as plt1112# Kernel to initialize a qubit and set it to the state |ψ⟩ = cos(θ/2)|0⟩ + sin(θ/2)e^{iφ}|1⟩13@cudaq.kernel14def state_kernel(theta: float, phi: float):15qubit = cudaq.qubit()16ry(theta, qubit)17rz(phi, qubit)1819# Function to update the Bloch sphere plot based on the slider values20def update_bloch_sphere(theta, phi):21with output_bloch:22clear_output(wait=True) # Clear previous output23state = cudaq.get_state(state_kernel, theta, phi)24bloch_sphere = cudaq.add_to_bloch_sphere(state)25cudaq.show(bloch_sphere)2627# Function to update the probability amplitude bar chart28def update_prob_chart(theta, phi):29with output_prob:30clear_output(wait=True) # Clear previous output31alpha_squared = np.cos(theta/2)**232beta_squared = np.sin(theta/2)**23334plt.figure(figsize=(4,4)) # Make the figure square and smaller35plt.bar(['0', '1'], [alpha_squared, beta_squared], color=['#76b900', '#76b900']) # NVIDIA green36plt.ylim(0, 1)37plt.ylabel('Probability')38plt.title('Measurement Probability Distribution')39plt.show()4041# Function to display the state in plain text with rounded angles42def update_state_output(theta, phi):43with output_state:44clear_output(wait=True) # Clear previous output45theta_half = round(theta / 2, 3)46phi = round(phi, 3)47expression = f"|ψ⟩ = cos({theta_half})|0⟩ + sin({theta_half})e^(i{phi})|1⟩"48print('|ψ⟩ = cos(θ/2)|0⟩ + sin(θ/2)e^(iφ)|1⟩ = α|0⟩ + β|1⟩')49print(expression)50alpha = np.cos(theta_half)51beta = np.sin(theta_half) * np.exp(1j * phi)52alpha_squared = np.abs(alpha)**253beta_squared = np.abs(beta)**254print(f"|α|^2: {alpha_squared:.3f}")55print(f"|β|^2: {beta_squared:.3f}")5657# Create interactive sliders for angles58slider_theta = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='θ:', continuous_update=False)59slider_phi = widgets.FloatSlider(min=0, max=2*np.pi, step=0.01, value=0, description='φ:', continuous_update=False)6061# Add a new title to the widget62title = widgets.HTML(value="<h3>Visualize Born's Rule for the State |ψ⟩ with Varying Angles θ and φ (in radians)</h3>")6364# Create output widgets65output_bloch = widgets.Output()66output_prob = widgets.Output()67output_state = widgets.Output()6869def update_all(theta, phi):70update_bloch_sphere(theta, phi)71update_prob_chart(theta, phi)72update_state_output(theta, phi)7374# Create the interactive output and immediately call update_all with initial values75interactive_plot = interactive_output(update_all, {'theta': slider_theta, 'phi': slider_phi})7677# Arrange sliders horizontally78slider_box = HBox([slider_theta, slider_phi])7980# Single display statement for the entire widget81display(VBox([82title,83output_state,84slider_box,85HBox([output_bloch, output_prob])86]))8788# When imported, this will create and display a new widget89if __name__ == '__main__':90widget = create_borns_rule_widget()91display(widget)9293