feat: implement Circuit::optimize (basic gate-level optimisation) - #193
Open
manishpaulish wants to merge 1 commit into
Open
feat: implement Circuit::optimize (basic gate-level optimisation)#193manishpaulish wants to merge 1 commit into
manishpaulish wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements
Circuit::optimize, which until now was a stub thatpanicked. As suggested in #157, this is the lightweight "middle ground"
rather than a full port of pyzx's
basic_optimization: it does theobvious cancellations and commutations and leaves heavier rewriting to
the ZX level.
Approach
gate it can cancel or fuse with, provided every gate in between
commutes with it. Run to a fixpoint.
gates and the S/Sdg, T/Tdg pairs).
phases, dropping to identity when the sum is zero.
Measurement, reset, ancilla-init and post-selection are hard barriers.
This guarantees the pass never reorders non-commuting gates.
Correctness
the linear maps before and after via
equal_circuit_tensor.in
circuits/small(tof, barenco_tof, mod5_4, qft, hwb6, ...), allequivalent.
Results
circuits/small, total gate count drops from 8625 to 2665 (about69%). 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)