qiskit.opflow.gradients#

Gradients (qiskit.opflow.gradients)#

Obsoleto desde la versi贸n 0.24.0: The qiskit.opflow module is deprecated and will be removed no earlier than 3 months after the release date. For code migration guidelines, visit https://qisk.it/opflow_migration.

Given an operator that represents either a quantum state resp. an expectation value, the gradient framework enables the evaluation of gradients, natural gradients, Hessians, as well as the Quantum Fisher Information.

Suppose a parameterized quantum state |蠄(胃)銆 = V(胃)|蠄銆 with input state |蠄銆 and parameterized Ansatz V(胃), and an Operator O(蠅).

Gradients

We want to compute one of: * \(d鉄ㄏ(胃)|O(蠅)|蠄(胃)銆/ d蠅\) * \(d鉄ㄏ(胃)|O(蠅)|蠄(胃)銆/ d胃\) * \(d鉄ㄏ(胃)|i銆夆煥i|蠄(胃)銆/ d胃\)

The last case corresponds to the gradient w.r.t. the sampling probabilities of |蠄(胃). These gradients can be computed with different methods, i.e. a parameter shift, a linear combination of unitaries and a finite difference method.

Examples

x = Parameter('x')
ham = x * X
a = Parameter('a')

q = QuantumRegister(1)
qc = QuantumCircuit(q)
qc.h(q)
qc.p(params[0], q[0])
op = ~StateFn(ham) @ CircuitStateFn(primitive=qc, coeff=1.)

value_dict = {x: 0.1, a: np.pi / 4}

ham_grad = Gradient(grad_method='param_shift').convert(operator=op, params=[x])
ham_grad.assign_parameters(value_dict).eval()

state_grad = Gradient(grad_method='lin_comb').convert(operator=op, params=[a])
state_grad.assign_parameters(value_dict).eval()

prob_grad = Gradient(grad_method='fin_diff').convert(
   operator=CircuitStateFn(primitive=qc, coeff=1.), params=[a]
)
prob_grad.assign_parameters(value_dict).eval()

Hessians

We want to compute one of: * \(d^2鉄ㄏ(胃)|O(蠅)|蠄(胃)銆/ d蠅^2\) * \(d^2鉄ㄏ(胃)|O(蠅)|蠄(胃)銆/ d胃^2\) * \(d^2鉄ㄏ(胃)|O(蠅)|蠄(胃)銆/ d胃 d蠅\) * \(d^2鉄ㄏ(胃)|i銆夆煥i|蠄(胃)銆/ d胃^2\)

The last case corresponds to the Hessian w.r.t. the sampling probabilities of |蠄(胃)銆. Just as the first order gradients, the Hessians can be evaluated with different methods, i.e. a parameter shift, a linear combination of unitaries and a finite difference method. Given a tuple of parameters Hessian().convert(op, param_tuple) returns the value for the second order derivative. If a list of parameters is given Hessian().convert(op, param_list) returns the full Hessian for all the given parameters according to the given parameter order.

QFI

The Quantum Fisher Information QFI is a metric tensor which is representative for the representation capacity of a parameterized quantum state |蠄(胃)銆 = V(胃)|蠄銆 generated by an input state |蠄銆 and a parameterized Ansatz V(胃). The entries of the QFI for a pure state read \(\mathrm{QFI}_{kl} = 4 \mathrm{Re}[銆堚垈k蠄|鈭俵蠄銆夆垝銆堚垈k蠄|蠄銆夈埾坾鈭俵蠄銆塢\).

Just as for the previous derivative types, the QFI can be computed using different methods: a full representation based on a linear combination of unitaries implementation, a block-diagonal and a diagonal representation based on an overlap method.

Examples

q = QuantumRegister(1)
qc = QuantumCircuit(q)
qc.h(q)
qc.p(params[0], q[0])
op = ~StateFn(ham) @ CircuitStateFn(primitive=qc, coeff=1.)

value_dict = {x: 0.1, a: np.pi / 4}

qfi = QFI('lin_comb_full').convert(
      operator=CircuitStateFn(primitive=qc, coeff=1.), params=[a]
)
qfi.assign_parameters(value_dict).eval()

NaturalGradients

The natural gradient is a special gradient method which re-scales a gradient w.r.t. a state parameter with the inverse of the corresponding Quantum Fisher Information (QFI) \(\mathrm{QFI}^{-1} d鉄ㄏ(胃)|O(蠅)|蠄(胃)銆/ d胃\). Hereby, we can choose a gradient as well as a QFI method and a regularization method which is used together with a least square solver instead of exact inversion of the QFI:

Examples

op = ~StateFn(ham) @ CircuitStateFn(primitive=qc, coeff=1.)
nat_grad = NaturalGradient(grad_method='lin_comb,
                           qfi_method='lin_comb_full',
                           regularization='ridge').convert(operator=op, params=params)

The derivative classes come with a gradient_wrapper() function which returns the corresponding callable and are thus compatible with the optimizers.

Base Classes#

DerivativeBase()

Deprecated: Base class for differentiating opflow objects.

GradientBase([grad_method])

Deprecated: Base class for first-order operator gradient.

HessianBase([hess_method])

Deprecated: Base class for the Hessian of an expected value.

QFIBase([qfi_method])

Deprecated: Base class for Quantum Fisher Information (QFI).

Converters#

CircuitGradient()

Deprecated: Circuit to gradient operator converter.

CircuitQFI()

Deprecated: Circuit to Quantum Fisher Information operator converter.

Derivatives#

Gradient([grad_method])

Deprecated: Convert an operator expression to the first-order gradient.

Hessian([hess_method])

Deprecated: Compute the Hessian of an expected value.

NaturalGradient([grad_method, qfi_method, ...])

Deprecated: Convert an operator expression to the first-order gradient.

QFI([qfi_method])

Deprecated: Compute the Quantum Fisher Information (QFI).