-
Notifications
You must be signed in to change notification settings - Fork 200
Change how pauli_keys are represented in CummulativeObservableAnnotation #982
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
d0edee1
1d06580
b4d5c02
814f013
1bfb296
879e4f9
03dbe13
b18f98c
219fc73
c5b148b
cb75a6d
d49664d
aae9330
5f7b8e4
5541833
5112318
f474a26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| from typing import Any, Dict, Iterable, List, Tuple | ||
| from typing import Any, Dict, Iterable, List, Mapping, Tuple | ||
|
|
||
| import cirq | ||
| import stim | ||
|
|
@@ -16,7 +16,7 @@ def __init__( | |
| *, | ||
| parity_keys: Iterable[str] = (), | ||
| relative_keys: Iterable[int] = (), | ||
| pauli_keys: Iterable[str] = (), | ||
| pauli_keys: Iterable[tuple[cirq.Qid, str]] | Iterable[str] = (), | ||
| observable_index: int, | ||
| ): | ||
| """ | ||
|
|
@@ -29,15 +29,31 @@ def __init__( | |
| """ | ||
| self.parity_keys = frozenset(parity_keys) | ||
| self.relative_keys = frozenset(relative_keys) | ||
| self.pauli_keys = frozenset(pauli_keys) | ||
| _pauli_keys = [] | ||
| for k in pauli_keys: | ||
| if isinstance(k, str): | ||
| # For backward compatibility | ||
| _pauli_keys.append((cirq.LineQubit(int(k[1:])), k[0])) | ||
| else: | ||
| _pauli_keys.append(tuple(k)) | ||
| self.pauli_keys = frozenset(_pauli_keys) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if a user has code
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you mean that changing the type of We could keep the type Anyway, again I'm open to whatever you think is the best solution here. Let me know!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main thing is that you must make the changes in a way that doesn't break existing code. Existing code is likely to depend on the type of public fields, so you cannot change their type. You can add other fields, though.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I tried a version of this. Let me know what you think!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this would still break existing code that is expecting the field to be of type Iterable[string].
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the The input pauli_keys now accepts a two input types:
I don't think this would break existing code. Although I'm happy to restructure differently (for example we could separate the input into two fields
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Existing code consuming this field may assume that I agree the constructor won't break, as it accepts a superset of what it used to. But for consumption code to remain correct the type must be a subset of what it used to be; not a superset. You likely need to introduce a different field to avoid this problem. |
||
| self.observable_index = observable_index | ||
|
|
||
| @property | ||
| def qubits(self) -> Tuple[cirq.Qid, ...]: | ||
| return () | ||
| return tuple(sorted(q for q, _ in self.pauli_keys)) | ||
|
|
||
| def with_qubits(self, *new_qubits) -> 'CumulativeObservableAnnotation': | ||
| return self | ||
| if len(self.qubits) == len(new_qubits): | ||
| pauli_map = dict(self.pauli_keys) | ||
| return CumulativeObservableAnnotation( | ||
| parity_keys=self.parity_keys, | ||
| relative_keys=self.relative_keys, | ||
| pauli_keys=tuple((new_q, pauli_map[q]) for new_q, q in zip(new_qubits, self.qubits)), | ||
| observable_index=self.observable_index, | ||
| ) | ||
|
|
||
| raise ValueError("Number of qubits does not match") | ||
|
|
||
| def _value_equality_values_(self) -> Any: | ||
| return self.parity_keys, self.relative_keys, self.pauli_keys, self.observable_index | ||
|
|
@@ -85,6 +101,7 @@ def _stim_conversion_( | |
| edit_measurement_key_lengths: List[Tuple[str, int]], | ||
| have_seen_loop: bool = False, | ||
| tag: str, | ||
| targets: list[int], | ||
| **kwargs, | ||
| ): | ||
| # Ideally these references would all be resolved ahead of time, to avoid the redundant | ||
|
|
@@ -109,10 +126,11 @@ def _stim_conversion_( | |
| rec_targets.append(stim.target_rec(-1 - offset)) | ||
| if not remaining: | ||
| break | ||
| pauli_map = dict(self.pauli_keys) | ||
| rec_targets.extend( | ||
| [ | ||
| stim.target_pauli(qubit_index=int(k[1:]), pauli=k[0]) | ||
| for k in sorted(self.pauli_keys) | ||
| stim.target_pauli(qubit_index=tid, pauli=pauli_map[q]) | ||
| for q, tid in zip(self.qubits, targets) | ||
| ] | ||
| ) | ||
| if remaining: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.