qutip / qutip-qip

The QuTiP quantum information processing package
https://qutip-qip.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
116 stars 63 forks source link

How can i generate the quantum circuit image with qutip? #64

Closed FarooqKhaleel closed 3 years ago

FarooqKhaleel commented 3 years ago

Dear Respected Members of the Group, I hope you are doing well. I am trying to reproduce the results of the following post:

https://nbviewer.ipython.org/github/qutip/qutip-notebooks/blob/master/examples/quantum-gates.ipynb#Gates-in-QuTiP-and-their-representation

in windows 10 environment and with Imagemagick, perl, Ghostscript and Latex are all installed and verified. However, it seems like it cant generate the qcirc.pdf file. I receive the following error,

Warning (from warnings module): File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 128 warnings.warn("Could not locate system 'pdfcrop':" UserWarning: Could not locate system 'pdfcrop': image output may have additional margins. Traceback (most recent call last): File "C:\Users\msi-pc\Desktop\farrrrr.py", line 11, in q.png File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit.py", line 1821, in png return DisplayImage(self._raw_png(), embed=True) File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit.py", line 1811, in _raw_png return _latex.image_from_latex(self.latex_code(), "png") File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 245, in image_from_latex out = CONVERTERSfile_type File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 180, in converter _run_command((which, *configuration.arguments, in_file, out_file)) File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 65, in _run_command raise RuntimeError(e.stderr.decode(sys.stderr.encoding)) from None RuntimeError: magick: unable to open image 'qcirc.pdf': No such file or directory @ error/blob.c/OpenBlob/3537.

I kindly ask for your help with many thanks in advance. Thank you very much Yours sincerely,

purva-thakre commented 3 years ago

UserWarning: Could not locate system 'pdfcrop': image output may have additional margins

You need to install pdfcrop. It could be solved by installing texlive-extra-utils or one of the recommended prerequisites for latextools.

@BoxiLi This seems to be a Windows issue.

FarooqKhaleel commented 3 years ago

Is there a pip command for texlive-extra-utils tool?

FarooqKhaleel commented 3 years ago

I installed the Latextool by the pip command, and i get the issue. I am looking forward for other solutions. Thank you very much for your help

purva-thakre commented 3 years ago

@FarooqKhaleel You need not install latextools.

I meant the prerequisites for latextools which are listed on the linked webpage - texlive-latex-recommended texlive-latex-extra.

Also, if you are trying to run the examples in the notebook, you need to change how Gate is imported. In the latest version of qutip-qip , gates were moved from qutip_qip.circuit to qutip_qip.operations.

FarooqKhaleel commented 3 years ago

@purva-thakre Thank you very much for your answer. I tried and didnt work. They couldnt be installed using pip commands. I am running on windows 10 64 bits. All other wheels that are required to be installed such magick, perl , etc. Are installed and verified. I am looking forward for your help. Thanks alot

BoxiLi commented 3 years ago

Hi @FarooqKhaleel , may I ask how you installed the LaTeX distribution? QuTiP's plotting functionality relies on the qcircuit package of LaTeX and it could be that you only installed the core latex without any packages.

On windows, I would recommend using MikTeX, which automatically install new packages if it is required. It takes a few extra minutes on the first run, but solves the trouble.

FarooqKhaleel commented 3 years ago

@BoxiLi Thank you very much for your answer.

Regarding the LaTeX, i installed using pip commands

pip install Latex pip install latextools

However, your advice almost worked. Installing MikTeX almost solved the problem. However, I get the following error. It seems that qutip can not generate the pdf file. qcirc.pdf.

Traceback (most recent call last): File "C:\Users\msi-pc\Desktop\farrrrr.py", line 13, in q.png File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit.py", line 1821, in png return DisplayImage(self._raw_png(), embed=True) File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit.py", line 1811, in _raw_png return _latex.image_from_latex(self.latex_code(), "png") File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 233, in image_from_latex _crop_pdf(filename + ".pdf") File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 121, in _crop_pdf _run_command((_pdfcrop, filename, temporary)) File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 65, in _run_command raise RuntimeError(e.stderr.decode(sys.stderr.encoding)) from None RuntimeError: pdfcrop: security risk: running with elevated privileges

initexmf: security risk: running with elevated privileges

initexmf: security risk: running with elevated privileges

!!! Error: Input file `qcirc.pdf' not found!

I am looking forward for your help. Thank you very much for your help. Yours sincerely,

BoxiLi commented 3 years ago

The packages

pip install Latex
pip install latextools

are not needed. They are both Python packages that build a link between LaTeX and Python. An independent LaTeX distribution like MikTex is still required. Sorry if our installation guide is a bit vague on this.

Although the error message changes, it doesn't seem like we went very far... The qcric.pdf is still not generated. In principle MikTeX should have handled everything. Have you tried to rerun the same python script? It could be that the first time MikTeX is installing new dependencies and needs to be reloaded.

Edit: The warning security risk: running with elevated privileges indicates that you probably installed MikTeX with in the administrator mode. I'm not sure if that is the reason.

purva-thakre commented 3 years ago

@FarooqKhaleel You might want to re-install MikTex as a single user instead of as an admin. See this comment for more information.

FarooqKhaleel commented 3 years ago

Dear @BoxiLi @purva-thakre I hope you are doing well. The problem is the same. The qcirc.pdf file still cant be generated. :( I restarted my PC after re-installing miktex as a single user and I am running the following code:

from IPython.display import Image from numpy import pi import numpy as np import latex from qutip import from qutip_qip.operations import from qutip_qip.circuit import QubitCircuit, Gate q = QubitCircuit(2, reverse_states=False) q.add_gate("CSIGN", controls=[0], targets=[1]) q.png

I still receive the following error: Traceback (most recent call last): File "C:\Users\msi-pc\Desktop\farrrrr.py", line 10, in q.png File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit.py", line 1821, in png return DisplayImage(self._raw_png(), embed=True) File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit.py", line 1811, in _raw_png return _latex.image_from_latex(self.latex_code(), "png") File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 233, in image_from_latex _crop_pdf(filename + ".pdf") File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 121, in _crop_pdf _run_command((_pdfcrop, filename, temporary)) File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 65, in _run_command raise RuntimeError(e.stderr.decode(sys.stderr.encoding)) from None RuntimeError: pdfcrop: security risk: running with elevated privileges

!!! Error: Input file `qcirc.pdf' not found!

Thank you very much for your help. Yours sincerely,

purva-thakre commented 3 years ago

@FarooqKhaleel

Here's a couple of links that might be able to help :

It's possible there's a clash with some packages being installed as an admin and some not.

Have you also checked if the latest version of MikTex is installed ? After the installation, did you check for updates ?

Check if all of the needed latex dependencies are installed - braket, qcircuit, pdfcrop, pdflatex

FarooqKhaleel commented 3 years ago

Dear @purva-thakre Thank you for your help. The following error stuck. I re-installed miktex and go to packages and installed all the aforementioned latex dependencies. The error is the same. :(( Maybe there is an issue with my PC.

Traceback (most recent call last): File "C:\Users\msi-pc\Desktop\farrrrr.py", line 10, in q.png File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit.py", line 1821, in png return DisplayImage(self._raw_png(), embed=True) File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit.py", line 1811, in _raw_png return _latex.image_from_latex(self.latex_code(), "png") File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 233, in image_from_latex _crop_pdf(filename + ".pdf") File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 121, in _crop_pdf _run_command((_pdfcrop, filename, temporary)) File "C:\Program Files (x86)\Python36-32\lib\site-packages\qutip_qip\circuit_latex.py", line 65, in _run_command raise RuntimeError(e.stderr.decode(sys.stderr.encoding)) from None RuntimeError: pdfcrop: security risk: running with elevated privileges

!!! Error: Input file `qcirc.pdf' not found!

Anyway, if there are other solutions. I would be appreciate it. Thank you very much for your help. Yours sincerely,

BoxiLi commented 3 years ago

Ok, sorry to hear that it is still not working. The pdf is still not successfully generated. It is really hard to check the LaTeX error source from python. So if you still have hope, let's do a more thorough diagnostics from the LaTeX side.

Could you copy the following lines into a text file and name it qcircuit_example.tex

\documentclass{article}
\usepackage{qcircuit}

\begin{document}
\Qcircuit @C=1em @R=.7em {
& \gate{H} & \gate{Z} & \gate{H} & \qw \\
& \qw & \gate{X} & \qw & \qw
}
\end{document}

It is a minimal example to plot quantum circuit using qcircuit. Then, please go to the file directory and run the command line pdflatex qcircuit_example.tex and see what is the output. You will probably see quite a few lines of logging info. If everything succeeds, it should end with something like

Output written on qcircuit_example.pdf (1 page, 18859 bytes).
Transcript written on qcircuit_example.log.

The pdf will look like this (with large margin) image

If something went wrong, the log information should tell.

FarooqKhaleel commented 3 years ago

Dear @BoxiLi I hope you are doing well and thank you very much for your help and replies. I appreciate it. I did what you suggest literally, and nothing happened. The command line in cmd just execute the command pdflatex and nothing happened. No pdf is generated. Just a new command line appeared. I am looking forward to hearing from you thanks again for your help. Yours sincerely,

BoxiLi commented 3 years ago

Usually, the command pdflatex will leave some log files in the current directory. Did you see any new file? Like qcircuit_example.log or qcircuit_example.aux.

If you don't see anything, then there is probably still something wrong with MikTeX. It is quite weird that nothings shows in the cmd.

FarooqKhaleel commented 3 years ago

I opened the windows command window as admin and run the above script. Just a new line appears and no log file is generated at the Desktop. :((

BoxiLi commented 3 years ago

What about executing it not as an admin?

FarooqKhaleel commented 3 years ago

Capture Just a new line appears. I run it as not admin.

BoxiLi commented 3 years ago

Ok... Nothing appears, could you try where pdflatex and check if the returned path is the MikTeX one?

FarooqKhaleel commented 3 years ago

Capture

BoxiLi commented 3 years ago

Ha, you have two pdflatex installed somehow.

Try to use pip uninstall pdflatex to remove that python one. And check if only the MiKTeX one remains with where pdflatex.

Then restart cmd and see if pdflatex qcircuit_example.tex gives you something reasonable.

BoxiLi commented 3 years ago

Or pip uninstall latextools, pip uninstall latex, if that pdflatex comes with them.

Just remove everything in pip related to latex.

FarooqKhaleel commented 3 years ago

I did it but a lot of things happening when i run the command pdflatex example.tex, I will be back as soon as possible.

BoxiLi commented 3 years ago

a lot of things happening when i run the command pdflatex example.tex

Don't panic. That is as expected.

FarooqKhaleel commented 3 years ago

Capture

FarooqKhaleel commented 3 years ago

The pdf file is generated and the circuit is drawn perfectly. Now i return to python?

BoxiLi commented 3 years ago

Now i return to python?

Yep

FarooqKhaleel commented 3 years ago

Capture

FarooqKhaleel commented 3 years ago

The drawing does not appear. Just a new line appears.

BoxiLi commented 3 years ago

Cool, there is no error message, which means that we are likely to have succeeded.

The figure will only be rendered properly in a Jupyter Notebook. Just as the link you gave: https://nbviewer.ipython.org/github/qutip/qutip-notebooks/blob/master/examples/quantum-gates.ipynb#Gates-in-QuTiP-and-their-representation

You can now install a Jupyter notebook by pip install jupyter notebook and then use jupyter notebook in cmd to start it. It will be opened in your default browser.

FarooqKhaleel commented 3 years ago

Capture

BoxiLi commented 3 years ago

edited

Okay, it seems that you already installed jupyter notebook but it is not in the system path. How did you start jupyter notebook before?

Could you try python3 -m notebook?

It could also be python -m notebook, py -m notebook depending on your system.

FarooqKhaleel commented 3 years ago

Capture

BoxiLi commented 3 years ago

It could also be python -m notebook, py -m notebook depending on your system.

FarooqKhaleel commented 3 years ago

by applying python -m notebook

It opened in the browser, but there too many file. how can i find the picture?

BoxiLi commented 3 years ago

It will open in the current directory. if you change the directory in cmd before calling the notebook, it will also change.

You need to create a new Jupyter Notebook by clicking the new button, image

Copy your code there and run it.

FarooqKhaleel commented 3 years ago

Capture

FarooqKhaleel commented 3 years ago

Capture

FarooqKhaleel commented 3 years ago

Finally, you are a legend. Thank you very much for your time and efforts. I appreciate it and i wish you all the best.

BoxiLi commented 3 years ago

That's it. Bravo!

FarooqKhaleel commented 3 years ago

@BoxiLi Dear Dr. Boxi Li I hope you are doing well. Could you please tell me how can i add an input matrix and observe the output matrix independently. For Example, If the above circuit is CNOT, how can i verify the I/O matrices INPUT -- OUTPUT 00 -- 00 01 -- 01 10 -- 11 11 -- 10 Thank you for your help Dr. Boxi. Yours sincerely,

BoxiLi commented 3 years ago

Hi, @FarooqKhaleel, the simplest thing is to run circuit.run(init_state) for all possible computational basis states. Checking if the outputs are expected.

PS: I'm no Dr. yet :)

FarooqKhaleel commented 3 years ago

@BoxiLi Thank you very much for your reply. I am entering different inputs. However, the dimensions error occurs. Could you please provide an example? Thank you very much for your help. I wish you the Ph.D. degree soon with Excellent marks. Yours sincerely,

FarooqKhaleel commented 3 years ago

Capture

FarooqKhaleel commented 3 years ago

@BoxiLi I am sorry i am taking your time @BoxiLi . Should the output be 1 0 1 0 ? since at the input we have the first bit (at the bottom) is |0> and the first bit (upper one) is |1>? Thank you very much for your help. Yours sincerely,

purva-thakre commented 3 years ago

Should the output be 1 0 1 0 ?

If you need this output then a CNOT gate cannot be applied on your initial state |00>. You need to apply a hadamard gate which is labeled as SNOT in the qip module. . The output of this will also have included normalization constants.

q.add_gate("SNOT",targets=[0])

You are getting a basis not defined error because you are calling the function from qutip after basis(3,2).

FarooqKhaleel commented 3 years ago

@purva-thakre Thank you very much for your time and help. I need to enter Capture

0 1 so the output remains 0 1 and if i enter 1 0 the output is 1 1 I just want to understand this. so I can build larger circuits. Thank you very much @purva-thakre Yours sincerely,

FarooqKhaleel commented 3 years ago

I studied the fundamentals of quantum computations. but I am beginner in qutip. These are my first steps. Thank you for your help.

purva-thakre commented 3 years ago

If you are still starting with a 2 qubit state then your initial state must be changed for the preferred output.

Instead of tensor(basis(2,0), basis(2,0)) change the state to tensor(basis(2,0), basis(2,0)) + tensor(basis(2,1), basis(2,1)).

Following is the changed zero_state.

Quantum object: dims = [[2, 2], [1, 1]], shape = (4, 1), type = ket
Qobj data =
[[1.]
 [0.]
 [0.]
 [1.]]

Once the initial state is changed, here's how you can use CNOT for what you want to do :

q = QubitCircuit(2, reverse_states=False)
q.add_gate("CNOT",controls =[0], targets=[1])
q.png

will output following circuit

image

initial_state = tensor(basis(2,0), basis(2,0)) + tensor(basis(2,1), basis(2,1))
result = q.run(state=initial_state)
print(result)

Result of above is

Quantum object: dims = [[2, 2], [1, 1]], shape = (4, 1), type = ket
Qobj data =
[[1.]
 [0.]
 [1.]
 [0.]]
purva-thakre commented 3 years ago

I need to enter 0 1 so the output remains 0 1 and if i enter 1 0 the output is 1 1

You will need to change the initial state and gates for this. I used a different initial state /gates for above example.