Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix Cirq Datasets and LGST Bug #420

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions pygsti/algorithms/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ def run_lgst(dataset, prep_fiducials, effect_fiducials, target_model, op_labels=
circuit = rhostr
dsRow_fractions = dataset[circuit].fractions
# outcome labels should just be effect labels (no instruments!)
EVec[0, i] = dsRow_fractions[(effectLabel,)]
# when using a sparse data set format it might not be the case
# that all effect labels are present (only ones with non-zero counts are)
# so return 0 for the fraction in that case.
EVec[0, i] = dsRow_fractions.get((effectLabel,), 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, exactly the bugfix I was thinking of!

EVec_p = _np.dot(_np.dot(EVec, Vd), Pj) # truncate Evec => Evec', shape (1,trunc)
povm_effects.append((effectLabel, _np.transpose(EVec_p)))
lgstModel.povms[povmLabel] = _povm.UnconstrainedPOVM(povm_effects, evotype='default')
Expand All @@ -262,7 +265,10 @@ def run_lgst(dataset, prep_fiducials, effect_fiducials, target_model, op_labels=
# try without prepLabel since it will be the default
circuit = estr
dsRow_fractions = dataset[circuit].fractions
rhoVec[eoff:eoff + povmLen, 0] = [dsRow_fractions[(ol,)] for ol in target_model.povms[povmLbl]]
# when using a sparse data set format it might not be the case
# that all effect labels are present (only ones with non-zero counts are)
# so return 0 for the fraction in that case.
rhoVec[eoff:eoff + povmLen, 0] = [dsRow_fractions.get((ol,),0) for ol in target_model.povms[povmLbl]]
eoff += povmLen
rhoVec_p = _np.dot(Pjt, _np.dot(Ud, rhoVec)) # truncate rhoVec => rhoVec', shape (trunc, 1)
rhoVec_p = _np.dot(invABMat_p, rhoVec_p)
Expand Down
23 changes: 21 additions & 2 deletions pygsti/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ def add_count_arrays(self, circuit, outcome_index_array, count_array,
self._add_raw_arrays(circuit, outcome_index_array, time_array, count_array,
overwriteExisting, record_zero_counts, aux)

def add_cirq_trial_result(self, circuit, trial_result, key):
def add_cirq_trial_result(self, circuit, trial_result, key, convert_int_to_binary = True, num_qubits = None):
"""
Add a single circuit's counts --- stored in a Cirq TrialResult --- to this DataSet

Expand All @@ -1619,6 +1619,16 @@ def add_cirq_trial_result(self, circuit, trial_result, key):
key : str
The string key of the measurement. Set by cirq.measure.

convert_int_to_binary : bool, optional (defaut True)
By default the keys in the cirq Results object are the integers representing
the bitstrings of the measurements on a set of qubits, in big-endian convention.
If True this uses the cirq function `cirq.big_endian_int_to_bits` to convert back
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if we were going to get bit by endian-ness... Nice job covering that edge case!

to a binary string before adding the counts as a entry into the pygsti dataset.

num_qubits : int, optional (default None)
Number of qubits used in the conversion from integers to binary when convert_int_to_binary
is True. If None, then the number of line_labels on the input circuit is used.

Returns
-------
None
Expand All @@ -1631,8 +1641,17 @@ def add_cirq_trial_result(self, circuit, trial_result, key):

# TrialResult.histogram returns a collections.Counter object, which is a subclass of dict.
histogram_counter = trial_result.histogram(key=key)

if num_qubits is None:
num_qubits = len(circuit.line_labels)

# The keys in histogram_counter are integers, but pyGSTi likes dictionary keys to be strings.
count_dict = {str(key): value for key, value in histogram_counter.items()}
count_dict = {}
for key, value in histogram_counter.items():
if convert_int_to_binary:
count_dict[_np.binary_repr(key, width= num_qubits)] = value
else:
count_dict[str(key)] = value
self.add_count_dict(circuit, count_dict)

def add_raw_series_data(self, circuit, outcome_label_list, time_stamp_list,
Expand Down