Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not unroll gates in basis in add_control #13475

Merged
merged 5 commits into from
Dec 5, 2024
Merged

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Nov 21, 2024

Summary

The add_control mechanism currently does some redundant unrolling and re-wrapping of gates that are already in the basis of gates we know how to control. This has knock-on effects and causes #13473, by translating an RZ operation (already supported) to a Phase gate.

This PR avoids this unrolling. As discussed with @alexanderivrii, in the longer term we should consider rewriting this logic entirely and have a controlled gate plugin, which also allows users to supply their own control mechanism.

Details and comments

@Cryoris Cryoris marked this pull request as ready for review November 21, 2024 21:50
@Cryoris Cryoris requested a review from a team as a code owner November 21, 2024 21:50
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Nov 21, 2024

Pull Request Test Coverage Report for Build 12179929323

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 48 of 50 (96.0%) changed or added relevant lines in 1 file are covered.
  • 26 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.02%) to 88.924%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/add_control.py 48 50 96.0%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/two_qubit_decompose.rs 1 92.13%
qiskit/circuit/add_control.py 1 96.49%
crates/qasm2/src/expr.rs 1 94.02%
crates/qasm2/src/lex.rs 5 92.48%
crates/qasm2/src/parse.rs 18 96.69%
Totals Coverage Status
Change from base Build 12131777933: -0.02%
Covered Lines: 79347
Relevant Lines: 89230

💛 - Coveralls

@Cryoris Cryoris linked an issue Nov 22, 2024 that may be closed by this pull request
qiskit/circuit/add_control.py Outdated Show resolved Hide resolved
test/python/circuit/test_controlled_gate.py Outdated Show resolved Hide resolved
test/python/circuit/test_controlled_gate.py Outdated Show resolved Hide resolved
@mtreinish mtreinish added this to the 1.3.1 milestone Dec 4, 2024
@ShellyGarion ShellyGarion added the Changelog: None Do not include in changelog label Dec 5, 2024
if operation.definition is not None and operation.definition.global_phase:
global_phase += operation.definition.global_phase

basis = ["p", "u", "x", "z", "rx", "ry", "rz", "cx"]
Copy link
Member

@ShellyGarion ShellyGarion Dec 5, 2024

Choose a reason for hiding this comment

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

Any reason for this fixed list of gates? what about other gates like:
y / s / t / sx / cz / cp ?

Copy link
Member

Choose a reason for hiding this comment

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

for example, it seems that mcy is not as efficiently decomposed as mcx or mcz, see #13514

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the original list in the code, this PR ensures we don't unroll unnecessarily but doesn't change what we unroll. For a bugfix PR I'd keep it this way, but we should add the cases above for main!

target,
use_basis_gates=True,
)
circuit.mcp(phi, controls, target)
Copy link
Member

Choose a reason for hiding this comment

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

why do you use here mcp-mcry-mcp and not mcrz-mcry-mcrz (which should have less CX gates) ?

Copy link
Contributor Author

@Cryoris Cryoris Dec 5, 2024

Choose a reason for hiding this comment

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

I didn't touch any of the synthesis logic in this PR, just how we unroll 🙂

We can change this in a PR to main, but MCP-MCRY-MCP is not the same as MCRZ-MCRY-MCRZ due to the phase difference, so wouldn't that implement a different unitary?

@ShellyGarion ShellyGarion added Changelog: Bugfix Include in the "Fixed" section of the changelog and removed Changelog: None Do not include in changelog labels Dec 5, 2024
Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fix

@Cryoris Cryoris added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Dec 5, 2024
@Cryoris Cryoris enabled auto-merge December 5, 2024 12:50
@Cryoris Cryoris added this pull request to the merge queue Dec 5, 2024
Merged via the queue into Qiskit:main with commit 582070d Dec 5, 2024
18 checks passed
mergify bot pushed a commit that referenced this pull request Dec 5, 2024
* Do not unroll gates in basis

* add tests

* review comments

* Add reno

(cherry picked from commit 582070d)
@Cryoris Cryoris deleted the fix-mcrz branch December 5, 2024 14:58
@Cryoris
Copy link
Contributor Author

Cryoris commented Dec 5, 2024

@Mergifyio backport stable/1.4

Copy link
Contributor

mergify bot commented Dec 5, 2024

backport stable/1.4

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Dec 5, 2024
* Do not unroll gates in basis

* add tests

* review comments

* Add reno

(cherry picked from commit 582070d)
github-merge-queue bot pushed a commit that referenced this pull request Dec 5, 2024
* Do not unroll gates in basis

* add tests

* review comments

* Add reno

(cherry picked from commit 582070d)

Co-authored-by: Julien Gacon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The multi-controlled gate by RZGate().control() is less optimized.
6 participants