Skip to content

Fix/commuting2q block parameter preservation#16459

Open
TalKozlovski wants to merge 7 commits into
Qiskit:mainfrom
TalKozlovski:fix/commuting2q-block-parameter-preservation
Open

Fix/commuting2q block parameter preservation#16459
TalKozlovski wants to merge 7 commits into
Qiskit:mainfrom
TalKozlovski:fix/commuting2q-block-parameter-preservation

Conversation

@TalKozlovski

Copy link
Copy Markdown

This PR fixes parameter loss in Commuting2qBlock where params was hardcoded to an empty list, breaking parametric circuits.

Fix issue #16456

Details

Problem: Commuting2qBlock.__init__ hardcoded params=[], losing all parameters from input gates.

Solution: Extract parameters from gate operations.

Changes:

  • Modified __init__ to collect parameters from ParameterExpression objects
  • Added two tests in test_swap_strategy_router.py

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description: IBM Bob
  • I used the following tool to generate or modify code: IBM Bob

@TalKozlovski TalKozlovski requested a review from a team as a code owner June 21, 2026 15:44
@TalKozlovski TalKozlovski requested a review from mtreinish June 21, 2026 15:44
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 21, 2026
@qiskit-bot

Copy link
Copy Markdown
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

  • @Qiskit/terra-core

@CLAassistant

CLAassistant commented Jun 21, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ShellyGarion ShellyGarion removed the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 21, 2026
@ShellyGarion ShellyGarion linked an issue Jun 21, 2026 that may be closed by this pull request
@ShellyGarion ShellyGarion added mod: transpiler Issues and PRs related to Transpiler Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. labels Jun 21, 2026
@ShellyGarion

Copy link
Copy Markdown
Member

Thanks @TalKozlovski for finding and fixing the issue!
Could you also add a bug-fix release note to your PR?

@Cryoris Cryoris left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the contribution -- this needs a bit more testing to ensure the parameters also handled correctly in a circuit context. I left some comments below.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we add tests where

  • we check the circuit containing the Commuting2qBlock also knows about the parameters, and
  • we assign parameters to a circuit containing Commuting2qBlock -- especially with an example where we re-use the same parameter multiple times inside a block

This is to ensure that not only the block knows about the parameters but that they are also handled correctly

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for the suggestions, added respective tests

for param in node.op.params:
if isinstance(param, ParameterExpression):
params_set.update(param.parameters)
params_list = sorted(params_set, key=lambda p: p.name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The parameters should support unique sorting already, which will take into account indices of parameters coming from a ParameterVector. Sorting by name will produce unexpected sortings like v[0], v[1], v[10], v[2], v[3], ....

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I drop the sorting and just convert the set into a list

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Dropping the sorting seems correct since the block does not need to know anything about parameter order -- the circuit will figure out the right order when assigning parameters by array. But you have a test which asserts that the order is what you expect, we have to remove this test if we no longer sort.

"""
qubits: set[Qubit] = set()
cbits: set[Clbit] = set()
params_set: set[ParameterExpression] = set()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This contains only Parameters, not expressions

Suggested change
params_set: set[ParameterExpression] = set()
params_set: set[Parameter] = set()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. mod: transpiler Issues and PRs related to Transpiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Commuting2qBlock loses parameters from parametric gates

5 participants