[Bug]: Parameterized diagonal gates fail when printing circuit diagrams
Describe the issue
Printing a circuit containing a DiagonalGate with a symbolic entry raises a TypeError when circuit diagram precision is enabled.
Numeric-only diagonal gates print successfully.
The same behavior was also reproduced with TwoQubitDiagonalGate and ThreeQubitDiagonalGate.
The issue appears to occur because the diagonal angles are converted to a NumPy array and rounded according to the circuit diagram precision. When symbolic values are present, the resulting object-dtype array causes the NumPy rounding operation to fail.
When the precision is set to None, the rounding step is bypassed and the circuit formats successfully.
Explain how to reproduce the bug or problem
import cirq
import sympy
theta = sympy.Symbol("theta")
gate = cirq.DiagonalGate([0.12345, 1.0, theta, 2.0])
circuit = cirq.Circuit(gate(*cirq.LineQubit.range(2)))
print(circuit)
This produces:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
print(circuit)
File "cirq-core/cirq/circuits/circuit.py", line 299, in __str__
return self.to_text_diagram()
File "cirq-core/cirq/circuits/circuit.py", line 1228, in to_text_diagram
diagram = self.to_text_diagram_drawer(...)
File "cirq-core/cirq/circuits/circuit.py", line 2802, in _draw_moment_in_diagram
info = get_circuit_diagram_info(op, args)
File "cirq-core/cirq/ops/gate_operation.py", line 272, in _circuit_diagram_info_
return protocols.circuit_diagram_info(self.gate, args, NotImplemented)
File "cirq-core/cirq/ops/diagonal_gate.py", line 128, in _circuit_diagram_info_
rounded_angles = rounded_angles.round(args.precision)
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method
The expected behavior is that the circuit prints successfully, with numeric entries respecting the requested precision and symbolic entries remaining representable.
Tell us the version of Cirq where this happens
1.8.0.dev0
I'd be happy to work on this issue and submit a PR with a regression test and fix. If possible, could you please assign this issue to me?
@jyothikacodes Thank you for your interest. It's assigned to you now.
Please make sure to read the developers' docs at https://quantumai.google/cirq/dev/development
Hello @mhucka , I’ve submitted a PR addressing the issue you assigned to me. I would really appreciate it if you could take a look when you have a chance. Thank you for giving me the opportunity to work on it.