Skip to content

Sum VF2Layout average errors in a deterministic order - #16732

Merged
Julien Gacon (Cryoris) merged 3 commits into
Qiskit:mainfrom
hfwen0502:fix-vf2layout-nondeterminism
Aug 13, 2026
Merged

Julien Gacon (Cryoris) merged 3 commits into
Qiskit:mainfrom
hfwen0502:fix-vf2layout-nondeterminism

Conversation

@hfwen0502

Copy link
Copy Markdown
Member

Summary

Fixes the non-deterministic optimization_level=2 transpiler output reported in #16490: with a
fixed circuit, backend, and seed_transpiler, the 1-qubit gate count and depth varied run-to-run
(the 2-qubit count stayed stable) on a Target that has control flow and a symmetric coupling
map
.

Details and comments

Root cause (full analysis in #16490): build_average_error_map in VF2Layout summed each qubit's
per-operation gate errors while iterating the HashSet returned by
Target::operation_names_for_qargs, whose iteration order is randomized per call. Floating-point
addition is not associative, so two symmetric layouts scored Ξ΅-apart in a non-deterministic
direction, and VF2's strictly-decreasing comparison then picked one of the two symmetric embeddings
~50/50 per run. The chosen layout deterministically fixes the resulting 1q-gate count and depth
(which is why the 2q count β€” unaffected by a symmetric relabel β€” stayed stable, and why both
control_flow=True and make_symmetric() are required to trigger it).

The fix is the short-term direct VF2 change preferred by Jake Lishman (@jakelishman) and Matthew Treinish (@mtreinish) in the issue
(commit 8ea89ab): collect the operation names into a Vec, sort_unstable(), and sum in that stable
order. No public API change β€” operation_names_for_qargs still returns a set.

Per the issue discussion, this is a candidate for backport to 2.5.2.

Fixes #16490.

AI/LLM disclosure

`build_average_error_map` summed per-qubit gate errors while iterating the
`HashSet` returned by `Target::operation_names_for_qargs`, whose order is
randomized per call.  Floating-point addition is non-associative, so the averaged
error -- and therefore the `VF2Layout` score of two symmetric layouts -- varied
between runs.  On a target with control-flow support and a symmetric coupling map
this made the selected layout, and hence the 1q-gate count and depth of the output
circuit, non-deterministic.

Sort the operation names before summing so the result is reproducible.

Fixes Qiskit#16490.

Signed-off-by: Sophia Wen <hfwen@us.ibm.com>
(cherry picked from commit 8ea89ab)
@hfwen0502
Sophia Wen (hfwen0502) requested a review from a team as a code owner August 10, 2026 18:23
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Aug 10, 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

@Cryoris Julien Gacon (Cryoris) added the stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge. label Aug 11, 2026
…4e6f0a1c3d57.yaml

Co-authored-by: Julien Gacon <gaconju@gmail.com>
@ShellyGarion Shelly Garion (ShellyGarion) removed the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Aug 11, 2026
@Cryoris

Copy link
Copy Markdown
Contributor

We discussed offline to merge this w/o a dedicated test, which proved to be quite tricky. The fix is verified locally and we're tracking adding a proper test in #16760.

@Cryoris
Julien Gacon (Cryoris) added this pull request to the merge queue Aug 13, 2026
Merged via the queue into Qiskit:main with commit b3716b5 Aug 13, 2026
31 checks passed
Ian Hincks (ihincks) added a commit to ihincks/qiskit-terra that referenced this pull request Aug 13, 2026
Ian Hincks (ihincks) added a commit to ihincks/qiskit-terra that referenced this pull request Aug 13, 2026
xmirya pushed a commit to qollabxyz/qiskit that referenced this pull request Aug 13, 2026
…iskit#16761)

* Sum VF2Layout average errors in a deterministic order

`build_average_error_map` summed per-qubit gate errors while iterating the
`HashSet` returned by `Target::operation_names_for_qargs`, whose order is
randomized per call.  Floating-point addition is non-associative, so the averaged
error -- and therefore the `VF2Layout` score of two symmetric layouts -- varied
between runs.  On a target with control-flow support and a symmetric coupling map
this made the selected layout, and hence the 1q-gate count and depth of the output
circuit, non-deterministic.

Sort the operation names before summing so the result is reproducible.

Fixes Qiskit#16490.


(cherry picked from commit 8ea89ab)

* Update releasenotes/notes/fix-vf2layout-nondeterminism-error-sum-2b8d4e6f0a1c3d57.yaml



* Update releasenotes/notes/fix-vf2layout-nondeterminism-error-sum-2b8d4e6f0a1c3d57.yaml

---------



(cherry picked from commit b3716b5)

Signed-off-by: Sophia Wen <hfwen@us.ibm.com>
Co-authored-by: Sophia Wen <h_f_wen@yahoo.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
pull Bot pushed a commit to AmirulAndalib/qiskit that referenced this pull request Aug 14, 2026
…6762)

* Add a unit test for 16732

* Revert "Sum VF2Layout average errors in a deterministic order (Qiskit#16732)"

This reverts commit b3716b5.

* Revert "Revert "Sum VF2Layout average errors in a deterministic order (Qiskit#16732)""

This reverts commit e71c5a5.
VedantMahadik-qc pushed a commit to VedantMahadik-qc/qiskit that referenced this pull request Oct 21, 2026
* Sum VF2Layout average errors in a deterministic order

`build_average_error_map` summed per-qubit gate errors while iterating the
`HashSet` returned by `Target::operation_names_for_qargs`, whose order is
randomized per call.  Floating-point addition is non-associative, so the averaged
error -- and therefore the `VF2Layout` score of two symmetric layouts -- varied
between runs.  On a target with control-flow support and a symmetric coupling map
this made the selected layout, and hence the 1q-gate count and depth of the output
circuit, non-deterministic.

Sort the operation names before summing so the result is reproducible.

Fixes Qiskit#16490.

Signed-off-by: Sophia Wen <hfwen@us.ibm.com>
(cherry picked from commit 8ea89ab)

* Update releasenotes/notes/fix-vf2layout-nondeterminism-error-sum-2b8d4e6f0a1c3d57.yaml

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Update releasenotes/notes/fix-vf2layout-nondeterminism-error-sum-2b8d4e6f0a1c3d57.yaml

---------

Signed-off-by: Sophia Wen <hfwen@us.ibm.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
VedantMahadik-qc pushed a commit to VedantMahadik-qc/qiskit that referenced this pull request Oct 21, 2026
…6762)

* Add a unit test for 16732

* Revert "Sum VF2Layout average errors in a deterministic order (Qiskit#16732)"

This reverts commit b3716b5.

* Revert "Revert "Sum VF2Layout average errors in a deterministic order (Qiskit#16732)""

This reverts commit e71c5a5.
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. stable backport potential Make Mergify open a backport PR to the most recent stable branch on merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-deterministic L2 (optimization_level=2) transpilation (1q-gate count/depth) with control-flow target + symmetric coupling map

4 participants