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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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

Pull Request Test Coverage Report for Build 11962255098

Details

  • 49 of 51 (96.08%) changed or added relevant lines in 1 file are covered.
  • 7 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.005%) to 88.94%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/add_control.py 49 51 96.08%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
qiskit/circuit/add_control.py 2 95.69%
crates/qasm2/src/lex.rs 4 91.98%
Totals Coverage Status
Change from base Build 11957091507: 0.005%
Covered Lines: 79414
Relevant Lines: 89289

💛 - Coveralls

@Cryoris Cryoris linked an issue Nov 22, 2024 that may be closed by this pull request
if operation.name in basis:
apply_basic_controlled_gate(controlled_circ, operation, q_control, q_target[0])

# TODO: double check if this is necessary
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this todo completed? if not, how could we check if this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not yet: I was wondering whether this was necessary because here we don't decompose the gate which we want to control and shouldn't be able to accumulate any global phase. I left it for the first version since the previous code had it, but I'll try removing and see if everything passes 🙂

Comment on lines +1564 to +1566

print(self.ugu1)
print(cgate)
Copy link
Contributor

Choose a reason for hiding this comment

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

sneaky prints!

Suggested change
print(self.ugu1)
print(cgate)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

heh classic -- sorry about this one, I think I should add a pre-commit hook to check for these 😂

@@ -1577,7 +1581,7 @@ def test_single_controlled_rotation_gates(self, gate, cgate):
elif gate.name == "rz":
self.assertLessEqual(uqc.size(), 43, f"\n{uqc}")
else:
self.assertLessEqual(uqc.size(), 20, f"\n{uqc}")
self.assertLessEqual(uqc.size(), 23, f"\n{uqc}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does the size increase in this test? shouldn't the decomposition be more efficient? (I admit I haven't looked in depth so I might be missing something)

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.

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