Enhancement cirq preprocessing#248
Conversation
…supported_gates in our passes.py file in cirq
…our preprocessing
…irq's newer API no longer includes it, so we compute and attach it here
….py files for cirq
…r global phase differences
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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Why is this removed from the suite?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I have added tests for that
|
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
|
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 |
|
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 |
|
Could you validate that the version of |
Could you please confirm which versions of black, isort, and pylint are being used in CI? |
|
@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 |
|
Hi @feelerx , apologies for the delay in response but I think the coverage for this PR is still < the target ( You can either -
|
…Set and passes in the cirq module
I have added more comprehensive tests for the files I updated (QIRTargetGateSet.py and passes.py for the cirq module). |
Refactor circuit preprocessing to use Cirq's
optimize_for_target_gatesetCloses #93
Summary
This PR refactors the circuit preprocessing pipeline to use Cirq's built-in
optimize_for_target_gatesettransformer instead of manualtry-exceptdecomposition logic.This improves code quality, performance, and maintainability.
Changes
Core Implementation
QirTargetGateSetclass extendingcirq.TwoQubitCompilationTargetGatesetto define QIR-supported gatescirq.optimize_for_target_gateset_decompose_gate_opand_decompose_unsupported_gateshelper functionspreprocess_circuitto use the new gateset-based approachKey Features
H,X,Y,Z,CNOT,CZ,SWAP,TOFFOLI, measurements) are preservedpreprocess_transformersto prevent automatic decomposition of 3-qubit TOFFOLI gatesClassicallyControlledOperationbypass full optimization to avoid unitary merge errors_add_rads_attributetransformer to maintain_radsfield on rotation gates for downstream QIR visitorImplementation Details
The
QirTargetGateSetimplements 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 toCZoperations when needed_decompose_multi_qubit_operation: PreservesTOFFOLIgates without decompositionOperations wrapped in
TaggedOperationorCircuitOperationare properly unwrapped before validation to ensure correct gate type checking.Benefits
try-exceptcontrol flow for gate validationTesting
All existing tests pass, including: