Skip to content

Some operations involving the identity gate cause an error #7120

Open
@lmarti-dev

Description

@lmarti-dev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/linear-operatorsarea/pauliscomplexity/lowintroduces/modifies 1-2 concepts, should take 1-2 days max for an advanced contributorgood first issueThis issue can be resolved by someone who is not familiar with the codebase. A good starting issue.kind/bug-reportSomething doesn't seem to work.no QC knowledge neededWant to contribute to Cirq, but don't know quantum computing? This issue is for you.triage/acceptedA consensus emerged that this bug report, feature request, or other action should be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions