Skip to content

DensePauliString.__mul__ crashes on index bounds and parity gates #7640

@daxfohl

Description

@daxfohl

Describe the issue

DensePauliString (DPS) is designed to accept multiplication by pauli-like gates on linequbits, using the linequibit index as the string index in the DPS. However there's no bounds check, so it crashes if the linequbit index is greater than the DPS string. It also fails on multi-qubit pauli gates like parity gates, which causes associativity problems where they shouldn't exist.

Explain how to reproduce the bug or problem

import cirq
q0, q1, q2 = cirq.LineQubit.range(3)
dps0 = cirq.DensePauliString('x')
dps2 = cirq.DensePauliString('iix')
x0 = cirq.X(q0)
x1 = cirq.X(q1)
x2 = cirq.X(q2)

print(repr(dps0 * dps2))  # cirq.DensePauliString('XIX', coefficient=(1+0j))
print(repr(x0 * dps2))  # cirq.DensePauliString('XIX', coefficient=(1+0j))
print(repr(dps0 * x2))  # index 2 is out of bounds for axis 0 with size 1

print(repr(x0 * (x1 * dps2)))  # cirq.DensePauliString('XXX', coefficient=(1+0j))
print(repr(x1 * dps2 * x0))  # cirq.DensePauliString('XXX', coefficient=(1+0j))
print(repr(x0 * x1 * dps2))  # unsupported operand type(s) for *: 'PauliString' and 'DensePauliString'
xx01 = cirq.XX(q0, q1)
print(repr(xx01 * dps2))  # unsupported operand type(s) for *: 'GateOperation' and 'DensePauliString'

Similar issues occur in the _commutes_ protocol on DPS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/paulisgood 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.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions