Commit f047aaf
Fix QPY loading delay integers durations incorrectly (#16076)
* Fix QPY loading delay integers durations incorrectly
This commit fixes an issue with parsing QPY payloads which have circuits
that contain delays with duration of units dt. Durations of dt are
integers and that is preserved in the QPY data. However our rust data
model doesn't have support for an integer in a Rust PackedInstruction's
parameters (this is an inconsistency which we should arguably fix but
that is separate from this bugfix). To workaround this the QPY reader in
rust was sticking the integer into a ParameterExpression as a constant
without any symbols. This resulted in storing the integer in Rust but
treating the object as a ParameterExpression and not an int, which in
Qiskit's rust data model is mapped to a Param::Obj (indicating a Python
object parameter). This mismatch in types was not really noticeable to
Python because the ParameterExpression with the constant integer was
coerced to an integer when it's passed to Python. However, this would
break underlying assumptions for Rust code that is interacting with the
delay. For example, the experimental rust qasm3 exporter would encounter
the ParameterExpression on the delay and error because it can't handle
parameter expressions yet. However fundamentally it could because this
is just an integer. The reproducer for this failure is:
```python
import io
from qiskit import qpy, qasm3, QuantumCircuit
qc = QuantumCircuit(1)
qc.delay(1, 0)
qasm3.dumps_experimental(qc)
with io.BytesIO() as fptr:
qpy.dump(qc, fptr)
fptr.seek(0)
qc2 = qpy.load(fptr)[0]
qasm3.dumps_experimental(qc2)
```
The OQ3 experimental exporter is wrong not to handle
`ParameterExpression::try_as_value` returning an `int`, but it's _more_
wrong that QPY is producing a `ParameterExpression` on deserialisation
in the first place.
To fix this issue this commit removes the conversion of the `int` in the
qpy payload into a ParameterExpression and just retains an integer until
we write out the Delay's PackedInstruction where we convert that rust
int into a python int for the parameter. Doing this unraveled a deeper
issue in how endianess is handled in QPY. In general everything in QPY
is supposed to be encoded using network byte order (i.e. big endian).
However, in the case of instructions' parameters there was a mistake
made in QPY where the integer and float values for an instruction's
parameters were encoded in little endian. All other uses of floats or
ints are correctly big endian. When the raw int was returned to Python
it was incorrectly assuming all integers were a big endian bytes value.
To fix this an endian arg is added to the function which is converting
the bytes arrays into a `GenericValue` enum for floats and ints. Then
the callers of this function in circuit_reader are updated to explicitly
assert what endianess the data in the circuit payload is if there are
any floats or ints. This is `Endian::Little` for any instruction params
that are in the parameters list explicitly and `Endian::Big` everywhere
else. At the same time the handling of flipping the endianess of value
in several places was fixed because this was no longer necessary as the
data was read now using the correct byte order.
This fundamentally stems from all the base values being stored in a
single binrw generic value pack which is trying to encode all the primitive
types in a single place. Ideally we should be handling the primitive types
explicitly for each data pack field. But this was not
changed to keep the diff minimal for backport
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
* Fix typo in qpy compat test
* Fix tests again
* Fix delay stretch expr test typo
* Add more round-trip tests
* Remove dead control-flow switching logic
All the entries in this list either take zero parameters, or would have
caused program control flow to enter `unpack_control_flow` and _not_
`unpack_py_instruction`, so this switching logic is dead.
* Remove endianness switching from `BigUint` parsing
The `BigUint` keys are only valid in contexts where they are guaranteed
to be in network order. The `pack_biguint` function knew this, but
`unpack_biguint` mistakenly gained an `endian` argument, which was
required to always be `Big`.
* Reword release note
---------
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>1 parent 56beeb8 commit f047aaf
8 files changed
Lines changed: 175 additions & 64 deletions
File tree
- crates/qpy/src
- qiskit/qpy
- releasenotes/notes
- test
- python
- qasm3
- qpy
- qpy_compat
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | 51 | | |
53 | 52 | | |
54 | 53 | | |
| |||
153 | 152 | | |
154 | 153 | | |
155 | 154 | | |
156 | | - | |
| 155 | + | |
157 | 156 | | |
158 | 157 | | |
159 | 158 | | |
| |||
243 | 242 | | |
244 | 243 | | |
245 | 244 | | |
| 245 | + | |
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
252 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
253 | 255 | | |
254 | 256 | | |
255 | 257 | | |
| |||
287 | 289 | | |
288 | 290 | | |
289 | 291 | | |
290 | | - | |
| 292 | + | |
291 | 293 | | |
292 | | - | |
293 | | - | |
| 294 | + | |
294 | 295 | | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | 296 | | |
303 | 297 | | |
304 | 298 | | |
| |||
310 | 304 | | |
311 | 305 | | |
312 | 306 | | |
313 | | - | |
| 307 | + | |
314 | 308 | | |
315 | 309 | | |
316 | 310 | | |
| |||
403 | 397 | | |
404 | 398 | | |
405 | 399 | | |
406 | | - | |
| 400 | + | |
407 | 401 | | |
408 | 402 | | |
409 | 403 | | |
| |||
420 | 414 | | |
421 | 415 | | |
422 | 416 | | |
423 | | - | |
| 417 | + | |
424 | 418 | | |
425 | 419 | | |
426 | 420 | | |
| |||
433 | 427 | | |
434 | 428 | | |
435 | 429 | | |
436 | | - | |
| 430 | + | |
437 | 431 | | |
438 | 432 | | |
439 | 433 | | |
440 | 434 | | |
441 | 435 | | |
442 | 436 | | |
443 | | - | |
| 437 | + | |
444 | 438 | | |
445 | 439 | | |
446 | 440 | | |
447 | 441 | | |
448 | 442 | | |
449 | 443 | | |
450 | | - | |
| 444 | + | |
451 | 445 | | |
452 | 446 | | |
453 | 447 | | |
| |||
470 | 464 | | |
471 | 465 | | |
472 | 466 | | |
473 | | - | |
| 467 | + | |
474 | 468 | | |
475 | 469 | | |
476 | 470 | | |
477 | 471 | | |
478 | 472 | | |
479 | 473 | | |
480 | | - | |
| 474 | + | |
481 | 475 | | |
482 | 476 | | |
483 | 477 | | |
484 | 478 | | |
485 | 479 | | |
486 | 480 | | |
487 | | - | |
488 | | - | |
| 481 | + | |
| 482 | + | |
489 | 483 | | |
490 | 484 | | |
491 | 485 | | |
| |||
498 | 492 | | |
499 | 493 | | |
500 | 494 | | |
501 | | - | |
| 495 | + | |
502 | 496 | | |
503 | 497 | | |
504 | 498 | | |
| |||
543 | 537 | | |
544 | 538 | | |
545 | 539 | | |
546 | | - | |
| 540 | + | |
547 | 541 | | |
548 | 542 | | |
549 | 543 | | |
| |||
566 | 560 | | |
567 | 561 | | |
568 | 562 | | |
569 | | - | |
| 563 | + | |
| 564 | + | |
570 | 565 | | |
571 | 566 | | |
572 | 567 | | |
| |||
590 | 585 | | |
591 | 586 | | |
592 | 587 | | |
593 | | - | |
| 588 | + | |
594 | 589 | | |
595 | 590 | | |
596 | 591 | | |
597 | 592 | | |
598 | 593 | | |
599 | | - | |
| 594 | + | |
600 | 595 | | |
601 | 596 | | |
602 | 597 | | |
603 | | - | |
| 598 | + | |
| 599 | + | |
604 | 600 | | |
605 | 601 | | |
606 | 602 | | |
| |||
708 | 704 | | |
709 | 705 | | |
710 | 706 | | |
711 | | - | |
| 707 | + | |
712 | 708 | | |
713 | 709 | | |
714 | 710 | | |
715 | 711 | | |
716 | | - | |
| 712 | + | |
717 | 713 | | |
718 | 714 | | |
719 | 715 | | |
| |||
872 | 868 | | |
873 | 869 | | |
874 | 870 | | |
875 | | - | |
| 871 | + | |
876 | 872 | | |
877 | 873 | | |
878 | 874 | | |
879 | 875 | | |
880 | | - | |
| 876 | + | |
881 | 877 | | |
882 | 878 | | |
883 | 879 | | |
| |||
1171 | 1167 | | |
1172 | 1168 | | |
1173 | 1169 | | |
1174 | | - | |
1175 | | - | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
1176 | 1176 | | |
1177 | 1177 | | |
1178 | 1178 | | |
| |||
1193 | 1193 | | |
1194 | 1194 | | |
1195 | 1195 | | |
1196 | | - | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
1197 | 1202 | | |
1198 | 1203 | | |
1199 | 1204 | | |
| |||
1499 | 1504 | | |
1500 | 1505 | | |
1501 | 1506 | | |
1502 | | - | |
1503 | | - | |
1504 | | - | |
1505 | | - | |
1506 | | - | |
1507 | | - | |
1508 | | - | |
1509 | | - | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
1510 | 1513 | | |
1511 | 1514 | | |
1512 | 1515 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
451 | 451 | | |
452 | 452 | | |
453 | 453 | | |
| 454 | + | |
454 | 455 | | |
455 | 456 | | |
456 | 457 | | |
| |||
628 | 629 | | |
629 | 630 | | |
630 | 631 | | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
637 | | - | |
638 | | - | |
| 632 | + | |
639 | 633 | | |
640 | 634 | | |
641 | 635 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
405 | 405 | | |
406 | 406 | | |
407 | 407 | | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
408 | 422 | | |
409 | 423 | | |
410 | 424 | | |
411 | 425 | | |
| 426 | + | |
412 | 427 | | |
413 | 428 | | |
414 | 429 | | |
| |||
417 | 432 | | |
418 | 433 | | |
419 | 434 | | |
420 | | - | |
421 | | - | |
422 | | - | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
423 | 444 | | |
424 | 445 | | |
425 | 446 | | |
426 | 447 | | |
427 | 448 | | |
428 | | - | |
| 449 | + | |
429 | 450 | | |
430 | 451 | | |
431 | 452 | | |
| |||
465 | 486 | | |
466 | 487 | | |
467 | 488 | | |
468 | | - | |
| 489 | + | |
469 | 490 | | |
470 | 491 | | |
471 | 492 | | |
| |||
619 | 640 | | |
620 | 641 | | |
621 | 642 | | |
| 643 | + | |
622 | 644 | | |
623 | | - | |
| 645 | + | |
624 | 646 | | |
625 | 647 | | |
626 | 648 | | |
| |||
636 | 658 | | |
637 | 659 | | |
638 | 660 | | |
639 | | - | |
| 661 | + | |
640 | 662 | | |
641 | 663 | | |
642 | 664 | | |
| |||
690 | 712 | | |
691 | 713 | | |
692 | 714 | | |
| 715 | + | |
693 | 716 | | |
694 | 717 | | |
695 | 718 | | |
696 | 719 | | |
697 | | - | |
| 720 | + | |
698 | 721 | | |
699 | 722 | | |
700 | 723 | | |
| |||
0 commit comments