Skip to content

Fix inverse() on circuits containing initialize() to raise CircuitError early#15607

Open
macatizm wants to merge 4 commits intoQiskit:mainfrom
macatizm:fix-initialize-inverse-error-15595
Open

Fix inverse() on circuits containing initialize() to raise CircuitError early#15607
macatizm wants to merge 4 commits intoQiskit:mainfrom
macatizm:fix-initialize-inverse-error-15595

Conversation

@macatizm
Copy link

Fixes #15595

Previously, calling inverse() on a circuit containing initialize() would crash with a misleading internal error about invalid param types when trying to construct the inverse gate. This happened because Initialize has a definition (via _define()), so it passed the base Instruction.inverse() check, but then failed later during gate construction.

Now, Initialize.inverse() explicitly raises CircuitError early with a clear message, similar to how reset() behaves. This provides a better user experience with an informative error message instead of an internal error.

The fix adds an inverse() method to the Initialize class that immediately raises CircuitError, explaining that initialize is a non-unitary instruction and cannot be inverted.

Changes

  • Added inverse() method to Initialize class that raises CircuitError early
  • Added import for CircuitError exception
  • Error message follows the same pattern as other non-invertible instructions (e.g., PauliProductMeasurement)

Testing

The fix prevents the following error:

CircuitError: "Invalid param type <class 'complex'> for gate initialize_dg."

And instead raises:

CircuitError: "inverse() not implemented for initialize. initialize is a non-unitary instruction and cannot be inverted."

…or early

Fixes Qiskit#15595

Previously, calling inverse() on a circuit containing initialize() would
crash with a misleading internal error about invalid param types when
trying to construct the inverse gate. This happened because Initialize
has a definition (via _define()), so it passed the base Instruction
inverse() check, but then failed later during gate construction.

Now, Initialize.inverse() explicitly raises CircuitError early with a
clear message, similar to how reset() behaves. This provides a better
user experience with an informative error message instead of an internal
error.

The fix adds an inverse() method to the Initialize class that immediately
raises CircuitError, explaining that initialize is a non-unitary
instruction and cannot be inverted.
@macatizm macatizm requested a review from a team as a code owner January 27, 2026 05:55
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jan 27, 2026
@qiskit-bot
Copy link
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:

  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia

@CLAassistant
Copy link

CLAassistant commented Jan 27, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ macatizm
❌ Gittensor Miner


Gittensor Miner seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Collaborator

@Cryoris Cryoris 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 the contribution! Could you add a test and a release note (under fixes) for this?

macatizm and others added 3 commits January 26, 2026 22:14
Co-authored-by: Julien Gacon <gaconju@gmail.com>
- Add test_inverse_raises_circuit_error() to verify CircuitError is raised
- Add release note documenting the fix for issue Qiskit#15595
@macatizm
Copy link
Author

Hi, @Cryoris
I updated initializer.py file and added test and release note.

@ShellyGarion ShellyGarion added Changelog: Fixed Add a "Fixed" entry in the GitHub Release changelog. mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Jan 27, 2026
@coveralls
Copy link

Pull Request Test Coverage Report for Build 21386925525

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • 15 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.008%) to 87.982%

Files with Coverage Reduction New Missed Lines %
crates/circuit/src/parameter/parameter_expression.rs 1 87.17%
crates/transpiler/src/passes/unitary_synthesis/decomposers.rs 1 96.02%
crates/circuit/src/parameter/symbol_expr.rs 2 73.24%
crates/qasm2/src/lex.rs 5 91.77%
crates/qasm2/src/parse.rs 6 97.63%
Totals Coverage Status
Change from base Build 21365885240: -0.008%
Covered Lines: 100086
Relevant Lines: 113757

💛 - Coveralls

@ShellyGarion
Copy link
Member

thanks for your contribution to qiskit @macatizm
any idea why the CLA has not been properly signed? #15607 (comment)

@ShellyGarion
Copy link
Member

The CLA was not signed properly since the commits were made by Gittensor Miner.

@jakelishman
Copy link
Member

I believe this may have been LLM generated without attribution. Please confirm the model used, and that the PR was tested and checked by a human before submission.

I also see that this is being done for some sort of bug bounty. Qiskit did not sign up for this bug bounty and does not endorse it. We welcome quality contributions from humans, but not externally incentivised AI spam.

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. Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

inverse() on circuits containing initialize() crashes with a misleading internal error instead of raising CircuitError, like in the case of reset()

7 participants