Skip to content

Commit

Permalink
Tidy up following merge
Browse files Browse the repository at this point in the history
  • Loading branch information
cdbf1 committed Dec 12, 2024
1 parent ea68771 commit 11c2de6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 75 deletions.
75 changes: 61 additions & 14 deletions docs/source/apps/supermarq/qcvv/qcvv_css.ipynb

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions docs/source/apps/supermarq/qcvv/qcvv_xeb_css.ipynb

Large diffs are not rendered by default.

43 changes: 16 additions & 27 deletions supermarq-benchmarks/supermarq/qcvv/base_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,38 +504,27 @@ def results_from_records(self, records: dict[uuid.UUID, dict[str, int]]) -> Resu
if uid not in samples_dict:
break
sample = samples_dict[uid]
bitstrings = [format(k, f"0{self.num_qubits}b") for k in range(2**self.num_qubits)]
# Check if any of the provided bitstrings are invalid.
if any(k not in bitstrings for k in counts.keys()):
warnings.warn(
f"Some counts provided in the record with ID {uid} have invalid bitstrings, "
"these will be ignored."
try:
if any(not isinstance(c, int) for c in counts.values()):
raise ValueError("Counts must be integer.")
if any(c < 0 for c in counts.values()):
raise ValueError("Counts must be positive.")
if sum(counts.values()) == 0:
raise ValueError("No non-zero counts.")

probabilities = self._canonicalize_probabilities(
{key: count / sum(counts.values()) for key, count in counts.items()},
self.num_qubits,
)
# Check all counts are positive intergers
if any(not isinstance(c, int) or c < 0 for c in counts.values()):
raise ValueError(
f"Some counts provided for record with ID {uid} are not positive integers."
)
# Drop invalid bitstrings
counts = {bitstring: counts.get(bitstring, 0) for bitstring in bitstrings}
# Convert to probabilities
total_count = sum(counts.values())
if total_count == 0:
warnings.warn(
f"Record with ID {uid} contains no valid, non-zero counts. This record"
"will be ignored."
)
break
else:
probability = {
bitstring: count / total_count for bitstring, count in counts.items()
}
except (ValueError, RuntimeError) as e:
warnings.warn(f"Processing sample {str(sample.uuid)} raised error. {e}")
continue # Skip this record

# Add to results data
results_data.append({**sample.data, **probability})
results_data.append({**sample.data, **probabilities})

return self._results_cls(
target="Records",
target="records",
experiment=self,
data=pd.DataFrame(results_data),
)
Expand Down
30 changes: 16 additions & 14 deletions supermarq-benchmarks/supermarq/qcvv/base_experiment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ def test_results_from_records(abc_experiment: ExampleExperiment) -> None:

def test_results_from_records_bad_input(abc_experiment: ExampleExperiment) -> None:
samples = abc_experiment.samples
samples[0].uuid = uuid.UUID("0e9421da-3700-42e9-9281-a0e24cc0986c")
# Warn for missing samples
with pytest.warns(
UserWarning,
Expand All @@ -584,8 +585,8 @@ def test_results_from_records_bad_input(abc_experiment: ExampleExperiment) -> No
with pytest.warns(
UserWarning,
match=re.escape(
f"Some counts provided in the record with ID {samples[0].uuid} have invalid "
"bitstrings, these will be ignored."
"Processing sample 0e9421da-3700-42e9-9281-a0e24cc0986c raised error. The key "
"contains the wrong number of bits. Got 3 entries but expected 2 bits."
),
):
abc_experiment.results_from_records({samples[0].uuid: {"001": 10}})
Expand All @@ -594,26 +595,27 @@ def test_results_from_records_bad_input(abc_experiment: ExampleExperiment) -> No
with pytest.warns(
UserWarning,
match=re.escape(
f"Record with ID {samples[0].uuid} contains no valid, non-zero counts. This record"
"will be ignored."
"Processing sample 0e9421da-3700-42e9-9281-a0e24cc0986c raised error. No "
"non-zero counts."
),
):
abc_experiment.results_from_records({samples[0].uuid: {"001": 10}})
abc_experiment.results_from_records({samples[0].uuid: {"01": 0}})

# Raise exception for non positive integer counts
with pytest.raises(
ValueError,
# Warn for non positive integer counts
with pytest.warns(
UserWarning,
match=re.escape(
f"Some counts provided for record with ID {samples[0].uuid} are not "
"positive integers."
"Processing sample 0e9421da-3700-42e9-9281-a0e24cc0986c raised error. Counts "
"must be positive."
),
):
abc_experiment.results_from_records({samples[0].uuid: {"01": -10}})
with pytest.raises(
ValueError,

with pytest.warns(
UserWarning,
match=re.escape(
f"Some counts provided for record with ID {samples[0].uuid} are not "
"positive integers."
"Processing sample 0e9421da-3700-42e9-9281-a0e24cc0986c raised error. Counts "
"must be integer."
),
):
abc_experiment.results_from_records(
Expand Down

0 comments on commit 11c2de6

Please sign in to comment.