1# Copyright 2018 The Cirq Developers
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#         https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from typing import Iterable
16
17from cirq.devices import GridQubit
18from cirq_google.line.placement.chip import chip_as_adjacency_list, above, below, right_of, left_of
19from cirq_google import XmonDevice
20from cirq.value import Duration
21
22
23def test_neighbours():
24    qubit = GridQubit(0, 0)
25    assert above(qubit) == GridQubit(0, -1)
26    assert below(qubit) == GridQubit(0, 1)
27    assert right_of(qubit) == GridQubit(1, 0)
28    assert left_of(qubit) == GridQubit(-1, 0)
29
30
31def test_qubit_not_mutated():
32    qubit = GridQubit(0, 0)
33
34    above(qubit)
35    assert qubit == GridQubit(0, 0)
36
37    below(qubit)
38    assert qubit == GridQubit(0, 0)
39
40    right_of(qubit)
41    assert qubit == GridQubit(0, 0)
42
43    left_of(qubit)
44    assert qubit == GridQubit(0, 0)
45
46
47def _create_device(qubits: Iterable[GridQubit]) -> XmonDevice:
48    return XmonDevice(Duration(nanos=0), Duration(nanos=0), Duration(nanos=0), qubits)
49
50
51def test_empty():
52    assert chip_as_adjacency_list(_create_device([])) == {}
53
54
55def test_single_qubit():
56    q00 = GridQubit(0, 0)
57    assert chip_as_adjacency_list(_create_device([q00])) == {q00: []}
58
59
60def test_two_close_qubits():
61    q00 = GridQubit(0, 0)
62    q01 = GridQubit(0, 1)
63    assert chip_as_adjacency_list(_create_device([q00, q01])) == {q00: [q01], q01: [q00]}
64
65
66def test_two_qubits_apart():
67    q00 = GridQubit(0, 0)
68    q11 = GridQubit(1, 1)
69    assert chip_as_adjacency_list(_create_device([q00, q11])) == {q00: [], q11: []}
70
71
72def test_three_qubits_in_row():
73    q00 = GridQubit(0, 0)
74    q01 = GridQubit(0, 1)
75    q02 = GridQubit(0, 2)
76    assert chip_as_adjacency_list(_create_device([q00, q01, q02])) == {
77        q00: [q01],
78        q01: [q00, q02],
79        q02: [q01],
80    }
81
82
83def test_square_of_four():
84    q00 = GridQubit(0, 0)
85    q01 = GridQubit(0, 1)
86    q10 = GridQubit(1, 0)
87    q11 = GridQubit(1, 1)
88    assert chip_as_adjacency_list(_create_device([q00, q01, q10, q11])) == {
89        q00: [q01, q10],
90        q01: [q00, q11],
91        q10: [q00, q11],
92        q11: [q10, q01],
93    }
94