Skip to content

Commit 6ae4e00

Browse files
verultrht
authored andcommitted
Change GridDeviceMetadata qubit type to GridQubit (quantumlib#5633)
Proposal to change qubit types in `GridDeviceMetadata` to `GridQubit`, per @maffoo 's and @mpharrigan 's suggestion. Looks like `GridQid` is not a superclass of `GridQubit`. Also included a quick docstring fix to disambiguate from `cirq_google.GridDevice`. @MichaelBroughton WDYT?
1 parent 3ab8e6e commit 6ae4e00

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

cirq-core/cirq/devices/grid_device_metadata.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""Metadata subtype for 2D Homogenous devices."""
1515

16-
from typing import TYPE_CHECKING, Optional, FrozenSet, Iterable, Tuple, Dict
16+
from typing import TYPE_CHECKING, cast, Optional, FrozenSet, Iterable, Tuple, Dict
1717

1818
import networkx as nx
1919
from cirq import value
@@ -29,21 +29,21 @@ class GridDeviceMetadata(device.DeviceMetadata):
2929

3030
def __init__(
3131
self,
32-
qubit_pairs: Iterable[Tuple['cirq.Qid', 'cirq.Qid']],
32+
qubit_pairs: Iterable[Tuple['cirq.GridQubit', 'cirq.GridQubit']],
3333
gateset: 'cirq.Gateset',
3434
gate_durations: Optional[Dict['cirq.GateFamily', 'cirq.Duration']] = None,
35-
all_qubits: Optional[Iterable['cirq.Qid']] = None,
35+
all_qubits: Optional[Iterable['cirq.GridQubit']] = None,
3636
compilation_target_gatesets: Iterable['cirq.CompilationTargetGateset'] = (),
3737
):
3838
"""Create a GridDeviceMetadata object.
3939
40-
Create a GridDevice which has a well defined set of couplable
40+
Create a grid device which has a well defined set of couplable
4141
qubit pairs that have the same two qubit gates available in
4242
both coupling directions. Gate times (if provided) are expected
4343
to be uniform across all qubits on the device.
4444
4545
Args:
46-
qubit_pairs: Iterable of pairs of `cirq.Qid`s representing
46+
qubit_pairs: Iterable of pairs of `cirq.GridQubit`s representing
4747
bi-directional couplings.
4848
gateset: `cirq.Gateset` indicating gates supported
4949
everywhere on the device.
@@ -114,7 +114,16 @@ def __init__(
114114
self._gate_durations = gate_durations
115115

116116
@property
117-
def qubit_pairs(self) -> FrozenSet[FrozenSet['cirq.Qid']]:
117+
def qubit_set(self) -> FrozenSet['cirq.GridQubit']:
118+
"""Returns the set of grid qubits on the device.
119+
120+
Returns:
121+
Frozenset of qubits on device.
122+
"""
123+
return cast(FrozenSet['cirq.GridQubit'], super().qubit_set)
124+
125+
@property
126+
def qubit_pairs(self) -> FrozenSet[FrozenSet['cirq.GridQubit']]:
118127
"""Returns the set of all couple-able qubits on the device.
119128
120129
Each element in the outer frozenset is a 2-element frozenset representing a bidirectional
@@ -123,7 +132,7 @@ def qubit_pairs(self) -> FrozenSet[FrozenSet['cirq.Qid']]:
123132
return self._qubit_pairs
124133

125134
@property
126-
def isolated_qubits(self) -> FrozenSet['cirq.Qid']:
135+
def isolated_qubits(self) -> FrozenSet['cirq.GridQubit']:
127136
"""Returns the set of all isolated qubits on the device (if appliable)."""
128137
return self._isolated_qubits
129138

cirq-google/cirq_google/devices/serializable_device.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,22 @@ def __init__(
101101
self.qubits = qubits
102102
self.gate_definitions = gate_definitions
103103
has_subcircuit_support: bool = cirq.FrozenCircuit in gate_definitions
104+
104105
self._metadata = cirq.GridDeviceMetadata(
105-
qubit_pairs=[
106-
(pair[0], pair[1])
107-
for gate_defs in gate_definitions.values()
108-
for gate_def in gate_defs
109-
if gate_def.number_of_qubits == 2
110-
for pair in gate_def.target_set
111-
if len(pair) == 2 and pair[0] < pair[1]
112-
],
106+
qubit_pairs=cast(
107+
List[Tuple[cirq.GridQubit, cirq.GridQubit]],
108+
[
109+
(pair[0], pair[1])
110+
for gate_defs in gate_definitions.values()
111+
for gate_def in gate_defs
112+
if gate_def.number_of_qubits == 2
113+
for pair in gate_def.target_set
114+
if len(pair) == 2
115+
and pair[0] < pair[1]
116+
and isinstance(pair[0], cirq.GridQubit)
117+
and isinstance(pair[1], cirq.GridQubit)
118+
],
119+
),
113120
gateset=cirq.Gateset(
114121
*(g for g in gate_definitions.keys() if issubclass(g, cirq.Gate)),
115122
cirq.GlobalPhaseGate,

cirq-google/cirq_google/line/placement/anneal.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import cast, Callable, List, Optional, Tuple, Set, Any, TYPE_CHECKING
15+
from typing import Callable, List, Optional, Tuple, Set, Any, TYPE_CHECKING
1616

1717
import numpy as np
1818

@@ -37,7 +37,7 @@ def __init__(self, device: 'cirq_google.GridDevice', seed=None) -> None:
3737
device: Chip description.
3838
seed: Optional seed value for random number generator.
3939
"""
40-
self._c = cast(Set[cirq.GridQubit], device.metadata.qubit_set)
40+
self._c = device.metadata.qubit_set
4141
self._c_adj = chip_as_adjacency_list(device)
4242
self._rand = np.random.RandomState(seed)
4343

cirq-google/cirq_google/line/placement/chip.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import cast, Dict, List, Set, Tuple, TYPE_CHECKING
15+
from typing import Dict, List, Tuple, TYPE_CHECKING
1616

1717
import cirq
1818

@@ -86,7 +86,7 @@ def chip_as_adjacency_list(
8686
Map from nodes to list of qubits which represent all the neighbours of
8787
given qubit.
8888
"""
89-
c_set = cast(Set[cirq.GridQubit], device.metadata.qubit_set)
89+
c_set = device.metadata.qubit_set
9090
c_adj: Dict[cirq.GridQubit, List[cirq.GridQubit]] = {}
9191
for n in c_set:
9292
c_adj[n] = []

cirq-google/cirq_google/line/placement/greedy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import cast, Dict, List, Optional, Set, TYPE_CHECKING
15+
from typing import Dict, List, Optional, Set, TYPE_CHECKING
1616

1717
import abc
1818
import collections
@@ -304,7 +304,7 @@ def place_line(self, device: 'cirq_google.GridDevice', length: int) -> GridQubit
304304
if not device.metadata.qubit_set:
305305
return GridQubitLineTuple()
306306

307-
start: GridQubit = cast(GridQubit, min(device.metadata.qubit_set))
307+
start: GridQubit = min(device.metadata.qubit_set)
308308
sequences: List[LineSequence] = []
309309
greedy_search: Dict[str, List[GreedySequenceSearch]] = {
310310
'minimal_connectivity': [_PickFewestNeighbors(device, start)],

0 commit comments

Comments
 (0)