-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add control_keys protocol (simplified) (#4610)
Use control keys in circuit construction to block controlled gates "earliest" append method from falling back to before the measurement. Parts 5&7 of https://tinyurl.com/cirq-feedforward. (I grouped them together in a single PR so that circuit.append could be a POC that control_keys protocol design works as intended). Replaces/simplifies #4490 since we will handle "extern" control keys in subcircuits as a separate PR.
- Loading branch information
Showing
7 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2021 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Protocol for object that have control keys.""" | ||
|
||
from typing import AbstractSet, Any, Iterable, TYPE_CHECKING | ||
|
||
from typing_extensions import Protocol | ||
|
||
from cirq._doc import doc_private | ||
|
||
if TYPE_CHECKING: | ||
import cirq | ||
|
||
|
||
class SupportsControlKey(Protocol): | ||
"""An object that is a has a classical control key or keys. | ||
Control keys are used in referencing the results of a measurement. | ||
Users should implement `_control_keys_` returning an iterable of | ||
`MeasurementKey`. | ||
""" | ||
|
||
@doc_private | ||
def _control_keys_(self) -> Iterable['cirq.MeasurementKey']: | ||
"""Return the keys for controls referenced by the receiving object. | ||
Returns: | ||
The measurement keys the value is controlled by. If the value is not | ||
classically controlled, the result is the empty tuple. | ||
""" | ||
|
||
|
||
def control_keys(val: Any) -> AbstractSet['cirq.MeasurementKey']: | ||
"""Gets the keys that the value is classically controlled by. | ||
Args: | ||
val: The object that may be classically controlled. | ||
Returns: | ||
The measurement keys the value is controlled by. If the value is not | ||
classically controlled, the result is the empty tuple. | ||
""" | ||
getter = getattr(val, '_control_keys_', None) | ||
result = NotImplemented if getter is None else getter() | ||
if result is not NotImplemented and result is not None: | ||
return set(result) | ||
|
||
return set() |
This file contains 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2021 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import cirq | ||
|
||
|
||
def test_control_key(): | ||
class Named: | ||
def _control_keys_(self): | ||
return [cirq.MeasurementKey('key')] | ||
|
||
class NoImpl: | ||
def _control_keys_(self): | ||
return NotImplemented | ||
|
||
assert cirq.control_keys(Named()) == {cirq.MeasurementKey('key')} | ||
assert not cirq.control_keys(NoImpl()) | ||
assert not cirq.control_keys(5) |
This file contains 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