From 9d3c9b0ea91f220c52436a508108e979009210f3 Mon Sep 17 00:00:00 2001 From: dsdsdshe Date: Wed, 20 Aug 2025 19:03:03 +0800 Subject: [PATCH 1/2] Docs: fix mqchem API documentation issues --- ...ndquantum.simulator.mqchem.CIHamiltonian.rst | 17 ++++++++++++++++- ...quantum.simulator.mqchem.MQChemSimulator.rst | 8 ++++---- ...antum.simulator.mqchem.UCCExcitationGate.rst | 17 ++++++++++++++++- ...antum.simulator.mqchem.prepare_uccsd_vqe.rst | 4 ++-- .../simulator/mqchem/mqchem_simulator.py | 6 +++--- mindquantum/simulator/mqchem/vqe_preparation.py | 3 ++- 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/docs/api_python/simulator/mindquantum.simulator.mqchem.CIHamiltonian.rst b/docs/api_python/simulator/mindquantum.simulator.mqchem.CIHamiltonian.rst index 0d0af4608..1779329eb 100644 --- a/docs/api_python/simulator/mindquantum.simulator.mqchem.CIHamiltonian.rst +++ b/docs/api_python/simulator/mindquantum.simulator.mqchem.CIHamiltonian.rst @@ -1,7 +1,7 @@ mindquantum.simulator.mqchem.CIHamiltonian ========================================== -.. py:class:: mindquantum.simulator.mqchem.CIHamiltonian(fermion_hamiltonian) +.. py:class:: mindquantum.simulator.mqchem.CIHamiltonian(fermion_hamiltonian: FermionOperator) 一个费米子哈密顿量的包装器,用于与 :class:`~.simulator.mqchem.MQChemSimulator` 一同使用。 @@ -12,3 +12,18 @@ mindquantum.simulator.mqchem.CIHamiltonian 参数: - **fermion_hamiltonian** (FermionOperator) - 一个正规序的费米子哈密顿量。 + + .. py:method:: get_cpp_obj(backend, n_qubits, n_electrons) + + 返回用于模拟的C++对象。 + + .. note:: + 此方法供 :class:`~.simulator.mqchem.MQChemSimulator` 内部使用。 + + 参数: + - **backend** (``_mq_chem.float`` 或 ``_mq_chem.double``) - C++后端模块。 + - **n_qubits** (int) - 系统中的总量子比特数(自旋轨道数)。 + - **n_electrons** (int) - 系统中的总电子数。 + + 返回: + C++对象,底层绑定类型为 ``CppCIHamiltonian``,用于模拟。 diff --git a/docs/api_python/simulator/mindquantum.simulator.mqchem.MQChemSimulator.rst b/docs/api_python/simulator/mindquantum.simulator.mqchem.MQChemSimulator.rst index 58274f932..10a15179b 100644 --- a/docs/api_python/simulator/mindquantum.simulator.mqchem.MQChemSimulator.rst +++ b/docs/api_python/simulator/mindquantum.simulator.mqchem.MQChemSimulator.rst @@ -20,7 +20,7 @@ mindquantum.simulator.mqchem.MQChemSimulator - **seed** (int) - 此模拟器的随机种子。如果为 ``None``,将生成一个随机种子。默认值:``None``。 - **dtype** (str) - 模拟的数据类型,可以是 ``"float"`` 或 ``"double"``。默认值:``"double"``。 - .. py:method:: apply_circuit(circuit, pr=None) + .. py:method:: apply_circuit(circuit: Union[Circuit, Iterable[UCCExcitationGate]], pr: ParameterResolver = None) 将量子线路应用于当前模拟器状态。 @@ -31,7 +31,7 @@ mindquantum.simulator.mqchem.MQChemSimulator - **circuit** (Union[Circuit, Iterable[UCCExcitationGate]]) - 要应用的量子线路或UCC门的可迭代对象。 - **pr** (Union[ParameterResolver, dict, numpy.ndarray, list, numbers.Number]) - 用于替换参数值的参数解析器。如果为 ``None``,则直接使用门中定义的参数。默认值:``None``。 - .. py:method:: apply_gate(gate, pr=None) + .. py:method:: apply_gate(gate: UCCExcitationGate, pr: ParameterResolver = None) 将单个UCC激发门应用于当前模拟器状态。 @@ -57,7 +57,7 @@ mindquantum.simulator.mqchem.MQChemSimulator 异常: - **TypeError** - 如果 `ham` 不是 :class:`~.simulator.mqchem.CIHamiltonian`。 - .. py:method:: get_expectation_with_grad(ham, circuit) + .. py:method:: get_expectation_with_grad(ham: CIHamiltonian, circuit: Union[Circuit, Iterable[UCCExcitationGate]]) 生成一个计算期望值及其梯度的函数。 @@ -74,7 +74,7 @@ mindquantum.simulator.mqchem.MQChemSimulator 异常: - **TypeError** - 如果 `ham` 不是 :class:`~.simulator.mqchem.CIHamiltonian`。 - .. py:method:: get_qs(ket=False) + .. py:method:: get_qs(ket: bool = False) 获取模拟器当前的量子态。 diff --git a/docs/api_python/simulator/mindquantum.simulator.mqchem.UCCExcitationGate.rst b/docs/api_python/simulator/mindquantum.simulator.mqchem.UCCExcitationGate.rst index 23e543199..0abfe5e0b 100644 --- a/docs/api_python/simulator/mindquantum.simulator.mqchem.UCCExcitationGate.rst +++ b/docs/api_python/simulator/mindquantum.simulator.mqchem.UCCExcitationGate.rst @@ -1,7 +1,7 @@ mindquantum.simulator.mqchem.UCCExcitationGate =============================================== -.. py:class:: mindquantum.simulator.mqchem.UCCExcitationGate(fermion_operator) +.. py:class:: mindquantum.simulator.mqchem.UCCExcitationGate(fermion_operator: FermionOperator) 酉耦合簇(UCC)激发门,用于与 :class:`~.simulator.mqchem.MQChemSimulator` 一同使用。 @@ -23,3 +23,18 @@ mindquantum.simulator.mqchem.UCCExcitationGate 参数: - **fermion_operator** (FermionOperator) - 费米子激发算符 :math:`T`。 它必须只包含一项。该项的系数用作旋转角 :math:`\theta`。如果系数是变量,则该门是参数化的。 + + .. py:method:: get_cpp_obj(n_qubits: int, n_electrons: int, backend) + + 返回用于模拟的C++对象。 + + .. note:: + 此方法供 :class:`~.simulator.mqchem.MQChemSimulator` 内部使用。 + + 参数: + - **n_qubits** (int) - 系统中的总量子比特数(自旋轨道数)。 + - **n_electrons** (int) - 系统中的总电子数。 + - **backend** (``_mq_chem.float`` 或 ``_mq_chem.double``) - C++后端模块。 + + 返回: + C++对象,底层绑定类型为 ``CppExcitationOperator``,用于模拟。 diff --git a/docs/api_python/simulator/mindquantum.simulator.mqchem.prepare_uccsd_vqe.rst b/docs/api_python/simulator/mindquantum.simulator.mqchem.prepare_uccsd_vqe.rst index 14d226bc6..43dcf1484 100644 --- a/docs/api_python/simulator/mindquantum.simulator.mqchem.prepare_uccsd_vqe.rst +++ b/docs/api_python/simulator/mindquantum.simulator.mqchem.prepare_uccsd_vqe.rst @@ -1,12 +1,12 @@ mindquantum.simulator.mqchem.prepare_uccsd_vqe ================================================= -.. py:function:: mindquantum.simulator.mqchem.prepare_uccsd_vqe(molecular, threshold=1e-6) +.. py:function:: mindquantum.simulator.mqchem.prepare_uccsd_vqe(molecular, threshold: float = 1e-6)) 为使用 MQChemSimulator 进行 UCCSD-VQE 模拟准备所有组件。 该工厂函数通过以下步骤简化了VQE模拟的设置: - 1. 使用 :func:`~.algorithm.nisq.chem.uccsd_singlet_generator` 生成所有单重态UCCSD激发算符。 + 1. 使用 :function:`~.algorithm.nisq.chem.uccsd_singlet_generator` 生成所有单重态UCCSD激发算符。 2. 从 `molecular` 数据中包含的预先计算的CCSD结果中提取相应的振幅。 3. 根据 `threshold` 筛选激发(基于它们的CCSD振幅)。 4. 使用 :class:`~.simulator.mqchem.UCCExcitationGate` 构建参数化的UCCSD拟设线路。 diff --git a/mindquantum/simulator/mqchem/mqchem_simulator.py b/mindquantum/simulator/mqchem/mqchem_simulator.py index a22a40528..4ef58cb7b 100644 --- a/mindquantum/simulator/mqchem/mqchem_simulator.py +++ b/mindquantum/simulator/mqchem/mqchem_simulator.py @@ -131,7 +131,7 @@ class MQChemSimulator: If ``False``, returns the state as a NumPy array. Default: ``False``. Returns: - Union[numpy.ndarray, str]: The quantum state vector as a NumPy array or a string + Union[numpy.ndarray, str], The quantum state vector as a NumPy array or a string in ket notation. Raises: @@ -289,7 +289,7 @@ class MQChemSimulator: ham (CIHamiltonian): The Hamiltonian for which to compute the expectation. Returns: - float: The real-valued expectation value. + float, The real-valued expectation value. Raises: TypeError: If `ham` is not a :class:`~.simulator.mqchem.CIHamiltonian`. @@ -328,7 +328,7 @@ class MQChemSimulator: :math:`U(\theta)`. The circuit must have parameters for gradient calculation. Returns: - Callable: A function that accepts a NumPy array of parameter values `x` and + Callable, A function that accepts a NumPy array of parameter values `x` and returns a tuple `(expectation, gradient)`. `expectation` is the float expectation value, and `gradient` is a NumPy array containing the derivatives with respect to each parameter in `x`. The order of parameters is determined diff --git a/mindquantum/simulator/mqchem/vqe_preparation.py b/mindquantum/simulator/mqchem/vqe_preparation.py index cd5f43f24..d80569139 100644 --- a/mindquantum/simulator/mqchem/vqe_preparation.py +++ b/mindquantum/simulator/mqchem/vqe_preparation.py @@ -27,8 +27,9 @@ def prepare_uccsd_vqe(molecular, threshold: float = 1e-6): Prepare all components for a UCCSD-VQE simulation with the MQChemSimulator. This factory function streamlines the setup for a VQE simulation by: + 1. Generating all singlet UCCSD excitation operators using - :func:`~.algorithm.nisq.chem.uccsd_singlet_generator`. + :function:`~.algorithm.nisq.chem.uccsd_singlet_generator`. 2. Extracting the corresponding amplitudes from a pre-computed CCSD calculation included in the `molecular` data. 3. Filtering excitations based on their CCSD amplitudes via the `threshold`. -- Gitee From f177ee416eb0822478881e7a06bcaa9bba5099ec Mon Sep 17 00:00:00 2001 From: dsdsdshe Date: Wed, 20 Aug 2025 19:11:59 +0800 Subject: [PATCH 2/2] Fix(qaia): Refine SB GPU import error message --- mindquantum/algorithm/qaia/SB.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/mindquantum/algorithm/qaia/SB.py b/mindquantum/algorithm/qaia/SB.py index d7d3803ee..b4cf67f10 100644 --- a/mindquantum/algorithm/qaia/SB.py +++ b/mindquantum/algorithm/qaia/SB.py @@ -42,14 +42,10 @@ try: GPU_AVAILABLE = True except ImportError as err: - GPU_DISABLE_REASON = "Unable to import SB GPU backend. This backend requires CUDA 11 or higher." - GPU_AVAILABLE = False - - # Temporarily disable GPU backend due to PyPI package size limitations GPU_DISABLE_REASON = ( - "GPU backend is temporarily disabled in 0.10.0 version due to PyPI package size limitations. " - "To use GPU backend, please install mindquantum 0.10.0 from https://www.mindspore.cn/versions" + "Unable to import SB GPU backend. This backend requires CUDA 11 or higher.\n" f"--> Specific error: {err}" ) + GPU_AVAILABLE = False except RuntimeError as err: GPU_DISABLE_REASON = f"Disable SB GPU backend due to: {err}." GPU_AVAILABLE = False -- Gitee