[unitaryHack 2025] Refactor gate decomposition to support controlled gates#1653
[unitaryHack 2025] Refactor gate decomposition to support controlled gates#1653renatomello merged 63 commits intoqiboteam:masterfrom
Conversation
- Updated Gate.decompose() in abstract.py to propagate controls to decomposed gates. - Refactored gates in gates.py to use _base_decompose for consistency.
for more information, see https://pre-commit.ci
- Enhanced Gate.decompose() to apply controls only to non-cancelled gates using control_mask_after_stripping. - Updated decomposition logic to avoid unnecessary controls on gates that cancel out.
for more information, see https://pre-commit.ci
|
Hi @renatomello,
Thanks in advance for your time and help! |
Co-authored-by: Renato M S Farias <renato.msf@gmail.com>
Co-authored-by: Renato M S Farias <renato.msf@gmail.com>
Co-authored-by: Renato M S Farias <renato.msf@gmail.com>
for more information, see https://pre-commit.ci
…consistent gate decomposition
- Ensure that when decomposing a controlled gate, the base gate instance used for decomposition has the correct control and target qubits set. - This allows the overlap check between free qubits and (control + target) qubits to work as intended.
|
Hi @scarrazza, now all tests should pass! |
|
Hi @scarrazza, I've now run the full test suite and confirmed that all tests pass. That said, I'm aware that the solution may not be the most elegant, so I’d be happy to improve it with your input. Summary of the issue and fix:
@pytest.mark.parametrize("use_toffolis", [True, False])
def test_x_decomposition_errors(use_toffolis):
gate = gates.X(0).controlled_by(1, 2, 3, 4)
with pytest.raises(ValueError):
_ = gate.decompose(2, 3, use_toffolis=use_toffolis)
Let me know if you see a cleaner way to handle this — happy to revise it further. |
|
@GiacomoFrn I'm sorry for the delay, I'll revisit this PR tomorrow. |
|
@GiacomoFrn I like your solution the best. Can you comment on the issue so I can assign it to you, please? Could you also star our repo? |
for more information, see https://pre-commit.ci
|
BTW @GiacomoFrn: #1659 |
Description
This PR addresses the first step of issue #1585, which aims to generalize the
decompose()method for all gates, including controlled gates.Summary of changes
gates.pyto implement a new internal method_base_decompose(), which defines their standard decomposition.decompose()method in abstract.py to support controlled gates by:_base_decompose()on the base gate (i.e., the gate with no control qubits),control_mask_after_stripping()to determine which gates in the decomposition require controls,Notes
_base_decompose()method is for internal use only and separates decomposition logic from control logic.