Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.04k stars 2.32k forks source link

Converting QFI with PauliExpectation yields unphysical QFI #6336

Closed Durd3nT closed 3 years ago

Durd3nT commented 3 years ago

Information

What is the current behavior?

Evaluating the Quantum Fisher Information (QFI) of a parameterized circuit and subsequently converting it with PauliExpectation can yield a different and wrong QFI compared to evaluation without PauliExpectation. Specifically, the below example leads to negative eigenvalues of the positive-semidefinite QFI. This was found together with @Zoufalc.

Steps to reproduce the problem

import numpy as np
from qiskit import Aer, QuantumCircuit
from qiskit.circuit import ParameterVector

from qiskit.opflow import StateFn, PauliExpectation, CircuitSampler
from qiskit.opflow.gradients import QFI

num_parameters = 2
x = ParameterVector('x', num_parameters)

circ = QuantumCircuit(2)
circ.rz(0.5*x[0], 0)
circ.rz(x[1], 1)

state = StateFn(circ)
print(state)

params = np.zeros(num_parameters)
param_dict = dict(zip(x, params))

backend = Aer.get_backend('statevector_simulator')

qfi = QFI(qfi_method='lin_comb_full').convert(state)
qfi_res = CircuitSampler(backend).convert(qfi, param_dict).eval()
w, _ = np.linalg.eigh(qfi_res)

print('\nQFI: ', qfi_res)
print('QFI eigenvalues: ', w)

qfi = PauliExpectation().convert(qfi)

qfi_res = CircuitSampler(backend).convert(qfi, param_dict).eval()
w, _ = np.linalg.eigh(qfi_res)

print('\nQFI after PauliExpecation: ', qfi_res)
print('QFI eigenvalues: ', w)

What is the expected behavior?

PauliExpectation should not change the QFI.

Suggested solutions

This issue seems to occur when a parameter is multiplied with a number as in rz(0.5*x[0], 0) and the issue could not be reproduced without such multiplication.

Cryoris commented 3 years ago

I obtain the same problem with AerPauliExpectation, I presume this is coming from somewhere in the QFI implementation.

Cryoris commented 3 years ago

I calculated the QFI of one example by hand so we can better compare the problems. This is the circuit I used

     ┌──────────────┐┌──────────┐
q_0: ┤ RX(0.5*x[0]) ├┤ RY(x[1]) ├
     └──────────────┘└──────────┘

and here are the results

By hand:
[[0.25       0.        ]
 [0.         0.77015115]]
Plain eval
[[2.50000000e-01+0.j 1.66500000e-16+0.j]
 [1.66500000e-16+0.j 7.70151153e-01+0.j]]
Matrix expectation
[[2.50000000e-01+0.j 6.90000000e-17+0.j]
 [6.90000000e-17+0.j 7.70151153e-01+0.j]]
Pauli expectation
[[ 1.00010014+0.j -0.05096054+0.j]
 [-0.05096054+0.j  0.77565002+0.j]]
AerPauli expectation
[[4.        +0.j 0.        +0.j]
 [0.        +0.j 3.08060461+0.j]]

Note that the result with the PauliExpectation seems to disregard the coefficient 0.5 that we get from deriving RX(0.5 * theta). Similarly, AerPauliExpectation disregards that factor but on top has an additional factor of 4.