Skip to content

Gradient fix#1

Draft
oscar-wallis wants to merge 5 commits intomainfrom
gradient_fix
Draft

Gradient fix#1
oscar-wallis wants to merge 5 commits intomainfrom
gradient_fix

Conversation

@oscar-wallis
Copy link
Copy Markdown
Collaborator

Summary

I have added self._isa_circuit_cache dictionaries to the base gradient methods to act as a cache. The cache stores the ISA circuits for each gradient method with a circuit key from Qiskit 1.4 _circuit_key function, which I have vendored here. The idea being that before a circuit is transpiled, the gradient object checks if the transpilation has already occurred and, if so, pulls the previously transpiled circuit.

Details and comments

I have been testing that this works using a script I will share internally, and I have noticed 2 things,

  1. The gradient calculations are still incredibly slow. I assume that this is due to the GenericBackend jobs and not our code, though it would be worth testing with some other backend to check.

  2. Adding the caches is making the runtime longer, which is less than ideal. I am guessing this is because the computation time added by the caching is longer than the computation time saved by not transpiling on the small instances I have checked. I have not been able to check larger circuit sizes as the run time is just nuts.

To Do

  1. Need to add testing. Look at Declan's FidelityStatevectorKernel for inspiration. I think really all we need to do is check that the cache isn't empty when we run circuits through the gradient, run 2 identical circuits and 1 different and make sure the cache size is 2 and then also that the cache clearing methods work.
  2. Figure out whether the fix actually provides speed up, maybe throw it onto hardware to be honest, open up a session, and do some runs. I think that especially if we use like a higher optimisation level, it should work. It would also mean we know that the stuff works on hardware.

edoaltamura and others added 4 commits March 24, 2025 14:52
* Add support for `Python 3.13`

* Add note for tutorial tests

* Add support for `Python 3.13`

* Add note for tutorial tests

* Update .github/workflows/main.yml

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* Update `def __eq__` in optimizers

* Implement the convention `def __eq__(self, other: object) -> bool:`

* Fix typos

* Fix shape in steppable optimizer

---------

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
@edoaltamura
Copy link
Copy Markdown

@oscar-wallis you could try with the new code in 0.9, and see if you get a speedup on this proj.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants