Repository: Qiskit/qiskit

Issue:
Draw barrier in `latex_drawer`
### What is the current behavior?
Right now the barriers are not drawn with latex. We want them to be drawn for `barrier`, `noise`, `save`, `load`, and `snapshot` instructions at that depth.



Discussion:
So I did a little digging and the Qcircuit latex package doesn't support drawing barriers, which is probably why it's not implemented in latex_circuit_drawer(). I'll look into seeing if we can draw a vertical dotted line on top of the xypic that the Qcircuit package generates. But alignment will be tricky doing it that way, it would be better to teach the Qcircuit package how to draw it, because the xypic/xymatrix it uses will be able to always put the vertical line in the right spot.
FYI, the matplotlib drawer in qiskit supports barrier, and much more ...
https://nbviewer.jupyter.org/github/Qiskit/qiskit-tutorial/blob/master/reference/tools/matplotlib_circuit_drawer.ipynb#Show-Barriers
@mtreinish indeed that's why we did not have barriers originally (Qcircuit doesn't have it). I agree with you about the 2 possible solutions. 

@rraymondhp yes we are aware of that. This issue is about the latex drawer. We continue to support both drawers in Qiskit. Latex produces higher quality pictures (no one uses matplotlib to draw quantum circuits in publications). But matplotlib has fewer software dependencies. 
@ajavadia  Thanks! I see. Sorry for this. I agree LaTeX give a better circuit for papers. It will be great to have barriers and others on LaTeX drawer. 
Hi @ajavadia. What kind of figures are you planning for `noise`, `save`, `load`, and `snapshot`? We can implement them to matplotlib_circuit_drawer.
@t-imamichi for barrier the dotted line that you have is great. All the others also behave like barriers, in the sense that they prevent reordering of operations. Perhaps we can label the top of dotted line to indicate the type of simulator instruction this is.
Thanks! We will implement a prototype with matplotlib_circuit_drawer.
Hi all  - 

If you do teach Qcircuit how to draw barriers, those of us maintaining the [package](https://github.com/CQuIC/qcircuit) would be happy to accept a PR adding that functionality.

@Travis-S yep, that was my plan. The patch I pushed up earlier was just me saving my work from playing around. I just pushed https://github.com/CQuIC/qcircuit/pull/33 which adds a new barrier command to Qcircuit.

Resolving patch:
diff --git a/qiskit/tools/visualization/_circuit_visualization.py b/qiskit/tools/visualization/_circuit_visualization.py
--- a/qiskit/tools/visualization/_circuit_visualization.py
+++ b/qiskit/tools/visualization/_circuit_visualization.py
@@ -521,11 +521,16 @@ def latex(self, aliases=None):
             output.write("\t \t")
             for j in range(self.img_depth + 1):
                 cell_str = self._latex[i][j]
-                # floats can cause "Dimension too large" latex error in xymatrix
-                # this truncates floats to avoid issue.
-                cell_str = re.sub(r'[-+]?\d*\.\d{2,}|\d{2,}', _truncate_float,
-                                  cell_str)
-                output.write(cell_str)
+                # Don't truncate offset float if drawing a barrier
+                if 'barrier' in cell_str:
+                    output.write(cell_str)
+                else:
+                    # floats can cause "Dimension too large" latex error in
+                    # xymatrix this truncates floats to avoid issue.
+                    cell_str = re.sub(r'[-+]?\d*\.\d{2,}|\d{2,}',
+                                      _truncate_float,
+                                      cell_str)
+                    output.write(cell_str)
                 if j != self.img_depth:
                     output.write(" & ")
                 else:
@@ -1221,13 +1226,23 @@ def _build_latex_array(self, aliases=None):
 
                 try:
                     self._latex[pos_1][columns] = "\\meter"
+                    prev_entry = self._latex[pos_1][columns - 1]
+                    if 'barrier' in prev_entry:
+                        self._latex[pos_1][columns - 1] = prev_entry.replace(
+                            '\\barrier{', '\\barrier[-1.15em]{')
                     self._latex[pos_2][columns] = \
                         "\\cw \\cwx[-" + str(pos_2 - pos_1) + "]"
                 except Exception as e:
                     raise QISKitError('Error during Latex building: %s' %
                                       str(e))
             elif op['name'] == "barrier":
-                pass
+                qarglist = [self.qubit_list[i] for i in op['qubits']]
+                if aliases is not None:
+                    qarglist = map(lambda x: aliases[x], qarglist)
+                start = self.img_regs[(qarglist[0][0],
+                                       qarglist[0][1])]
+                span = len(op['qubits']) - 1
+                self._latex[start][columns] += " \\barrier{" + str(span) + "}"
             else:
                 assert False, "bad node data"
 


Test patch: