Skip to content

-O2/-O3 leave a self-cancelling CCX pair behind that --simplify/-O1 remove on their own #91

Description

@jangyoujin0917

Environment

  • staq commit: a2acd39e60ed0e5f1978fa530df7e1e0c7cedf7a (main branch, current upstream HEAD as of 2026-09-18)
  • Built with cmake -S . -B build && cmake --build build --target staq
  • Ubuntu 22.04.5 LTS (WSL2), g++ 11.4.0, cmake 3.22.1

What is happening?

A bare pair of identical, adjacent ccx gates (the identity) is removed entirely by --simplify and -O1, but -O2 and -O3 leave a nonempty residue behind instead.

How can we reproduce the issue?

OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];
ccx q[0],q[1],q[2];
ccx q[0],q[1],q[2];
measure q -> c;
$ ./build/staq --simplify in.qasm   # -> no unitary gates remain
$ ./build/staq -O1 in.qasm          # -> no unitary gates remain
$ ./build/staq -O3 in.qasm          # -> h q[2]; s q[1]; s q[2]; cx q[1],q[2]; sdg q[2]; ...

-O3's output has 6 cx and 9 single-qubit gates. It is still unitarily equivalent to the input (checked with Operator.equiv), so this is a missed-optimization issue rather than a correctness one. Re-running --simplify, -O1, or -O3 on the resulting circuit leaves the same 15 unitary gates, so the missed cancellation is not recovered by a subsequent pass.

What should happen?

Ideally, running optimizations in -O2/-O3 should not prevent an adjacent inverse pair from being removed when --simplify can already eliminate it directly.

Possible mitigation

-O1 runs simplify before any inlining, whereas -O2/-O3 run inline before their first simplify. Since simplify detects this cancellation structurally by gate name, expanding ccx first removes the pattern it would otherwise match.

We prototyped adding an initial simplify before inline in both -O2 and -O3. This resolves the reproducer, and all 123 existing unit tests still pass.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions