8000 Update BaseEstimator import to support qiskit 2.0 by ElePT · Pull Request #2327 · Qiskit/qiskit-aer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update BaseEstimator import to support qiskit 2.0 #2327

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

Merged
merged 33 commits into from
Mar 13, 2025

Conversation

ElePT
Copy link
Collaborator
@ElePT ElePT commented Mar 5, 2025

Summary

Qiskit/qiskit#13877 removes the non-versioned primitive V1 base class aliases. It looks like the AerSampler was already using the versioned base class, but AerEstimator wasn't. This PR updates the BaseEstimator import to BaseEstimatorV1.

Details and comments

The PR has been extended to handle more incompatibilities with 2.0.

Copy link
Member
@kt474 kt474 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, this is also causing issues in the qiskit-ibm-runtime unit tests

@wshanks
Copy link
Contributor
wshanks commented Mar 10, 2025

In my testing with Aer and Qiskit 2.0, this line is also problematic. Do you want to address it here as well?

from qiskit.primitives.utils import final_measurement_mapping, init_circuit

The problem addressed by the fix in this PR and the line I pointed out are both problems with trying to do import qiskit_aer.primitives with Qiskit 2.0.

@wshanks
Copy link
Contributor
wshanks commented Mar 11, 2025

Ah, there is a helpful test run that now picks up the Qiskit rc and encounters further issues:

   File "/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/qiskit_aer/primitives/sampler.py", line 24, in <module>
    from qiskit.circuit.bit import Bit
ModuleNotFoundError: No module named 'qiskit.circuit.bit'

@ElePT ElePT force-pushed the update-primitive-imports branch from 45d1bbe to e2d93d5 Compare March 11, 2025 15:09
@ElePT
Copy link
Collaborator Author
ElePT commented Mar 12, 2025

A couple of unit tests with fractional gates were failing because of Qiskit/qiskit#14002. Given that this PR is blocking other efforts, I added a co 10000 nditional skip for these tests until the issue is addresed.

@ElePT
Copy link
Collaborator Author
ElePT commented Mar 12, 2025

The estimator unit tests are currently failing because of another bug in qiskit: Qiskit/qiskit#14003. A fix is on the way. In the meantime, I have chosen to skip transpilation in that specific test, as it's the part of the code that triggers the qiskit bug.

ElePT added 3 commits March 12, 2025 15:18
…ng up the circuit to an arbitrary coupling map, which is something independent of the enable_truncation flag. The test now skips transpilation and exclusively tests the truncation (with enable_truncation=True, the test returns 2 instead of 4 qubits)
@@ -322,7 +322,7 @@ def test_result_order(self):
qc2.ry(np.pi / 2 * param, 0)
qc2.measure_all()

estimator = Estimator(approximation=True)
estimator = Estimator(approximation=True, skip_transpilation=True)
Copy link
@AlbertJP AlbertJP Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try run_options = {'shots': None} to get exact values for the circuits that result in 0.

Alternative: change qc2 to give -1 instead of 0, then you won't have shot noise either. This can be done by changing np.pi / 2 to np.pi in the circuit, or [1] to [2] in the parameter.
(Estimator has no shot noise when the expectation value is exactly -1 or 1)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I haven't used the aer estimator in a while and forgot how to set these.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option is documented at

When combined with the approximation option, we get the expectation values as follows:
* shots is None and approximation=False: Return an expectation value with sampling-noise w/
warning.
* shots is int and approximation=False: Return an expectation value with sampling-noise.
* shots is None and approximation=True: Return an exact expectation value.
* shots is int and approximation=True: Return expectation value with sampling-noise using a
normal distribution approximation.
. Honestly, I don't understand why it did not work. I suspect a bug in the Estimator class in combination with skip_transpilation=True.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's likely. Something to look at in a follow-up.

@ElePT ElePT requested a review from gadial March 12, 2025 17:20
gadial
gadial previously approved these changes Mar 13, 2025
Copy link
Collaborator
@gadial gadial left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this substantial work. I left a few small comments which may be addressed but I believe we can merge without handling them as well.

self.assertEqual(result.results[0].header.metadata, metadata)
try:
out_metadata = result.results[0].header.metadata
except AttributeError:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps using hasattr is preferable to failing, although it's not that important.


result = backend.run(circuit, shots=100).result()
metadata = result.results[0].metadata
self.assertEqual(metadata["num_qubits"], 10)
self.assertEqual(metadata["num_qubits"], 4)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to subtly change the test. Previously, there was a difference between the num_qubits value (which seems to have been determined by the now-extinct coupling map) and the number of active qubits - I believe this difference results from how truncation works in the CPP level. However, I did not manage to recreate this desired behavior yet when working with qiskit 2.0 so we might want to keep this fix as it is for now.

Copy link
Collaborator Author
@ElePT ElePT Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think there is no difference in the way truncation works, the only actual difference I have seen is in the way the transpiler works between 1.4 and 2.0, but that is intentional because we added extra consistency checks between the inputs. The original test provided a backend with 5 qubits and a coupling map with 10, and this is something that transpile now processes differently. I tested transpiling only with the coupling map and the output metadata matches the original test. So maybe we could do that.

@@ -595,6 +595,10 @@ def test_shot_branching_kraus_circuit_noise(self, method, device):
# ---------------------------------------------------------------------
# Test conditional
# ---------------------------------------------------------------------
@unittest.skipUnless(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these tests be fixed to work with the new if mechanism in qiskit 2.0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To some extent, yes. From what I have seen, the new if mechanism is only supported by some simulator methods (for example, statevector doesn't support it). I think that moving forward the tests would have to be refactored and only test conditionals with the supported simulator methods, but this is a bit tricky at the moment because we still want to support the "old" path, and there are already some tests for the new if, so I figured that the best intermediate solution would be the conditional skip. I can open an issue to keep track of this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue seems like the correct way to proceed, thank you.

Copy link
Collaborator
@gadial gadial left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ElePT ElePT added the automerge This PR will automatically merge once its CI has passed label Mar 13, 2025
@ElePT ElePT merged commit f77dbd3 into Qiskit:main Mar 13, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge This PR will automatically merge once its CI has passed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0