Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Estimator on SparsePauliOp(["XY", "XX"], ...) and SparsePauliOp([Pauli("XYZ"), Pauli("XX")], ...) have different result #1948

Closed
rht opened this issue Sep 26, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@rht
Copy link

rht commented Sep 26, 2023

Informations

  • Qiskit Aer version: 0.12.2
  • Python version: 3.11
  • Operating system: Linux

What is the current behavior?

"XX" and Pauli("XX") as an argument on SparsePauliOp has different results for qiskit_aer.primitives, while qiskit.primitives results are correct.

Steps to reproduce the problem

from qiskit import QuantumCircuit
from qiskit.primitives import Estimator as e1
from qiskit_aer.primitives import Estimator as e2
from qiskit.quantum_info import Pauli, SparsePauliOp


qc = QuantumCircuit(3)
qc.h(0)
qc.rx(0.2, 0)
qc.h(1)
qc.ry(0.6, 1)
qc.h(2)
qc.rz(0.7, 2)
pauli_sum1 = SparsePauliOp(["XYZ", "XXX"], coeffs=[0.5, 0.2j])
pauli_sum2 = SparsePauliOp([Pauli("XYZ"), Pauli("XXX")], coeffs=[0.5, 0.2j])

estimator1 = e1()
estimator2 = e2(run_options={"approximation": True, "shots": None})
print("qiskit.primitives")
print(estimator1.run(qc, pauli_sum1).result().values)
print(estimator1.run(qc, pauli_sum2).result().values)
print()
print("qiskit_aer.primitives")
print(estimator2.run(qc, pauli_sum1).result().values)
print(estimator2.run(qc, pauli_sum2).result().values)

Output:

qiskit.primitives
[0.+0.1262503j]
[0.+0.1262503j]

qiskit_aer.primitives
[-0.00195312+0.13476562j]
[0.03710938+0.13125j]

What is the expected behavior?

They should all be the same number, but qiskit_aer.primitives output are inconsistent.

@rht rht added the bug Something isn't working label Sep 26, 2023
@ikkoham
Copy link
Collaborator

ikkoham commented Oct 4, 2023

The fluctuation is due to sampling errors in the measurement. In other words, it is not approximation=True.

approximation is not run_option, so

estimator2 = e2(approximation=True)

is correct. However, this does not support non-Hermitian operator. Actually, whether Estimator supports non-Hermitian operator is implementation-dependent.

@rht
Copy link
Author

rht commented Oct 4, 2023

I specified both in the run_options as recommended in the migration guide: https://github.com/Qiskit/qiskit/blob/ff4dc0becb84af7b38ea3b7590fc285796b7566b/docs/migration_guides/opflow_migration.rst?plain=1#L1265.

Actually, whether Estimator supports non-Hermitian operator is implementation-dependent.

I replaced the 0.2j with 0.2, and the result still doesn't match with qiskit.primitives

Upon retrying several times, I did get different number each time. So it seems that it is not doing exact computation according to

The number of shots. If None and approximation is True, it calculates the exact
expectation values. Otherwise, it calculates expectation values with sampling.
.

@rht
Copy link
Author

rht commented Oct 4, 2023

I confirmed that estimator2 = e2(approximation=True, run_options={"shots": None}) does indeed give the correct answer, as long as the observables are Hermitian.

@ikkoham
Copy link
Collaborator

ikkoham commented Oct 4, 2023

Oh... It's a typo. Thank you for finding.

@rht
Copy link
Author

rht commented Oct 4, 2023

Closed via Qiskit/qiskit#10966.

@rht rht closed this as completed Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants