Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some operations involving the identity gate cause an error #7120

Open
lmarti-dev opened this issue Mar 4, 2025 · 2 comments
Open

Some operations involving the identity gate cause an error #7120

lmarti-dev opened this issue Mar 4, 2025 · 2 comments
Labels
area/linear-operators area/paulis complexity/low introduces/modifies 1-2 concepts, should take 1-2 days max for an advanced contributor good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/bug-report Something doesn't seem to work. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@lmarti-dev
Copy link

Describe the issue

If one tries to add or subtract the identity gate to another gate (or vice-versa) on a qubit q, for example, I(q) - Z(q), then the following error occurs:

Traceback (most recent call last):
  File "\misc\cirq_sub_i.py", line 31, in <module>
    op(p1, p2)
  File "\misc\cirq_sub_i.py", line 9, in add
    return x + y
TypeError: unsupported operand type(s) for +: 'GateOperation' and 'GateOperation'

One could perhaps add the Pauli beforehand and then apply them on qubits (this doesn't cause the error), but it isn't clear to me how one turns a LinearCombinationsOfGates to a PauliSum. Alternatively, replacing the - with +(-1.0)* solves the issue in some cases. Otherwise, one may replace the offending I(q) by X(q)*X(q) or PauliString(). Is this very last option the preferred way of denoting the identity?

The following assertions are satisfied (and the individual expressions do not throw an error)

assert PauliString() - Z(q) == I(q) + (-1) * Z(q)
assert X(q) * X(q) - Z(q) == I(q) + (-1) * Z(q)

They are all "paraphrases" of the original I(q) - Z(q). As an aside,

assert X(q) ** 2 - Z(q) == I(q) + (-1) * Z(q)

also fails, because X(q) ** 2 is converted to an incompatible class.

Explain how to reproduce the bug or problem

The code below goes through all possible combinations of gates and operations, and prints those not implemented. In fact, moving the op(p1,p2) out of the try block will reproduce the error above.

from cirq import I, X, Y, Z, LineQubit

q = LineQubit(0)

paulis = (I(q), X(q), Y(q), Z(q))


def add(x, y):
    return x + y


add.sym = "+"


def sub(x, y):
    return x - y


sub.sym = "-"


def addm(x, y):
    return x + (-1.0) * y


addm.sym = "+(-1.0)*"

for p1 in paulis:
    for p2 in paulis:
        for op in (add, sub, addm):
            try:
                op(p1, p2)
                msg = "pass"
            except Exception:
                msg = "error"
                print(f"{p1.gate}{op.sym}{p2.gate}: {msg}")

This snippet outputs

I+I: error
I-I: error
I-X: error
I-Y: error
I-Z: error
X-I: error
Y-I: error
Z-I: error

This happens on cirq 1.4.1. I updated right before running the code.

I assume the error can be fixed by adding the missing __add__ and __sub__ in some class, but I am confused by the class structure and have not managed to locate the correct one.

@lmarti-dev lmarti-dev added the kind/bug-report Something doesn't seem to work. label Mar 4, 2025
@daxfohl
Copy link
Collaborator

daxfohl commented Mar 10, 2025

Looks like PauliSum.__add__ has some special handling for identity, that PauliSum.__sub__ is missing. Should be an easy fix if you want to try.

https://github.com/quantumlib/cirq/blob/72e1d203f2afb56313e8eb618ec994cc001275b8/cirq-core/cirq/ops/linear_combinations.py#L773

As for I+I, IdentityGate is not an instance of Pauli and thus doesn't have a special on(q) method that converts it to a PauliString.

https://github.com/quantumlib/cirq/blob/fd95547ac4962cfa3d0d1ff707a8de26ce148511/cirq-core/cirq/ops/pauli_gates.py#L93-L105

Plausibly there should be a _PauliI class similar to _PauliX/Y/Z, that inherits both IdentityGate and Pauli, and I should be an instance of that, similar to X/Y/Z instead of being over by itself. That would be a bigger design change though, and maybe conflicts with other things.

@mhucka mhucka added the triage/discuss Needs decision / discussion, bring these up during Cirq Cynque label Mar 19, 2025
@mhucka
Copy link
Contributor

mhucka commented Mar 19, 2025

Discussed in Cirq Cynq 2025-03-19:

  • Does look like a bug.
  • We're worried that the bigger change idea (adding a new class) might be more work.

Let's accept, with a minimum scope for now.

@mhucka mhucka added good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. area/linear-operators area/paulis complexity/low introduces/modifies 1-2 concepts, should take 1-2 days max for an advanced contributor no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/linear-operators area/paulis complexity/low introduces/modifies 1-2 concepts, should take 1-2 days max for an advanced contributor good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/bug-report Something doesn't seem to work. no QC knowledge needed Want to contribute to Cirq, but don't know quantum computing? This issue is for you. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

No branches or pull requests

3 participants