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

Move the circuit library's entanglement logic to Rust #12950

Merged
merged 13 commits into from
Aug 26, 2024

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Aug 13, 2024

Summary

Move the entanglement logic used in the circuit library's NLocal circuits & friends to Rust. This PR is also the basis to move frequently used circuits, such as EfficientSU2, TwoLocal or ZZFeatureMap to Rust.

This also fixes a bug in the circular entanglement which missed some entanglement blocks for entangling gates with 3+ qubits.

Details and comments

NLocal will directly use this function, but there's also a series of tests to check the different errors and possible input types.

The attentive reader might notice that handling elaborate constructs such as list[list[list[int]]] are not handled, which does not affect the current code, but will change what we accept once the circuit library is moved to Rust. That's an intentional change as these types were used to allow different entanglements for different circuit layers (controlled by offset), but this was redundantly covered by passing a callable. Moving forward, the suggestion is to only allow a callable in these cases to have only 1 method of doing 1 thing 🙂

@Cryoris Cryoris added Changelog: Bugfix Include in the "Fixed" section of the changelog Rust This PR or issue is related to Rust code in the repository labels Aug 13, 2024
@Cryoris Cryoris added this to the 1.3 beta milestone Aug 13, 2024
@Cryoris Cryoris requested a review from a team as a code owner August 13, 2024 12:09
@qiskit-bot
Copy link
Collaborator

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

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

@coveralls
Copy link

coveralls commented Aug 13, 2024

Pull Request Test Coverage Report for Build 10510704837

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

  • 137 of 142 (96.48%) changed or added relevant lines in 5 files are covered.
  • 83 unchanged lines in 7 files lost coverage.
  • Overall coverage increased (+0.01%) to 89.605%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/circuit_library/mod.rs 4 5 80.0%
crates/accelerate/src/circuit_library/entanglement.rs 126 130 96.92%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/library/data_preparation/initializer.py 1 97.06%
crates/qasm2/src/lex.rs 6 91.73%
crates/qasm2/src/parse.rs 6 97.61%
qiskit/circuit/library/standard_gates/x.py 6 98.1%
qiskit/transpiler/instruction_durations.py 6 91.18%
qiskit/transpiler/passes/synthesis/high_level_synthesis.py 7 96.62%
qiskit/circuit/quantumcircuit.py 51 93.54%
Totals Coverage Status
Change from base Build 10472589710: 0.01%
Covered Lines: 67899
Relevant Lines: 75776

💛 - Coveralls

@Cryoris Cryoris added stable backport potential The bug might be minimal and/or import enough to be port to stable and removed stable backport potential The bug might be minimal and/or import enough to be port to stable labels Aug 14, 2024
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.

Thanks @Cryoris - I only have some minor comments.
BTW, does porting this code to rust improves its performance ?

Comment on lines 170 to 196
if let Ok(strategy) = entanglement.downcast::<PyString>() {
let as_str = strategy.to_string();
return Ok(Box::new(
get_entanglement_from_str(num_qubits, block_size, as_str.as_str(), offset)?.map(Ok),
));
} else if let Ok(list) = entanglement.downcast::<PyList>() {
let entanglement_iter = list.iter().map(move |el| {
let connections = el
.downcast::<PyTuple>()
.expect("Entanglement must be list of tuples") // clearer error message than `?`
.iter()?
.map(|index| index?.downcast::<PyInt>()?.extract())
.collect::<Result<Vec<u32>, _>>()?;

if connections.len() != block_size as usize {
return Err(QiskitError::new_err(format!(
"Entanglement {:?} does not match block size {}",
connections, block_size
)));
}
Ok(connections)
});
return Ok(Box::new(entanglement_iter));
}
Err(QiskitError::new_err(
"Entanglement must be a string or list of qubit indices.",
))
Copy link
Contributor

Choose a reason for hiding this comment

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

To me this looks good, however I would appreciate if someone with more knowledge of Rust/Pyo3 would take a look as well (maybe @raynelfss)? In particular, is there a more ergonomic way to convert python lists of integers into a rust vector (avoiding some of the downcasting/extracting)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there's a way I'd be glad to know it -- the NLocal refactoring (on my local branch, not here) currently has quadruple nested lists which need casting to PyList 😅

Comment on lines 122 to 127
if entanglement == "pairwise" && block_size > 2 {
return Err(QiskitError::new_err(format!(
"block_size ({}) can be at most 2 for pairwise entanglement",
block_size
)));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add this to the match code below (by including ("pairwise", _) after handling the other "pairwise" cases)?

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 👍🏻

Copy link
Contributor

@alexanderivrii alexanderivrii 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 this development!

@alexanderivrii alexanderivrii added this pull request to the merge queue Aug 26, 2024
Merged via the queue into Qiskit:main with commit 54ac8f1 Aug 26, 2024
15 checks passed
@Cryoris Cryoris deleted the rust/entanglement branch August 26, 2024 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog 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.

6 participants