mindquantum.algorithm.nisq.ansatz_variance

查看源文件
mindquantum.algorithm.nisq.ansatz_variance(ansatz: Circuit, ham: Hamiltonian, focus: str, var_range: Tuple[float, float] = (0, np.pi * 2), other_var: np.array = None, atol: float = 0.1, init_batch: int = 20, sim: Union[Simulator, str] = 'mqvector')[源代码]

计算变分量子线路中的某个参数的梯度的方差。

参数:
  • ansatz (Circuit) - 输入的变分量子线路。

  • ham (Hamiltonian) - 输入的可观察量哈密顿量。

  • focus (str) - 需要检查哪个参数。

  • var_range (Tuple[float, float]) - 参数的随机变化范围。默认值: (0, 2*np.pi)

  • other_var (numpy.array) - 其他变量的数值。如果为 None,则每次采样是都是随机数。默认值: None

  • atol (float) - 方差浮动的容忍度。默认值 0.1

  • init_batch (int) - 初始采样时样本点的个数。默认值: 20

  • sim (Union[Simulator, str]) - 用哪种模拟器来完成任务。默认值: mqvector

样例:

>>> import numpy as np
>>> from mindquantum.algorithm.nisq import ansatz_variance
>>> from mindquantum.algorithm.nisq.chem import HardwareEfficientAnsatz
>>> from mindquantum.core.gates import RY, RZ, Z
>>> from mindquantum.core.operators import Hamiltonian, QubitOperator
>>> np.random.seed(42)
>>> ham = Hamiltonian(QubitOperator('Z0 Z1'))
>>> q_list = [4, 6, 8, 10]
>>> vars = []
>>> for q in q_list:
...     circ = HardwareEfficientAnsatz(q, [RY, RZ], Z, depth=50).circuit
...     vars.append(ansatz_variance(circ, ham, circ.params_name[0]))
...     print(f'qubit: {q},\t var: {vars[-1]}')
qubit: 4,        var: 0.03366677155540075
qubit: 6,        var: 0.007958129595835611
qubit: 8,        var: 0.0014247908876269244
qubit: 10,       var: 0.0006696567877430079