Skip to content

fix(quilatom): format complex params with nonzero real and unit imaginary part#1850

Open
devteamaegis wants to merge 1 commit into
rigetti:masterfrom
devteamaegis:fix/format-parameter-complex
Open

fix(quilatom): format complex params with nonzero real and unit imaginary part#1850
devteamaegis wants to merge 1 commit into
rigetti:masterfrom
devteamaegis:fix/format-parameter-complex

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

format_parameter raises ValueError on a complex number whose imaginary part is exactly +1/-1 while the real part is nonzero (e.g. 2+1j). It's reachable through the non-deprecated noise path _create_kraus_pragmas, which serializes Kraus-operator entries into a PRAGMA ADD-KRAUS string — and Kraus operators commonly have entries like 2+1j:

import numpy as np
from pyquil.noise import _create_kraus_pragmas
_create_kraus_pragmas("MYGATE", [0], [np.array([[2+1j, 0],[0, 1]])])
# ValueError: Invalid complex number: (2+1j)

Why it happens

The i == 1 / i == -1 branches assumed the real part must be ~0 and raised otherwise. The np.isclose(r, 0) check is only meant to drop tiny floating residuals; when the real part is genuinely nonzero it should append +i/-i to the real part, like every other complex value.

Fix

When the real part is nonzero, append +i/-i instead of raising. Output round-trips through pyQuil's own parser (2.0+i).

Test

Extended test_format_parameter with 2+1j, 2-1j, -3+1j; the existing 1e-15±1j → ±"i" cases still pass, and test_noise.py (the Kraus path) stays green.

…real and unit imaginary part

format_parameter raised ValueError on complex values like 2+1j (real part
nonzero, imaginary part exactly +/-1), e.g. when serializing Kraus operator
entries into PRAGMA ADD-KRAUS via _create_kraus_pragmas. Emit <real>+i /
<real>-i instead of raising, matching the existing format for other complex
values.
@devteamaegis devteamaegis requested a review from a team as a code owner June 11, 2026 06:41

@windsurf-bot windsurf-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good to me 🤙

💡 To request another review, post a new comment with "/windsurf-review".

@devteamaegis devteamaegis force-pushed the fix/format-parameter-complex branch from 5f24a34 to fc86cbf Compare June 11, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant