From 762ecb3e2ae78691e03ec9d930218cc8859c5c31 Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Fri, 13 Sep 2024 13:07:35 -0500 Subject: [PATCH] Adjust bipartite hamming counts generation interface --- qiskit_addon_sqd/counts.py | 12 ++++++------ .../notes/subsample-hamming-76674dbaf6f411c2.yaml | 10 +++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/qiskit_addon_sqd/counts.py b/qiskit_addon_sqd/counts.py index 1a1d453..7ca402c 100644 --- a/qiskit_addon_sqd/counts.py +++ b/qiskit_addon_sqd/counts.py @@ -90,8 +90,8 @@ def generate_counts_uniform( def generate_counts_bipartite_hamming( num_samples: int, num_bits: int, - hamming_left: int, hamming_right: int, + hamming_left: int, rand_seed: None | int = None, ) -> dict[str, int]: """ @@ -100,8 +100,8 @@ def generate_counts_bipartite_hamming( Args: num_samples: The number of samples to draw num_bits: The number of bits in the bitstrings - hamming_left: The hamming weight on the left half of each bitstring hamming_right: The hamming weight on the right half of each bitstring + hamming_left: The hamming weight on the left half of each bitstring rand_seed: A seed for controlling randomness Returns: @@ -128,17 +128,17 @@ def generate_counts_bipartite_hamming( sample_dict: dict[str, int] = {} for _ in range(num_samples): # Pick random bits to flip such that the left and right hamming weights are correct - up_flips = np.random.choice(np.arange(num_bits // 2), hamming_left, replace=False).astype( + up_flips = np.random.choice(np.arange(num_bits // 2), hamming_right, replace=False).astype( "int" ) - dn_flips = np.random.choice(np.arange(num_bits // 2), hamming_right, replace=False).astype( + dn_flips = np.random.choice(np.arange(num_bits // 2), hamming_left, replace=False).astype( "int" ) # Create a bitstring with the chosen bits flipped bts_arr = np.zeros(num_bits) - bts_arr[up_flips] = 1 - bts_arr[dn_flips + num_bits // 2] = 1 + 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] diff --git a/releasenotes/notes/subsample-hamming-76674dbaf6f411c2.yaml b/releasenotes/notes/subsample-hamming-76674dbaf6f411c2.yaml index d192e81..c0baa63 100644 --- a/releasenotes/notes/subsample-hamming-76674dbaf6f411c2.yaml +++ b/releasenotes/notes/subsample-hamming-76674dbaf6f411c2.yaml @@ -5,7 +5,7 @@ prelude: > upgrade: - | - The :func:`qiskit_addon_sqd.subsampling.postselect_and_subsample` and :func:`qiskit_addon_sqd.configuration_recovery.post_select_by_hamming_weight` now take the ``hamming_right`` positional argument before the ``hamming_left`` argument to better match the rest of the workflow. + The :func:`qiskit_addon_sqd.counts.generate_counts_bipartite_hamming`, :func:`qiskit_addon_sqd.subsampling.postselect_and_subsample`, and :func:`qiskit_addon_sqd.configuration_recovery.post_select_by_hamming_weight` now take the ``hamming_right`` positional argument before the ``hamming_left`` argument to better match the rest of the workflow. To upgrade @@ -13,6 +13,11 @@ upgrade: from qiskit_addon_sqd.configuration_recovery import post_select_by_hamming_weight from qiskit_addon_sqd.subsampling import postselect_and_subsample + from qiskit_addon_sqd.counts import generate_counts_bipartite_hamming + + counts = generate_counts_bipartite_hamming(num_samples, num_bits, num_elec_b, num_elec_a) + + ... bs_mat = post_select_by_hamming_weight(bs_mat_full, num_elec_b, num_elec_a) @@ -33,6 +38,9 @@ upgrade: from qiskit_addon_sqd.configuration_recovery import post_select_by_hamming_weight from qiskit_addon_sqd.subsampling import postselect_and_subsample + from qiskit_addon_sqd.counts import generate_counts_bipartite_hamming + + counts = generate_counts_bipartite_hamming(num_samples, num_bits, num_elec_a, num_elec_b) bs_mat = post_select_by_hamming_weight(bs_mat_full, num_elec_a, num_elec_b)