Skip to content

feat: implement Circuit::optimize (basic gate-level optimisation) - #193

Open
manishpaulish wants to merge 1 commit into
zxcalc:masterfrom
manishpaulish:basic-circuit-optimisation
Open

feat: implement Circuit::optimize (basic gate-level optimisation)#193
manishpaulish wants to merge 1 commit into
zxcalc:masterfrom
manishpaulish:basic-circuit-optimisation

Conversation

@manishpaulish

Copy link
Copy Markdown

Implements Circuit::optimize, which until now was a stub that
panicked. As suggested in #157, this is the lightweight "middle ground"
rather than a full port of pyzx's basic_optimization: it does the
obvious cancellations and commutations and leaves heavier rewriting to
the ZX level.

Approach

  • Repeatedly scan the gate list; combine each gate with the first later
    gate it can cancel or fuse with, provided every gate in between
    commutes with it. Run to a fixpoint.
  • Cancellation: a gate adjacent to its adjoint (covers self-inverse
    gates and the S/Sdg, T/Tdg pairs).
  • Fusion: adjacent same-axis phase gates (ZPhase, XPhase) add their
    phases, dropping to identity when the sum is zero.
  • Commutation is conservative: disjoint qubits, or two diagonal gates.
    Measurement, reset, ancilla-init and post-selection are hard barriers.
    This guarantees the pass never reorders non-commuting gates.

Correctness

  • Unit tests for each cancellation/fusion/commutation/barrier case.
  • A tensor-equivalence test over random Clifford+T circuits, comparing
    the linear maps before and after via equal_circuit_tensor.
  • Locally I also verified equivalence on the tensor-checkable circuits
    in circuits/small (tof, barenco_tof, mod5_4, qft, hwb6, ...), all
    equivalent.

Results

  • On circuits/small, total gate count drops from 8625 to 2665 (about
    69%). This is total gate count, not T-count; the bulk is Clifford
    cancellation from compute/uncompute structure, which is exactly what a
    basic cleanup pass should catch before ZX simplification.

Possible follow-ups (kept out to stay minimal and correct)

  • Fuse S/T/Z aliases by normalising to ZPhase first.
  • Exploit CNOT control-line commutation with diagonal gates.

Fills the previously-stubbed Circuit::optimize (closes zxcalc#157) with a
forward-scanning pass that cancels inverse gate pairs and fuses adjacent
phase gates, sliding gates past one another where they commute.
Measurement, reset, ancilla-init and post-selection act as barriers.

Commutation is deliberately conservative (gates on disjoint qubits, or
two diagonal gates) so the pass never reorders genuinely non-commuting
gates. Adds unit tests and a tensor-equivalence test over random
Clifford+T circuits.
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