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

Fix scheduling units #11782

Merged
merged 4 commits into from
Feb 15, 2024
Merged

Fix scheduling units #11782

merged 4 commits into from
Feb 15, 2024

Conversation

ElePT
Copy link
Contributor

@ElePT ElePT commented Feb 13, 2024

Summary

While trying to fix #11780, @Cryoris and I noticed that the rounding errors after scheduling were reporting durations of the order of e14 dt, instead of e4 dt. This was due to a conversion to dt of circuit units that were already in dt. This PR adds a fix that checks the existing units before converting.

Details and comments

To reproduce the issue, run the following code without the fix in #11780

from qiskit.circuit import QuantumCircuit
from qiskit.providers.fake_provider import GenericBackendV2
from qiskit.compiler import transpile
from qiskit.compiler.scheduler import schedule

simple = QuantumCircuit(3)
simple.sx([0, 1, 2])
simple.cx(0, 1)
simple.cx(1, 2)
simple.cx(0, 1)
simple.sx(0)

backend = GenericBackendV2(num_qubits=3, calibrate_instructions=True, seed=10)
tqc = transpile(simple, backend, scheduling_method="asap")
sched = schedule(tqc, backend)

Where the following line shows the output:

>>> sched = schedule(tqc, backend)
/Users/ept/qiskit_workspace/qiskit/qiskit/circuit/duration.py:37: UserWarning: Duration is rounded to 72234234234234 [dt] = 1.603600e+04 [s] from 1.603600e+04 [s]
  warnings.warn(

@ElePT ElePT added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Feb 13, 2024
@ElePT ElePT added this to the 1.0.0 milestone Feb 13, 2024
@ElePT ElePT requested a review from a team as a code owner February 13, 2024 10:43
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

@ElePT ElePT added the Changelog: None Do not include in changelog label Feb 13, 2024
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks for finding this. Looking at the lines above in the code, I think this might need a bit more of a tweak - would this handle the case circ.unit == "ms" correctly, for example?

Could we also add a couple of regression tests on it and a bugfix note? Do you think it's eligible for backport to 0.46 as well?

@coveralls
Copy link

coveralls commented Feb 13, 2024

Pull Request Test Coverage Report for Build 7889916990

Details

  • -1 of 7 (85.71%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.03%) to 89.205%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/duration.py 6 7 85.71%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 91.44%
Totals Coverage Status
Change from base Build 7886503866: 0.03%
Covered Lines: 58830
Relevant Lines: 65949

💛 - Coveralls

@ElePT ElePT removed the Changelog: None Do not include in changelog label Feb 13, 2024
@mtreinish mtreinish added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Feb 13, 2024
@ElePT
Copy link
Contributor Author

ElePT commented Feb 13, 2024

Could we also add a couple of regression tests on it and a bugfix note? Do you think it's eligible for backport to 0.46 as well?

Added. I think it's elegible for backport to both 0.45 and 0.46.

Thanks for finding this. Looking at the lines above in the code, I think this might need a bit more of a tweak - would this handle the case circ.unit == "ms" correctly, for example?

The bug was in convert_durations_to_dt(), which I understand only expects inputs in dt or s:
https://github.com/Qiskit/qiskit/blob/main/qiskit/circuit/duration.py#L46-L60. These are the only 2 cases contemplated for operations (where the units were checked), that's why I applied the same logic to the full circuit duration.

@jakelishman
Copy link
Member

The bug was in convert_durations_to_dt(), which I understand only expects inputs in dt or s

Well, convert_durations_to_dt expects inputs in any unit (the choices are prefixed versions of seconds or dt) - for the operations in the lines above the ones touched here, it does the apply_prefix stuff, but for QuantumCircuit.duration it doesn't - if it's not already dt it assumes it's s, but I don't know of any reason that couldn't be ms. I think we ought to be doing the apply_prefix stuff on QuantumCircuit.duration, like this function does for operation.duration.

Copy link
Member

@jakelishman jakelishman 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 fine to me, thanks. I think the bug's sufficiently non-critical (must have been around for ages, I think?) that it's best to wait to merge til we can target it at 1.0.1, rather than accepting the heightened risk of merging it between rc1 and 1.0.0 final.

I'll leave this approved but untagged until stable/1.0 is open for bugfixes for 1.0.1.

@jakelishman jakelishman added this pull request to the merge queue Feb 15, 2024
Merged via the queue into Qiskit:main with commit 944e20c Feb 15, 2024
13 of 14 checks passed
mergify bot pushed a commit that referenced this pull request Feb 15, 2024
* Fix units

* Add test and reno

* Add support for non-IS units, move test to test_scheduled_circuit.py

* Adapt reno wording

(cherry picked from commit 944e20c)
github-merge-queue bot pushed a commit that referenced this pull request Feb 16, 2024
* Fix units

* Add test and reno

* Add support for non-IS units, move test to test_scheduled_circuit.py

* Adapt reno wording

(cherry picked from commit 944e20c)

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
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.

None yet

5 participants