diff --git a/qiskit_addon_sqd/configuration_recovery.py b/qiskit_addon_sqd/configuration_recovery.py index 3c50b83..c3716b2 100644 --- a/qiskit_addon_sqd/configuration_recovery.py +++ b/qiskit_addon_sqd/configuration_recovery.py @@ -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()]) 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 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. diff --git a/test/test_configuration_recovery.py b/test/test_configuration_recovery.py index 02dfb3b..cb43e6a 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 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])