From e7a2e9fbe59f38523c39a247890bfe1209146cba Mon Sep 17 00:00:00 2001 From: mhucka Date: Thu, 2 Jul 2026 05:19:04 +0000 Subject: [PATCH 1/2] Support Cirq 1.7 change in exponents in circuit diagrams MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In versions of Cirq below 1.7, `cirq.EigenGate._diagram_exponent()` automatically canonicalized exponents into the interval (-period/2, period/2]. Because `DoubleExcitationGate` has a diagram period of 2, an exponent of `2.3` used to be canonicalized to `0.3` for diagram purposes (because 2.3 mod 2 ≡ 0.3). In the just-released Cirq 1.7.0, the canonicalization logic was removed from `EigenGate._diagram_exponent()`. It now simply rounds the numeric exponent to the specified precision without doing a modulo: ```python def _diagram_exponent(self, args: protocols.CircuitDiagramInfoArgs): if not isinstance(self._exponent, (int, float)): return self._exponent result = float(self._exponent) if args.precision is not None: result = np.around(result, args.precision).item() return result ``` As a result, `DoubleExcitation(d, b, a, c) ** 2.3` is now rendered in diagrams with `^2.3` instead of `^0.3`, which caused some assertions in `four_qubit_gates_test.py` to fail. Since we need to support both 1.7.0 and lower versions of Cirq, this PR adds a step to put the exponent in the canonical form that was used before. This seems like the best approach to preserve backward compatibility and still work with the latest version of Cirq. --- src/openfermion/circuits/gates/four_qubit_gates.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/openfermion/circuits/gates/four_qubit_gates.py b/src/openfermion/circuits/gates/four_qubit_gates.py index 401d378f7..349c3a99d 100644 --- a/src/openfermion/circuits/gates/four_qubit_gates.py +++ b/src/openfermion/circuits/gates/four_qubit_gates.py @@ -124,9 +124,17 @@ def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.Circ # Split up this string to avoid SyntaxError in Python 3.12. down_up = r'\/ /' + '\\' wire_symbols = (up_down, up_down, down_up, down_up) - return cirq.CircuitDiagramInfo( - wire_symbols=wire_symbols, exponent=self._diagram_exponent(args) - ) + + exponent = self._diagram_exponent(args) + if isinstance(exponent, (int, float)): + # Canonicalize rounded exponent into (-1, 1] range (DoubleExcitation has period 2) + h = 1.0 + if not (-h < exponent <= h): + exponent = h - exponent + exponent %= 2.0 + exponent = h - exponent + + return cirq.CircuitDiagramInfo(wire_symbols=wire_symbols, exponent=exponent) def __repr__(self): if self.exponent == 1: From 57a25c20747b04b989524aa728b1872d3b1d6231 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Wed, 1 Jul 2026 22:39:51 -0700 Subject: [PATCH 2/2] Update src/openfermion/circuits/gates/four_qubit_gates.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- src/openfermion/circuits/gates/four_qubit_gates.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/openfermion/circuits/gates/four_qubit_gates.py b/src/openfermion/circuits/gates/four_qubit_gates.py index 349c3a99d..8916f0d03 100644 --- a/src/openfermion/circuits/gates/four_qubit_gates.py +++ b/src/openfermion/circuits/gates/four_qubit_gates.py @@ -126,13 +126,9 @@ def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.Circ wire_symbols = (up_down, up_down, down_up, down_up) exponent = self._diagram_exponent(args) - if isinstance(exponent, (int, float)): + if isinstance(exponent, (int, float, np.integer, np.floating)): # Canonicalize rounded exponent into (-1, 1] range (DoubleExcitation has period 2) - h = 1.0 - if not (-h < exponent <= h): - exponent = h - exponent - exponent %= 2.0 - exponent = h - exponent + exponent = 1.0 - (1.0 - exponent) % 2.0 return cirq.CircuitDiagramInfo(wire_symbols=wire_symbols, exponent=exponent)