-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add optional qubit_set property to cirq.Device #2605
Changes from 1 commit
9ccde42
c6a0522
537b2f9
d629fe8
1387882
3233a0d
d11d72c
53c251d
fd1098c
3721ced
6270608
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 |
---|---|---|
|
@@ -12,20 +12,44 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import abc | ||
|
||
from typing import TYPE_CHECKING, Optional, AbstractSet | ||
|
||
if TYPE_CHECKING: | ||
import cirq | ||
|
||
# Note: circuit/schedule types specified by name to avoid circular references. | ||
|
||
|
||
class Device(metaclass=abc.ABCMeta): | ||
"""Hardware constraints for validating circuits.""" | ||
|
||
def qubit_set(self) -> Optional[AbstractSet['cirq.Qid']]: | ||
"""Returns a set or frozenset of qubits on the device, if possible. | ||
|
||
This method returns None when the set of qubits is unavailable. For | ||
example, `cirq.UnconstrainedDevice` returns `None` because allows any | ||
and all qubits; it has no finite qubit set to return. | ||
|
||
Returns: | ||
If the device has a finite set of qubits, then a set or frozen set | ||
of all qubits on the device is returned. | ||
|
||
If the device has no well defined finite set of qubits (e.g. | ||
`cirq.UnconstrainedDevice` has this property), then `None` is | ||
returned. | ||
""" | ||
|
||
# Compatibility hack to work with devices that were written before this | ||
# method was defined. | ||
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. As @dstrain115 mentioned, it'd be nice to have a plan for where we are going with 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. Acknowledged. |
||
for name in ['qubits', '_qubits']: | ||
if hasattr(self, name): | ||
val = getattr(self, name) | ||
if callable(val): | ||
val = val() | ||
return frozenset(val) | ||
|
||
# Default to the qubits being unknown. | ||
return None | ||
|
||
def decompose_operation(self, operation: 'cirq.Operation' | ||
) -> 'cirq.OP_TREE': | ||
"""Returns a device-valid decomposition for the given operation. | ||
|
@@ -36,7 +60,6 @@ def decompose_operation(self, operation: 'cirq.Operation' | |
""" | ||
return operation | ||
|
||
@abc.abstractmethod | ||
def validate_operation(self, operation: 'cirq.Operation') -> None: | ||
"""Raises an exception if an operation is not valid. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
|
||
import cirq | ||
|
||
|
||
def test_qubit_set(): | ||
|
||
class RawDevice(cirq.Device): | ||
pass | ||
|
||
assert RawDevice().qubit_set() is None | ||
|
||
class QubitFieldDevice(cirq.Device): | ||
|
||
def __init__(self): | ||
self.qubits = cirq.LineQubit.range(3) | ||
|
||
assert QubitFieldDevice().qubit_set() == frozenset(cirq.LineQubit.range(3)) | ||
|
||
class PrivateQubitFieldDevice(cirq.Device): | ||
|
||
def __init__(self): | ||
self._qubits = cirq.LineQubit.range(4) | ||
|
||
assert PrivateQubitFieldDevice().qubit_set() == frozenset( | ||
cirq.LineQubit.range(4)) | ||
|
||
class QubitMethodDevice(cirq.Device): | ||
|
||
def qubits(self): | ||
return cirq.LineQubit.range(5) | ||
|
||
assert QubitMethodDevice().qubit_set() == frozenset(cirq.LineQubit.range(5)) | ||
|
||
class PrivateQubitMethodDevice(cirq.Device): | ||
|
||
def qubits(self): | ||
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. Should be 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. Done. |
||
return cirq.LineQubit.range(6) | ||
|
||
assert PrivateQubitMethodDevice().qubit_set() == frozenset( | ||
cirq.LineQubit.range(6)) |
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.
This is redundant with the Returns statement. I would probably keep one or the other rather than repeating it.
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.
Done.