Repository: quantumlib/Cirq

Issue:
QCircuit does not render gates that cross each other correctly
**Description of the issue**

QCircuit creates extra wires in diagram.

**How to reproduce the issue**

Here is a circuit where CNOTs pass over each other:
```
q0, q1, q2, q3 = cirq.LineQubit.range(4)
c = cirq.Circuit(cirq.CX(q0, q2), cirq.CX(q1, q3))
print(c)
```
prints
```
      ┌──┐
0: ────@─────
       │
1: ────┼@────
       ││
2: ────X┼────
        │
3: ─────X────
      └──┘
```
If you run this through the qcircuit convertor,
```
cirq.contrib.qcircuit.circuit_to_latex_using_qcircuit(c)
```
it produces output
```
\Qcircuit @R=1em @C=0.75em {
 \\
 &                      & \qw    &         \qw    &         \qw    &     &\qw\\
 &\lstick{\text{0}}& \qw& \qw    &\control \qw    &         \qw    & \qw &\qw\\
 &\lstick{\text{1}}& \qw& \qw    &         \qw\qwx&\control \qw    & \qw &\qw\\
 &\lstick{\text{2}}& \qw& \qw    &\targ    \qw\qwx&         \qw\qwx& \qw &\qw\\
 &\lstick{\text{3}}& \qw& \qw    &         \qw    &\targ    \qw\qwx& \qw &\qw\\
 &                      & \qw\qwx&         \qw    &         \qw    & \qwx&\qw\\
 \\
}
```
you can see there are extra wires here not corresponding to any of the four qubits.  Compiling this in LaTeX confirms that the diagram is broken.

**Cirq version**
pip installed cirq-unstable
0.9.0.dev




Discussion:
Thanks for opening! This could be a great first issue for someone in the community to pick up. 

Here's the compiled latex for reference:

