Skip to content

Commit

Permalink
Merge pull request #50 from kevinsung/no-array2string
Browse files Browse the repository at this point in the history
Support bitstrings longer than 72 bits by removing array2string usage
  • Loading branch information
kevinsung committed Sep 20, 2024
2 parents 87bfa37 + 36626ff commit 2a7a1ac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion qiskit_addon_sqd/configuration_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def recover_configurations(
num_elec_b,
rand_seed=rand_seed,
)
bs_str = np.array2string(bs_corrected.astype(int), separator="")[1:-1]
bs_str = "".join("1" if bit else "0" for bit in bs_corrected)
corrected_dict[bs_str] += freq
bs_mat_out = np.array([[bit == "1" for bit in bs] for bs in corrected_dict])
freqs_out = np.array([f for f in corrected_dict.values()])
Expand Down
4 changes: 2 additions & 2 deletions qiskit_addon_sqd/counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def generate_counts_uniform(
bts_matrix = np.random.choice([0, 1], size=(num_samples, num_bits))
for i in range(num_samples):
bts_arr = bts_matrix[i, :].astype("int")
bts = np.array2string(bts_arr, separator="")[1:-1]
bts = "".join("1" if bit else "0" for bit in bts_arr)
sample_dict[bts] = sample_dict.get(bts, 0) + 1

return sample_dict
Expand Down Expand Up @@ -142,7 +142,7 @@ def generate_counts_bipartite_hamming(
bts_arr[dn_flips] = 1
bts_arr[up_flips + num_bits // 2] = 1
bts_arr = bts_arr.astype("int")
bts = np.array2string(bts_arr, separator="")[1:-1]
bts = "".join("1" if bit else "0" for bit in bts_arr)

# Add the bitstring to the sample dict
sample_dict[bts] = sample_dict.get(bts, 0) + 1
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/no-array2string-efb9b050ca6efc29.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fixes a bug that caused configuration recovery to fail on bitstrings of length greater than 72.
15 changes: 15 additions & 0 deletions test/test_configuration_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ def test_recover_configurations(self):
)
self.assertTrue((expected_mat == mat_rec).all())
self.assertTrue((expected_probs == probs_rec).all())
with self.subTest("Test with more than 72 bits. Ones to zeros."):
n_bits = 74
rng = np.random.default_rng(554)
bs_mat = rng.integers(2, size=(1, n_bits), dtype=bool)
probs = np.array([1.0])
occs = np.zeros(n_bits)
num_a = 0
num_b = 0
expected_mat = np.zeros((1, n_bits), dtype=bool)
expected_probs = np.array([1.0])
mat_rec, probs_rec = recover_configurations(
bs_mat, probs, occs, num_a, num_b, rand_seed=4224
)
self.assertTrue((expected_mat == mat_rec).all())
self.assertTrue((expected_probs == probs_rec).all())
with self.subTest("Bad hamming."):
bs_mat = np.array([[True, True, True, True]])
probs = np.array([1.0])
Expand Down

0 comments on commit 2a7a1ac

Please sign in to comment.