QNSPSA#
- class qiskit.algorithms.optimizers.QNSPSA(fidelity, maxiter=100, blocking=True, allowed_increase=None, learning_rate=None, perturbation=None, resamplings=1, perturbation_dims=None, regularization=None, hessian_delay=0, lse_solver=None, initial_hessian=None, callback=None, termination_checker=None)[소스]#
기반 클래스:
SPSA
The Quantum Natural SPSA (QN-SPSA) optimizer.
The QN-SPSA optimizer [1] is a stochastic optimizer that belongs to the family of gradient descent methods. This optimizer is based on SPSA but attempts to improve the convergence by sampling the natural gradient instead of the vanilla, first-order gradient. It achieves this by approximating Hessian of the
fidelity
of the ansatz circuit.Compared to natural gradients, which require \(\mathcal{O}(d^2)\) expectation value evaluations for a circuit with \(d\) parameters, QN-SPSA only requires \(\mathcal{O}(1)\) and can therefore significantly speed up the natural gradient calculation by sacrificing some accuracy. Compared to SPSA, QN-SPSA requires 4 additional function evaluations of the fidelity.
The stochastic approximation of the natural gradient can be systematically improved by increasing the number of
resamplings
. This leads to a Monte Carlo-style convergence to the exact, analytic value.참고
This component has some function that is normally random. If you want to reproduce behavior then you should set the random number generator seed in the algorithm_globals (
qiskit.utils.algorithm_globals.random_seed = seed
).예제
This short example runs QN-SPSA for the ground state calculation of the
Z ^ Z
observable where the ansatz is aPauliTwoDesign
circuit.import numpy as np from qiskit.algorithms.optimizers import QNSPSA from qiskit.circuit.library import PauliTwoDesign from qiskit.primitives import Estimator, Sampler from qiskit.quantum_info import Pauli # problem setup ansatz = PauliTwoDesign(2, reps=1, seed=2) observable = Pauli("ZZ") initial_point = np.random.random(ansatz.num_parameters) # loss function estimator = Estimator() def loss(x): result = estimator.run([ansatz], [observable], [x]).result() return np.real(result.values[0]) # fidelity for estimation of the geometric tensor sampler = Sampler() fidelity = QNSPSA.get_fidelity(ansatz, sampler) # run QN-SPSA qnspsa = QNSPSA(fidelity, maxiter=300) result = qnspsa.optimize(ansatz.num_parameters, loss, initial_point=initial_point)
This is a legacy version solving the same problem but using Qiskit Opflow instead of the Qiskit Primitives. Note however, that this usage is deprecated.
import numpy as np from qiskit.algorithms.optimizers import QNSPSA from qiskit.circuit.library import PauliTwoDesign from qiskit.opflow import Z, StateFn ansatz = PauliTwoDesign(2, reps=1, seed=2) observable = Z ^ Z initial_point = np.random.random(ansatz.num_parameters) def loss(x): bound = ansatz.bind_parameters(x) return np.real((StateFn(observable, is_measurement=True) @ StateFn(bound)).eval()) fidelity = QNSPSA.get_fidelity(ansatz) qnspsa = QNSPSA(fidelity, maxiter=300) result = qnspsa.optimize(ansatz.num_parameters, loss, initial_point=initial_point)
참조
[1] J. Gacon et al, “Simultaneous Perturbation Stochastic Approximation of the Quantum Fisher Information”, arXiv:2103.09232
- 매개변수:
fidelity (FIDELITY) – A function to compute the fidelity of the ansatz state with itself for two different sets of parameters.
maxiter (int) – The maximum number of iterations. Note that this is not the maximal number of function evaluations.
blocking (bool) – If True, only accepts updates that improve the loss (up to some allowed increase, see next argument).
allowed_increase (float | None) – If
blocking
isTrue
, this argument determines by how much the loss can increase with the proposed parameters and still be accepted. IfNone
, the allowed increases is calibrated automatically to be twice the approximated standard deviation of the loss function.learning_rate (float | Callable[[], Iterator] | None) – The update step is the learning rate is multiplied with the gradient. If the learning rate is a float, it remains constant over the course of the optimization. It can also be a callable returning an iterator which yields the learning rates for each optimization step. If
learning_rate
is setperturbation
must also be provided.perturbation (float | Callable[[], Iterator] | None) – Specifies the magnitude of the perturbation for the finite difference approximation of the gradients. Can be either a float or a generator yielding the perturbation magnitudes per step. If
perturbation
is setlearning_rate
must also be provided.resamplings (int | dict[int, int]) – The number of times the gradient (and Hessian) is sampled using a random direction to construct a gradient estimate. Per default the gradient is estimated using only one random direction. If an integer, all iterations use the same number of resamplings. If a dictionary, this is interpreted as
{iteration: number of resamplings per iteration}
.perturbation_dims (int | None) – The number of perturbed dimensions. Per default, all dimensions are perturbed, but a smaller, fixed number can be perturbed. If set, the perturbed dimensions are chosen uniformly at random.
regularization (float | None) – To ensure the preconditioner is symmetric and positive definite, the identity times a small coefficient is added to it. This generator yields that coefficient.
hessian_delay (int) – Start multiplying the gradient with the inverse Hessian only after a certain number of iterations. The Hessian is still evaluated and therefore this argument can be useful to first get a stable average over the last iterations before using it as preconditioner.
lse_solver (Callable[[np.ndarray, np.ndarray], np.ndarray] | None) – The method to solve for the inverse of the Hessian. Per default an exact LSE solver is used, but can e.g. be overwritten by a minimization routine.
initial_hessian (np.ndarray | None) – The initial guess for the Hessian. By default the identity matrix is used.
callback (CALLBACK | None) – A callback function passed information in each iteration step. The information is, in this order: the parameters, the function value, the number of function evaluations, the stepsize, whether the step was accepted.
termination_checker (TERMINATIONCHECKER | None) – A callback function executed at the end of each iteration step. The arguments are, in this order: the parameters, the function value, the number of function evaluations, the stepsize, whether the step was accepted. If the callback returns True, the optimization is terminated. To prevent additional evaluations of the objective method, if the objective has not yet been evaluated, the objective is estimated by taking the mean of the objective evaluations used in the estimate of the gradient.
Attributes
- bounds_support_level#
Returns bounds support level
- gradient_support_level#
Returns gradient support level
- initial_point_support_level#
Returns initial point support level
- is_bounds_ignored#
Returns is bounds ignored
- is_bounds_required#
Returns is bounds required
- is_bounds_supported#
Returns is bounds supported
- is_gradient_ignored#
Returns is gradient ignored
- is_gradient_required#
Returns is gradient required
- is_gradient_supported#
Returns is gradient supported
- is_initial_point_ignored#
Returns is initial point ignored
- is_initial_point_required#
Returns is initial point required
- is_initial_point_supported#
Returns is initial point supported
- setting#
Return setting
- settings#
The optimizer settings in a dictionary format.
Methods
- static calibrate(loss, initial_point, c=0.2, stability_constant=0, target_magnitude=None, alpha=0.602, gamma=0.101, modelspace=False, max_evals_grouped=1)#
Calibrate SPSA parameters with a powerseries as learning rate and perturbation coeffs.
The powerseries are:
\[a_k = \frac{a}{(A + k + 1)^\alpha}, c_k = \frac{c}{(k + 1)^\gamma}\]- 매개변수:
loss (Callable[[np.ndarray], float]) – The loss function.
initial_point (np.ndarray) – The initial guess of the iteration.
c (float) – The initial perturbation magnitude.
stability_constant (float) – The value of A.
target_magnitude (float | None) – The target magnitude for the first update step, defaults to \(2\pi / 10\).
alpha (float) – The exponent of the learning rate powerseries.
gamma (float) – The exponent of the perturbation powerseries.
modelspace (bool) – Whether the target magnitude is the difference of parameter values or function values (= model space).
max_evals_grouped (int) – The number of grouped evaluations supported by the loss function. Defaults to 1, i.e. no grouping.
- 반환:
- A tuple of powerseries generators, the first one for the
learning rate and the second one for the perturbation.
- 반환 형식:
tuple(generator, generator)
- static estimate_stddev(loss, initial_point, avg=25, max_evals_grouped=1)#
Estimate the standard deviation of the loss function.
- 반환 형식:
- static get_fidelity(circuit, backend=None, expectation=None, *, sampler=None)[소스]#
Get a function to compute the fidelity of
circuit
with itself.참고
Using this function with a backend and expectation converter is pending deprecation, instead pass a Qiskit Primitive sampler, such as
Sampler
. The sampler can be passed as keyword argument or, positionally, as second argument.Let
circuit
be a parameterized quantum circuit performing the operation \(U(\theta)\) given a set of parameters \(\theta\). Then this method returns a function to evaluate\[F(\theta, \phi) = \big|\langle 0 | U^\dagger(\theta) U(\phi) |0\rangle \big|^2.\]The output of this function can be used as input for the
fidelity
to theQNSPSA
optimizer.버전 0.24.0부터 폐지됨:
qiskit.algorithms.optimizers.qnspsa.QNSPSA.get_fidelity()
’s argumentexpectation
is deprecated as of qiskit-terra 0.24.0. It will be removed no earlier than 3 months after the release date. See https://qisk.it/algo_migration for a migration guide.버전 0.24.0부터 폐지됨:
qiskit.algorithms.optimizers.qnspsa.QNSPSA.get_fidelity()
’s argumentbackend
is deprecated as of qiskit-terra 0.24.0. It will be removed no earlier than 3 months after the release date. See https://qisk.it/algo_migration for a migration guide.- 매개변수:
circuit (QuantumCircuit) – The circuit preparing the parameterized ansatz.
backend (Backend | QuantumInstance | None) – Deprecated. A backend of quantum instance to evaluate the circuits. If None, plain matrix multiplication will be used.
expectation (ExpectationBase | None) – Deprecated. An expectation converter to specify how the expected value is computed. If a shot-based readout is used this should be set to
PauliExpectation
.sampler (BaseSampler | None) – A sampler primitive to sample from a quantum state.
- 반환:
A handle to the function \(F\).
- 반환 형식:
Callable[[np.ndarray, np.ndarray], float]
- get_support_level()#
Get the support level dictionary.
- static gradient_num_diff(x_center, f, epsilon, max_evals_grouped=None)#
We compute the gradient with the numeric differentiation in the parallel way, around the point x_center.
- 매개변수:
- 반환:
the gradient computed
- 반환 형식:
grad
- minimize(fun, x0, jac=None, bounds=None)#
Minimize the scalar function.
- 매개변수:
fun (Callable[[POINT], float]) – The scalar function to minimize.
x0 (POINT) – The initial point for the minimization.
jac (Callable[[POINT], POINT] | None) – The gradient of the scalar function
fun
.bounds (list[tuple[float, float]] | None) – Bounds for the variables of
fun
. This argument might be ignored if the optimizer does not support bounds.
- 반환:
The result of the optimization, containing e.g. the result as attribute
x
.- 반환 형식:
- optimize(num_vars, objective_function, gradient_function=None, variable_bounds=None, initial_point=None)#
Perform optimization.
버전 0.21.0부터 폐지됨: The method
qiskit.algorithms.optimizers.spsa.SPSA.optimize()
is deprecated as of qiskit-terra 0.21.0. It will be removed no earlier than 3 months after the release date. Instead, useSPSA.minimize
as a replacement, which supports the same arguments but follows the interface of scipy.optimize and returns a complete result object containing additional information.- 매개변수:
num_vars (int) – Number of parameters to be optimized.
objective_function (callable) – A function that computes the objective function.
gradient_function (callable) – Not supported for SPSA.
variable_bounds (list[(float, float)]) – Not supported for SPSA.
initial_point (numpy.ndarray[float]) – Initial point.
- 반환:
- point, value, nfev
point: is a 1D numpy.ndarray[float] containing the solution value: is a float with the objective function value nfev: number of objective function calls made if available or None
- 반환 형식:
- print_options()#
Print algorithm-specific options.
- set_max_evals_grouped(limit)#
Set max evals grouped
- set_options(**kwargs)#
Sets or updates values in the options dictionary.
The options dictionary may be used internally by a given optimizer to pass additional optional values for the underlying optimizer/optimization function used. The options dictionary may be initially populated with a set of key/values when the given optimizer is constructed.
- 매개변수:
kwargs (dict) – options, given as name=value.