qiskit/src
Paul Nation 5f3c9969db
Sampled expectation value for distributions (#8748)
* Add base code

* Add tests

* Add more tests

* move files to results and move imports

* fix test

* add more tests

* change from dict to counts

* Run black

* Convert sampled exp val function to rust

This commit rewrites the python implementation of the expectation value
calculation to rust. The rust implementation should be significantly
faster to compute the expectation value.

* Fix lint

* ignore cyclic import warning since inside func

* black

* Add some checks on inputs

* Add release note

* Speed up bitstring access in rust code

This commit fixes the primary bottleneck in the rust expectation value
calculation. The bit string access of the nth position previously used
in the code was O(n) and for large numbers of qubits this access could
add up in aggregate while computing the expectation value (even
with profiling overhead for 127 qubit this was still roughly .3
seconds for calling the rust function). This commit adjusts how we
access the nth character of the bit string to be O(1) and removing that
bottlneck and for 127 qubit bit string provides a ~3x speedup. The
tradeoff is that this isn't safe (in that there is potential incorrect
behavior) if someone injects random unicode characters in the bitstring.
However as this is unlikely making the change is worth it considering
the speed up. After this commit the largest bottleneck is conversion
overhead from python->rust and memory management of the temp rust
object, at ~59% of the time to run the sampled_expval_complex()
function, which is hard to workaround. Although, there might be further
tuning to reduce the time required for the remaining 41%.

* Update qiskit/result/sampled_expval.py

If this works then yes indeed.  Each operator appears to be coded differently so happy there is some overlap here

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* explain raw dict inputs

* Update test/python/result/test_sampled_expval.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* Apply suggestions from code review

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* Make oper_table_size a const variable instead of a magic number

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2022-10-01 02:15:56 +00:00
..
results Refactor marginal_memory() hex to bin lookup table to be a true static (#8223) 2022-06-23 01:18:53 +00:00
sabre_swap Use Sabre by default for optimization levels 1 and 2 (#8552) 2022-09-29 03:43:28 +00:00
dense_layout.rs Reimplement DenseLayout as a Parallel Algorithm in Rust (#7740) 2022-03-10 22:47:34 +00:00
edge_collections.rs Implement multithreaded stochastic swap in rust (#7658) 2022-02-28 21:49:54 +00:00
lib.rs Sampled expectation value for distributions (#8748) 2022-10-01 02:15:56 +00:00
nlayout.rs Further oxidize sabre (#8388) 2022-08-22 15:46:23 +00:00
optimize_1q_gates.rs Move compose_u3 into Rust (#8307) 2022-07-08 14:09:09 +00:00
pauli_exp_val.rs Sampled expectation value for distributions (#8748) 2022-10-01 02:15:56 +00:00
sampled_exp_val.rs Sampled expectation value for distributions (#8748) 2022-10-01 02:15:56 +00:00
sparse_pauli_op.rs dict-based `SparsePauliOp.simplify` (#7656) 2022-03-17 18:09:00 +00:00
stochastic_swap.rs Replace pauli expectation value cython with multithreaded rust implementation (#7702) 2022-03-10 20:54:24 +00:00