Skip to content

Enhancement cirq preprocessing#248

Merged
TheGupta2012 merged 24 commits intoqBraid:mainfrom
feelerx:enhancement-cirq_preprocessing
Nov 29, 2025
Merged

Enhancement cirq preprocessing#248
TheGupta2012 merged 24 commits intoqBraid:mainfrom
feelerx:enhancement-cirq_preprocessing

Conversation

@feelerx
Copy link
Copy Markdown
Contributor

@feelerx feelerx commented Oct 25, 2025

Refactor circuit preprocessing to use Cirq's optimize_for_target_gateset

Closes #93


Summary

This PR refactors the circuit preprocessing pipeline to use Cirq's built-in optimize_for_target_gateset transformer instead of manual try-except decomposition logic.
This improves code quality, performance, and maintainability.


Changes

Core Implementation

  • Created QirTargetGateSet class extending cirq.TwoQubitCompilationTargetGateset to define QIR-supported gates
  • Replaced manual gate-by-gate decomposition with cirq.optimize_for_target_gateset
  • Removed _decompose_gate_op and _decompose_unsupported_gates helper functions
  • Simplified preprocess_circuit to use the new gateset-based approach

Key Features

  • Selective decomposition: Only unsupported gates are decomposed; gates already in the target set (H, X, Y, Z, CNOT, CZ, SWAP, TOFFOLI, measurements) are preserved
  • TOFFOLI preservation: Overridden preprocess_transformers to prevent automatic decomposition of 3-qubit TOFFOLI gates
  • Conditional operation support: Circuits containing ClassicallyControlledOperation bypass full optimization to avoid unitary merge errors
  • Backward compatibility: Added _add_rads_attribute transformer to maintain _rads field on rotation gates for downstream QIR visitor

Implementation Details

The QirTargetGateSet implements custom decomposition methods:

  • _decompose_single_qubit_operation: Handles single-qubit gates, preserving supported gates and decomposing others via unitary matrix
  • _decompose_two_qubit_operation: Handles two-qubit gates, decomposing to CZ operations when needed
  • _decompose_multi_qubit_operation: Preserves TOFFOLI gates without decomposition

Operations wrapped in TaggedOperation or CircuitOperation are properly unwrapped before validation to ensure correct gate type checking.


Benefits

  • Eliminates try-except control flow for gate validation
  • Leverages Cirq's optimized compilation pipeline
  • More maintainable and idiomatic code
  • Better performance through batch circuit transformation

Testing

All existing tests pass, including:

  • Single, double, and triple qubit gate tests
  • Rotation gate tests
  • Measurement and conditional operation tests
  • Circuit conversion tests

…supported_gates in our passes.py file in cirq
…irq's newer API no longer includes it, so we compute and attach it here
H gate and other supported gates were being unnecessarily decomposed
(e.g., H -> Ry) because the decomposition method didn't properly check
if gates were already in the target gateset. Fixed by unwrapping
TaggedOperation wrappers and yielding supported gates unchanged.
Applied same unwrapping logic to _decompose_two_qubit_operation to
prevent unnecessary decomposition of CNOT, CZ, SWAP, and TOFFOLI gates.
TwoQubitCompilationTargetGateset's preprocess_transformers was decomposing
TOFFOLI gates before custom decomposition methods could preserve them.
Fixed by overriding preprocess_transformers to exclude operations already
in the target gateset from expand_composite decomposition.
Skip optimize_for_target_gateset for circuits containing
ClassicallyControlledOperation to avoid unitary merge errors.
@codecov
Copy link
Copy Markdown

codecov bot commented Oct 25, 2025

Codecov Report

❌ Patch coverage is 96.00000% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
qbraid_qir/cirq/QIRTargetGateSet.py 96.42% 3 Missing ⚠️
qbraid_qir/cirq/passes.py 93.75% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment thread qbraid_qir/cirq/QIRTargetGateSet.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed from the suite?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it relies entirely on the _decompose_unsupported_gates function in the former implementation of qbraid_qir.cirq.passes which is no longer present in the current implementation

Copy link
Copy Markdown
Member

@TheGupta2012 TheGupta2012 Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay but I think the test should be implemented with the new functionality i.e. having a Cirq program which is containing a custom gate, and then converted to QIR with the new decomposition function that you have developed. This test should validate the conversion for a non-native gate which "can" be decomposed with Cirq internal methods.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added tests for that

@TheGupta2012
Copy link
Copy Markdown
Member

Hi @feelerx , thank you for fixing this pending issue. Gave a couple of comments but the overall structure looks good! Can you also fix the formatting and bump coverage?

…ed_two_qubit_set, _supported_multi_qubit_set direct attributes of the QIRTargetGateSet class
… gateset 2. The preprocessed circuit is functionally equivalent to the original
@TheGupta2012
Copy link
Copy Markdown
Member

Hi @feelerx , thank you for the changes! I think their are still some linting issues in the format check. Could you please update that so that we can merge? Changes look good mostly

@feelerx
Copy link
Copy Markdown
Contributor Author

feelerx commented Nov 1, 2025

I genuinely have no idea why there are still linting issues in the format check. Generates no error on my end and even has a 10/10 score here too

@TheGupta2012
Copy link
Copy Markdown
Member

Could you validate that the version of black, isort and pylint matches the one used in CI? I've encountered this issue sometimes where the version mismatch caused test failures.

@feelerx
Copy link
Copy Markdown
Contributor Author

feelerx commented Nov 2, 2025

Could you validate that the version of black, isort and pylint matches the one used in CI? I've encountered this issue sometimes where the version mismatch caused test failures.

Could you please confirm which versions of black, isort, and pylint are being used in CI?

@TheGupta2012
Copy link
Copy Markdown
Member

@feelerx apparently one of the test file which you added did not have a qbraid header. Fixed it now, and thanks a ton for the work! Can you finally add the CHANGELOG entry for the change?

@TheGupta2012
Copy link
Copy Markdown
Member

Hi @feelerx , apologies for the delay in response but I think the coverage for this PR is still < the target (main). Can you please bump it to at least 94% for us to merge the code?

You can either -

  • Take a look at the uncovered lines on codecov
  • Generate the coverage locally using the command pytest --cov=qbraid_qir tests/ --cov-report=html and opening the coverage using open htmlcov/class_index.html (all assume the root dir of qbraid-qir)

@feelerx
Copy link
Copy Markdown
Contributor Author

feelerx commented Nov 28, 2025

  • open htmlcov/class_index.html

I have added more comprehensive tests for the files I updated (QIRTargetGateSet.py and passes.py for the cirq module).
The current coverage report is 93% when I run this pytest --cov=qbraid_qir tests/ --cov-report=html and open htmlcov/class_index.html
But the coverage for the two aforementioned files I primarily worked on are 96% and 95%, respectively.

@TheGupta2012 TheGupta2012 merged commit 7b19222 into qBraid:main Nov 29, 2025
7 checks passed
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.

Improve circuit pre-processing for Cirq converter with existing gate decomposers

2 participants