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() invalid cross-device link error on linux #8542

Closed
jmcelroy01 opened this issue Jul 21, 2022 · 6 comments · Fixed by #8629
Closed

QuantumCircuit.draw() invalid cross-device link error on linux #8542

jmcelroy01 opened this issue Jul 21, 2022 · 6 comments · Fixed by #8629
Labels
bug Something isn't working

Comments

@jmcelroy01
Copy link
Contributor

jmcelroy01 commented Jul 21, 2022

Informations

  • Qiskit version:
    qiskit-terra | 0.21.0
    qiskit-aer | 0.10.4
    qiskit-ibmq-provider | 0.19.2
    qiskit | 0.37.0
    qiskit-nature | 0.4.1
    qiskit-finance | 0.3.3
    qiskit-optimization | 0.4.0
    qiskit-machine-learning | 0.4.0

  • Python version:
    Python version | 3.10.5
    Python compiler | GCC

  • Operating system:
    Linux, OpenSUSE Tumbleweed

What is the current behavior?

When running QuantumCircuit.draw(output='latex', filename='qteleport.pdf'), I get the following output:

OSError                                   Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 teleport.draw(output='latex', filename='qteleport.pdf')

File ~/.local/lib/python3.10/site-packages/qiskit/circuit/quantumcircuit.py:1907, in QuantumCircuit.draw(self, output, scale, filename, style, interactive, plot_barriers, reverse_bits, justify, vertical_compression, idle_wires, with_layout, fold, ax, initial_state, cregbundle)
   1904 # pylint: disable=cyclic-import
   1905 from qiskit.visualization import circuit_drawer
-> 1907 return circuit_drawer(
   1908     self,
   1909     scale=scale,
   1910     filename=filename,
   1911     style=style,
   1912     output=output,
   1913     interactive=interactive,
   1914     plot_barriers=plot_barriers,
   1915     reverse_bits=reverse_bits,
   1916     justify=justify,
   1917     vertical_compression=vertical_compression,
   1918     idle_wires=idle_wires,
   1919     with_layout=with_layout,
   1920     fold=fold,
   1921     ax=ax,
   1922     initial_state=initial_state,
   1923     cregbundle=cregbundle,
   1924 )

File ~/.local/lib/python3.10/site-packages/qiskit/visualization/circuit_visualization.py:203, in circuit_drawer(circuit, scale, filename, style, output, interactive, plot_barriers, reverse_bits, justify, vertical_compression, idle_wires, with_layout, fold, ax, initial_state, cregbundle)
    189     return _text_circuit_drawer(
    190         circuit,
    191         filename=filename,
   (...)
    200         cregbundle=cregbundle,
    201     )
    202 elif output == "latex":
--> 203     image = _latex_circuit_drawer(
    204         circuit,
    205         filename=filename,
    206         scale=scale,
    207         style=style,
    208         plot_barriers=plot_barriers,
    209         reverse_bits=reverse_bits,
    210         justify=justify,
    211         idle_wires=idle_wires,
    212         with_layout=with_layout,
    213         initial_state=initial_state,
    214         cregbundle=cregbundle,
    215     )
    216 elif output == "latex_source":
    217     return _generate_latex_source(
    218         circuit,
    219         filename=filename,
   (...)
    228         cregbundle=cregbundle,
    229     )

File ~/.local/lib/python3.10/site-packages/qiskit/utils/lazy_tester.py:149, in LazyDependencyManager.require_in_call.<locals>.decorator.<locals>.out(*args, **kwargs)
    146 @functools.wraps(function)
    147 def out(*args, **kwargs):
    148     self.require_now(feature)
