量子计算原理 ============== 量子计算流程的教学实验 NMR现象与信号 ~~~~~~~~~~~~~ 最基本的NMR实验,用于观察核磁共振现象: .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse from spinqlablink import print_graph def nmr_signal_experiment(): """NMR Phenomenon and Signal Experiment""" # Connect to device spinqlablink = SpinQLabLink("192.168.9.121", 8181, "username", "password") spinqlablink.connect() spinqlablink.wait_for_login() try: # Register experiment _, exp_para = spinqlablink.register_experiment(ExperimentType.NMR_PHENOMENON_AND_SIGNAL) # Set pulse sequence exp_para.pulses = [Pulse(path=0, width=40, amplitude=100, phase=90, detuning=0)] # Set frequency parameters exp_para.freq_h = 37.852105 # Hydrogen resonance frequency exp_para.freq_p = 15.322872 # Phosphorus resonance frequency exp_para.makePps = True exp_para.samplePath = 0 exp_para.custom_freq = False # Run experiment spinqlablink.run_experiment() spinqlablink.wait_for_experiment_completion() # Get and display results result = spinqlablink.get_experiment_result() print_graph(result["result"]) finally: spinqlablink.deregister_experiment() spinqlablink.disconnect() if __name__ == "__main__": nmr_signal_experiment() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择NMR_PHENOMENON_AND_SIGNAL(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Pulse**: 定义脉冲序列,设置脉冲的路径、宽度、振幅、相位和频率偏移(详情参阅 :class:`spinqlablink.Pulse` 类) - **freq_h**: 氢共振频率(hz) [0-100M] - **freq_p**: 磷共振频率(hz) [0-100M] - **makePps**: 是否使用PPS脉冲序列 - **samplePath**: 采样路径,0=氢通道,1=磷通道 [0-1] - **custom_freq**: 是否使用自定义频率 [True,False] 实验结果解析 ^^^^^^^^^^^^ .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "fftMod":[[x,y],...], "fidMod":[[x,y],...], "fftFit":[[x,y],...] }, { # step 1}, ... ] } } } - **graph**: 图表数据,包含实部和虚部,graph中包含1个step,每个step都可能包含以下数据: - **fidRe**: fid实部数据 - **fidIm**: fid虚部数据 - **fidMod**: fid模值 - **lorenz**: Lorenz拟合曲线 - **fftRe**: fft实部数据 - **fftIm**: fft虚部数据 - **fftMod**: fft模值 - **fftFit**: fft拟合曲线 拉比振荡实验 ~~~~~~~~~~~~ 测量不同脉冲宽度下的拉比振荡,确定π/2和π脉冲宽度: .. code-block:: python import time import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt from spinqlablink import SpinQLabLink, ExperimentType, Pulse def rabi_oscillation_experiment(): """Rabi Oscillation Experiment - Pulse Width Scanning""" spinqlablink = SpinQLabLink("192.168.9.121", 8181, "username", "password") spinqlablink.connect() spinqlablink.wait_for_login() # Pulse width range width_list = [i * 40 for i in range(10)] # 0, 40, 80, ..., 360 μs intensities = [] try: for width in width_list: print(f"Testing pulse width: {width} μs") # Register experiment _, exp_para = spinqlablink.register_experiment(ExperimentType.RABI_OSCILLATIONS) # Set parameters exp_para.freq_h = 37.852105 exp_para.freq_p = 15.322872 exp_para.makePps = True exp_para.samplePath = 0 exp_para.custom_freq = False exp_para.pulses = [Pulse(path=0, width=width, amplitude=100, phase=90, detuning=0)] # Run experiment spinqlablink.run_experiment() spinqlablink.wait_for_experiment_completion() # Get results result = spinqlablink.get_experiment_result() signal_data = result["result"]["real"] # Calculate signal intensity (FFT peak) if signal_data is not None: if isinstance(signal_data, (int, float)): signal_array = np.array([signal_data]) elif isinstance(signal_data, (list, tuple, np.ndarray)): signal_array = np.array([point[1] for point in signal_data if isinstance(point, (list, tuple, np.ndarray))]) else: signal_array = np.array([]) if len(signal_array) > 0: intensity = np.max(np.abs(np.fft.fft(signal_array))) else: intensity = 0 intensities.append(intensity) else: intensities.append(0) spinqlablink.deregister_experiment() time.sleep(2) # Wait for system stabilization # Fit Rabi oscillation curve def rabi_function(x, A, B, freq, phase): return A * np.cos(2 * np.pi * freq * x + phase) + B try: popt, _ = curve_fit(rabi_function, width_list, intensities) pi_pulse_width = np.pi / (2 * np.pi * popt[2]) print(f"Estimated π pulse width: {pi_pulse_width:.1f} μs") # Plot results plt.figure(figsize=(10, 6)) plt.plot(width_list, intensities, 'bo-', label='Experimental data') x_fit = np.linspace(min(width_list), max(width_list), 100) y_fit = rabi_function(x_fit, *popt) plt.plot(x_fit, y_fit, 'r-', label='Fitted curve') plt.xlabel('Pulse width (μs)') plt.ylabel('Signal intensity') plt.title('Rabi Oscillation') plt.legend() plt.grid(True) plt.show() except Exception as e: print(f"Fitting failed: {e}") finally: spinqlablink.disconnect() if __name__ == "__main__": rabi_oscillation_experiment() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择RABI_OSCILLATIONS(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Pulse**: 定义脉冲序列,设置脉冲的路径、宽度、振幅、相位和频率偏移(详情参阅 :class:`spinqlablink.Pulse` 类) - **freq_h**: 氢共振频率(hz) [0-100M] - **freq_p**: 磷共振频率(hz) [0-100M] - **makePps**: 是否使用PPS脉冲序列 - **samplePath**: 采样路径,0=氢通道,1=磷通道 [0-1] - **custom_freq**: 是否使用自定义频率 [True,False] 代码流程解析 ^^^^^^^^^^^^ 拉比振荡实验的代码流程可以分为以下几个关键步骤: 1. **初始化连接**: - 创建SpinQLabLink对象并连接到量子计算设备 - 验证登录状态,确保连接成功 2. **实验参数设置**: - 注册拉比振荡实验(RABI_OSCILLATIONS) - 设置实验参数,包括共振频率、采样路径等 - 定义脉冲宽度列表,用于观察不同宽度下的振荡效应 3. **实验执行循环**: - 对每个脉冲宽度进行迭代 - 为每次迭代设置相应的脉冲参数 - 运行实验并等待完成 - 收集实验结果,提取信号强度数据 - 在迭代之间注销实验并等待系统稳定 4. **数据分析与可视化**: - 使用非线性最小二乘法拟合拉比振荡曲线 - 从拟合参数中计算π脉冲宽度 - 绘制实验数据点和拟合曲线 - 显示结果图表 5. **资源释放**: - 实验完成后断开与设备的连接 该实验通过观察不同脉冲宽度下的信号强度变化,展示了量子比特在射频脉冲作用下的拉比振荡现象,这是量子控制的基础,也是确定π脉冲宽度(用于量子比特翻转)的重要方法。 实验结果解析 ^^^^^^^^^^^^ .. figure:: ../_static/images/rabi_result.png :width: 100% :align: center :alt: 拉比震荡实验结果 .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "width": 80.0, # Pulse Width "real": 0.85 # Real part of signal strength } } - **graph**: 图表数据,包含实部和虚部,graph中包含1个step - **width**: 脉冲宽度 - **real**: 信号强度实部值 量子比特实验 ~~~~~~~~~~~~ 观测量子比特的物理特性 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse from spinqlablink import print_graph def main(): # Create connection spinqlablink = SpinQLabLink("192.168.15.4", 8181, "anyword", "anyword") spinqlablink.connect() if not spinqlablink.wait_for_login(): print("Login failed") return # Register Quantum Bit experiment # This experiment demonstrates the basic operations and measurements of a single qubit # In NMR systems, nuclear spins serve as qubits with two energy states (|0⟩ and |1⟩) _, exp_qubit_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_BIT) # Set experiment parameters # Channel selection exp_qubit_para.samplePath = 0 # Sampling path: 0=Hydrogen channel, 1=Phosphorus channel # Hydrogen nuclei are commonly used as qubits in NMR quantum computing # Define the pulse sequence for qubit manipulation # This example uses a π/2 pulse (90° rotation) which creates a superposition state # Parameters: # - path: 0 for hydrogen channel # - width: 40 microseconds pulse duration # - amplitude: 100% of maximum amplitude # - phase: 90 degrees phase angle (creates rotation around Y-axis) # - detuning: 0 Hz frequency offset from resonance exp_qubit_para.pulses = [Pulse(path=0,width=40, amplitude=100, phase=90, detuning=0)] # Run the experiment spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() # Get the results exp_info = spinqlablink.get_experiment_result() # Clean up and disconnect spinqlablink.deregister_experiment() spinqlablink.disconnect() # Display the results # The results should show the quantum state of the qubit after manipulation if "result" in exp_info: exp_result = exp_info["result"] for key, value in exp_result.items(): if key != "graph": print(f"{key}: {value}") print_graph(exp_info["result"]) else: print("Experiment failed") if __name__ == "__main__": main() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择QUANTUM_BIT(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Pulse**: 定义脉冲序列,设置脉冲的路径、宽度、振幅、相位和频率偏移(详情参阅 :class:`spinqlablink.Pulse` 类) - **samplePath**: 采样路径,0=氢通道,1=磷通道 [0-1] - **pulses**: 脉冲序列列表,包含一系列Pulse对象 实验结果解析 ^^^^^^^^^^^^ .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "coordinate": { # The coordinates of a qubit on the Bloch sphere "x": [0, 1], "y": [0, 1], "z": [0, 1] }, "matrix": { # The density matrix of a quantum state, including its real and imaginary parts "real": [[1, 0], [0, 1]], "imag": [[0, 0], [0, 0]] }, "probability": [0.5, 0.5] # Probability distribution of measurement results } } - **graph**: 图表数据,包含实部和虚部,graph中包含2个step - **coordinate**: 量子比特在布洛赫球上的坐标 - **matrix**: 量子态的密度矩阵,包含实部和虚部 - **probability**: 测量结果的概率分布 量子退相干T1实验 ~~~~~~~~~~~~~~~~ 观测量子退相干现象T1 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse # Drawing charts import pyqtgraph as pg from pyqtgraph.Qt import QtWidgets import numpy as np from scipy.optimize import curve_fit def main(): # Create connection spinqlablink = SpinQLabLink("192.168.15.4", 8181, "anyword", "anyword") spinqlablink.connect() if not spinqlablink.wait_for_login(): print("Login failed") return width_real_map = {} width_list = [] for i in range(5): width_list.append(i * 100000) for width in width_list: _, exp_decot1_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_DECOHERENCE_T1) # Set other parameters exp_decot1_para.samplePath = 0 # Sampling path: 0=Hydrogen channel, 1=Phosphorus channel exp_decot1_para.pulses = [Pulse(path=0,width=80, amplitude=100, phase=90, detuning=0) ,Pulse(path=0,width=width, amplitude=0, phase=0, detuning=0)] spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() exp_info = spinqlablink.get_experiment_result() width_real_map[width] = exp_info["result"]["real"] spinqlablink.deregister_experiment() spinqlablink.disconnect() print("width_real_map: ", width_real_map) # Convert data to format suitable for fitting x_data = list(width_real_map.keys()) y_data = list(width_real_map.values()) # Plot experiment results and fitting curve app = QtWidgets.QApplication([]) win = pg.GraphicsLayoutWidget(show=True, title="Rabi Oscillation Experiment Results") win.resize(800, 600) # Set background to white win.setBackground('w') # Add plot area plot = win.addPlot(title="Decoherence T1") plot.setLabel('left', 'Amplitude') plot.setLabel('bottom', 'Pulse Width (us)') # Execute fitting and display results fit_results = fit_decot1_oscillations(x_data, y_data, plot) # Start Qt event loop app.exec_() def fit_decot1_oscillations(x_data, y_data, plot): """ Fit T1 relaxation data with an exponential decay model and display the results. T1 relaxation (longitudinal/amplitude relaxation) follows the model: A + (|A| - A) * (1 - exp(-t/T1)) where: - A is the equilibrium amplitude - T1 is the relaxation time constant - t is the delay time Parameters: x_data: List of delay times (in microseconds) y_data: Corresponding experimental signal amplitudes plot: pyqtgraph Plot object to display the results Returns: Dictionary containing fitting parameters: A (equilibrium amplitude), T1 (relaxation time), and R (correlation coefficient) """ # Draw experimental data points scatter = pg.ScatterPlotItem(x=x_data, y=y_data, pen=None, brush=pg.mkBrush(0, 0, 255, 200), size=10) plot.addItem(scatter) # Prepare fitting parameters min_sig = min(y_data) # T1 relaxation model: equilibrium + (max_change) * (1 - exp(-t/T1)) # This models how the system returns to equilibrium after excitation def t1_model(x, A, t1): return A + (np.abs(A) - A) * (1 - np.exp(-x / t1)); # Initial parameter guesses: equilibrium amplitude and approximate T1 time p0 = [min_sig, 5000000] # Execute fitting try: popt, pcov = curve_fit(t1_model, x_data, y_data, p0=p0) # Get fitting parameters A_fit, t1_fit = popt # Calculate fitting quality R² (coefficient of determination) # R² = 1 - (residual sum of squares / total sum of squares) residuals = y_data - t1_model(np.array(x_data), *popt) ss_res = np.sum(residuals**2) ss_tot = np.sum((y_data - np.mean(y_data))**2) r = np.sqrt(1 - (ss_res / ss_tot)) # Generate fitting curve with higher resolution for smooth display x_fit = np.linspace(0, 20000000, 5000) y_fit = t1_model(x_fit, *popt) # Draw fitting curve fit_curve = pg.PlotCurveItem(x=x_fit, y=y_fit, pen=pg.mkPen('r', width=2)) plot.addItem(fit_curve) # Add legend legend = plot.addLegend() legend.addItem(scatter, 'experimental data') legend.addItem(fit_curve, 'fitting curve') # Display fitting parameters text = pg.TextItem(text=f"A = {A_fit:.4f}\nt1 = {t1_fit:.4f}\nR = {r:.4f}", color=(0, 0, 0), anchor=(0, 0)) text.setPos(min(x_data), max(y_data) * 0.5) plot.addItem(text) print("fitting parameters:") print(f"A = {A_fit:.4f}") print(f"t1 = {t1_fit:.4f}") print(f"fitting quality R = {r:.4f}") return { "A": A_fit, "t1": t1_fit, "r": r } except Exception as e: print(f"fitting error: {e}") return None if __name__ == "__main__": main() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择QUANTUM_DECOHERENCE_T1(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Pulse**: 定义脉冲序列,设置脉冲的路径、宽度、振幅、相位和频率偏移(详情参阅 :class:`spinqlablink.Pulse` 类) - **samplePath**: 采样路径,0=氢通道,1=磷通道 [0-1] - **pulses**: 脉冲序列列表,包含一系列Pulse对象,通常包含两个脉冲: - 第一个脉冲:π/2脉冲,用于激发量子比特 - 第二个脉冲:延迟脉冲,宽度变化用于测量T1弛豫时间 代码流程解析 ^^^^^^^^^^^^ 量子退相干T1实验的代码流程可以分为以下几个关键步骤: 1. **初始化连接**: - 创建SpinQLabLink对象并连接到量子计算设备 - 验证登录状态,确保连接成功 2. **实验参数准备**: - 创建数据结构存储不同延迟时间下的测量结果 - 定义延迟时间列表,用于观察量子比特的弛豫过程 3. **实验执行循环**: - 对每个延迟时间进行迭代 - 注册量子退相干T1实验(QUANTUM_DECOHERENCE_T1) - 设置实验参数,包括采样路径和脉冲序列 - 脉冲序列通常包含两个脉冲: * 第一个脉冲:π/2脉冲(90°),将量子比特从|0⟩态激发到叠加态 * 第二个脉冲:延迟脉冲,宽度变化用于测量不同等待时间后的量子态 - 运行实验并等待完成 - 收集实验结果,提取信号强度数据 - 在迭代之间注销实验 4. **数据分析与拟合**: - 使用指数衰减模型拟合T1弛豫曲线:f(t) = A * exp(-t/T1) + C - 从拟合参数中提取T1时间常数 - 计算拟合质量(R值)评估拟合精度 5. **结果可视化**: - 绘制实验数据点和拟合曲线 - 显示拟合参数,包括振幅A、T1时间和拟合质量R - 在图表上添加图例和参数文本 6. **资源释放**: - 实验完成后断开与设备的连接 该实验通过观察量子比特从激发态回到基态的过程,测量了量子比特的纵向弛豫时间(T1),这是评估量子比特质量的重要参数,表示量子比特能够保持能量的时间尺度。 实验结果解析 ^^^^^^^^^^^^ .. figure:: ../_static/images/t1_result.png :width: 100% :align: center :alt: T1实验结果 .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "width": 100000, # Delay pulse width "real": 0.75, # Real part of signal strength "coordinate": { # The coordinates of a qubit on the Bloch sphere "x": [0, 1], "y": [0, 1], "z": [0, 1] }, "matrix": { # The density matrix of a quantum state, including its real and imaginary parts "real": [[1, 0], [0, 1]], "imag": [[0, 0], [0, 0]] }, "probability": [0.5, 0.5] # Probability distribution of measurement results } } - **graph**: 图表数据,包含实部和虚部,graph中包含2个step - **width**: 延迟脉冲宽度 - **real**: 信号强度实部值 - **coordinate**: 量子比特在布洛赫球上的坐标 - **matrix**: 量子态的密度矩阵,包含实部和虚部 - **probability**: 测量结果的概率分布 量子退相干T2实验 ~~~~~~~~~~~~~~~~ 观测量子退相干现象T2 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse # Drawing charts import pyqtgraph as pg from pyqtgraph.Qt import QtWidgets import numpy as np from scipy.optimize import curve_fit def main(): # Create connection spinqlablink = SpinQLabLink("192.168.15.4", 8181, "anyword", "anyword") spinqlablink.connect() if not spinqlablink.wait_for_login(): print("Login failed") return width_mod_map = {} width_list = [] for i in range(10): width_list.append(i * 500000) for width in width_list: _, exp_decot2_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_DECOHERENCE_T2) # Set other parameters exp_decot2_para.samplePath = 0 # Sampling path: 0=Hydrogen channel, 1=Phosphorus channel exp_decot2_para.pulses = [Pulse(path=0,width=40, amplitude=100, phase=90, detuning=0) ,Pulse(path=0,width=width/2, amplitude=0, phase=0, detuning=0) ,Pulse(path=0,width=80, amplitude=100, phase=90, detuning=0) ,Pulse(path=0,width=width/2, amplitude=0, phase=0, detuning=0)] spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() exp_info = spinqlablink.get_experiment_result() width_mod_map[width] = exp_info["result"]["mod"] spinqlablink.deregister_experiment() spinqlablink.disconnect() print("width_mod_map: ", width_mod_map) # Convert data to format suitable for fitting x_data = list(width_mod_map.keys()) y_data = list(width_mod_map.values()) # Plot experiment results and fitting curve app = QtWidgets.QApplication([]) win = pg.GraphicsLayoutWidget(show=True, title="Decoherence T2 Experiment Results") win.resize(800, 600) # Set background to white win.setBackground('w') # Add plot area plot = win.addPlot(title="Decoherence T2") plot.setLabel('left', 'Amplitude') plot.setLabel('bottom', 'Pulse Width (us)') # Execute fitting and display results fit_results = fit_decot2_oscillations(x_data, y_data, plot) # Start Qt event loop app.exec_() def fit_decot2_oscillations(x_data, y_data, plot): """ Fit Decoherence t2 oscillation data with a sine function and display the results on the specified plot Parameters: x_data: List of pulse widths y_data: Corresponding experimental data plot: pyqtgraph Plot object to display the results Returns: Fitting parameters and results """ # Draw experimental data points scatter = pg.ScatterPlotItem(x=x_data, y=y_data, pen=None, brush=pg.mkBrush(0, 0, 255, 200), size=10) plot.addItem(scatter) # Prepare fitting parameters max_sig = max(y_data) # Decoherence T2 fitting function A * exp(- x / B) def t2_model(x, A, t2): return abs(A) * np.exp(- x / t2); p0 = [max_sig, 500000] # Execute fitting try: popt, pcov = curve_fit(t2_model, x_data, y_data, p0=p0) # Get fitting parameters A_fit, t2_fit = popt # Calculate fitting quality R² residuals = y_data - t2_model(np.array(x_data), *popt) ss_res = np.sum(residuals**2) ss_tot = np.sum((y_data - np.mean(y_data))**2) r = np.sqrt(1 - (ss_res / ss_tot)) # Generate fitting curve x_fit = np.linspace(0, 5000000, 1000) y_fit = t2_model(x_fit, *popt) # Draw fitting curve fit_curve = pg.PlotCurveItem(x=x_fit, y=y_fit, pen=pg.mkPen('r', width=2)) plot.addItem(fit_curve) # Add legend legend = plot.addLegend() legend.addItem(scatter, 'experimental data') legend.addItem(fit_curve, 'fitting curve') # Display fitting parameters text = pg.TextItem(text=f"A = {A_fit:.4f}\nt2 = {t2_fit:.4f}\nR = {r:.4f}", color=(0, 0, 0), anchor=(0, 0)) text.setPos(min(x_data), max(y_data) * 0.5) plot.addItem(text) print("fitting parameters:") print(f"A = {A_fit:.4f}") print(f"t2 = {t2_fit:.4f}") print(f"fitting quality R = {r:.4f}") return { "A": A_fit, "t2": t2_fit, "r": r } except Exception as e: print(f"fitting error: {e}") return None if __name__ == "__main__": main() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择QUANTUM_DECOHERENCE_T2(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Pulse**: 定义脉冲序列,设置脉冲的路径、宽度、振幅、相位和频率偏移(详情参阅 :class:`spinqlablink.Pulse` 类) - **samplePath**: 采样路径,0=氢通道,1=磷通道 [0-1] - **pulses**: 脉冲序列列表,包含一系列Pulse对象,通常包含四个脉冲: - 第一个脉冲:π/2脉冲,用于激发量子比特 - 第二个脉冲:延迟脉冲,宽度变化用于测量T2弛豫时间 - 第三个脉冲:π脉冲,用于回波 - 第四个脉冲:延迟脉冲,宽度变化用于测量T2弛豫时间 代码流程解析 ^^^^^^^^^^^^ 量子退相干T2实验的代码流程可以分为以下几个关键步骤: 1. **初始化连接**: - 创建SpinQLabLink对象并连接到量子计算设备 - 验证登录状态,确保连接成功 2. **实验参数准备**: - 创建数据结构存储不同延迟时间下的测量结果 - 定义延迟时间列表,用于观察量子比特的相干性随时间的衰减 3. **实验执行循环**: - 对每个延迟时间进行迭代 - 注册量子退相干T2实验(QUANTUM_DECOHERENCE_T2) - 设置实验参数,包括采样路径和脉冲序列 - 脉冲序列通常包含四个脉冲: * 第一个脉冲:π/2脉冲(90°),将量子比特从|0⟩态激发到赤道平面 * 第二个脉冲:延迟脉冲,宽度变化用于测量自由演化时间 * 第三个脉冲:π脉冲(180°),用于回波形成,抵消静态场不均匀性 * 第四个脉冲:延迟脉冲,与第二个脉冲相同的宽度,用于回波形成 - 运行实验并等待完成 - 收集实验结果,提取信号强度数据 - 在迭代之间注销实验 4. **数据分析与拟合**: - 使用指数衰减模型拟合T2弛豫曲线:f(t) = A * exp(-t/T2) - 从拟合参数中提取T2时间常数 - 计算拟合质量(R值)评估拟合精度 5. **结果可视化**: - 绘制实验数据点和拟合曲线 - 显示拟合参数,包括振幅A、T2时间和拟合质量R - 在图表上添加图例和参数文本 6. **资源释放**: - 实验完成后断开与设备的连接 该实验通过观察量子比特的相干性随时间的衰减,测量了量子比特的横向弛豫时间(T2),这是评估量子比特质量的另一个重要参数,表示量子比特能够保持相干叠加态的时间尺度。T2时间通常短于T1时间,因为相干性对环境噪声更加敏感。 实验结果解析 ^^^^^^^^^^^^ .. figure:: ../_static/images/t2_result.png :width: 100% :align: center :alt: T2实验结果 .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "width": 500000, # Delay pulse width "mod": 0.65, # Signal Strength Magnitude "coordinate": { # The coordinates of a qubit on the Bloch sphere "x": [0, 1], "y": [0, 1], "z": [0, 1] }, "matrix": { # The density matrix of a quantum state, including its real and imaginary parts "real": [[1, 0], [0, 1]], "imag": [[0, 0], [0, 0]] }, "probability": [0.5, 0.5] # Probability distribution of measurement results } } - **graph**: 图表数据,包含实部和虚部,graph中包含2个step - **width**: 延迟脉冲宽度 - **mod**: 信号强度模值 - **coordinate**: 量子比特在布洛赫球上的坐标 - **matrix**: 量子态的密度矩阵,包含实部和虚部 - **probability**: 测量结果的概率分布 量子控制实验 ~~~~~~~~~~~~ 利用射频场调控量子比特状态 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse from spinqlablink import print_graph def main(): # Create connection spinqlablink = SpinQLabLink("192.168.15.4", 8181, "anyword", "anyword") spinqlablink.connect() if not spinqlablink.wait_for_login(): print("Login failed") return _, exp_qcontrol_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_CONTROL) # Set other parameters exp_qcontrol_para.samplePath = -1 # Sampling path: 0=Hydrogen channel, 1=Phosphorus channel exp_qcontrol_para.pulses = [Pulse(path=0,width=40, amplitude=100, phase=90, detuning=0), Pulse(path=1,width=40, amplitude=100, phase=90, detuning=0)] spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() exp_info = spinqlablink.get_experiment_result() spinqlablink.deregister_experiment() spinqlablink.disconnect() if "result" in exp_info: exp_result = exp_info["result"] for key, value in exp_result.items(): if key != "graph": print(f"{key}: {value}") print_graph(exp_info["result"]) else: print("Experiment failed") if __name__ == "__main__": main() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择QUANTUM_CONTROL(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Pulse**: 定义脉冲序列,设置脉冲的路径、宽度、振幅、相位和频率偏移(详情参阅 :class:`spinqlablink.Pulse` 类) - **samplePath**: 采样路径,-1=所有通道,0=氢通道,1=磷通道 [-1,0,1] - **pulses**: 脉冲序列列表,包含一系列Pulse对象,这里包含两个脉冲: - 第一个脉冲:作用在氢通道(path=0)的π/2脉冲 - 第二个脉冲:作用在磷通道(path=1)的π/2脉冲 实验结果解析 ^^^^^^^^^^^^ .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] }, { # step 1 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "coordinate": { # The coordinates of a qubit on the Bloch sphere "x": [0, 1], "y": [0, 1], "z": [0, 1] }, "matrix": { # The density matrix of a two-qubit system, including the real and imaginary parts "real": [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], "imag": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] }, "probability": [0.25, 0.25, 0.25, 0.25] # Probability distribution of measurement results } } - **graph**: 图表数据,graph中包含2/6个step - **coordinate**: 量子比特在布洛赫球上的坐标 - **matrix**: 两个量子比特系统的密度矩阵,包含实部和虚部 - **probability**: 测量结果的概率分布 量子系统初始化实验 ~~~~~~~~~~~~~~~~~~~ 实现量子系统的初始化过程 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse from spinqlablink import print_graph def main(): # Create connection spinqlablink = SpinQLabLink("192.168.15.4", 8181, "anyword", "anyword") spinqlablink.connect() if not spinqlablink.wait_for_login(): print("Login failed") return _, exp_sysinit_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_SYSTEM_INITIALIZATION) # Set other parameters exp_sysinit_para.repeat = 9 exp_sysinit_para.pulses = [Pulse(path=0,phase=90,amplitude=100,width=40), Pulse(path=0,phase=0,amplitude=0,width=718), Pulse(path=0,phase=0,amplitude=100,width=40), Pulse(path=0,phase=0,amplitude=0,width=718), Pulse(path=0,phase=0,amplitude=0,width=40), Pulse(path=0,phase=0,amplitude=0,width=500000), Pulse(path=1,phase=0,amplitude=0,width=40), Pulse(path=1,phase=0,amplitude=0,width=718), Pulse(path=1,phase=90,amplitude=100,width=40), Pulse(path=1,phase=0,amplitude=0,width=718), Pulse(path=1,phase=0,amplitude=100,width=40), Pulse(path=1,phase=0,amplitude=0,width=500000)] spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() exp_info = spinqlablink.get_experiment_result() spinqlablink.deregister_experiment() spinqlablink.disconnect() if "result" in exp_info: exp_result = exp_info["result"] for key, value in exp_result.items(): if key != "graph": print(f"{key}: {value}") print_graph(exp_info["result"]) else: print("Experiment failed") if __name__ == "__main__": main() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择QUANTUM_SYSTEM_INITIALIZATION(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Pulse**: 定义脉冲序列,设置脉冲的路径、宽度、振幅、相位和频率偏移(详情参阅 :class:`spinqlablink.Pulse` 类) - **repeat**: 重复次数,用于增强信号强度,通常设置为9次 - **pulses**: 脉冲序列列表,包含一系列Pulse对象,这里包含12个脉冲: - 前6个脉冲:作用在氢通道(path=0)的脉冲序列,用于初始化氢核自旋 - 后6个脉冲:作用在磷通道(path=1)的脉冲序列,用于初始化磷核自旋 实验结果解析 ^^^^^^^^^^^^ .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] }, { # step 1 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "Hlamda": 8349439, # Lambda parameter of the hydrogen nucleus "Plamda": 2714902, # Lambda parameter of the hydrogen nucleus "coordinate": { # The coordinates of the qubit on the Bloch sphere, initialized to the |00⟩ state "x": [0, 0], "y": [0, 0], "z": [1, 1] }, "matrix": { # The density matrix of a two-qubit system, initialized in the |00⟩ state "real": [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], "imag": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] }, "probability": [1, 0, 0, 0] # The probability distribution of measurement results, initialized with the probability of the |00⟩ state being 1 } } - **graph**: 图表数据,graph中包含6个step - **Hlamda**: 氢核的Lambda参数,用于后续实验中的自定义PPS设置 - **Plamda**: 磷核的Lambda参数,用于后续实验中的自定义PPS设置 - **coordinate**: 量子比特在布洛赫球上的坐标,初始化为|00⟩态 - **matrix**: 两个量子比特系统的密度矩阵,初始化为|00⟩态 - **probability**: 测量结果的概率分布,初始化为|00⟩态的概率为1 量子逻辑门与量子线路 ~~~~~~~~~~~~~~~~~~~~ 构建基于控制操作的量子门与量子线路 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse, Gate, CustomGate, Circuit from spinqlablink import print_graph def main(): # Create connection spinqlablink = SpinQLabLink("192.168.15.4", 8181, "anyword", "anyword") spinqlablink.connect() if not spinqlablink.wait_for_login(): print("Login failed") return _, exp_gates_and_circuit_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_GATES_AND_CIRCUIT) # Configuration flags using_pulse = False # True: use direct pulse sequence, False: use quantum gates using_custom_gate = False # True: use custom-defined gates, False: use built-in gates using_custom_pps = False # True: use custom Pseudo-Pure State initialization, False: use default PPS # Custom PPS (Pseudo-Pure State) initialization sequence # This JSON defines pulse sequences for both channels (hydrogen and phosphorus) pps_json = '{"pulse":{"channel1_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":500000.0}],"channel2_pulse":[{"amplitude":0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100.0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":500000.0}]}}' # System parameters from system initialization experiment Hlamda = 8349439 # Hydrogen lambda parameter (obtained from system initialization) Plamda = 2714902 # Phosphorus lambda parameter (obtained from system initialization) # Set experiment parameters exp_gates_and_circuit_para.samplePath = -1 # -1 for all channels, 0 for hydrogen channel, 1 for phosphorus channel exp_gates_and_circuit_para.using_pulse = using_pulse # True for using pulse, False for using gate if not using_pulse: exp_gates_and_circuit_para.using_custom_gate = using_custom_gate # True for using custom gate, False for using default gate exp_gates_and_circuit_para.using_custom_pps = using_custom_pps # True for using custom pps, False for using default pps # Configure custom PPS parameters if enabled if using_custom_pps: exp_gates_and_circuit_para.pps_repeat = 9 # Number of times to repeat the PPS sequence exp_gates_and_circuit_para.pps_json = pps_json # Custom PPS pulse sequence exp_gates_and_circuit_para.Hlamda = Hlamda # Hydrogen lambda parameter for PPS exp_gates_and_circuit_para.Plamda = Plamda # Phosphorus lambda parameter for PPS # Create a 2-qubit quantum circuit circuit = Circuit(2) if using_custom_gate: # using custom gate # Example of custom gate definition using JSON (commented out) # H0_file_json = '{"pulse":{"channel1_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0}],"channel2_pulse":[{"amplitude":0.0,"detuning":0.0,"phase":0.0,"width":40.0}]}}' # H1_file_json = '{"pulse":{"channel1_pulse":[{"amplitude":0.0,"detuning":0.0,"phase":0.0,"width":40.0}],"channel2_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0}]}}' # circuit << CustomGate(type='H',customType='H_custom_gate',qubitIndex=0,gateJson=H0_file_json) # circuit << CustomGate(type='H',customType='H_custom_gate',qubitIndex=1,gateJson=H1_file_json) # Define custom Hadamard gates for each qubit using direct pulse definitions # H0_custom_gate: Custom Hadamard gate for qubit 0 (hydrogen channel) circuit << CustomGate(type='H',customType='H0_custom_gate',qubitIndex=0,pulses=[Pulse(path=0,phase=90,amplitude=100,width=40)]) # H1_custom_gate: Custom Hadamard gate for qubit 1 (phosphorus channel) circuit << CustomGate(type='H',customType='H1_custom_gate',qubitIndex=1,pulses=[Pulse(path=1,phase=90,amplitude=100,width=40)]) circuit.print_circuit() else: # using built-in gate # Add standard Hadamard gates to both qubits circuit << Gate(type='H', qubitIndex=0) # Hadamard gate on qubit 0 circuit << Gate(type='H', qubitIndex=1) # Hadamard gate on qubit 1 circuit.print_circuit() # Set the circuit to the experiment exp_gates_and_circuit_para.set_circuit(circuit) else: # using pulse # Define a direct pulse sequence instead of quantum gates exp_gates_and_circuit_para.pulses = [Pulse(path=0,phase=90,amplitude=100,width=40)] # Run the experiment spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() # Get experiment results exp_info = spinqlablink.get_experiment_result() spinqlablink.deregister_experiment() spinqlablink.disconnect() # Process and display results if "result" in exp_info: exp_result = exp_info["result"] for key, value in exp_result.items(): if key != "graph": print(f"{key}: {value}") print_graph(exp_info["result"]) else: print("Experiment failed") if __name__ == "__main__": main() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择QUANTUM_GATES_AND_CIRCUIT(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Pulse**: 定义脉冲序列,设置脉冲的路径、宽度、振幅、相位和频率偏移(详情参阅 :class:`spinqlablink.Pulse` 类) - **Gate**: 定义量子门操作,指定门类型和作用的量子比特(详情参阅 :class:`spinqlablink.Gate` 类) - **CustomGate**: 定义自定义量子门,可以通过脉冲序列或JSON定义门操作(详情参阅 :class:`spinqlablink.CustomGate` 类) - **Circuit**: 构建量子电路,添加量子门操作(详情参阅 :class:`spinqlablink.Circuit` 类) - **samplePath**: 采样路径,-1=所有通道,0=氢通道,1=磷通道 [-1,0,1] - **using_pulse**: 是否直接使用脉冲序列而不是量子门 [True,False] - **using_custom_gate**: 是否使用自定义量子门 [True,False] - **using_custom_pps**: 是否使用自定义PPS初始化 [True,False] - **pps_json**: 自定义PPS初始化的JSON配置(字符串格式详情参阅 :class:`spinqlablink.CustomGate` 类) - **pps_repeat**: 自定义PPS初始化的重复次数 - **Hlamda**: 氢核的Lambda参数,用于自定义PPS设置(参数来源:量子系统初始化实验) - **Plamda**: 磷核的Lambda参数,用于自定义PPS设置(参数来源:量子系统初始化实验) 实验结果解析 ^^^^^^^^^^^^ .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] }, { # step 1 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "matrix": { # The density matrix of a quantum state, including its real and imaginary parts "real": [[0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25]], "imag": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] }, "probability": [0.25, 0.25, 0.25, 0.25] # Probability distribution of measurement results } } - **graph**: 图表数据,graph中包含2/6个step - **matrix**: 量子态的密度矩阵,包含实部和虚部 - **probability**: 测量结果的概率分布,对于两个量子比特的Hadamard门操作后的结果,各个基态的概率相等 量子态重构 ~~~~~~~~~~ 对量子态进行观测与重构 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse, Gate, CustomGate, Circuit from spinqlablink import print_graph def main(): # Create connection spinqlablink = SpinQLabLink("192.168.15.4", 8181, "anyword", "anyword") spinqlablink.connect() if not spinqlablink.wait_for_login(): print("Login failed") return # Register Quantum State Tomography experiment # Quantum State Tomography is a technique to fully reconstruct the quantum state # by performing multiple measurements in different bases _, exp_state_tomography_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_STATE_TOMOGRAPHY) # Configuration flags using_custom_gate = False # True: use custom-defined gates, False: use built-in gates using_custom_pps = False # True: use custom Pseudo-Pure State initialization, False: use default PPS # Custom Pseudo-Pure State (PPS) initialization sequence # This JSON defines pulse sequences for both hydrogen (channel1) and phosphorus (channel2) # Each entry in the array represents a pulse with parameters: amplitude, detuning, phase, and width pps_json = '{"pulse":{"channel1_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":500000.0}],"channel2_pulse":[{"amplitude":0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100.0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":500000.0}]}}' # System parameters from system initialization experiment # These values represent the J-coupling constants between nuclei in Hz Hlamda = 8349439 # Hydrogen lambda coupling constant Plamda = 2714902 # Phosphorus lambda coupling constant # Set experiment parameters # Channel selection for measurement exp_state_tomography_para.samplePath = -1 # -1 for all channels, 0 for hydrogen channel, 1 for phosphorus channel # Using all channels is necessary for full state tomography # Configure custom Pseudo-Pure State if enabled if using_custom_pps: exp_state_tomography_para.pps_repeat = 9 # Number of times to repeat the PPS preparation sequence exp_state_tomography_para.pps_json = pps_json # Custom PPS pulse sequence exp_state_tomography_para.Hlamda = Hlamda # Hydrogen coupling constant from system initialization exp_state_tomography_para.Plamda = Plamda # Phosphorus coupling constant from system initialization # Create a quantum circuit for state preparation # This circuit will prepare the state that we want to characterize with tomography circuit = Circuit(2) # Create a 2-qubit circuit if using_custom_gate: # Using custom-defined gates # Commented code shows alternative way to define custom gates using JSON # H0_file_json = '{"pulse":{"channel1_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0}],"channel2_pulse":[{"amplitude":0.0,"detuning":0.0,"phase":0.0,"width":40.0}]}}' # H1_file_json = '{"pulse":{"channel1_pulse":[{"amplitude":0.0,"detuning":0.0,"phase":0.0,"width":40.0}],"channel2_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0}]}}' # circuit << CustomGate(type='H',customType='H_custom_gate',qubitIndex=0,gateJson=H0_file_json) # circuit << CustomGate(type='H',customType='H_custom_gate',qubitIndex=1,gateJson=H1_file_json) # Define custom Hadamard gates for each qubit using direct pulse definitions # H gate on qubit 0 (hydrogen nucleus) circuit << CustomGate(type='H',customType='H0_custom_gate',qubitIndex=0, pulses=[Pulse(path=0,phase=90,amplitude=100,width=40)]) # H gate on qubit 1 (phosphorus nucleus) circuit << CustomGate(type='H',customType='H1_custom_gate',qubitIndex=1, pulses=[Pulse(path=1,phase=90,amplitude=100,width=40)]) circuit.print_circuit() else: # Using built-in gates # Apply Hadamard gates to create a superposition state on both qubits # This creates the state |++⟩ = (|00⟩ + |01⟩ + |10⟩ + |11⟩)/2 circuit << Gate(type='H', qubitIndex=0) # Hadamard on qubit 0 circuit << Gate(type='H', qubitIndex=1) # Hadamard on qubit 1 circuit.print_circuit() # Set the circuit for the experiment exp_state_tomography_para.set_circuit(circuit) # Run the experiment spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() # Get the results exp_info = spinqlablink.get_experiment_result() # Clean up and disconnect spinqlablink.deregister_experiment() spinqlablink.disconnect() # Display the results # The results will include the reconstructed density matrix of the quantum state if "result" in exp_info: exp_result = exp_info["result"] for key, value in exp_result.items(): if key != "graph": print(f"{key}: {value}") print_graph(exp_info["result"]) else: print("Experiment failed") if __name__ == "__main__": main() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择QUANTUM_STATE_TOMOGRAPHY(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Gate**: 定义量子门操作,指定门类型和作用的量子比特(详情参阅 :class:`spinqlablink.Gate` 类) - **CustomGate**: 定义自定义量子门,可以通过脉冲序列或JSON定义门操作(详情参阅 :class:`spinqlablink.CustomGate` 类) - **Circuit**: 构建量子电路,添加量子门操作(详情参阅 :class:`spinqlablink.Circuit` 类) - **samplePath**: 采样路径,-1=所有通道,0=氢通道,1=磷通道 [-1,0,1] - **using_custom_gate**: 是否使用自定义量子门 [True,False] - **using_custom_pps**: 是否使用自定义PPS初始化 [True,False] - **pps_json**: 自定义PPS初始化的JSON配置 - **pps_repeat**: 自定义PPS初始化的重复次数 - **Hlamda**: 氢核的Lambda参数,用于自定义PPS设置 - **Plamda**: 磷核的Lambda参数,用于自定义PPS设置 实验结果解析 ^^^^^^^^^^^^ .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] }, { # step 1 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "matrix": { # Reconstructed quantum state density matrix, including real and imaginary parts "real": [[0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25]], "imag": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] }, "probability": [0.25, 0.25, 0.25, 0.25], # Probability distribution of measurement results "fidelity": 0.95, # Fidelity between quantum state and ideal state "purity": 0.98, # The purity of a quantum state indicates the degree of mixing of the quantum state. "entropy": 0.05 # The entropy of a quantum state represents the degree of disorder of the quantum state. } } - **graph**: 图表数据,graph中包含2/6个step - **matrix**: 重构的量子态密度矩阵,包含实部和虚部 - **probability**: 测量结果的概率分布 - **fidelity**: 量子态与理想态的保真度 - **purity**: 量子态的纯度,表示量子态的混合程度 - **entropy**: 量子态的熵,表示量子态的混乱程度 量子计算任务 ~~~~~~~~~~~~ 进行真实量子计算任务 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Pulse, Gate, CustomGate, Circuit from spinqlablink import print_graph def main(): # Create connection spinqlablink = SpinQLabLink("192.168.15.4", 8181, "anyword", "anyword") spinqlablink.connect() if not spinqlablink.wait_for_login(): print("Login failed") return _, exp_computing_task_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_COMPUTING_TASK) using_custom_gate = False using_custom_pps = False pps_json = '{"pulse":{"channel1_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":500000.0}],"channel2_pulse":[{"amplitude":0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":718.0},{"amplitude":100.0,"detuning":0.0,"phase":0.0,"width":40.0},{"amplitude":0,"detuning":0.0,"phase":0.0,"width":500000.0}]}}' Hlamda = 8349439 Plamda = 2714902 # Set other parameters exp_computing_task_para.samplePath = 0 # -1 for all channels, 0 for hydrogen channel, 1 for phosphorus channel exp_computing_task_para.using_custom_gate = using_custom_gate # True for using custom gate, False for using default gate exp_computing_task_para.using_custom_pps = using_custom_pps # True for using custom pps, False for using default pps if using_custom_pps: exp_computing_task_para.pps_repeat = 9 exp_computing_task_para.pps_json = pps_json # Custom pps json exp_computing_task_para.Hlamda = Hlamda # Hlamda,which is from exp_system_initialization exp_computing_task_para.Plamda = Plamda # Plamda,which is from exp_system_initialization circuit = Circuit(2) if using_custom_gate: # using custom gate # H0_file_json = '{"pulse":{"channel1_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0}],"channel2_pulse":[{"amplitude":0.0,"detuning":0.0,"phase":0.0,"width":40.0}]}}' # H1_file_json = '{"pulse":{"channel1_pulse":[{"amplitude":0.0,"detuning":0.0,"phase":0.0,"width":40.0}],"channel2_pulse":[{"amplitude":100.0,"detuning":0.0,"phase":90.0,"width":40.0}]}}' # circuit << CustomGate(type='H',customType='H_custom_gate',qubitIndex=0,gateJson=H0_file_json) # circuit << CustomGate(type='H',customType='H_custom_gate',qubitIndex=1,gateJson=H1_file_json) circuit << CustomGate(type='X',customType='X0_custom_gate',qubitIndex=0,pulses=[Pulse(path=0,phase=90,amplitude=100,width=80)]) circuit << CustomGate(type='X',customType='X1_custom_gate',qubitIndex=1,pulses=[Pulse(path=1,phase=90,amplitude=100,width=80)]) circuit.print_circuit() else: # using built-in gate circuit << Gate(type='H', qubitIndex=0) circuit << Gate(type='H', qubitIndex=1) circuit.print_circuit() exp_computing_task_para.set_circuit(circuit) spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() exp_info = spinqlablink.get_experiment_result() spinqlablink.deregister_experiment() spinqlablink.disconnect() if "result" in exp_info: exp_result = exp_info["result"] for key, value in exp_result.items(): if key != "graph": print(f"{key}: {value}") print_graph(exp_info["result"]) else: print("Experiment failed") if __name__ == "__main__": main() 实验参数解析 ^^^^^^^^^^^^ - **ExperimentType**: 实验类型选择QUANTUM_COMPUTING_TASK(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **Gate**: 定义量子门操作,指定门类型和作用的量子比特(详情参阅 :class:`spinqlablink.Gate` 类) - **CustomGate**: 定义自定义量子门,可以通过脉冲序列或JSON定义门操作(详情参阅 :class:`spinqlablink.CustomGate` 类) - **Circuit**: 构建量子电路,添加量子门操作(详情参阅 :class:`spinqlablink.Circuit` 类) - **samplePath**: 采样路径,-1=所有通道,0=氢通道,1=磷通道 [-1,0,1] - **using_custom_gate**: 是否使用自定义量子门 [True,False] - **using_custom_pps**: 是否使用自定义PPS初始化 [True,False] - **pps_json**: 自定义PPS初始化的JSON配置 - **pps_repeat**: 自定义PPS初始化的重复次数 - **Hlamda**: 氢核的Lambda参数,用于自定义PPS设置 - **Plamda**: 磷核的Lambda参数,用于自定义PPS设置 实验结果解析 ^^^^^^^^^^^^ .. code-block:: { "result":{ "graph":[ { # step 0 "fidRe": [[x,y],...], "fidIm":[[x,y],...], "fftRe":[[x,y],...], "fftIm":[[x,y],...], "lorenz":[[x,y],...], "fftMod":[[x,y],...] } ], "matrix": { # The density matrix of a quantum state, including its real and imaginary parts "real": [[0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25]], "imag": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] }, "probability": [0.25, 0.25, 0.25, 0.25], # Probability distribution of measurement results "execution_time": 2500, # Circuit Execution Time (Microseconds) "circuit_depth": 2, # Circuit depth "gate_count": 2 # Number of gates in the circuit } } - **graph**: 图表数据,graph中包含2/6个step - **matrix**: 量子态的密度矩阵,包含实部和虚部 - **probability**: 测量结果的概率分布 - **execution_time**: 电路执行时间(微秒) - **circuit_depth**: 电路深度 - **gate_count**: 电路中的门数量