Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

Cannot print `PauliSumExponential` of identity gate

Open lmarti-dev opened this issue 3 years ago • 3 comments

Description of the issue

Exponentiating an identity gate with PauliSumExponential and then printing it raises a ValueError, similar to #5927. I could not tell whether the pull request that fixes the aforementioned issue would fix this one too. Feel free to close this issue if this is the case.

How to reproduce the issue


q = cirq.LineQubit(0)
op = cirq.PauliSumExponential(cirq.I(q))
circ = cirq.Circuit(op)
print(circ)

ValueError: Wanted diagram info from cirq.PauliStringPhasor(((1+0j)*cirq.PauliString()), qubits=(), exponent_neg=-0.31830988618379075, exponent_pos=0.3183098861837907) for []) but got cirq.CircuitDiagramInfo(wire_symbols=(), exponent=-0.6366197723675815, connected=True, exponent_qubit_index=None, auto_exponent_parens=True)

Cirq version 1.0.0

lmarti-dev avatar Nov 08 '22 13:11 lmarti-dev

Discussion from cirq cync: This is likely because the exponential of the identity has no operation since the PauliStringPhasor is stripping out identity. This should be accepted. Probably an empty PauliString needs a proper diagram strategy.

Possible cause: "in circuit_diagram_info_protocol there's a if max(1, len(rows)) != len(info.wire_symbols):. That max seems to break things because there's no qubits on this one."

dstrain115 avatar Nov 09 '22 18:11 dstrain115

PauliString._circuit_diagram_info_ gets around this by returning NotImplemented if it is empty, which causes the diagramming logic to put the op below the circuit along with global phase and other non-qubit ops. Same strategy would work here.

Though to note, this strategy invokes the str method to display the op, and the PauliStringPhasor.__str__ doesn't match the style of a PSP in the diagram. On the diagram it appears as [I]^0.5 but the str is (I)**0.5. Maybe that's okay? (An empty PauliString just shows a weird isolated I under the diagram).

daxfohl avatar Nov 10 '22 15:11 daxfohl

I'd like to try fixing this.

taochengfei avatar Mar 01 '23 19:03 taochengfei