From d4b610753791d6c6bfbc3ba1dec69f8d473e7786 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Fri, 20 Sep 2024 09:49:39 -0500 Subject: [PATCH 1/4] add tests that shows config recovery fails at 74 bits --- test/test_configuration_recovery.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/test_configuration_recovery.py b/test/test_configuration_recovery.py index 02dfb3b..218cd38 100644 --- a/test/test_configuration_recovery.py +++ b/test/test_configuration_recovery.py @@ -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 73 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]) From 89e6259bb1c2ca4cd8a27aa1180088271627957b Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Thu, 19 Sep 2024 21:10:55 -0500 Subject: [PATCH 2/4] don't use array2string --- qiskit_addon_sqd/configuration_recovery.py | 2 +- qiskit_addon_sqd/counts.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/qiskit_addon_sqd/configuration_recovery.py b/qiskit_addon_sqd/configuration_recovery.py index 8b8c6a2..e09d24e 100644 --- a/qiskit_addon_sqd/configuration_recovery.py +++ b/qiskit_addon_sqd/configuration_recovery.py @@ -115,7 +115,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] = corrected_dict.get(bs_str, 0.0) + 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()]) diff --git a/qiskit_addon_sqd/counts.py b/qiskit_addon_sqd/counts.py index 85b871d..01bd3fa 100644 --- a/qiskit_addon_sqd/counts.py +++ b/qiskit_addon_sqd/counts.py @@ -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 @@ -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 From fc0c21939c4385bc17e2c9e5720034aaf1e49263 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Fri, 20 Sep 2024 09:51:27 -0500 Subject: [PATCH 3/4] actually 72 is the largest even number --- test/test_configuration_recovery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_configuration_recovery.py b/test/test_configuration_recovery.py index 218cd38..cb43e6a 100644 --- a/test/test_configuration_recovery.py +++ b/test/test_configuration_recovery.py @@ -93,7 +93,7 @@ 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 73 bits. Ones to zeros."): + 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) From 75d8ba224a98c00ab4f4ba1287850adab604a1d8 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Fri, 20 Sep 2024 11:20:48 -0500 Subject: [PATCH 4/4] add release note --- releasenotes/notes/no-array2string-efb9b050ca6efc29.yaml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 releasenotes/notes/no-array2string-efb9b050ca6efc29.yaml diff --git a/releasenotes/notes/no-array2string-efb9b050ca6efc29.yaml b/releasenotes/notes/no-array2string-efb9b050ca6efc29.yaml new file mode 100644 index 0000000..80f6fef --- /dev/null +++ b/releasenotes/notes/no-array2string-efb9b050ca6efc29.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fixes a bug that caused configuration recovery to fail on bitstrings of length greater than 72.