-
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
Optimize ConsolidateBlocks further #10467
Merged
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cd166e7
Initial commit: Added convert_2q_block_rust
raynelfss 762c399
Feat: Perform final matrix multiplication in rust
raynelfss b1167fa
Merge branch 'Qiskit:main' into consolidate_rust
raynelfss b436fb6
Feat: `blocks_to_matrix` performs all operations
raynelfss 9ce069a
Fix: Prevent certain copying in Rust.
raynelfss 292a431
Merge branch 'main' into consolidate_rust
raynelfss cb1b259
Fix: Adapted to newest changes
raynelfss 3e50849
Fix: Small modifications to blocks_to_matrix
raynelfss 4d16388
Fix: Basis change without Matrix Multiplication.
raynelfss 303b4fd
Merge branch 'main' into consolidate_rust
raynelfss d47849c
Remove:`calculate_matrix` function
raynelfss 871a75e
Merge branch 'main' into consolidate_rust
raynelfss 0e2bc10
Fix: Use swap to swap rows
raynelfss 1523a71
Merge branch 'main' into consolidate_rust
raynelfss 7819cc2
Fix: Skip initial matrix multiplication.
raynelfss dc31408
Merge branch 'main' into consolidate_rust
raynelfss 5356702
Fix: Deal with empty list edege case
raynelfss 4b8f3d7
Merge branch 'main' into consolidate_rust
raynelfss 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 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,103 @@ | ||
// This code is part of Qiskit. | ||
// | ||
// (C) Copyright IBM 2022 | ||
// | ||
// 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 pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
use pyo3::Python; | ||
|
||
use num_complex::Complex64; | ||
use numpy::ndarray::linalg::kron; | ||
use numpy::ndarray::{array, Array, Array2, ArrayView2}; | ||
use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2}; | ||
|
||
/// Return the matrix Operator resulting from a block of Instructions. | ||
#[pyfunction] | ||
#[pyo3(text_signature = "(matrix_list, /")] | ||
pub fn blocks_to_matrix( | ||
py: Python, | ||
op_list: Vec<(PyReadonlyArray2<Complex64>, Vec<usize>)>, | ||
) -> PyResult<Py<PyArray2<Complex64>>> { | ||
let mut matrix: Array2<Complex64> = Array::eye(4); | ||
mtreinish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let swap_gate = array![ | ||
[ | ||
Complex64::new(1., 0.), | ||
Complex64::new(0., 0.), | ||
Complex64::new(0., 0.), | ||
Complex64::new(0., 0.) | ||
], | ||
[ | ||
Complex64::new(0., 0.), | ||
Complex64::new(0., 0.), | ||
Complex64::new(1., 0.), | ||
Complex64::new(0., 0.) | ||
], | ||
[ | ||
Complex64::new(0., 0.), | ||
Complex64::new(1., 0.), | ||
Complex64::new(0., 0.), | ||
Complex64::new(0., 0.) | ||
], | ||
[ | ||
Complex64::new(0., 0.), | ||
Complex64::new(0., 0.), | ||
Complex64::new(0., 0.), | ||
Complex64::new(1., 0.) | ||
] | ||
]; | ||
raynelfss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let identity: Array2<Complex64> = Array::eye(2); | ||
for (op_matrix, q_list) in op_list { | ||
let op_matrix = op_matrix.as_array(); | ||
let result = calculate_matrix(op_matrix, &q_list, &swap_gate, &identity); | ||
matrix = match result { | ||
Some(result) => result.dot(&matrix), | ||
None => op_matrix.dot(&matrix), | ||
}; | ||
} | ||
Ok(matrix.into_pyarray(py).to_owned()) | ||
} | ||
|
||
/// Performs the matrix operations for an Instruction in a 2 qubit system | ||
fn calculate_matrix( | ||
matrix: ArrayView2<Complex64>, | ||
q_list: &Vec<usize>, | ||
raynelfss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
swap_gate: &Array2<Complex64>, | ||
identity: &Array2<Complex64>, | ||
) -> Option<Array2<Complex64>> { | ||
let mut basis_change = false; | ||
let current: Option<Array2<Complex64>> = if q_list.len() < 2 { | ||
if q_list[0] == 1 { | ||
Some(kron(&matrix, identity)) | ||
} else { | ||
Some(kron(identity, &matrix)) | ||
} | ||
} else { | ||
if q_list[0] > q_list[1] && matrix != swap_gate { | ||
basis_change = true; | ||
} | ||
None | ||
}; | ||
|
||
if basis_change { | ||
match current { | ||
Some(current) => Some(swap_gate.dot(¤t).dot(swap_gate)), | ||
None => Some(swap_gate.dot(&matrix).dot(swap_gate)), | ||
} | ||
raynelfss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
current | ||
} | ||
} | ||
raynelfss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[pymodule] | ||
pub fn convert_2q_block_matrix(_py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(blocks_to_matrix))?; | ||
Ok(()) | ||
} |
This file contains 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 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 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 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
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.
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.
Very minor (and it's particularly unimportant because the
/
in the text signature implies we can't use named arguments), but the text signature doesn't match the function. I think in this case, PyO3 will probably actually populate the__text_signature__
attribute correctly without being told, but it's worth checking.