-
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
Add more efficient bit packing functions for BitArray #13039
base: main
Are you sure you want to change the base?
Conversation
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 11500126152Details
💛 - Coveralls |
Thank you for the improvement. I tested it with the following script and confirmed the performance improvement for from timeit import timeit
import numpy as np
from qiskit.primitives.containers import BitArray
def bench_from_bool_array(num_shots: int, num_qubits: int, seed: int):
rng = np.random.default_rng(seed)
arr = rng.integers(2, size=(num_shots, num_qubits), dtype=bool)
print("from_bool_array")
print(f"{timeit(lambda: BitArray.from_bool_array(arr), number=1)} sec\n")
ba = BitArray.from_bool_array(arr)
return ba
def bench_slice_bits(ba: BitArray):
indices = [1] * 4
print("slice_bits")
print(ba.slice_bits(indices).get_counts())
print(f"{timeit(lambda: ba.slice_bits(indices), number=1)} sec\n")
def bench_concatenate(ba: BitArray):
print("concatenate")
print(f"{timeit(lambda: BitArray.concatenate([ba] * 10), number=1)} sec\n")
ba = bench_from_bool_array(100_000, 100_000, 123)
ba = ba.reshape(1, ba.size)
bench_slice_bits(ba)
bench_concatenate(ba) main branch
this PR
|
Co-authored-by: Takashi Imamichi <[email protected]>
This PR just came back onto my radar. @t-imamichi thanks for the timings. Based on them, do you think that we should only consider slice_bits? |
Thank you for paying attention to this PR, @ihincks from timeit import timeit
import numpy as np
from qiskit.primitives.containers import BitArray
rng = np.random.default_rng(123)
def bench_from_bool_array(num_shots: int, num_qubits: int):
arr = rng.integers(2, size=(num_shots, num_qubits), dtype=bool)
print("from_bool_array")
print(f"{timeit(lambda: BitArray.from_bool_array(arr), number=1)} sec\n")
ba = BitArray.from_bool_array(arr)
return ba
def bench_from_counts(ba: BitArray):
counts = ba.get_counts(np.arange(10_000))
print("from_counts")
print(f"{timeit(lambda: BitArray.from_counts(counts), number=1)} sec\n")
def bench_slice_bits(ba: BitArray):
indices = [1] * 4
print("slice_bits")
print(ba.slice_bits(indices).get_counts())
print(f"{timeit(lambda: ba.slice_bits(indices), number=1)} sec\n")
def bench_concatenate(ba: BitArray):
print("concatenate")
print(f"{timeit(lambda: BitArray.concatenate([ba] * 10), number=1)} sec\n")
def bench_postselect(ba: BitArray):
print("postselect")
n = 100
print(f"{timeit(lambda: ba.postselect(np.arange(n), np.ones(n)), number=1)} sec\n")
def bench_expval(ba: BitArray):
ba = ba.slice_bits(np.arange(2000))
obs = "Z" * ba.num_bits
print("expectation_values")
print(f"{timeit(lambda: ba.expectation_values(obs), number=1)} sec\n")
ba = bench_from_bool_array(100_000, 100_000)
bench_from_counts(ba)
ba = ba.reshape(1, ba.size)
bench_slice_bits(ba)
bench_concatenate(ba)
bench_postselect(ba)
bench_expval(ba)
|
I tried to push reno, but I cannot do it. Anyways, I wait for your decision, @ihincks. |
I'm not sure why I cannot push a commit with reno, but I leave it here just in case.
|
Is this still on track for 1.3? |
Summary
This adds internal utility functions for improved bit-packing, unpacking, and slicing for internal use by BitArray.
Details and comments
These functions give additional functionality for packing and unpacking arbitrary indexes bits in larger arrays by only working with the non-zero bytes, rather than having to pack or unpack the full array as was previous done.
The functions can also be used independently for working with byte and boolean arrays outside of BitArray, since they don't assume the shot-axis formatting required by BitArray. These helper functions might be a good candidate for porting to Rust in the future.