-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Port ElidePermutations transpiler pass to Rust
#13094
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
Merged
kevinhartman
merged 20 commits into
Qiskit:main
from
alexanderivrii:elide-permutations-in-rust
Oct 3, 2024
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d5e9a01
initial commit
alexanderivrii 2b667b6
Rust docstring improvements
alexanderivrii 7b38300
Merge branch 'main' into elide-permutations-in-rust
alexanderivrii 16eca49
lint
alexanderivrii 90ef328
restoring elided comment
alexanderivrii 004446c
explicitlt setting dtype=int for permutation gates
alexanderivrii baf6213
another attempt
alexanderivrii 53af0b5
Merge branch 'main' into elide-permutations-in-rust
alexanderivrii a20d077
fmt after merge
alexanderivrii d9a48ae
Merge branch 'main' into elide-permutations-in-rust
alexanderivrii 7227e73
update after merge + comment from code review
alexanderivrii 8e0198f
switching to apply_operation_back
alexanderivrii 05870e6
Comments from code review
alexanderivrii 0268d50
Merge branch 'main' into elide-permutations-in-rust
alexanderivrii fd3421e
Merge branch 'main' into elide-permutations-in-rust
alexanderivrii 8209757
fix using params
alexanderivrii 23461f2
Merge branch 'main' into elide-permutations-in-rust
alexanderivrii fa7fc9d
Merge branch 'main' into elide-permutations-in-rust
kevinhartman 9a2804a
Merge branch 'main' into elide-permutations-in-rust
alexanderivrii 4cb7ca2
Merge branch 'main' into elide-permutations-in-rust
alexanderivrii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // This code is part of Qiskit. | ||
| // | ||
| // (C) Copyright IBM 2024 | ||
| // | ||
| // This code is licensed under the Apache License, Version 2.0. You may | ||
| // obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| // of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| // | ||
| // Any modifications or derivative works of this code must retain this | ||
| // copyright notice, and modified files need to carry a notice indicating | ||
| // that they have been altered from the originals. | ||
|
|
||
| use numpy::PyReadonlyArray1; | ||
| use pyo3::prelude::*; | ||
|
|
||
| use qiskit_circuit::dag_circuit::{DAGCircuit, NodeType}; | ||
| use qiskit_circuit::operations::{Operation, Param}; | ||
| use qiskit_circuit::Qubit; | ||
|
|
||
| /// Run the ElidePermutations pass on `dag`. | ||
| /// Args: | ||
| /// dag (DAGCircuit): the DAG to be optimized. | ||
| /// Returns: | ||
| /// An `Option`: the value of `None` indicates that no optimization was | ||
| /// performed and the original `dag` should be used, otherwise it's a | ||
| /// tuple consisting of the optimized DAG and the induced qubit permutation. | ||
| #[pyfunction] | ||
| fn run(py: Python, dag: &mut DAGCircuit) -> PyResult<Option<(DAGCircuit, Vec<usize>)>> { | ||
| let permutation_gate_names = ["swap".to_string(), "permutation".to_string()]; | ||
| let op_counts = dag.count_ops(py, false)?; | ||
| if !permutation_gate_names | ||
| .iter() | ||
| .any(|name| op_counts.contains_key(name)) | ||
| { | ||
| return Ok(None); | ||
| } | ||
| let mut mapping: Vec<usize> = (0..dag.num_qubits()).collect(); | ||
|
|
||
| // note that DAGCircuit::copy_empty_like clones the interners | ||
| let mut new_dag = dag.copy_empty_like(py, "alike")?; | ||
| for node_index in dag.topological_op_nodes()? { | ||
| if let NodeType::Operation(inst) = &dag.dag()[node_index] { | ||
| match (inst.op.name(), inst.condition()) { | ||
| ("swap", None) => { | ||
| let qargs = dag.get_qargs(inst.qubits); | ||
| let index0 = qargs[0].0 as usize; | ||
| let index1 = qargs[1].0 as usize; | ||
| mapping.swap(index0, index1); | ||
| } | ||
| ("permutation", None) => { | ||
| if let Param::Obj(ref pyobj) = inst.params.as_ref().unwrap()[0] { | ||
| let pyarray: PyReadonlyArray1<i32> = pyobj.extract(py)?; | ||
| let pattern = pyarray.as_array(); | ||
|
|
||
| let qindices: Vec<usize> = dag | ||
| .get_qargs(inst.qubits) | ||
| .iter() | ||
| .map(|q| q.0 as usize) | ||
| .collect(); | ||
|
|
||
| let remapped_qindices: Vec<usize> = (0..qindices.len()) | ||
| .map(|i| pattern[i]) | ||
| .map(|i| qindices[i as usize]) | ||
| .collect(); | ||
|
|
||
| qindices | ||
| .iter() | ||
| .zip(remapped_qindices.iter()) | ||
| .for_each(|(old, new)| { | ||
| mapping[*old] = *new; | ||
| }); | ||
| } else { | ||
| unreachable!(); | ||
| } | ||
| } | ||
| _ => { | ||
| // General instruction | ||
| let qargs = dag.get_qargs(inst.qubits); | ||
| let cargs = dag.get_cargs(inst.clbits); | ||
| let mapped_qargs: Vec<Qubit> = qargs | ||
| .iter() | ||
| .map(|q| q.0 as usize) | ||
| .map(|q| mapping[q]) | ||
| .map(|q| Qubit(q.try_into().unwrap())) | ||
| .collect(); | ||
|
|
||
| new_dag.apply_operation_back( | ||
| py, | ||
| inst.op.clone(), | ||
| &mapped_qargs, | ||
| cargs, | ||
| inst.params.as_deref().cloned(), | ||
| inst.extra_attrs.clone(), | ||
| #[cfg(feature = "cache_pygates")] | ||
| None, | ||
| )?; | ||
| } | ||
| } | ||
| } else { | ||
| unreachable!(); | ||
| } | ||
| } | ||
| Ok(Some((new_dag, mapping))) | ||
| } | ||
|
|
||
| pub fn elide_permutations(m: &Bound<PyModule>) -> PyResult<()> { | ||
| m.add_wrapped(wrap_pyfunction!(run))?; | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
releasenotes/notes/port-elide-permutations-ed91c3d9cef2fec6.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| features_transpiler: | ||
| - | | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we haven't actually written release notes like this for any (most of?) the other passes being ported, have we? IMO, we can leave this in for now and the release manager (whoever that ends up being) can just remove this if it's out of place. |
||
| Port most of the logic of the transpiler pass :class:`~.ElidePermutations` | ||
| to Rust. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.