--> 149     return function(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/qiskit/utils/lazy_tester.py:149, in LazyDependencyManager.require_in_call.<locals>.decorator.<locals>.out(*args, **kwargs)
    146 @functools.wraps(function)
    147 def out(*args, **kwargs):
    148     self.require_now(feature)
--> 149     return function(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/qiskit/utils/lazy_tester.py:149, in LazyDependencyManager.require_in_call.<locals>.decorator.<locals>.out(*args, **kwargs)
    146 @functools.wraps(function)
    147 def out(*args, **kwargs):
    148     self.require_now(feature)
--> 149     return function(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/qiskit/visualization/circuit_visualization.py:445, in _latex_circuit_drawer(circuit, scale, style, filename, plot_barriers, reverse_bits, justify, idle_wires, with_layout, initial_state, cregbundle)
    443 if filename:
    444     if filename.endswith(".pdf"):
--> 445         os.rename(base + ".pdf", filename)
    446     else:
    447         try:

OSError: [Errno 18] Invalid cross-device link: '/tmp/tmpe6afugdr/circuit.pdf' -> 'qteleport.pdf'

Steps to reproduce the problem

Use .draw with filename='something.pdf' on linux.

What is the expected behavior?

The pdf file should be created in the cwd.

Suggested solutions

It might be an issue with Pillow / Linux root permissions. Perhaps give a way to specify the output directory to bypass automatic use of /tmp.

I temporarily got around the issue with:

im = QuantumCircuit.draw(output='latex')
im.save('output.pdf', quality=100)
im.close()
@jmcelroy01 jmcelroy01 added the bug Something isn't working label Jul 21, 2022
@jmcelroy01
Copy link
Contributor Author

jmcelroy01 commented Jul 21, 2022

Also just found out that
QuantumCircuit.draw('mpl').savefig('qc.pdf')
works as I'd expect
QuantumCircuit.draw(output='latex', filename='qc.pdf')
to. Trying
QuantumCircuit.draw('latex').save('qc.pdf')
gives the image output, but the top and bottom parts of the image are cut off.

@jakelishman jakelishman transferred this issue from Qiskit/qiskit-metapackage Aug 15, 2022
@jakelishman
Copy link
Member

If possible, could you try a development version of Qiskit Terra and change this line: https://github.com/Qiskit/qiskit-terra/blob/eaf55816470fd64c934b36b7684a02e133dfbc5f/qiskit/visualization/circuit_visualization.py#L491

to

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

You're working on a system where the /tmp directory is mounted on a different file-system to your working directory, and I don't have easy access to a system like that to test on. The shutil.move form should handle the cross-filesystem copy correctly, but it'd be good if you'd be able to test it for us. Thanks!

@jmcelroy01
Copy link
Contributor Author

That did it! It was just on line 445 for me for some reason.

@jakelishman
Copy link
Member

That's great! Would you be interested in making a PR to apply that change? (No pressure.)

The line-number difference isn't important - it'll just be that we've made some changes to the file I linked to between Terra 0.21.0 (which you have installed) and the current development version, which I linked to.

jmcelroy01 added a commit to jmcelroy01/qiskit-terra that referenced this issue Aug 29, 2022
@jmcelroy01
Copy link
Contributor Author

Done. Let me know if there's something more I can do in terms of the tests/documentation contributing steps, as I'm not familiar with tox.

mergify bot pushed a commit that referenced this issue Aug 29, 2022
* Update circuit_visualization.py

Corrects issue #8542

* Update circuit_visualization.py

* Add release note

Co-authored-by: Jake Lishman <[email protected]>
mergify bot pushed a commit that referenced this issue Aug 29, 2022
* Update circuit_visualization.py

Corrects issue #8542

* Update circuit_visualization.py

* Add release note

Co-authored-by: Jake Lishman <[email protected]>
(cherry picked from commit d163e89)
@jakelishman
Copy link
Member

Fixed by #8629.

@jakelishman jakelishman linked a pull request Aug 29, 2022 that will close this issue
3 tasks
mergify bot added a commit that referenced this issue Aug 29, 2022
* Update circuit_visualization.py

Corrects issue #8542

* Update circuit_visualization.py

* Add release note

Co-authored-by: Jake Lishman <[email protected]>
(cherry picked from commit d163e89)

Co-authored-by: Joseph McElroy <[email protected]>
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

Successfully merging a pull request may close this issue.

3 participants