Skip to content

Redefine CustomOperation::to_matrix - #16865

Open
davidfcohen wants to merge 3 commits into
Qiskit:mainfrom
davidfcohen:custom-operation-matrix-err
Open

Redefine CustomOperation::to_matrix#16865
davidfcohen wants to merge 3 commits into
Qiskit:mainfrom
davidfcohen:custom-operation-matrix-err

Conversation

@davidfcohen

@davidfcohen davidfcohen commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

This PR redefines CustomOperation::matrix as fallible CustomOperation::to_matrix. Different custom operations can fail for a different set of reasons. So, I used type-erased Box<dyn std::error::Error>. I would have liked to return Self::Err, however, because of the way CustomOperation is used dynamically, I boxed the error instead. We use the form Result<Option<T>, E> to cover the common "unimplemented" case.

closes #16862

AI/LLM disclosure

  • No part of this submission is LLM generated.
  • Some written text was generated by:
  • Some submitted code was generated by:

@davidfcohen davidfcohen added the Changelog: None Do not include in the GitHub Release changelog. label Aug 28, 2026
@davidfcohen davidfcohen changed the title [WIP] Redefine CustomOperation::to_matrix Redefine CustomOperation::to_matrix Aug 28, 2026
@davidfcohen
davidfcohen marked this pull request as ready for review August 28, 2026 18:32
@davidfcohen
davidfcohen requested a review from a team as a code owner August 28, 2026 18:32
@davidfcohen
davidfcohen requested a review from mtreinish August 28, 2026 18:32
@qiskit-bot

Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@davidfcohen
davidfcohen requested review from raynelfss and removed request for mtreinish August 28, 2026 18:32
@@ -998,7 +998,7 @@ impl PackedInstruction {
pub fn try_matrix(&self) -> Option<Array2<Complex64>> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm unsure whether this function should Result as well. Let me know!

@davidfcohen davidfcohen added Rust This PR or issue is related to Rust code in the repository type: enhancement It's working, but needs polishing mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Aug 28, 2026
@coveralls

coveralls commented Aug 28, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 33206100477

Coverage decreased (-0.008%) to 87.736%

Details

  • Coverage decreased (-0.008%) from the base build.
  • Patch coverage: 14 uncovered changes across 2 files (21 of 35 lines covered, 60.0%).
  • 21 coverage regressions across 4 files.

Uncovered Changes

File Changed Covered %
crates/circuit/src/packed_instruction.rs 9 0 0.0%
crates/circuit/src/operations.rs 26 21 80.77%

Coverage Regressions

21 previously-covered lines in 4 files lost coverage.

File Lines Losing Coverage Coverage
crates/qasm2/src/parse.rs 12 97.16%
crates/qasm2/src/lex.rs 6 91.77%
crates/circuit/src/packed_instruction.rs 2 86.02%
crates/circuit/src/operations.rs 1 83.68%

Coverage Stats

Coverage Status
Relevant Lines: 131095
Covered Lines: 115018
Line Coverage: 87.74%
Coverage Strength: 981056.38 hits per line

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in the GitHub Release changelog. mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library Rust This PR or issue is related to Rust code in the repository type: enhancement It's working, but needs polishing

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

Have CustomOperation::to_matrix return Result<T, E> instead of Option<T>.

4 participants