Skip to content

Added V2 and ISA support #197

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 146 commits into from
Aug 6, 2025
Merged

Added V2 and ISA support #197

merged 146 commits into from
Aug 6, 2025

Conversation

tnemoz
Copy link
Contributor

@tnemoz tnemoz commented Aug 9, 2024

Summary

This PR's goal is to adapt the whole code base to support V2 primitives and ISA circuits. Will fix #136, fix #164, fix #165, fix #194, fix #204, fix #213, fix #220, fix #224, and fix #229.

What's working

Qiskit 1.4/2.0 Qiskit 2.2 Comments
eigensolvers ✔️ ✔️
gradients ✔️ ✔️ A test is skipped as it takes quite some time, and we're not sure to see its point at the moment. The whole test suite takes about 20 minutes to run.
minimum_eigensolvers ✔️ ✔️
optimizers ✔️ ✔️
state_fidelities ✔️ ✔️
time_evolvers ✔️ The tests for Qiskit 2.2 don't pass because Qiskit/qiskit#14614 wasn't fixed, so we need to circumvent the problem
utils ✔️ ✔️
amplitude_estimators ✔️ ✔️ A test is skipped for now until Qiskit/qiskit#14250 is fixed and merged
grover ✔️ ✔️ A test is skipped for now but should probably be removed or refactored, see #136 (comment).
phase_estimators ✔️ ✔️
validation ✔️ ✔️

@CLAassistant
Copy link

CLAassistant commented Aug 9, 2024

CLA assistant check
All committers have signed the CLA.

@tnemoz tnemoz mentioned this pull request Aug 9, 2024
@coveralls
Copy link

coveralls commented Aug 9, 2024

Pull Request Test Coverage Report for Build 16777946518

Details

  • 780 of 845 (92.31%) changed or added relevant lines in 53 files are covered.
  • 11 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.3%) to 90.135%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit_algorithms/custom_types.py 6 7 85.71%
qiskit_algorithms/eigensolvers/vqd.py 34 35 97.14%
qiskit_algorithms/gradients/lin_comb/lin_comb_estimator_gradient.py 36 37 97.3%
qiskit_algorithms/minimum_eigensolvers/adapt_vqe.py 22 23 95.65%
qiskit_algorithms/minimum_eigensolvers/diagonal_estimator.py 41 42 97.62%
qiskit_algorithms/minimum_eigensolvers/sampling_vqe.py 10 11 90.91%
qiskit_algorithms/minimum_eigensolvers/vqe.py 38 39 97.44%
qiskit_algorithms/state_fidelities/compute_uncompute.py 25 26 96.15%
qiskit_algorithms/time_evolvers/pvqd/pvqd.py 19 20 95.0%
qiskit_algorithms/time_evolvers/variational/var_qte.py 20 21 95.24%
Files with Coverage Reduction New Missed Lines %
qiskit_algorithms/amplitude_amplifiers/grover.py 1 92.86%
qiskit_algorithms/amplitude_estimators/estimation_problem.py 1 92.21%
qiskit_algorithms/amplitude_estimators/ae.py 9 89.8%
Totals Coverage Status
Change from base Build 16777939751: -0.3%
Covered Lines: 6624
Relevant Lines: 7349

💛 - Coveralls

@woodsp-ibm
Copy link
Member

woodsp-ibm commented Aug 9, 2024

FYI. I merged a PR that fixed CI with the main branch and changes that will going into the shortly to be released 1.2.0 so that CI now passes fully here and does not fail with the two jobs that check things out against qiskit main, and updated the branch here so this now completely passes.

I think this is fine. I must admit though I had wondered, given that I think we only ever need something that has a run() method, that takes circuits and gives transpiled circuits back whether, something could be done around a type that was more targeted along those lines. The pass manager has a number of methods that I do not think we would call, and the AI Transpiler Service which has a run method could be used too but is not a passmanager - i,e, so that a Passmanager instance or the AI transpiler could be used - or anything else that had a run method that did the transpilation. I know Optimization went for BassPassManager too as the type and maybe thats ok.

