Skip to content

Commit

Permalink
Fix counts and samples (#545)
Browse files Browse the repository at this point in the history
* Fix counts

* raise error in counts if avg_count is 1

* Improvement.

* Fix tests

* Add check.

---------

Co-authored-by: victor <visangim@gmail.com>
  • Loading branch information
AlbertMitjans and visagim authored Oct 1, 2023
1 parent 49ab56b commit a4e7d55
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
22 changes: 11 additions & 11 deletions src/qililab/result/qblox_results/qblox_bins_acquisitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ def counts(self) -> Counts:
raise IndexError("Sequencers must have the same number of bins to compute the counts.")
# TODO: Add limitations to check we are doing single-shot for multi qubit?
counts_object = Counts(n_qubits=len(self.bins))
for seq in self.bins:
if any(bin_avg > 1 for bin_avg in seq.avg_cnt):
raise ValueError("Counts cannot be used when using a hardware average higher than 1.")
for bin_idx in range(num_bins):
# The threshold inside of a qblox bin is the name they use for already classified data as a value between
# 0 and 1, not the value used in the comparator to perform such classification.
measurement_as_list = [int(bins_data.threshold[bin_idx]) for bins_data in self.bins]
measurement_as_list = [bins_data.threshold[bin_idx] for bins_data in self.bins]
measurement = "".join(str(bit) for bit in measurement_as_list)
counts_object.add_measurement(state=measurement)
return counts_object

def samples(self) -> np.ndarray:
"""Returns an array containing the measured samples.
"""Returns an array containing the samples measured in each bin of each sequencer.
The shape of the returned array is ``(# sequencers, # bins)``.
Returns:
np.ndarray: An array containing the measured samples (0 or 1).
np.ndarray: An array containing the samples measured in each bin of each sequencer.
"""
# Check that all sequencers have the same number of bins.
if any(len(seq_bins) != (num_bins := len(self.bins[0])) for seq_bins in self.bins):
raise IndexError("Sequencers must have the same number of bins to compute the samples.")
samples = []
for bin_idx in range(num_bins):
# The threshold inside of a qblox bin is the name they use for already classified data as a value between
# 0 and 1, not the value used in the comparator to perform such classification.
measurement_as_list = [int(bins_data.threshold[bin_idx]) for bins_data in self.bins]
samples.append(measurement_as_list)
if any(len(seq_bins) != len(self.bins[0]) for seq_bins in self.bins):
raise IndexError("Sequencers must have the same number of bins to return the samples.")
samples = [seq_data.threshold for seq_data in self.bins]
return np.array(samples)
16 changes: 8 additions & 8 deletions tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,8 @@ class Galadriel:
},
"bins": {
"integration": {"path0": [-0.08875841551660968], "path1": [-0.4252879595139228]},
"threshold": [0.48046875],
"avg_cnt": [1024],
"threshold": [0],
"avg_cnt": [1],
},
}
],
Expand All @@ -542,8 +542,8 @@ class Galadriel:
},
"bins": {
"integration": {"path0": [-0.14089025097703958], "path1": [-0.3594594414081583]},
"threshold": [0.4599609375],
"avg_cnt": [1024],
"threshold": [0],
"avg_cnt": [1],
},
}
],
Expand Down Expand Up @@ -576,8 +576,8 @@ class Galadriel:
},
"bins": {
"integration": {"path0": [-0.08875841551660968], "path1": [-0.4252879595139228]},
"threshold": [0.48046875],
"avg_cnt": [1024],
"threshold": [0],
"avg_cnt": [1],
},
}
],
Expand All @@ -593,8 +593,8 @@ class Galadriel:
},
"bins": {
"integration": {"path0": [-0.14089025097703958], "path1": [-0.3594594414081583]},
"threshold": [0.4599609375],
"avg_cnt": [1024],
"threshold": [0],
"avg_cnt": [1],
},
}
],
Expand Down
2 changes: 1 addition & 1 deletion tests/execution/test_execution_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def test_execute_method_with_keyboard_interrupt(
"bins": {
"integration": {"path0": [1, 2, 3], "path1": [4, 5, 6]},
"threshold": [1, 1, 1],
"avg_cnt": [1000, 1000, 1000],
"avg_cnt": [1, 1, 1],
},
},
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ def mock_instruments(mock_rs: MagicMock, mock_pulsar: MagicMock, mock_keithley:
"index": 0,
"acquisition": {
"scope": {
"path0": {"data": [1, 1, 1, 1, 1, 1, 1, 1], "out-of-range": False, "avg_cnt": 1000},
"path1": {"data": [0, 0, 0, 0, 0, 0, 0, 0], "out-of-range": False, "avg_cnt": 1000},
"path0": {"data": [1, 1, 1, 1, 1, 1, 1, 1], "out-of-range": False, "avg_cnt": 1},
"path1": {"data": [0, 0, 0, 0, 0, 0, 0, 0], "out-of-range": False, "avg_cnt": 1},
},
"bins": {
"integration": {"path0": [-0.08875841551660968], "path1": [-0.4252879595139228]},
"threshold": [0.48046875],
"avg_cnt": [1024],
"threshold": [0],
"avg_cnt": [1],
},
},
}
Expand Down

0 comments on commit a4e7d55

Please sign in to comment.