Fix controlled CU QPY serialization - #16665
Conversation
|
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:
|
jakelishman
left a comment
There was a problem hiding this comment.
This can't be the cleanest way to go about the stated fix: it's neither a change to the CUGate class, nor to QPY. The CUGate/ControlledGate subclass relationship is somehow different to other gates: either that should be fixed directly (which probably has a myriad backwards compatibility concerns), or QPY needs a direct fix.
Certainly, modifying the generic backing of Gate.control (very indirectly) and not the ostensibly problematic CUGate.control is not the answer here.
|
@jakelishman , Thanks for the feedback! I've updated CUGate.control to handle the CU-specific base gate and reverted _add_control.py back to its original generic implementation. Both the controlled-CU base-gate and QPY round-trip regression tests are now passing. |
Summary
Fixes #11686. A circuit containing a controlled
CUGate(for exampleCUGate(...).control()) crashed onqpy.loadwithTypeError: label expects a string or None.Cause
CUGatecarries four parameters (θ, φ, λ, γ), but its base gate is athree-parameter
UGate. When another control was added, the generic controlhelper kept the three-parameter
UGateas the base while the resultingcontrolled gate exposed all four
CUGateparameters. On reload QPY rebuilt thebase as
UGate(θ, φ, λ, γ), passing the fourth value γ into thelabelargument.
Fix
For a controlled
CUGate, the base gate now explicitly representse^{iγ}·U(θ, φ, λ)(aUGatewith a global phase of γ), so it genuinely hasfour parameters. QPY round-trips it and the circuit operator is preserved.
Tests
test_controlled_cu_gateintest_circuit_load_from_qpy.py– QPY round-tripof a controlled
CUGate.test_controlled_cugate_base_gate_includes_global_phaseintest_controlled_gate.py– the base gate equalse^{iγ}·U(θ, φ, λ).AI/LLM disclosure