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

Fully port Depth and Size passes to Rust #13040

Merged
merged 4 commits into from
Aug 28, 2024

Conversation

kevinhartman
Copy link
Contributor

@kevinhartman kevinhartman commented Aug 26, 2024

Summary

This has a bit of a click-bait title since they're technically already ported (the calls to DAGCircuit::depth and DAGCircuit::size made by the Python passes are already in Rust). But, this PR avoids an isinstance call that was being done for every node in the DAG on calls to these methods when the recurse parameter was specified as true, which should resolve the performance issues we were seeing with these passes.

I've also done this for DAGCircuit::count_ops.

Details

Resolves #12272, #12273

@kevinhartman kevinhartman linked an issue Aug 26, 2024 that may be closed by this pull request
@kevinhartman kevinhartman marked this pull request as ready for review August 27, 2024 03:35
@kevinhartman kevinhartman requested a review from a team as a code owner August 27, 2024 03:35
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @kevinhartman
  • @mtreinish

@coveralls
Copy link

coveralls commented Aug 27, 2024

Pull Request Test Coverage Report for Build 10586731786

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 67 of 72 (93.06%) changed or added relevant lines in 1 file are covered.
  • 353 unchanged lines in 6 files lost coverage.
  • Overall coverage decreased (-0.01%) to 89.201%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/dag_circuit.rs 67 72 93.06%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 92.73%
crates/circuit/src/interner.rs 4 92.0%
crates/circuit/src/packed_instruction.rs 8 96.56%
crates/qasm2/src/parse.rs 12 97.15%
crates/circuit/src/circuit_data.rs 26 92.18%
crates/circuit/src/dag_circuit.rs 300 89.02%
Totals Coverage Status
Change from base Build 10563485346: -0.01%
Covered Lines: 71646
Relevant Lines: 80320

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

This LGTM, thanks for fixing this. Just a couple small inline comments.

Comment on lines 2263 to 2265
let blocks = raw_blocks.downcast::<PyTuple>()?;
let mut block_weights: Vec<usize> = Vec::with_capacity(blocks.len());
for block in blocks.iter() {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need the PyTuple downcast here? Should we be able to just call len() and iter() on a Bound<PyAny>. I think the only difference is their fallible in PyAnyMethods but not in PyTupleMethods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Done in 80ff154.

@@ -2239,34 +2240,33 @@ def _format(operand):
Ok(if recurse {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we want an && CONTROL_FLOW_OP_NAMES.iter().any(|x| self.op_names.contains_key(&x.to_string())) on this condition so we don't bother with the for loop if we know there are no control flow instructions. The same question applies to size() as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea, and allowed me to make the code a bit cleaner across both methods. I added a helper as well, which is private to DAGCircuit for now.

Done in af244c2.

Copy link
Member

@mtreinish mtreinish left a 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.

return Ok(length);
}
if !recurse {
return Err(DAGCircuitError::new_err(concat!(
Copy link
Member

Choose a reason for hiding this comment

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

Oh, I forgot about concat! there are a couple of long error messages in this module we should probably break up using it.

@mtreinish mtreinish added this pull request to the merge queue Aug 28, 2024
Merged via the queue into Qiskit:main with commit 37784c7 Aug 28, 2024
15 checks passed
@raynelfss raynelfss added the Changelog: None Do not include in changelog label Nov 7, 2024
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port Size to Rust Port Depth to Rust
5 participants