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')[source]
Calculate the variance of the gradient of certain parameters of parameterized quantum circuit.
- Parameters
ansatz (
Circuit
) – The given parameterized quantum circuit.ham (
Hamiltonian
) – The objective observable.focus (str) – Which parameters you want to check.
var_range (Tuple[float, float]) – The random range for focusing parameter. Default:
(0, 2*np.pi)
.other_var (numpy.array) – The value of other parameters. If
None
, it will be random number at every sampling. Default:None
.atol (float) – Tolerance for variance when samping. Default:
0.1
.init_batch (int) – Initial sampling size. Default:
20
.sim (Union[
Simulator
, str]) – Simulator you want to use. Default:mqvector
.
Examples
>>> 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