You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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
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.
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.
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.
RustThis PR or issue is related to Rust code in the repositorytype: qaIssues and PRs that relate to testing and code quality
5 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a Rust-space method
sim_unitary_circuit
that constructs a unitary matrix (represented asArray2<Complex64>
) from aCircuitData
with only unitary operations (or barriers). Functionality-wise, this is equivalent toOperator(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 ofunitary_compose
useeinsum
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 ofunitary_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.