Skip to content

Update merging of Conditional block in greedy_pauli_optimisation - #2014

Merged
sjdilkes merged 12 commits into
mainfrom
update-classical-handling-GPS
Sep 2, 2025
Merged

sjdilkes merged 12 commits into
mainfrom
update-classical-handling-GPS

Conversation

@sjdilkes

@sjdilkes sjdilkes commented Sep 2, 2025

Copy link
Copy Markdown
Contributor

Description

The circuit:

comp.add_op<unsigned>(OpType::Rx, 1.0246, {0});
comp.add_conditional_gate<unsigned>(OpType::Ry, {-0.25}, {1}, {0}, 0);
comp.add_conditional_gate<unsigned>(OpType::CZ, {}, {1, 0}, {0}, 0);

Is incorrectly optimised by greedy_pauli_optimisation. This is because the conditional Ry gate is considered commuting with the Rx gate, and then the conditional CZ gate is merged into the same "block" as the Ry gate. This block is incorrectly considered as commuting with the Rx gate, and later at construction the result is that the CZ gate is commuted ahead of the Rx gate, producing an incorrect circuit.

The fix I've added is to only allowing commuting of non-conditional gates forward. This is because we can only incorrectly produce non-commuting blocks by merging gates into a block, which only happens with conditional cases.

This leads to a test regression in this circuit:

circ.add_op<unsigned>(cond1, {0, 0, 1});
circ.add_op<unsigned>(OpType::Rz, 0.3, {0});
circ.add_op<unsigned>(cond2, {0, 0, 1});
circ.add_conditional_gate<unsigned>(OpType::CX, {}, {0, 1}, {0}, 0);
circ.add_conditional_gate<unsigned>(OpType::CX, {}, {0, 1}, {0}, 0);
circ.add_op<unsigned>(OpType::Rz, -0.3, {0});

where the Rotation gates are no longer correctly cancelled (but everything else is).

My opinion is that safe forward-merging of conditional blocks will take serious code changes, and that cases where identical Pauli exponentials are not merged through conditional circuits are a) unlikely to be common in circuits, and b) the Pauli exponentials will still be "close" in the PauliGraph, meaning regularly they may just appear as adjacent rotations in a final circuit that can be compiled out using a pass like RemoveRedundancies.

Related issues

Please mention any github issues addressed by this PR.

Checklist

  • I have performed a self-review of my code.
  • I have commented hard-to-understand parts of my code.
  • I have made corresponding changes to the public API documentation.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the changelog with any user-facing changes.

@sjdilkes
sjdilkes requested a review from yao-cqc September 2, 2025 10:52
@yao-cqc
yao-cqc marked this pull request as ready for review September 2, 2025 15:42
@sjdilkes
sjdilkes merged commit 3e53889 into main Sep 2, 2025
30 checks passed
@sjdilkes
sjdilkes deleted the update-classical-handling-GPS branch September 2, 2025 15:55
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.

2 participants