forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a marginal_distribution function (Qiskit#8026)
* Add a marginal_distribution function This commit adds a new function marginal_distribution which performs marginalization similar to the existing marginal_counts() function. This new function however differs in a few key ways. Most importantly the order of the bit indices is significant for the purpose of permuting the bits while marginalizing, while marginal_counts() doesn't do this. Additionally, this function only works with dictionaries and not Result objects and is written in Rust for performance. While the purposed of this function is mostly identical to marginal_counts(), because making the bit indices order significant is a breaking change this was done as a separate function to avoid that. Once this new function is released we can look at deprecated and eventually removing the existing marginal_counts() function. Fixes Qiskit#6230 * Fix typos in python function * Handle missing indices In case a bit string has stripped leading zeros this commit adds handling to treat a missing index from the bit string as a '0'. * Apply suggestions from code review Co-authored-by: Jake Lishman <[email protected]> * Remove unecessary rust index * Update test Co-authored-by: Jake Lishman <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4d459c8
commit 84cfd5c
Showing
9 changed files
with
234 additions
and
1 deletion.
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
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
16 changes: 16 additions & 0 deletions
16
releasenotes/notes/add-marginal-distribution-21060de506ed9cfc.yaml
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,16 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new function, :func:`~.marginal_distribution`, which is used to | ||
marginalize an input dictionary of bitstrings to an integer (such as | ||
:class:`~.Counts`). This is similar in functionality to the existing | ||
:func:`~.marginal_counts` function with three key differences. The first | ||
is that :func:`~.marginal_counts` works with either a counts dictionary | ||
or a :class:`~.Results` object while :func:`~.marginal_distribution` only | ||
works with a dictionary. The second is that :func:`~.marginal_counts` does | ||
not respect the order of indices in its ``indices`` argument while | ||
:func:`~.marginal_distribution` does and will permute the output bits | ||
based on the ``indices`` order. The third difference is that | ||
:func:`~.marginal_distribution` should be faster as its implementation | ||
is written in Rust and streamlined for just marginalizing a dictionary | ||
input. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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 hashbrown::HashMap; | ||
use pyo3::prelude::*; | ||
|
||
fn marginalize<T: std::ops::AddAssign + Copy>( | ||
counts: HashMap<String, T>, | ||
indices: Option<Vec<usize>>, | ||
) -> HashMap<String, T> { | ||
let mut out_counts: HashMap<String, T> = HashMap::with_capacity(counts.len()); | ||
let clbit_size = counts.keys().next().unwrap().replace(&['_', ' '], "").len(); | ||
let all_indices: Vec<usize> = (0..clbit_size).collect(); | ||
counts | ||
.iter() | ||
.map(|(k, v)| (k.replace(&['_', ' '], ""), *v)) | ||
.for_each(|(k, v)| match &indices { | ||
Some(indices) => { | ||
if all_indices == *indices { | ||
out_counts.insert(k, v); | ||
} else { | ||
let key_arr = k.as_bytes(); | ||
let new_key: String = indices | ||
.iter() | ||
.map(|bit| { | ||
let index = clbit_size - *bit - 1; | ||
match key_arr.get(index) { | ||
Some(bit) => *bit as char, | ||
None => '0', | ||
} | ||
}) | ||
.rev() | ||
.collect(); | ||
out_counts | ||
.entry(new_key) | ||
.and_modify(|e| *e += v) | ||
.or_insert(v); | ||
} | ||
} | ||
None => { | ||
out_counts.insert(k, v); | ||
} | ||
}); | ||
out_counts | ||
} | ||
|
||
#[pyfunction] | ||
pub fn marginal_counts( | ||
counts: HashMap<String, u64>, | ||
indices: Option<Vec<usize>>, | ||
) -> HashMap<String, u64> { | ||
marginalize(counts, indices) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn marginal_distribution( | ||
counts: HashMap<String, f64>, | ||
indices: Option<Vec<usize>>, | ||
) -> HashMap<String, f64> { | ||
marginalize(counts, indices) | ||
} |
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,23 @@ | ||
// 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. | ||
|
||
pub mod marginalization; | ||
|
||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
|
||
#[pymodule] | ||
pub fn results(_py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(marginalization::marginal_counts))?; | ||
m.add_wrapped(wrap_pyfunction!(marginalization::marginal_distribution))?; | ||
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