量子算法 ============== 量子算法实践 .. code-block:: python from spinqlablink import SpinQLabLink, ExperimentType, Gate, 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 a quantum algorithm experiment _, exp_algorithm_para = spinqlablink.register_experiment(ExperimentType.QUANTUM_ALGORITHM) # Available quantum algorithm types: # - INTRODUCTION_TO_QUANTUM_COMPUTING: Basic introduction to quantum computing concepts # - DEUTSCH_ALGORITHM: Demonstrates quantum parallelism with a single function evaluation # - BERNSTEIN_VARIRANI_ALGORITHM: Finds a hidden bit string with a single query # - GROVER_ALGORITHM: Searches an unstructured database with quadratic speedup # - QFT_ALGORITHM: Quantum Fourier Transform, the foundation for many quantum algorithms # - HHL_ALGORITHM: Harrow-Hassidim-Lloyd algorithm for solving linear systems # - VQE_ALGORITHM: Variational Quantum Eigensolver for chemistry problems # - QAOA_ALGORITHM: Quantum Approximate Optimization Algorithm for combinatorial problems # Select the algorithm type to run exp_algorithm_para.algorithm_type = ExperimentType.INTRODUCTION_TO_QUANTUM_COMPUTING # Set the sampling path: -1 for all channels, 0 for hydrogen channel, 1 for phosphorus channel exp_algorithm_para.samplePath = -1 # Special parameters for specific algorithms if exp_algorithm_para.algorithm_type == ExperimentType.GROVER_ALGORITHM: # For Grover's algorithm, specify which element to mark as the search target # 0 = |00⟩, 1 = |01⟩, 2 = |10⟩, 3 = |11⟩ exp_algorithm_para.grover_bin = 2 # Create a quantum circuit for the algorithm # This example creates a Bell state circuit: |00⟩ → (|00⟩ + |11⟩)/√2 circuit = Circuit(2) circuit << Gate(type='H', qubitIndex=0) # Apply Hadamard to first qubit circuit << Gate(type='CNOT', qubitIndex=1, controlQubit=0) # CNOT with control=qubit 0, target=qubit 1 # Print the circuit structure to console circuit.print_circuit() # Set the circuit for the experiment exp_algorithm_para.set_circuit(circuit) # Run the experiment spinqlablink.run_experiment() print("Waiting for experiment completion") spinqlablink.wait_for_experiment_completion() # Get the experiment results exp_info = spinqlablink.get_experiment_result() # Clean up and disconnect spinqlablink.deregister_experiment() spinqlablink.disconnect() # Process and display the 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**: 实验类型,设置实验类型(详情参阅 :class:`spinqlablink.ExperimentType` 类) - **algorithm_type**: 算法类型 - **grover_bin**: 在Grover算法中,指定要特别指定标记为搜索目标的元素,0 = \|00⟩, 1 = \|01⟩, 2 = \|10⟩, 3 = \|11⟩ [0,1,2,3] .. list-table:: :header-rows: 1 :widths: 40 60 * - 属性 - 描述 * - ``INTRODUCTION_TO_QUANTUM_COMPUTING`` - 量子计算基础实验 * - ``DEUTSCH_ALGORITHM`` - Deutsch算法实验 * - ``BERNSTEIN_VARIRANI_ALGORITHM`` - Bernstein-Vazirani算法实验 * - ``GROVER_ALGORITHM`` - Grover算法实验 * - ``QFT_ALGORITHM`` - QFT算法实验 * - ``HHL_ALGORITHM`` - HHL算法实验 * - ``VQE_ALGORITHM`` - VQE算法实验 * - ``QAOA_ALGORITHM`` - QAOA算法实验 - **Gate**: 定义量子门操作,指定门类型和作用的量子比特(详情参阅 :class:`spinqlablink.Gate` 类) - **Circuit**: 构建量子电路,添加量子门操作(详情参阅 :class:`spinqlablink.Circuit` 类) - **samplePath**: 采样路径,-1=所有通道,0=氢通道,1=磷通道 [-1,0,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],...] } ], "matrix": { # Quantum state density matrix, containing 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**: 图表数据,包含实验步骤的数据 - **matrix**: 量子态的密度矩阵,包含实部和虚部 - **probability**: 测量结果的概率分布