![image](https://user-images.githubusercontent.com/635073/88974451-7d14ba00-d26d-11ea-8751-b9e5190ddd93.png)

Hello @balopat @dabacon , may I try to work on this one? I presume I would try to modify the corresponding function under the branch 0.9.0.dev? If so, do I need some special qualifications to be able to see that branch? Thanks in advance for your help.
Hey @exAClior, thank you for volunteering! Yes, please, have a look, let me know if you get stuck! 
0.9.0.dev means currently that that's the `master` version. Just checkout master in your fork, create a branch based on that and raise a PR against the `master` in this repo! 

Resolving patch:
diff --git a/cirq/circuits/circuit.py b/cirq/circuits/circuit.py
--- a/cirq/circuits/circuit.py
+++ b/cirq/circuits/circuit.py
@@ -1729,6 +1729,7 @@ def to_text_diagram_drawer(
             qubit_namer: Optional[Callable[['cirq.Qid'], str]] = None,
             transpose: bool = False,
             include_tags: bool = True,
+            draw_moment_groups: bool = True,
             precision: Optional[int] = 3,
             qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT,
             get_circuit_diagram_info: Optional[
@@ -1742,6 +1743,7 @@ def to_text_diagram_drawer(
                 allowed (as opposed to ascii-only diagrams).
             qubit_namer: Names qubits in diagram. Defaults to str.
             transpose: Arranges qubit wires vertically instead of horizontally.
+            draw_moment_groups: Whether to draw moment symbol or not
             precision: Number of digits to use when representing numbers.
             qubit_order: Determines how qubits are ordered in the diagram.
             get_circuit_diagram_info: Gets circuit diagram info. Defaults to
@@ -1778,7 +1780,7 @@ def to_text_diagram_drawer(
         for i in qubit_map.values():
             diagram.horizontal_line(i, 0, w)
 
-        if moment_groups:
+        if moment_groups and draw_moment_groups:
             _draw_moment_groups_in_diagram(moment_groups,
                                            use_unicode_characters,
                                            diagram)
diff --git a/cirq/contrib/qcircuit/qcircuit_diagram.py b/cirq/contrib/qcircuit/qcircuit_diagram.py
--- a/cirq/contrib/qcircuit/qcircuit_diagram.py
+++ b/cirq/contrib/qcircuit/qcircuit_diagram.py
@@ -50,12 +50,12 @@ def _render(diagram: circuits.TextDiagramDrawer) -> str:
         for x in range(max(0, w - 1)):
             key = (x, y)
             diagram_text = diagram.entries.get(key)
-            v = '&' + (diagram_text.text if diagram_text else  '') + ' '
-            diagram2.write(2*x + 1, y, v)
+            v = '&' + (diagram_text.text if diagram_text else '') + ' '
+            diagram2.write(2 * x + 1, y, v)
             post1 = r'\qw' if key in qw else ''
             post2 = r'\qwx' if key in qwx else ''
-            diagram2.write(2*x + 2, y, post1 + post2)
-        diagram2.write(2*w - 1, y, r'&\qw\\')
+            diagram2.write(2 * x + 2, y, post1 + post2)
+        diagram2.write(2 * w - 1, y, r'&\qw\\')
     grid = diagram2.render(horizontal_spacing=0, vertical_spacing=0)
 
     output = '\\Qcircuit @R=1em @C=0.75em {\n \\\\\n' + grid + '\n \\\\\n}'
@@ -78,5 +78,6 @@ def circuit_to_latex_using_qcircuit(
     diagram = circuit.to_text_diagram_drawer(
         qubit_namer=qcircuit_qubit_namer,
         qubit_order=qubit_order,
-        get_circuit_diagram_info=get_qcircuit_diagram_info)
+        get_circuit_diagram_info=get_qcircuit_diagram_info,
+        draw_moment_groups=False)
     return _render(diagram)
diff --git a/cirq/google/line/placement/greedy.py b/cirq/google/line/placement/greedy.py
--- a/cirq/google/line/placement/greedy.py
+++ b/cirq/google/line/placement/greedy.py
@@ -34,8 +34,7 @@ class GreedySequenceSearch:
     method.
     """
 
-    def __init__(self,
-                 device: 'cirq.google.XmonDevice',
+    def __init__(self, device: 'cirq.google.XmonDevice',
                  start: GridQubit) -> None:
         """Greedy sequence search constructor.
 


Test patch:
diff --git a/cirq/contrib/qcircuit/qcircuit_test.py b/cirq/contrib/qcircuit/qcircuit_test.py
--- a/cirq/contrib/qcircuit/qcircuit_test.py
+++ b/cirq/contrib/qcircuit/qcircuit_test.py
@@ -133,3 +133,20 @@ def test_qcircuit_qubit_namer():
            == r'\lstick{\text{q\textasciicircum{}1}}&')
     assert(qcircuit_diagram.qcircuit_qubit_namer(cirq.NamedQubit('q_{1}'))
            == r'\lstick{\text{q\_\{1\}}}&')
+
+
+def test_two_cx_diagram():
+    # test for no moment indication
+    q0, q1, q2, q3 = cirq.LineQubit.range(4)
+    circuit = cirq.Circuit(cirq.CX(q0, q2), cirq.CX(q1, q3), cirq.CX(q0, q2),
+                           cirq.CX(q1, q3))
+    expected_diagram = r"""
+\Qcircuit @R=1em @C=0.75em {
+ \\
+ &\lstick{\text{0}}& \qw&\control \qw    &         \qw    &\control \qw    &         \qw    &\qw\\
+ &\lstick{\text{1}}& \qw&         \qw\qwx&\control \qw    &         \qw\qwx&\control \qw    &\qw\\
+ &\lstick{\text{2}}& \qw&\targ    \qw\qwx&         \qw\qwx&\targ    \qw\qwx&         \qw\qwx&\qw\\
+ &\lstick{\text{3}}& \qw&         \qw    &\targ    \qw\qwx&         \qw    &\targ    \qw\qwx&\qw\\
+ \\
+}""".strip()
+    assert_has_qcircuit_diagram(circuit, expected_diagram)