-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Port the count_ops method in QuantumCircuit to Rust #13050
Port the count_ops method in QuantumCircuit to Rust #13050
Conversation
Pull Request Test Coverage Report for Build 10636776886Details
💛 - Coveralls |
For a simple 2 qubit system in superposition, benchmarking shows ~3x improvement in performance on average. For a much larger, more complex circuit the improvement was ~9x.
|
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:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, thanks for doing this. I just have a few inline suggestions. After you apply them you might want to rerun the benchmarking too to see how it impacts the runtime performance of the method.
Co-authored-by: Matthew Treinish <[email protected]>
crates/circuit/src/circuit_data.rs
Outdated
/// | ||
/// # Returns | ||
/// An IndexMap containing the operation names as keys and their respective counts as values. | ||
pub fn count_ops(&mut self) -> IndexMap<String, usize, ::ahash::RandomState> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't need to be &mut
because we're not mutating self
here. So you can just use &self
crates/circuit/src/circuit_data.rs
Outdated
let mut ops_count: IndexMap<String, usize, ::ahash::RandomState> = IndexMap::default(); | ||
for instruction in &self.data { | ||
*ops_count | ||
.entry(instruction.op.view().name().to_string()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't actually need to build the OperationRef
view here, PackedOperation
has a name method you can just call. This is actually what I think blocked you from returning a &str
as the key before.
.entry(instruction.op.view().name().to_string()) | |
.entry(instruction.op.name().to_string()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah for some reason I didn't see that and that made things more complicated for me
crates/circuit/src/circuit_data.rs
Outdated
let mut ops_count: IndexMap<String, usize, ::ahash::RandomState> = IndexMap::default(); | ||
for instruction in &self.data { | ||
*ops_count | ||
.entry(instruction.op.view().name().to_string()) | ||
.or_insert(0) += 1; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not actually suggesting you make this change, as I think a for loop is probably more legible. But I was curious if you could do this with just an iterator which you can do like this:
let mut ops_count: IndexMap<String, usize, ::ahash::RandomState> = IndexMap::default(); | |
for instruction in &self.data { | |
*ops_count | |
.entry(instruction.op.view().name().to_string()) | |
.or_insert(0) += 1; | |
} | |
let mut ops_count = | |
self.data | |
.iter() | |
.fold(IndexMap::default(), |mut ops_count, instruction| { | |
*ops_count.entry(instruction.op.name()).or_insert(0) += 1; | |
ops_count | |
}); |
I don't think there is any advantage to writing it like this. So it's more just an interesting musing on how to use functional programming style iterators for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll test it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seemed to work
Co-authored-by: Matthew Treinish <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for doing this.
Summary
This PR is to address issue 12971 to port the count_ops method into Rust to improve performance.
Details and comments
I added a
count_ops
method to theCircuitData
class in qiskit/crates/circuit/src/circuit_data.rs and updated thecount_ops
method in qiskit/circuit/quantumcircuit.py to call CircuitData'scount_ops
method.