-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[WIP] Add Suzuki-Trotter to C API #15459
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
base: main
Are you sure you want to change the base?
Conversation
|
Esteban Ramirez seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
f810558 to
2bac78e
Compare
2bac78e to
35783dd
Compare
Cryoris
left a comment
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.
Thanks for the PR @Estebanrg21, this already looks pretty good! I think we can improve the datastructures a bit and avoid some clones and collections.
This would also need a release note 🙂
| } | ||
| } | ||
|
|
||
| pub fn evolution(order: u32, mut paulis: Vec<StrSparseTerm>) -> Vec<StrSparseTerm> { |
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.
paulis shouldn't be mutable here if we're returning a new clone, also we can should be able to change this to take a slice (paulis: &[SparseTermView]) I believe
| ) | ||
| } | ||
|
|
||
| let parsed_observable: Vec<StrSparseTerm> = observable |
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.
SparseObservable has a iter method which returns SparseTermView objects. These are pretty close to your StrSparseTerm here. Do you think we could directly use the SparseTermView to avoid cloning into a StrSparseTerm?
| }; | ||
| } | ||
|
|
||
| pub fn reorder_terms<'a>(terms: &'a Vec<StrSparseTerm<'a>>) -> Vec<StrSparseTerm<'a>> { |
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 can just use a slice here, that's more generic
| pub fn reorder_terms<'a>(terms: &'a Vec<StrSparseTerm<'a>>) -> Vec<StrSparseTerm<'a>> { | |
| pub fn reorder_terms<'a>(terms: &'a [StrSparseTerm<'a>]) -> Vec<StrSparseTerm<'a>> { |
| ) | ||
| }) | ||
| .collect_tuple() | ||
| .expect("Expected a combination of two values"); |
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.
Since this is not a C-specific function, can we make it return a proper Result<..> type, such that errors could be handled gracefully? You could just start with a &str as error type.
| } | ||
|
|
||
| pub fn reorder_terms<'a>(terms: &'a Vec<StrSparseTerm<'a>>) -> Vec<StrSparseTerm<'a>> { | ||
| let sorted: Vec<&StrSparseTerm> = terms |
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 have to collect this into a vec if you're just iterating over it later, i.e, we can do
let mut graph = ..
for term in terms.iter().sorted_by_key(..) {
graph.add_node(term);
}| .sorted_by_key(|view| (view.indices, view.terms.clone())) | ||
| .collect(); | ||
|
|
||
| let mut graph: Graph<&StrSparseTerm, Option<u8>, Undirected> = Graph::new_undirected(); |
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.
Is there a way to tell the graph what capacity we need, such that we can allocate the required memory only one?
| }; | ||
| } | ||
|
|
||
| pub fn reorder_terms<'a>(terms: &'a Vec<StrSparseTerm<'a>>) -> Vec<StrSparseTerm<'a>> { |
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 function does a lot of collecting and cloning, that we should try to avoid. We could change e.g. to return the indices in which the terms should be applied instead of cloning and collecting into a new container.
| insert_barriers: bool, | ||
| ) -> Result<CircuitData, PyErr> { | ||
| if order > 1 && order % 2 == 1 { | ||
| panic!( |
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.
Same here, could we turn this into an error instead of a panic? Panics should be reserved for cases that should not be reached -- or for C functions where we can't handle errors gracefully 🙂
|
|
||
| #[unsafe(no_mangle)] | ||
| #[cfg(feature = "cbinding")] | ||
| pub extern "C" fn qk_circuit_library_suzuki_trotter_evolution( |
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.
Could we rename this to just qk_circuit_library_suzuki_trotter? The name is long enough then... 😄
| use qiskit_circuit_library::suzuki_trotter::suzuki_trotter_evolution; | ||
| use qiskit_quantum_info::sparse_observable::SparseObservable; | ||
|
|
||
| #[unsafe(no_mangle)] |
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 function needs a docstring, see the other C functions for examples
Summary
This pull request implements the Suzuki-Trotter evolution within the C API.
Details and comments
1. New APIs:
crates/circuit_library/src/suzuki_trotter.rscrates/synthesis/src/evolution/suzuki_trotter.rscrates/cext/src/synthesis/suzuki_trotter.rs2. As part of the implementation, some APIs were made public:
synthesis/evolutionInstructionstruct fromcrates/circuit_library/src/pauli_evolution.rscrates/circuit_library/src/pauli_evolution.rs3. The
qiskit-synthesisandqiskit-quantum-infocrates were added as dependencies forqiskit-circuit-library