Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

print(moment) is confusing for crossing gates

Open tanujkhattar opened this issue 3 years ago • 2 comments

Description of the issue When using line qubits and crossing gates in a single moment; the output on printing the moment is confusing and does not show the overlap correctly, unlike the output obtained on printing the whole circuit.

How to reproduce the issue

a, b, c, d = cirq.LineQubit.range(4)
c1 = cirq.Circuit([cirq.CNOT(a, d), cirq.CNOT(b, c)])
c2 = cirq.Circuit([cirq.CNOT(a, c), cirq.CNOT(b, d)])
In [22]: print(c1, '\n', c2)
      ┌──┐
0: ────@─────
       │
1: ────┼@────
       ││
2: ────┼X────
       │
3: ────X─────
      └──┘
       ┌──┐
0: ────@─────
       │
1: ────┼@────
       ││
2: ────X┼────
        │
3: ─────X────
      └──┘
In [23]: print(c1[0], '\n', c2[0]) # <-- Confusing: The output should be different similar to circuit diagram above. 
  ╷ 0 1 2 3
╶─┼─────────
0 │ @─@─X─X
  │
   ╷ 0 1 2 3
╶─┼─────────
0 │ @─@─X─X
  │

Another Example:

In [18]: print(cirq.Circuit(cirq.ControlledGate(cirq.X, num_controls = 3).on(a, b, c, d))[0])
    ...: print(cirq.Circuit([cirq.CNOT(a, d), cirq.CZ(b, c)])[0])
  ╷ 0 1 2 3
╶─┼─────────
0 │ @─@─@─X
  │
  ╷ 0 1 2 3
╶─┼─────────
0 │ @─@─@─X
  │

Cirq version

0.11.0.dev

tanujkhattar avatar Mar 13 '21 19:03 tanujkhattar

Agreed, this is a case that was not covered seemingly in https://github.com/quantumlib/Cirq/pull/3403

balopat avatar Mar 13 '21 22:03 balopat

@tanujkhattar Is this still relevant ?

MichaelBroughton avatar Mar 28 '22 20:03 MichaelBroughton

@MichaelBroughton This issue is still relevant as of Cirq 1.1.0. I recently discovered this confusing print output while debugging.

cduck avatar Jan 24 '23 21:01 cduck

The behavior is similar but transposed if you use NamedQubit.

q0, q1, q2, q3 = cirq.LineQubit.range(4)
print(cirq.Moment(cirq.CZ(q0, q1), cirq.CZ(q2, q3)))
#   ╷ 0 1 2 3
# ╶─┼─────────
# 0 │ @─@ @─@
#   │

q0, q1, q2, q3 = cirq.LineQubit.range(4)
print(cirq.Moment(cirq.CZ(q0, q2), cirq.CZ(q1, q3)))  # q1<->q2
#  ╷ 0 1 2 3
#╶─┼─────────
#0 │ @─@─@─@
#  │

q0, q1, q2, q3 = cirq.NamedQubit.range(4, prefix='q')
print(cirq.Moment(cirq.CZ(q0, q1), cirq.CZ(q2, q3)))
#   ╷ None
#╶──┼──────
#q0 │ @
#   │ │
#q1 │ @
#   │
#q2 │ @
#   │ │
#q3 │ @
#   │

q0, q1, q2, q3 = cirq.NamedQubit.range(4, prefix='q')
print(cirq.Moment(cirq.CZ(q0, q2), cirq.CZ(q1, q3)))  # q1<->q2
#   ╷ None
#╶──┼──────
#q0 │ @
#   │ │
#q1 │ @
#   │ │
#q2 │ @
#   │ │
#q3 │ @
#   │

cduck avatar Jan 24 '23 21:01 cduck