From 3ee5074c5b40986ac0ca7dcd3c2936fa370a44be Mon Sep 17 00:00:00 2001 From: positr0nium Date: Sun, 2 Mar 2025 19:53:21 +0100 Subject: [PATCH] changed the behavior of the simular: given shots = None now returns the distribution in the form of floats (instead of integers scaled with some number) --- src/qrisp/jasp/testing_utils.py | 4 ++-- src/qrisp/misc/utility.py | 6 +++--- src/qrisp/simulator/simulator.py | 9 +++------ tests/test_uncomputation_example.py | 14 +++++++------- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/qrisp/jasp/testing_utils.py b/src/qrisp/jasp/testing_utils.py index 806a7c52..0be10481 100644 --- a/src/qrisp/jasp/testing_utils.py +++ b/src/qrisp/jasp/testing_utils.py @@ -51,9 +51,9 @@ def testing_function(*args): # new_key = int(new_key, base=2) try: - new_counts_dic[new_key] += counts[key]/100000 + new_counts_dic[new_key] += counts[key] except KeyError: - new_counts_dic[new_key] = counts[key]/100000 + new_counts_dic[new_key] = counts[key] for k in old_counts_dic.keys(): if abs(old_counts_dic[k] - new_counts_dic[k]) > 1E-4: diff --git a/src/qrisp/misc/utility.py b/src/qrisp/misc/utility.py index a3f98cbd..ca24c07c 100644 --- a/src/qrisp/misc/utility.py +++ b/src/qrisp/misc/utility.py @@ -1331,10 +1331,10 @@ def get_measurement_from_qc(qc, qubits, backend, shots=None): counts = new_counts_dic # Plot result (if needed) - + if shots is not None: # Normalize counts - for key in counts.keys(): - counts[key] = counts[key] / abs(no_of_shots_executed) + for key in counts.keys(): + counts[key] = counts[key] / abs(no_of_shots_executed) return counts diff --git a/src/qrisp/simulator/simulator.py b/src/qrisp/simulator/simulator.py index de596498..97d395fd 100644 --- a/src/qrisp/simulator/simulator.py +++ b/src/qrisp/simulator/simulator.py @@ -163,7 +163,7 @@ def run(qc, shots, token="", iqs=None, insert_reset=True): # The iqs object contains the outcome bitstrings in the attribute .outcome_list # and the probablities in .cl_prob. In order to ensure qiskit compatibility, we # reverse the bitstrings - + cl_prob = np.round(cl_prob, 5) norm = np.sum(cl_prob) cl_prob = cl_prob/norm @@ -171,18 +171,15 @@ def run(qc, shots, token="", iqs=None, insert_reset=True): #If shots >= 1000000, no samples will be drawn and the distribution will #be returned instead if shots is None: - shots = 100000 for j in range(len(outcome_list)): outcome_str = bin(outcome_list[j])[2:].zfill(len(mes_list)) - shot_val = int(np.round(cl_prob[j]*abs(shots))) - try: - res[outcome_str] += shot_val + res[outcome_str] += cl_prob[j] except KeyError: - res[outcome_str] = shot_val + res[outcome_str] = cl_prob[j] #Generate samples else: diff --git a/tests/test_uncomputation_example.py b/tests/test_uncomputation_example.py index f27b6d6a..95432bc6 100644 --- a/tests/test_uncomputation_example.py +++ b/tests/test_uncomputation_example.py @@ -75,13 +75,13 @@ def sqrt_oracle(qf): print(qf) assert qf.get_measurement() == {0.5: 0.9453289065781315, - 0.0: 0.007810156203124062, - 1.0: 0.007810156203124062, - 1.5: 0.007810156203124062, - 2.0: 0.007810156203124062, - 2.5: 0.007810156203124062, - 3.0: 0.007810156203124062, - 3.5: 0.007810156203124062} + 0.0: 0.007810156203124063, + 1.0: 0.007810156203124063, + 1.5: 0.007810156203124063, + 2.0: 0.007810156203124063, + 2.5: 0.007810156203124063, + 3.0: 0.007810156203124063, + 3.5: 0.007810156203124063} # ---------