Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage native UnitaryGate from rust #13765

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented Jan 30, 2025

Summary

This commit builds off of the native rust representation of a
UnitaryGate added in #13759 and uses the native representation
everywhere we were using UnitaryGate in rust via python previously:
the quantum_volume() function and consolidate blocks.

One future item is consolidate blocks can be updated to use nalgebra
types internally instead of ndarray as for the 1 and 2q cases we know
the fixed size of the array ahead of time. However the block
consolidation code is built using ndarray currently and later synthesis
code also works in ndarray so there isn't any real benefit yet, and we'd
ust add unecessary conversions and allocations. However, once #13649
merges this will change and it would make more sense to add the unitary
gate with a Matrix4. But this can be handled separately after this
merges.

Details and comments

This is based on top of #13759 and will need to be rebased once that merges. Until then you can see the contents of this PR by looking at the HEAD commit on the PR branch: 8c36ffd

@mtreinish mtreinish added on hold Can not fix yet performance Changelog: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository labels Jan 30, 2025
@mtreinish mtreinish added this to the 2.0.0 milestone Jan 30, 2025
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @levbishop

@mtreinish
Copy link
Member Author

I ran a quick benchmark of quantum_volume() with this PR and compared it against 1.3.2 from PyPI.

qv_construct_time

The release from PyPI will have PGO too which likely speeds it up a little bit.

@mtreinish mtreinish force-pushed the use-unitary-gate-rs branch from 2a151ec to 8c36ffd Compare January 30, 2025 18:50
This commit builds off of the native rust representation of a
UnitaryGate added in Qiskit#13759 and uses the native representation
everywhere we were using UnitaryGate in rust via python previously:
the quantum_volume() function, consolidate blocks, split2qunitaries,
and unitary synthesis.

One future item is consolidate blocks can be updated to use nalgebra
types internally instead of ndarray as for the 1 and 2q cases we know
the fixed size of the array ahead of time. However the block
consolidation code is built using ndarray currently and later synthesis
code also works in ndarray so there isn't any real benefit yet, and we'd
just add unecessary conversions and allocations. However, once Qiskit#13649
merges this will change and it would make more sense to add the unitary
gate with a Matrix4. But this can be handled separately after this
merges.
@mtreinish mtreinish force-pushed the use-unitary-gate-rs branch from 8c36ffd to 8bdc469 Compare February 5, 2025 23:46
@mtreinish mtreinish removed the on hold Can not fix yet label Feb 5, 2025
@mtreinish
Copy link
Member Author

This should be ready for review now, I rebased this on top on main now that #13759 has merged.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 13168722272

Details

  • 159 of 178 (89.33%) changed or added relevant lines in 6 files are covered.
  • 20 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.008%) to 88.592%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/consolidate_blocks.rs 40 42 95.24%
crates/accelerate/src/two_qubit_decompose.rs 6 12 50.0%
crates/circuit/src/dag_circuit.rs 74 85 87.06%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/two_qubit_decompose.rs 1 91.85%
crates/accelerate/src/remove_identity_equiv.rs 1 94.32%
crates/qasm2/src/lex.rs 3 91.98%
crates/accelerate/src/consolidate_blocks.rs 3 94.59%
crates/qasm2/src/parse.rs 12 96.68%
Totals Coverage Status
Change from base Build 13168373427: -0.008%
Covered Lines: 79381
Relevant Lines: 89603

💛 - Coveralls

.get_bound(py)
.call1((array, py.None(), false))?;
let array: Matrix2<Complex64> =
Matrix2::from_row_iterator(matrix.into_iter().flat_map(|x| x.into_iter()));
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems we'll end up needing a lot of these converters... it's a bit a bummer that nalgebra doesn't implement a From<Array2> or something

}

// Update self.op_names
self.decrement_op(op_name.as_str());
Copy link
Contributor

@Cryoris Cryoris Feb 6, 2025

Choose a reason for hiding this comment

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

These look like they might do a roundtrip &str -> String -> &str?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did just copy what was there before I split this out to a method for working with PackedInstruction directly. I'll play with it a little bit and see if I can remove the double conversion (I have a feeling it's acting as a copy to avoid a borrow checker failure).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I just looked at it the to_string() is to copy the string. There is a shared reference issue if a copy isn't done here because everything is based on a references to dag and we're mutating it. So we need to copy the name string before we mutate. But the decrement_op() and increment_op() methods require &str as the input type.

Copy link
Contributor

@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.

Overall LGTM, just a teeny tiny comment below 🙂

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 performance Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants