Skip to content

Commit 312b0c2

Browse files
committed
Force NumPy 2 legacy formatting mode for numbers
As a consequence of [NEP 51](https://numpy.org/neps/nep-0051-scalar-representation.html#nep51), the string representation of scalar numbers changed in NumPy 2 to include type information. This affected printing Cirq circuit diagrams: instead seeing numbers like 1.5, you would see `np.float64(1.5)` and similar. The simplest solution turned out to wrap relevant parts of the Cirq code with the context manager form of [`np.printoptions()`](https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html#numpy-set-printoptions), and use that to set legacy mode '1.25' to get the same string formats as in NumPy 1.x.
1 parent a5c557a commit 312b0c2

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

cirq-core/cirq/_compat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ def _print(self, expr, **kwargs):
192192
if hasattr(value, "__qualname__"):
193193
return f"{value.__module__}.{value.__qualname__}"
194194

195-
return repr(value)
195+
with np.printoptions(legacy='1.25'):
196+
return repr(value)
196197

197198

198199
def dataclass_repr(value: Any, namespace: str = 'cirq') -> str:

cirq-core/cirq/ops/fsim_gate.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,10 @@ def _decompose_(self, qubits) -> Iterator['cirq.OP_TREE']:
196196
yield cirq.CZ(a, b) ** (-self.phi / np.pi)
197197

198198
def _circuit_diagram_info_(self, args: 'cirq.CircuitDiagramInfoArgs') -> Tuple[str, ...]:
199-
t = args.format_radians(self.theta)
200-
p = args.format_radians(self.phi)
201-
return f'FSim({t}, {p})', f'FSim({t}, {p})'
199+
with np.printoptions(legacy='1.25'):
200+
t = args.format_radians(self.theta)
201+
p = args.format_radians(self.phi)
202+
return f'FSim({t}, {p})', f'FSim({t}, {p})'
202203

203204
def __pow__(self, power) -> 'FSimGate':
204205
return FSimGate(cirq.mul(self.theta, power), cirq.mul(self.phi, power))
@@ -476,15 +477,16 @@ def to_exponent(angle_rads: 'cirq.TParamVal') -> 'cirq.TParamVal':
476477
yield cirq.Z(q1) ** to_exponent(after[1])
477478

478479
def _circuit_diagram_info_(self, args: 'cirq.CircuitDiagramInfoArgs') -> Tuple[str, ...]:
479-
theta = args.format_radians(self.theta)
480-
zeta = args.format_radians(self.zeta)
481-
chi = args.format_radians(self.chi)
482-
gamma = args.format_radians(self.gamma)
483-
phi = args.format_radians(self.phi)
484-
return (
485-
f'PhFSim({theta}, {zeta}, {chi}, {gamma}, {phi})',
486-
f'PhFSim({theta}, {zeta}, {chi}, {gamma}, {phi})',
487-
)
480+
with np.printoptions(legacy='1.25'):
481+
theta = args.format_radians(self.theta)
482+
zeta = args.format_radians(self.zeta)
483+
chi = args.format_radians(self.chi)
484+
gamma = args.format_radians(self.gamma)
485+
phi = args.format_radians(self.phi)
486+
return (
487+
f'PhFSim({theta}, {zeta}, {chi}, {gamma}, {phi})',
488+
f'PhFSim({theta}, {zeta}, {chi}, {gamma}, {phi})',
489+
)
488490

489491
def __repr__(self) -> str:
490492
theta = proper_repr(self.theta)

cirq-core/cirq/protocols/circuit_diagram_info_protocol.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,14 @@ def __repr__(self) -> str:
243243
)
244244

245245
def format_real(self, val: Union[sympy.Basic, int, float]) -> str:
246-
if isinstance(val, sympy.Basic):
247-
return str(val)
248-
if val == int(val):
249-
return str(int(val))
250-
if self.precision is None:
251-
return str(val)
252-
return f'{float(val):.{self.precision}}'
246+
with np.printoptions(legacy='1.25'):
247+
if isinstance(val, sympy.Basic):
248+
return str(val)
249+
if val == int(val):
250+
return str(int(val))
251+
if self.precision is None:
252+
return str(val)
253+
return f'{float(val):.{self.precision}}'
253254

254255
def format_complex(self, val: Union[sympy.Basic, int, float, 'cirq.TParamValComplex']) -> str:
255256
if isinstance(val, sympy.Basic):

0 commit comments

Comments
 (0)