Skip to content

Conversation

@Vivek1106-04
Copy link

Description
Implemented the drop_diagonal_before_measurement transformer as requested in #4935.

This transformer identifies diagonal gates (Z, S, T, CZ, etc.) that are immediately followed by measurements. Since diagonal gates commute with the computational basis measurement, they can be removed without affecting the outcome probabilities.

Implementation Details:

  1. Pre-processing: Runs eject_z first to push Z gates as far right as possible (handling the Z-CZ commutation case described in the issue).
  2. Safety Logic: Uses strict safety checks. A diagonal gate is only removed if ALL qubits it acts on are being measured.
    • Example: CZ(q0, q1) followed by Measure(q0) is NOT removed (to preserve q1's phase).
    • Example: CZ(q0, q1) followed by Measure(q0), Measure(q1) IS removed.

Testing:
Added diagonal_optimization_test.py covering:

  • Single qubit removal (Z, S chains).
  • Multi-qubit safety (CZ removal vs retention).
  • Commutation blocking (Z blocked by X).
  • The specific use cases mentioned in the issue.

Fixes: #4935

This transformer removes diagonal gates (Z, CZ, etc.) that appear immediately before measurements, as they do not affect the measurement outcome in the computational basis. Uses eject_z to maximize optimization opportunities.

Fixes quantumlib#4935
@Vivek1106-04 Vivek1106-04 requested review from a team and vtomole as code owners December 3, 2025 10:47
@github-actions github-actions bot added the size: L 250< lines changed <1000 label Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: L 250< lines changed <1000

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant