SPSAEstimatorGradient

class SPSAEstimatorGradient(estimator, epsilon=1e-06, batch_size=1, seed=None, options=None, pass_manager=None)[source]

Bases: BaseEstimatorGradient

Compute the gradients of the expectation value by the Simultaneous Perturbation Stochastic Approximation (SPSA) [1].

Reference: [1] J. C. Spall, Adaptive stochastic approximation by the simultaneous perturbation method in IEEE Transactions on Automatic Control, vol. 45, no. 10, pp. 1839-1853, Oct 2020, doi: 10.1109/TAC.2000.880982

Parameters:
  • estimator (BaseEstimator) – The estimator used to compute the gradients.

  • epsilon (float) – The offset size for the SPSA gradients.

  • batch_size (int) – The number of gradients to average.

  • seed (int | None) – The seed for a random perturbation vector.

  • options (Options | None) – Primitive backend runtime options used for circuit execution. The order of priority is: options in run method > gradient’s default options > primitive’s default setting. Higher priority setting overrides lower priority setting.

  • pass_manager (BasePassManager | None) – The pass manager to transpile the circuits if necessary. Defaults to None, as some primitives do not need transpiled circuits.

Raises:

ValueError – If epsilon is not positive.

Attributes

derivative_type

Return the derivative type (real, imaginary or complex).

Returns:

The derivative type.

options

Return the union of estimator options setting and gradient default options, where, if the same field is set in both, the gradient’s default options override the primitive’s default setting.

Returns:

The gradient default + estimator options.

Methods

run(circuits, observables, parameter_values, parameters=None, **options)

Run the job of the estimator gradient on the given circuits.

Parameters:
  • circuits (Sequence[QuantumCircuit]) – The list of quantum circuits to compute the gradients.

  • observables (Sequence[BaseOperator]) – The list of observables.

  • parameter_values (Sequence[Sequence[float]] | ndarray) – The list of parameter values to be bound to the circuit.

  • parameters (Sequence[Sequence[Parameter] | None] | None) – The sequence of parameters to calculate only the gradients of the specified parameters. Each sequence of parameters corresponds to a circuit in circuits. Defaults to None, which means that the gradients of all parameters in each circuit are calculated. None in the sequence means that the gradients of all parameters in the corresponding circuit are calculated.

  • options – Primitive backend runtime options used for circuit execution. The order of priority is: options in run method > gradient’s default options > primitive’s default setting. Higher priority setting overrides lower priority setting

Returns:

The job object of the gradients of the expectation values. The i-th result corresponds to circuits[i] evaluated with parameters bound as parameter_values[i]. The j-th element of the i-th result corresponds to the gradient of the i-th circuit with respect to the j-th parameter.

Raises:

ValueError – Invalid arguments are given.

Return type:

AlgorithmJob

update_default_options(**options)

Update the gradient’s default options setting.

Parameters:

**options – The fields to update the default options.