Skip to content
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

QuantumCircuit.draw() cannot produce pdf successfully using 'latex' mode #10211

Open
JoshuaLau0220 opened this issue Jun 6, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@JoshuaLau0220
Copy link
Contributor

Environment

  • Qiskit Terra version: 0.22.2
  • Python version: 3.9.15
  • Operating system: Ubuntu 22.04.2 LTS

What is happening?

Upon calling QuantumCircuit.draw('latex', 'out.pdf'), qiskit fails to produce a pdf output with latex mode. The bug seems to occur not when compiling the pdf, but when qiskit tries to move the pdf from the temporary folder to the target directory.

I've looked around on the Internet and found out that this might be a bug due to os.rename(...). (Ref)
It seems to due to that os.rename(...) cannot handle moving files between filesystems.

How can we reproduce the issue?

I've used the following snippet, substituting <path_to_qasm_file> to any qasm filepath.

from qiskit import QuantumCircuit
qc = QuantumCircuit.from_qasm_file(<path_to_qasm_file>)
qc.draw('latex', filename='out.pdf') # error happens here

The bug though is probably related to filesystems of the device, so it may work just fine on other computers.

What should happen?

The following traceback is produced:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/<redacted_for_privacy>/qiskit/circuit/quantumcircuit.py", line 1957, in draw
    return circuit_drawer(
  File "/<redacted_for_privacy>/qiskit/visualization/circuit/circuit_visualization.py", line 233, in circuit_drawer
    image = _latex_circuit_drawer(
  File "/<redacted_for_privacy>/qiskit/utils/lazy_tester.py", line 149, in out
    return function(*args, **kwargs)
  File "/<redacted_for_privacy>/qiskit/utils/lazy_tester.py", line 149, in out
    return function(*args, **kwargs)
  File "/<redacted_for_privacy>/qiskit/utils/lazy_tester.py", line 149, in out
    return function(*args, **kwargs)
  File "/<redacted_for_privacy>/qiskit/visualization/circuit/circuit_visualization.py", line 491, in _latex_circuit_drawer
    os.rename(base + ".pdf", filename)
OSError: [Errno 18] Invalid cross-device link: '/tmp/tmp8gx5xtqi/circuit.pdf' -> 'out.pdf'

No pdf is outputted. Or more precisely, the pdf is created, but not moved to the target directory, and then the temporary folder is deleted.

Any suggestions?

Currently, if I change qiskit/visualization/circuit/circuit_visualization.py", line 491 from

os.rename(base + ".pdf", filename)

to

import shutil
shutil.move(base + ".pdf", filename)

then it seems to work just fine.

@JoshuaLau0220 JoshuaLau0220 added the bug Something isn't working label Jun 6, 2023
@jakelishman
Copy link
Member

Thanks for the report! I believe we fixed this in #8629 - could you check that the bug persists if you upgrade your version of Terra?

@jakelishman
Copy link
Member

jakelishman commented Jun 6, 2023

Ah, no, it looks like somehow we regressed after that fix was made, sorry. It's pretty hard to test, so we might not have noticed it. Would you like to make a PR making the change you suggest?

@JoshuaLau0220
Copy link
Contributor Author

Of course! I've filed my fix in this PR: #10212.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants