-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
transpilation-related commands take extremely long time in Jupyter notebook #8783
Comments
I run your example on the command line (ipython): 1) the code that produces an error 2) The code that runs slow in Jupyter The code does not run slow. It finishes in about 100 ms. I am using Python 3.10.5 . Did you try the command line ? |
No, I did not. Are you able to reproduce the issue in a Jupyter notebook? |
I'll try. I'm installing terra in a python 3.9 venv so I can test that. While it's installing requirements, I'll try Jupyter. |
I do not see the slowdown in Jupyter. Still 100ms to execute the example after first running the code that errors.
|
@kevinsung, I do see the slowdown with the following
If I change only the Python version to 3.10.5, I do not see the slowdown. EDIT: I also confirm that the bug does not occur if I omit first running the "unrelated" code that errors. |
This is very interesting. Thanks for the investigation @jlapeyre ! |
It appears that the number of calls to this line It seems almost certain that some state is being preserved unintentionally. (Mutable) state is the root of all evil. I won't be able to continue with this till at least after Thursday. I guess someone with a bit more familiarity with this code might find the problem quickly. |
I've found an example that causes slowdown with Python 3.10. Copy the following into a code cell. The first time you execute the cell, it's fast (and results in an error). Subsequent executions are very slow. %%time
from qiskit import *
from qiskit.circuit.library import *
from qiskit.providers.fake_provider import FakeBelemV2
from qiskit.quantum_info import Operator
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes import *
from qiskit.transpiler.passmanager import StagedPassManager
from qiskit.transpiler.passmanager_config import PassManagerConfig
from qiskit.transpiler.preset_passmanagers.common import *
from qiskit.transpiler.preset_passmanagers.plugin import *
def generate_pass_manager(backend) -> PassManager:
plugin_manager = PassManagerStagePluginManager()
pass_manager_config = PassManagerConfig.from_backend(backend)
init = generate_unroll_3q(backend.target)
layout = PassManager()
layout.append(
SabreLayout(backend.coupling_map, max_iterations=4, seed=None, swap_trials=20)
)
layout += generate_embed_passmanager(backend.coupling_map)
routing = plugin_manager.get_passmanager_stage(
"routing", "sabre", pass_manager_config, optimization_level=3
)
# TODO should not have to pass basis_gates separately
# see https://github.com/Qiskit/qiskit-terra/pull/8784
translation = generate_translation_passmanager(
backend.target, basis_gates=backend.operation_names
)
return StagedPassManager(
init=init,
layout=layout,
pre_routing=None,
routing=routing,
translation=translation,
pre_optimization=None,
optimization=None,
scheduling=None,
)
system_register = QuantumRegister(4)
control_register = QuantumRegister(1)
circuit = QuantumCircuit(system_register, control_register)
circuit.append(
RZZGate(0.1).control(1),
list(control_register) + [system_register[0], system_register[1]],
)
backend = FakeBelemV2()
pass_manager = generate_pass_manager(backend)
transpiled = pass_manager.run(circuit)
assert Operator.from_circuit(transpiled).equiv(
Operator.from_circuit(circuit), atol=1e-8
) |
That's a good clue. It's pretty clear that it's unlikely that the bug is in Python (3.9 or 3.10), but rather our code is erroneously keeping some state. Probably when creating ASTs. |
FWIW, I have run the code above in the jupyter notebook, windows 10, python 3.10, trunk version of terra; on the first run it takes about 2.3s; on every consecutive run it takes about 130 ms -- so for me it's actually much faster on subsequent executions. And I have run the above experiment several times -- the results are consistent across runs. |
I can reproduce this as well when raising an error and then trying to transpile. First do from qiskit import *
from qiskit.providers.fake_provider import FakeHanoi
backend = FakeHanoi()
for edge in backend.coupling_map:
print(edge) which raises an attribute error because the backend is v1, not v2. Then do: qc = QuantumCircuit(1)
qc.x(0)
qc.measure_all()
transpile(qc, backend) This always hangs. |
It does not happen for BackendV2 objects |
I wasn't able to use any of these cells to reliably reproduce the issue, but I think #9063 should hopefully fix the issue? I saw the Could people verify if it fixes their problems? |
Environment
What is happening?
In a Jupyter notebook, the following code cell sometimes takes a very long time (about one minute):
How can we reproduce the issue?
The most consistent way I have found to reproduce this issue is to first execute a seemingly unrelated cell that causes an error:
After running this cell, the following cell takes a long time (about one minute):
Interrupting the kernel while it is executing gives the following stack trace:
What should happen?
It should execute in at most a few seconds; certainly not one minute.
Any suggestions?
No response
The text was updated successfully, but these errors were encountered: