Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.19k stars 2.35k forks source link

increase spacing of parameters in latex drawer #693

Closed cruzar closed 5 years ago

cruzar commented 6 years ago

QISKit's visualization module is not properly drawing the cu1 gate. With the following code,

from qiskit import QuantumProgram
from qiskit.tools.visualization import circuit_drawer

qp = QuantumProgram()
qr = qp.create_quantum_register('qr', 3)
cr = qp.create_classical_register('cr', 1)
qc = qp.create_circuit('test', [qr], [cr])
qc.cu1(2.3, qr[0], qr[2])
qc.measure(qr[1], cr[0])

circuit_drawer(qc)

The obtained image is,

test

Which is clearly corrupt.

Informations

jaygambetta commented 6 years ago

It is correct but it needs more space before the measurement.

cruzar commented 6 years ago

It also happens without the measurement.

jaygambetta commented 6 years ago

Hi @cruzpmmq

I am not sure what you think is incorrect. The number is the phase that is added if both qubits are in the 1 state. I agree that in the picture you uploaded it is too far away and looks like it is part of the measurement.

cruzar commented 6 years ago

Hi @jaygambetta

I was expecting the usual boxed notation for controlled operations. Also, the following snippet implements the fourier transform on three qubits and the figure representation doesn't look so nice...

from qiskit import QuantumProgram
from qiskit.tools.visualization import circuit_drawer
from numpy import pi

qp = QuantumProgram()

qr = qp.create_quantum_register('qr', 3)
qc = qp.create_circuit('qc', [qr])

c_rk = lambda k, ctl, tgt: qc.cu1(2*pi/(2**k), ctl, tgt)

qc.h(qr[0])
c_rk(2, qr[1], qr[0])
c_rk(3, qr[2], qr[0])
qc.h(qr[1])
c_rk(2, qr[1], qr[2])
qc.h(qr[2])

circuit_drawer(qc)

test2

ajavadia commented 6 years ago

@cruzpmmq the CU1 gate is symmetric. So this is the correct notation. A boxed notation would imply that control and target are different.

I agree the angles should be spaced out better. It is due to latex's column layout where the label of one column runs into the other column. I'll try to adjust it.

cruzar commented 6 years ago

@ajavadia

Ok, I understand. I was just mentioning that because this operation is sometimes represented with a boxed notation (e.g. Fig.5.1 Nielsen & Chuang), and also because cu1() has ctl and tgt parameters. Thanks for looking into it.

By the way, on another subject: One very helpful feature to add to this drawing methods would be to include a parameter for the user to choose if the numbering on the qubits of the diagram goes 0,1,2,.. from top to bottom (as it is) or bottom-up. That would be very helpful.

rraymondhp commented 6 years ago

You may consider to use matplotlib_circuit_drawer.

from qiskit import QuantumProgram
from qiskit.tools.visualization import matplotlib_circuit_drawer as circuit_drawer
from numpy import pi
%matplotlib inline
%config InlineBackend.figure_format = "svg"

qp = QuantumProgram()

qr = qp.create_quantum_register('qr', 3)
qc = qp.create_circuit('qc', [qr])

c_rk = lambda k, ctl, tgt: qc.cu1(2*pi/(2**k), ctl, tgt)

qc.h(qr[0])
c_rk(2, qr[1], qr[0])
c_rk(3, qr[2], qr[0])
qc.h(qr[1])
c_rk(2, qr[1], qr[2])
qc.h(qr[2])

my_style = {"usepiformat":True}

circuit_drawer(qc, style=my_style)

image

rraymondhp commented 6 years ago

It allows you to write the box-ed control. Please refer to the tutorial here: https://nbviewer.jupyter.org/github/Qiskit/qiskit-tutorial/blob/master/reference/tools/matplotlib_circuit_drawer.ipynb

from qiskit import QuantumProgram
from qiskit.tools.visualization import matplotlib_circuit_drawer as circuit_drawer
from numpy import pi
%matplotlib inline
%config InlineBackend.figure_format = "jpeg"

qp = QuantumProgram()

qr = qp.create_quantum_register('qr', 3)
qc = qp.create_circuit('qc', [qr])

c_rk = lambda k, ctl, tgt: qc.cu1(2*pi/(2**k), ctl, tgt)

qc.h(qr[0])
c_rk(2, qr[1], qr[0])
c_rk(3, qr[2], qr[0])
qc.h(qr[1])
c_rk(2, qr[1], qr[2])
qc.h(qr[2])

my_style = {"usepiformat":True, "latexdrawerstyle":False}

circuit_drawer(qc, style=my_style)

image

rraymondhp commented 6 years ago

@diego-plan9 the latex drawer requires latex installation (several GBs) and the installation can be technically difficult for various environment. I suggest we close this issue by making the matplotlib drawer as the default drawer.

diego-plan9 commented 6 years ago

@diego-plan9 the latex drawer requires latex installation (several GBs) and the installation can be technically difficult for various environment. I suggest we close this issue by making the matplotlib drawer as the default drawer.

Hmm, currently if using circuit_drawer(), the latex drawer is tried first, but falls back to the matplotlib one; and the user can chose which one to use by calling the function invidually (latex_circuit_drawer() or matplotlib_circuit_drawer().

Which I think covers the case you mention, if I'm reading it right: if the user is not willing/not able to install latex and the dependencies (which indeed are huge), in practice the matplotlib will be used as "default" when calling circuit_drawer(). Does this behaviour map into your request? If not, could you comment on https://github.com/Qiskit/qiskit-terra/issues/612, for general improvements about the circuit drawer as whole?

1ucian0 commented 5 years ago

A summary about this issue: There is an error in the spacing of cu1 gate in the LaTeX drawer (as explained in https://github.com/Qiskit/qiskit-terra/issues/693#issuecomment-408645734). @ajavadia "will try to adjust it" (I'm assigning @ajavadia, feel free to remove yourself). Also, @qruzar mentioned the reversebits issue, which should be addressed by merged PR #762.

ajavadia commented 5 years ago

The only thing left to do for this issue is to increase spacing of parameters for better readability. I adjusted the title accordingly.