Source code for quantum_launcher.launcher.profiling_launcher
1from typing import Tuple
2import cProfile
3import pstats
4from quantum_launcher.base import Algorithm, Backend, Problem, Result
5from .qlauncher import QuantumLauncher
6
7
[docs]
8class ProfilingLauncher(QuantumLauncher):
9 """ Launcher made for debugging purposes of algorithms and other launchers focusing on performance issues """
10
11 def __init__(self, problem: Problem, algorithm: Algorithm, backend: Backend, profiler_path: str = 'profiling-results.prof'):
12 super().__init__(problem, algorithm, backend)
13 self._profiler_path = profiler_path
14
[docs]
15 def run(self) -> Tuple[Result, pstats.Stats]:
16 with cProfile.Profile() as pr:
17 result = super().run()
18 stats = pstats.Stats(pr)
19 stats.sort_stats(pstats.SortKey.TIME)
20 stats.reverse_order()
21 stats.print_stats()
22 stats.dump_stats(self._profiler_path)
23 return result