Skip to content

Support Cirq 1.7 change in exponents in circuit diagrams#1397

Open
mhucka wants to merge 3 commits into
quantumlib:mainfrom
mhucka:fix-diagram-change
Open

Support Cirq 1.7 change in exponents in circuit diagrams#1397
mhucka wants to merge 3 commits into
quantumlib:mainfrom
mhucka:fix-diagram-change

Conversation

@mhucka

@mhucka mhucka commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

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() – I think it was in PR quantumlib/Cirq#7954. As a result, DoubleExcitation(d, b, a, c) ** 2.3 is now rendered in diagrams with ^2.3 instead of ^0.3. Some tests in four_qubit_gates_test.py compared circuit diagrams, and those tests fail in Cirq 1.7.0

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.

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.
@mhucka mhucka requested review from fdmalone, ncrubin and pavoljuhas July 2, 2026 05:27

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the _circuit_diagram_info_ method in four_qubit_gates.py to canonicalize the gate exponent into the (-1, 1] range. The reviewer noted that the type check isinstance(exponent, (int, float)) does not cover NumPy numeric types, which are common in this context, and suggested a simplified mathematical expression for the canonicalization logic.

Comment thread src/openfermion/circuits/gates/four_qubit_gates.py Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant