-
Notifications
You must be signed in to change notification settings - Fork 1.2k
CircuitOperation: change use_repetition_ids default to False #6910
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb4bc11
CircuitOperation: change use_repetition_ids default to False
maffoo 219bdaf
Remove debug print
maffoo 15fda18
Fix circuit diagrams in tests
maffoo 3ea9493
Clarify comment
maffoo 8e28d22
Merge branch 'main' into u/maffoo/rep-ids
maffoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -89,7 +89,7 @@ def __init__( | |||||
| repetition_ids: Optional[Sequence[str]] = None, | ||||||
| parent_path: Tuple[str, ...] = (), | ||||||
| extern_keys: FrozenSet['cirq.MeasurementKey'] = frozenset(), | ||||||
| use_repetition_ids: bool = True, | ||||||
| use_repetition_ids: Optional[bool] = None, | ||||||
| repeat_until: Optional['cirq.Condition'] = None, | ||||||
| ): | ||||||
| """Initializes a CircuitOperation. | ||||||
|
|
@@ -156,6 +156,8 @@ def __init__( | |||||
| # Ensure that the circuit is invertible if the repetitions are negative. | ||||||
| self._repetitions = repetitions | ||||||
| self._repetition_ids = None if repetition_ids is None else list(repetition_ids) | ||||||
| if use_repetition_ids is None: | ||||||
| use_repetition_ids = repetition_ids is not None | ||||||
| self._use_repetition_ids = use_repetition_ids | ||||||
| if isinstance(self._repetitions, float): | ||||||
| if math.isclose(self._repetitions, round(self._repetitions)): | ||||||
|
|
@@ -263,7 +265,7 @@ def replace(self, **changes) -> 'cirq.CircuitOperation': | |||||
| 'repetition_ids': self.repetition_ids, | ||||||
| 'parent_path': self.parent_path, | ||||||
| 'extern_keys': self._extern_keys, | ||||||
| 'use_repetition_ids': self.use_repetition_ids, | ||||||
| 'use_repetition_ids': True if 'repetition_ids' in changes else self.use_repetition_ids, | ||||||
| 'repeat_until': self.repeat_until, | ||||||
| **changes, | ||||||
| } | ||||||
|
|
@@ -448,11 +450,9 @@ def __repr__(self): | |||||
| args += f'param_resolver={proper_repr(self.param_resolver)},\n' | ||||||
| if self.parent_path: | ||||||
| args += f'parent_path={proper_repr(self.parent_path)},\n' | ||||||
| if self.repetition_ids != self._default_repetition_ids(): | ||||||
| if self.use_repetition_ids: | ||||||
| # Default repetition_ids need not be specified. | ||||||
| args += f'repetition_ids={proper_repr(self.repetition_ids)},\n' | ||||||
| if not self.use_repetition_ids: | ||||||
| args += 'use_repetition_ids=False,\n' | ||||||
| if self.repeat_until: | ||||||
| args += f'repeat_until={self.repeat_until!r},\n' | ||||||
| indented_args = args.replace('\n', '\n ') | ||||||
|
|
@@ -477,14 +477,12 @@ def dict_str(d: Mapping) -> str: | |||||
| args.append(f'params={self.param_resolver.param_dict}') | ||||||
| if self.parent_path: | ||||||
| args.append(f'parent_path={self.parent_path}') | ||||||
| if self.repetition_ids != self._default_repetition_ids(): | ||||||
| if self.use_repetition_ids: | ||||||
| # Default repetition_ids need not be specified. | ||||||
| args.append(f'repetition_ids={self.repetition_ids}') | ||||||
| elif self.repetitions != 1: | ||||||
| # Only add loops if we haven't added repetition_ids. | ||||||
| args.append(f'loops={self.repetitions}') | ||||||
| if not self.use_repetition_ids: | ||||||
| args.append('no_rep_ids') | ||||||
| if self.repeat_until: | ||||||
| args.append(f'until={self.repeat_until}') | ||||||
| if not args: | ||||||
|
|
@@ -529,10 +527,9 @@ def _json_dict_(self): | |||||
| 'measurement_key_map': self.measurement_key_map, | ||||||
| 'param_resolver': self.param_resolver, | ||||||
| 'repetition_ids': self.repetition_ids, | ||||||
| 'use_repetition_ids': self.use_repetition_ids, | ||||||
| 'parent_path': self.parent_path, | ||||||
| } | ||||||
| if not self.use_repetition_ids: | ||||||
| resp['use_repetition_ids'] = False | ||||||
| if self.repeat_until: | ||||||
| resp['repeat_until'] = self.repeat_until | ||||||
| return resp | ||||||
|
|
@@ -566,7 +563,10 @@ def _from_json_dict_( | |||||
| # Methods for constructing a similar object with one field modified. | ||||||
|
|
||||||
| def repeat( | ||||||
| self, repetitions: Optional[IntParam] = None, repetition_ids: Optional[Sequence[str]] = None | ||||||
| self, | ||||||
| repetitions: Optional[IntParam] = None, | ||||||
| repetition_ids: Optional[Sequence[str]] = None, | ||||||
| use_repetition_ids: Optional[bool] = None, | ||||||
| ) -> 'CircuitOperation': | ||||||
| """Returns a copy of this operation repeated 'repetitions' times. | ||||||
| Each repetition instance will be identified by a single repetition_id. | ||||||
|
|
@@ -577,6 +577,10 @@ def repeat( | |||||
| defaults to the length of `repetition_ids`. | ||||||
| repetition_ids: List of IDs, one for each repetition. If unset, | ||||||
| defaults to `default_repetition_ids(repetitions)`. | ||||||
| use_repetition_ids: If given, this specifies the value for `use_repetition_ids` | ||||||
| of the resulting circuit operation. If the not given, we enable | ||||||
|
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.
Suggested change
Contributor
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. Fixed. |
||||||
| ids if `repetition_ids` is not None, and otherwise fall back to | ||||||
| `self.use_repetition_ids`. | ||||||
|
|
||||||
| Returns: | ||||||
| A copy of this operation repeated `repetitions` times with the | ||||||
|
|
@@ -591,6 +595,9 @@ def repeat( | |||||
| ValueError: Unexpected length of `repetition_ids`. | ||||||
| ValueError: Both `repetitions` and `repetition_ids` are None. | ||||||
| """ | ||||||
| if use_repetition_ids is None: | ||||||
| use_repetition_ids = True if repetition_ids is not None else self.use_repetition_ids | ||||||
|
|
||||||
| if repetitions is None: | ||||||
| if repetition_ids is None: | ||||||
| raise ValueError('At least one of repetitions and repetition_ids must be set') | ||||||
|
|
@@ -604,7 +611,7 @@ def repeat( | |||||
| expected_repetition_id_length: int = np.abs(repetitions) | ||||||
|
|
||||||
| if repetition_ids is None: | ||||||
| if self.use_repetition_ids: | ||||||
| if use_repetition_ids: | ||||||
| repetition_ids = default_repetition_ids(expected_repetition_id_length) | ||||||
| elif len(repetition_ids) != expected_repetition_id_length: | ||||||
| raise ValueError( | ||||||
|
|
@@ -617,7 +624,11 @@ def repeat( | |||||
|
|
||||||
| # The eventual number of repetitions of the returned CircuitOperation. | ||||||
| final_repetitions = protocols.mul(self.repetitions, repetitions) | ||||||
| return self.replace(repetitions=final_repetitions, repetition_ids=repetition_ids) | ||||||
| return self.replace( | ||||||
| repetitions=final_repetitions, | ||||||
| repetition_ids=repetition_ids, | ||||||
| use_repetition_ids=use_repetition_ids, | ||||||
| ) | ||||||
|
|
||||||
| def __pow__(self, power: IntParam) -> 'cirq.CircuitOperation': | ||||||
| return self.repeat(power) | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit - consider updating the
use_repetition_idsdocstring below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.