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

Allow mcrx, mcry and mcrz gate methods to accept angles of ParameterValueType #13507

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

Conversation

ShellyGarion
Copy link
Member

@ShellyGarion ShellyGarion commented Dec 1, 2024

Summary

Address #12863

There are currently several open issues that fail with errors like:
QiskitError: 'Cannot synthesize MCRY with unbound parameter:

#7975
#11142
#13192

This PR aims to fix them

Details and comments

@ShellyGarion ShellyGarion added synthesis mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Dec 1, 2024
@ShellyGarion ShellyGarion added this to the 2.0.0 milestone Dec 1, 2024
@ShellyGarion ShellyGarion requested a review from Cryoris December 1, 2024 09:59
@ShellyGarion ShellyGarion requested a review from a team as a code owner December 1, 2024 09:59
@qiskit-bot
Copy link
Collaborator

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

  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia

@ShellyGarion ShellyGarion changed the title [WIP] Allow mcrx, mcry and mcrz gate methods accept angles of ParameterValueType [WIP] Allow mcrx, mcry and mcrz gate methods to accept angles of ParameterValueType Dec 1, 2024
@ShellyGarion ShellyGarion added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Dec 1, 2024
@ShellyGarion
Copy link
Member Author

The failing tests were added as part of #12752.
Perhaps they could be removed?

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

This looks great, I'm glad that we can support parameterized multicontrolled rotations!

I'm wondering whether we can consolidate the method you added with the existing _mcsu2_real_diagonal to ensure we don't duplicate the logic. One option could be to take the unitary as Gate instead of np.ndarray:

def _mcsu2(gate: Gate, num_controls: int, use_basis_gates: bool) -> QuantumCircuit:
    if isinstance(gate, RYGate):
        s_gate = # ...
    elif # the other Pauli rotation cases, RX and RZ
    else:
       unitary = gate.to_matrix()
       s_gate = # compute s_gate
       
   # common logic here

do you think that might work?

@Cryoris
Copy link
Contributor

Cryoris commented Dec 2, 2024

The failing tests were added as part of #12752.
Perhaps they could be removed?

You refer to test_mc_failure_without_annotation? We can remove RX/RY/RZ in that test, but we likely want to keep the others, I'd think

@ShellyGarion
Copy link
Member Author

The failing tests were added as part of #12752.
Perhaps they could be removed?

You refer to test_mc_failure_without_annotation? We can remove RX/RY/RZ in that test, but we likely want to keep the others, I'd think

These are the failing tests, but all of them are failing (not only for RX / RY / RZ)

@coveralls
Copy link

coveralls commented Dec 3, 2024

Pull Request Test Coverage Report for Build 12251624511

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

  • 161 of 189 (85.19%) changed or added relevant lines in 8 files are covered.
  • 27 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.01%) to 88.933%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 67 71 94.37%
qiskit/synthesis/multi_controlled/multi_control_rotation_gates.py 80 104 76.92%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
crates/qasm2/src/lex.rs 4 92.98%
qiskit/circuit/add_control.py 4 96.49%
crates/circuit/src/circuit_data.rs 18 95.42%
Totals Coverage Status
Change from base Build 12131777933: -0.01%
Covered Lines: 79355
Relevant Lines: 89230

💛 - Coveralls

@ShellyGarion ShellyGarion changed the title [WIP] Allow mcrx, mcry and mcrz gate methods to accept angles of ParameterValueType Allow mcrx, mcry and mcrz gate methods to accept angles of ParameterValueType Dec 3, 2024
@ShellyGarion ShellyGarion requested a review from Cryoris December 3, 2024 14:03
@ShellyGarion ShellyGarion requested a review from Cryoris December 8, 2024 11:27
qiskit/circuit/quantumcircuit.py Outdated Show resolved Hide resolved
qiskit/circuit/quantumcircuit.py Outdated Show resolved Hide resolved
else:
mode = "noancilla"

if mode == "basic":
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not something to fix in this PR: but do you know why we have this special case only for MCRY and not for the others? If it's more efficient we could also use it there.

This is something we can fix very cleanly if we synthesize controlled gates via HLS, which knows the number of free auxiliaries.

Copy link
Member Author

Choose a reason for hiding this comment

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

The use of ancillas was done in a PR that you implemented #4787 following issue #4786. I'm not sure if there is some improvement anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

after checking it seems that adding ancillas can improve mcry in certain cases (num_controls >= 7). This is an optimization that should be explored in a follow-up work.

qiskit/synthesis/multi_controlled/mcx_synthesis.py Outdated Show resolved Hide resolved
@ShellyGarion ShellyGarion requested a review from Cryoris December 11, 2024 10:26
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 mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library synthesis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants