Closed floriankittelmann closed 10 months ago
A few things that may or may not help:
measure_all
in your example codeblock at the moment, which is causing invalid circuits to be passed to a Sampler (multiple classical registers), so nothing can actually run from it. I just removed one of these to test.ParameterReferences
may not necessarily mean that class is actually the culprit - it might just be that it's the largest "owner" of memory, and something else is causing a bunch of references to it to remain.ParameterTable
or ParameterReferences
because these objects are purely touched by Python code, and as long as there's no extant references to them being held by your code, they should get happily freed when their refcounts hit zero, or garbage collected if there are any cycles (which there largely shouldn't be). It is possible that Qiskit is somewhere causing extra references to these to be stored, however, but I can't think off the top of my head where this might be.As a suggestion for you to look: maybe try and audit your code base and make sure that you're not keeping references to all temporary QuantumCircuit
objects during your optimisation pass. If you're storing references to them in a "history" list or anything like that, that could be the reason that the references aren't getting freed correctly.
@jakelishman Thank you very much for your answer. I also think, that the ParameterTable
class is not the actual root cause of the problem. But unfortunately, I do not save my QuantumCircuit
in any kind of history list. I only create a list of QuantumCircuit
in the beginning, such that I don't need to generate the circuits in each forward pass. Like this:
class MyModel:
def __init__(self, _sampler: BaseSampler, _dataset: list[np.ndarray]):
self.__list_circuits = [self.__get_model_with_amplitude_encoding(feature) for feature in _dataset]
...
def forward(self, list_idx: list[int], params: np.ndarray) -> np.ndarray:
list_qcs = [self.__list_circuits[i] for i in list_idx]
...
I just tested that as well and it seems not to accumulate memory space in my minimal working example either. Anyway, I needed to change my trainings to TorchConnector
from Qiskit Machine Learning, because the Adam Optimizer using Parameter Shift Rule for gradient calculation resulted in much better results. So my whole training procedure changed and I don't experience similar problems anymore. I guess I wont do any further debugging
Environment
What is happening?
I am trying to learn some Quantum Machine Learning models with Qiskit and after a training of >2h my process is killed from the operating system, because the RAM usage got too high. The tracemalloc library hinted me to the class
ParameterReferences
insideqiskit/circuit/parametertable.py
, which seems to accumulate memory space after every iteration:How can we reproduce the issue?
Unfortunately every attempt to create a minimal working example failed on my side, because I don't find the exact circumstances why and where this happens. Nevertheless, I would like to provide a small example how I use the qiskit library and how my iterations look like
What should happen?
No accumulation of memory space
Any suggestions?
Can you think of any circumstances, in which an object of the class
ParameterReferences
might accumulate memory space? Can you give me some context about this class, such that it helps me to find the bug in my code? Any hint could be helpful, thank you in advance