Whatever is done I think we might want to note in the docstring for this that's its optional and that passing in None results in the circuit not being transpiled. Maybe for some algos like VQE one can already pass a circuit (ansatz) that is transpiled for a given backend so the note there might be a little different to accommodate that - for PE they build their own circuits of course so that aspect does not apply.

One minor comment I might have is that the pm used in the tests is created and ends up being used for all the tests rather than being created at the time the data is passed into the test. As long as there are no side-effects of using pm, i.e. its the same really as creating a new pm for each test run I guess its ok - my feeling is that it would be better to do it each time so it guarantees a fresh instance is used.

@tnemoz
Copy link
Contributor Author

tnemoz commented Jul 31, 2025

@tnemoz there has been no input to the above question around the unit test, I believe all the work that was intended for this PR is now done right so otherwise its ready to go right?

Hey @woodsp-ibm, sorry for the time without news!

I think it's up to you! As things currently are, AdaptVQE doesn't support transpiling, as transpiling a circuit would later fail the test that the ansatz is of the EvolvedOperatorAnsatz type (as per the discussion in #231). As mentioned in #231, we could take a shot to changing AdaptVQE to make it independent from EvolvedOperatorAnsatz, but that may delay the PR even more.

We also intended to fix the issues in the Monitoring Qiskit workflow, but there are still some issues that are not fixed, even though it seems PR are on their way.

All in all, it probably depends on whether you intend to release a new version of qiskit-algorithms right after this PR. If so, then maybe we could add a warning in the release notes that AdaptVQE doesn't support being passed a transpiler for now (which effectively prevents using it on a real backend when being passed an aux_operators). If not, then this could be done at the same time as fixing #231.

Concerning the monitoring, AFAIK this PR will work with Qiskit up to version 2.1, and maybe an issue could be opened to tackle this?

@woodsp-ibm
Copy link
Member

We also intended to fix the issues in the Monitoring Qiskit workflow, but there are still some issues that are not fixed, even though it seems PR are on their way.

The Monitoring task was done to just give us a heads up on what was happening as far as Qiskit was concerned by installing/testing against their main branch. As its work in progress there can be issues that arise that cause things to fail here - and we have given feedback in the past when things were more active here in case the issue we saw was unknown.

Concerning the monitoring, AFAIK this PR will work with Qiskit up to version 2.1, and maybe an issue could be opened to tackle this?

In looking at requirements.txt its set to qiskit >=1.0 but we limit the testing in requirements-dev.txt to not include 2.1.* so it does not test on the latest actual release. My recollection was that there were some issues with 2.1.0 but since then 2.1.1 has been released which I do not know if it fixed the issues we had here. If so the restriction could be changed to just 2.1.0 rather than the wild card at end preventing 2.1.1 from being used, Of maybe by up to version 2.1 you meant not including that though the main requirements.txt, and an end user installing qiskit, it would pick the latest by default, i.e 2.1.1

All in all, it probably depends on whether you intend to release a new version of qiskit-algorithms right after this PR.

Things can be coordinated around a release - if its felt more needs doing, can be done, before a release then that's fine. This PR has been going for some time I am sure a bit longer is ok.

And in terms of some issues, like changing to circuit library functions, as their removal from deprecation seems to have been moved out to Qiskit 3.0 it appears there is more time to look at things like that.

@woodsp-ibm
Copy link
Member

With 2.1.1 (and presumably same with 2.1.0) this looks akin to the failures that are occurring on main, in the monitor main task, though the reasons for stopping seems clearer with main - complaint about empty operator and non-complex coeffs when doing a conversion.

@woodsp-ibm
Copy link
Member

I raised an issue on Qiskit and linked this PR so the Qiskit issue shows up linked above. Hopefully we can get the failures under 2.1 figured.

@tnemoz
Copy link
Contributor Author

tnemoz commented Jul 31, 2025

Concerning the monitoring, AFAIK this PR will work with Qiskit up to version 2.1, and maybe an issue could be opened to tackle this?

In looking at requirements.txt its set to qiskit >=1.0 but we limit the testing in requirements-dev.txt to not include 2.1.* so it does not test on the latest actual release. My recollection was that there were some issues with 2.1.0 but since then 2.1.1 has been released which I do not know if it fixed the issues we had here. If so the restriction could be changed to just 2.1.0 rather than the wild card at end preventing 2.1.1 from being used, Of maybe by up to version 2.1 you meant not including that though the main requirements.txt, and an end user installing qiskit, it would pick the latest by default, i.e 2.1.1

Nope, I'd just forgot that we put this pin here, I somehow convinced myself that things were fine on 2.1.X, that's on me!

With 2.1.1 (and presumably same with 2.1.0) this looks akin to the failures that are occurring on main, in the monitor main task, though the reasons for stopping seems clearer with main - complaint about empty operator and non-complex coeffs when doing a conversion.

I raised an issue on Qiskit and linked this PR so the Qiskit issue shows up linked above. Hopefully we can get the failures under 2.1 figured.

If I'm not mistaken, there are already issues on Qiskit about this (concerning the particular problems though, without linking to this PR that gather them all). As far as I can tell, there are three issues at hand with Qiskit 2.1.X:

All in all, we have to tackle the case of nil observables manually and it should be fine. If the last PR makes it to the next Qiskit release, then it means that we'd only have to "forbid" Qiskit 2.1.0 and 2.1.1.

Also, thinking about it, I think I've found a way to include transpiling to AdaptVQE without changing it too much, by just changing the place where the type check of VQE's ansatz is performed. So, if it's OK for you, I think the ext steps could be:

  • manually handle the zero observables;
  • add transpilation to AdaptVQE.

And then we could probably merge this PR with the monitoring tests (and thus, hopefully by then, the next Qiskit release), passing. Would that work for you?

@a-matsuo
Copy link
Contributor

a-matsuo commented Aug 1, 2025

Hey there!

The reason why the tests are taking so long is probably due to the sampling. Indeed, in order to get a decent precision, the number of shots has to be set to a couple millions. In fact, if you run the tests, you can see that the whole test file for Sampler gradients takes a large amount of time to run. This is inconvenient, but I'd say still expected.

What Léna and I are not sure about is the reason why we have to run this test in the first place. The way we understand this, this test checks that FiniteDiffSamplerGradient and LinCombSamplerGradient return the same results. However, we already test for these being correct early in the file, so checking whether they return the same thing seems redundant. Or are we missing something?

If that's the case, you can reduce the number of shots (You need to take care the precision when you compare the results).
In LinCombSamplerGradient and ParamShiftSamplerGradient, we need complex preprocessing and postprocessing to handle differential rules (e.g. the chain rule). Most of test cases are there to make sure those cases are correctly handled in the preprocessing and posprocessing.

@woodsp-ibm
Copy link
Member

And then we could probably merge this PR with the monitoring tests (and thus, hopefully by then, the next Qiskit release), passing. Would that work for you?

Ah you had raised more specific issues. 14714 was merged last night and in the issue I raised I was asked to retry. This is how I responded

I just re-ran the two CI jobs (Monitor Qiskit Main in 3.9 and 3.13) that test against Qiskit main branch, in the above referenced PR, and the failures are unchanged and still there.

Do you think the tests should be passing - in looking at the operators we create n say the VQD std dev test I thought they were ok. Now I believe they get converted to some other type in the estimator - since you have the code locally does it look like the observables passed to the estimator at least are ok?

But yes you might like to add the pin back again to requirements-dev - we can remove it once things seem to pass for main and a newer version of qiskit has been released that fixes things (assuming things are all ok on the algs side of course)

@tnemoz
Copy link
Contributor Author

tnemoz commented Aug 4, 2025

So, by modifying evaluate_observables to bypass the Estimator altogether when the input observable is nil, it seems to solve most of the failing tests in the monitoring task. Out of the remaining four, one of them is solved by the changes implemented in Qiskit/qiskit#14611.

If I'm not mistaken, the remaining three are due to the fact that we directly use the run method of an Estimator instead of passing it to evaluate_observables. I understand that this function was initially destined for another usage, but I think it could be a general tool for evaluating expectation values.

This may however cause some problems. For instance, in the _run_unique method of LinCombEstimatorGradient (which is one of the places where a nil observable causes the time evolvers test to fail), one would have to replace

        job = self._estimator.run(pubs)

        try:
            results = job.result()
        except AlgorithmError as exc:
            raise AlgorithmError("Estimator job failed.") from exc

by

        results = [
            estimate_observables(
                self._estimator,
                quantum_state,
                [observable],
                parameters_values,
                threshold=-float("inf")
            )[0][0]
            for (quantum_state, observable, parameters_values) in pubs
        ]

However, a direct consequence of doing it like this is that it not only launches multiple jobs, but also waits for each job to end before launching the next one, which of course results in a large overhead.

The solution would be to modify estimate_observables to accept PUB-like inputs, but I'm not too sure what how that would go, especially with the ListOrDict typing. Another solution which would be much easier to work with would be to create a second function, say run_estimator_job that would be a glorified run method of an Estimator, which would additionally bypass nil observables (which means that the call to result should also be done in this function, which may not be convenient). This would make it much easier to make it accept PUB-like inputs I think. What are your views on this?

@woodsp-ibm
Copy link
Member

Another solution which would be much easier to work with would be to create a second

A different method like that seems more promising I agree. With the usage being inside algorithms calling this and result, instead of directly on an estimator seems reasonable enough, if that's what it takes,

@tnemoz
Copy link
Contributor Author

tnemoz commented Aug 5, 2025

@woodsp-ibm As seen here, it seems that the only remaining tests are failing because Qiskit/qiskit#14611 han't been merged yet, but they should pass once that's done. So I think it's ready to be merged now!

Copy link
Member

@woodsp-ibm woodsp-ibm left a comment

Choose a reason for hiding this comment

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

@tnemoz Thanks so much for doing this. It was a lot of work no doubt and thanks too for raising and sorting out the issues here with the latest Qiskit release. The monitor main should once again pass, as you say, when the last of those issues around the complex coeffs is fixed in 2.1.2. Tremendous piece of work, thanks once again.

@tnemoz
Copy link
Contributor Author

tnemoz commented Aug 6, 2025

@tnemoz Thanks so much for doing this. It was a lot of work no doubt and thanks too for raising and sorting out the issues here with the latest Qiskit release. The monitor main should once again pass, as you say, when the last of those issues around the complex coeffs is fixed in 2.1.2. Tremendous piece of work, thanks once again.

Thanks! It was fun (mostly, I now hate the Sampler gradients) working on this 😄 And thank you for the reactivity on answering my questions!

I'll probably try to continue contributing on other issues now that I have a good vision of the codebase :)

By the way, I don't think I have the rights to merge the PR by myself, even now that you've approved it!

@woodsp-ibm woodsp-ibm merged commit 28f486f into qiskit-community:main Aug 6, 2025
16 of 18 checks passed
@woodsp-ibm
Copy link
Member

Just an FYI. It seems the CI actions that were run following the merge failed copyright check on 3 files that look like they were last changed in 2024. So while the PR passed against the branch being done from after merging the dates got updated. The above linked PR has more info.

@tnemoz
Copy link
Contributor Author

tnemoz commented Aug 6, 2025

Well, this PR did take a year to complete, but that's a funny bug 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants