Skip to content

Conversation

alexanderivrii
Copy link
Member

Summary

This PR adds a Rust-space method sim_unitary_circuit that constructs a unitary matrix (represented as Array2<Complex64>) from a CircuitData with only unitary operations (or barriers). Functionality-wise, this is equivalent to Operator(circuit) in Python-space.

This is needed to enable better in-Rust testing of Rust-space synthesis methods, for example as requested in #14666.

Details and comments

This is a simple wrapper on top of the existing unitary_compose. One caveat, that while both Python and Rust implementations of unitary_compose use einsum tricks for composing smaller-sized matrices onto larger-sized matrices as subsystems, the Python's implementation works for more qubits and is quite a bit faster. On circuits with 13 or more qubits, the Rust implementation throws the "index out of bounds" error, while the Python implementation works up to at least 16 qubits. (Personally, I don't think this is such a huge restriction, since at this point the Python code is quite slow and we will not likely use it in practice.) In the future, we should possibly revisit our implementation of unitary_compose to see if we can speed it up a bit.

I have not added release notes since this is not planned to be user-facing.

Consructs a unitary matrix from a CircuitData with only unitary operations, exactly as
what Operator(quantum_circuit).data does.

The code is a simple wrapper on top of unitary_compose.
@alexanderivrii alexanderivrii added this to the 2.2.0 milestone Jul 16, 2025
@alexanderivrii alexanderivrii requested a review from a team as a code owner July 16, 2025 08:02
@alexanderivrii alexanderivrii added type: qa Issues and PRs that relate to testing and code quality Rust This PR or issue is related to Rust code in the repository labels Jul 16, 2025
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@ShellyGarion
Copy link
Member

ShellyGarion commented Jul 16, 2025

That's a great contribution to qiskit! How much is it faster and more scalable compared to the current Operator class?
Perhaps we should replace the internal implementation of the Operator class by a rust function (in a separate PR)?
This can also be helpful in some issues such as #11989.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 16313893357

Details

  • 31 of 37 (83.78%) changed or added relevant lines in 2 files are covered.
  • 10 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.006%) to 87.755%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/quantum_info/src/unitary_sim.rs 30 36 83.33%
Files with Coverage Reduction New Missed Lines %
crates/circuit/src/symbol_expr.rs 1 73.77%
crates/qasm2/src/lex.rs 3 91.75%
crates/qasm2/src/parse.rs 6 97.09%
Totals Coverage Status
Change from base Build 16297423930: -0.006%
Covered Lines: 81575
Relevant Lines: 92958

💛 - Coveralls

@alexanderivrii
Copy link
Member Author

Unfortunately, as I have written in the description, this is slower, not faster than the Operator code in Python, and suffers from the same einsum "index out of bounds"/"too many subcripts" problems. Maybe @sbrandhsn or @mtreinish can suggest how to speed up the underlying unitary_compose code.

@mtreinish
Copy link
Member

Right now the einsum is all based around https://github.com/mtreinish/ndarray-einsum (which is just a fork I made of the original library to update dependencies). The code there hasn't really been updated in many years so we would have to either find a different library, spend the time to look into improving ndarray-einsum, or implement an alternative that doesn't use einsum.

@ShellyGarion ShellyGarion added the Changelog: None Do not include in changelog label Jul 28, 2025
Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

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

This function is mainly used for rust synthesis tests, although it's not optimal, let's merge it and rebase #14666 after it's merged.

@ShellyGarion ShellyGarion added this pull request to the merge queue Aug 7, 2025
Merged via the queue into Qiskit:main with commit 3f04536 Aug 7, 2025
29 checks passed
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 changelog Rust This PR or issue is related to Rust code in the repository type: qa Issues and PRs that relate to testing and code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants