Skip to content

DiffMethod

DiffMethod is an enumeration that selects the differentiation strategy used by VQCircuit.get_gradients and VQCircuit.get_gradients_and_expectation to compute parameter gradients.

Overview

The choice of differentiation method determines how the gradient of the expectation value with respect to each variational parameter is computed. Different methods trade off circuit evaluation count, memory usage, and numerical accuracy.

Enum Values

ValueDescription
ADJOINT_DIFFAdjoint differentiation method. Computes gradients by running the circuit forward and then backward (adjoint), requiring only two circuit evaluations regardless of the number of parameters. Based on the method described in Jones and Gacon, "Efficient calculation of gradients in classical simulations of variational quantum algorithms" (arXiv:2009.02823).

Usage

Pass a DiffMethod value to the gradient computation methods of VQCircuit:

python
import numpy as np
from pyqpanda3.vqcircuit import VQCircuit, DiffMethod
from pyqpanda3.hamiltonian import Hamiltonian

vqc = VQCircuit()
# ... build variational circuit ...

params = np.array([0.1, 0.2, 0.3, 0.4])
hamiltonian = Hamiltonian(...)

# Use adjoint differentiation
grads = vqc.get_gradients(params, hamiltonian, DiffMethod.ADJOINT_DIFF)

# Also available with expectation value
result = vqc.get_gradients_and_expectation(params, hamiltonian, DiffMethod.ADJOINT_DIFF)

References

  • T. Jones and S. Gacon, "Efficient calculation of gradients in classical simulations of variational quantum algorithms," arXiv:2009.02823, 2020. Available at https://arxiv.org/abs/2009.02823.

See Also

Released under the MIT License.