-
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 all 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,73 @@ | ||
// 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, Array2, ArrayView2}; | ||
use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2}; | ||
|
||
/// Return the matrix Operator resulting from a block of Instructions. | ||
#[pyfunction] | ||
#[pyo3(text_signature = "(op_list, /")] | ||
pub fn blocks_to_matrix( | ||
py: Python, | ||
op_list: Vec<(PyReadonlyArray2<Complex64>, Vec<usize>)>, | ||
) -> PyResult<Py<PyArray2<Complex64>>> { | ||
let identity: Array2<Complex64> = Array::eye(2); | ||
let input_matrix = op_list[0].0.as_array(); | ||
let mut matrix: Array2<Complex64> = match op_list[0].1.as_slice() { | ||
[0] => kron(&identity, &input_matrix), | ||
[1] => kron(&input_matrix, &identity), | ||
[0, 1] => input_matrix.to_owned(), | ||
[1, 0] => change_basis(input_matrix), | ||
[] => Array::eye(4), | ||
_ => unreachable!(), | ||
}; | ||
for (op_matrix, q_list) in op_list.into_iter().skip(1) { | ||
let op_matrix = op_matrix.as_array(); | ||
let result = match q_list.as_slice() { | ||
[0] => Some(kron(&identity, &op_matrix)), | ||
[1] => Some(kron(&op_matrix, &identity)), | ||
[1, 0] => Some(change_basis(op_matrix)), | ||
[] => Some(Array::eye(4)), | ||
_ => None, | ||
}; | ||
matrix = match result { | ||
Some(result) => result.dot(&matrix), | ||
None => op_matrix.dot(&matrix), | ||
}; | ||
} | ||
Ok(matrix.into_pyarray(py).to_owned()) | ||
} | ||
|
||
/// Switches the order of qubits in a two qubit operation. | ||
fn change_basis(matrix: ArrayView2<Complex64>) -> Array2<Complex64> { | ||
let mut trans_matrix: Array2<Complex64> = matrix.reversed_axes().to_owned(); | ||
for index in 0..trans_matrix.ncols() { | ||
trans_matrix.swap([1, index], [2, index]); | ||
} | ||
trans_matrix = trans_matrix.reversed_axes(); | ||
for index in 0..trans_matrix.ncols() { | ||
trans_matrix.swap([1, index], [2, index]); | ||
} | ||
trans_matrix | ||
} | ||
|
||
#[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.
This clones the matrix, but we'd be able to mutate the one we're given if we took in an
ArrayViewMut2
, since we never need it in the other form.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.
Well, there's a problem with doing so, since we're receiving
PyReadOnlyArray
from Python, which doesn't implement theDataMut
trait that's necessary to turn the views to mutable views by callingview_mut()
.One way of avoiding that is by using the
as_array_mut()
method to obtain anArrayViewMut2
right away. However, this is an unsafe operation, and I'm not sure we'd want to include unsafe code for this solution.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.
Yeah, I was kind of meaning that we'd take in a
PyReadwriteArray2
at the top level instead, but really, I wouldn't worry about it - I think it'd need fairly significantly changes to how we're handling the iteration here to make it borrow-safe.