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
15import pytest
16
17import cirq
18from cirq_google.line.placement.sequence import GridQubitLineTuple, NotFoundError
19
20
21def test_best_of_gets_longest_needs_minimum():
22    q00 = cirq.GridQubit(0, 0)
23    q01 = cirq.GridQubit(0, 1)
24
25    assert GridQubitLineTuple.best_of([[]], 0) == ()
26    assert GridQubitLineTuple.best_of([[], [q00]], 0) == ()
27    assert GridQubitLineTuple.best_of([[q00], []], 0) == ()
28    assert GridQubitLineTuple.best_of([[], [q00]], 1) == (q00,)
29    assert GridQubitLineTuple.best_of([[q00], []], 1) == (q00,)
30    assert GridQubitLineTuple.best_of([[q00, q01], [q00]], 1) == (q00,)
31    assert GridQubitLineTuple.best_of([[q00, q01], [q00]], 2) == (q00, q01)
32    assert GridQubitLineTuple.best_of([[q00, q01]], 2) == (q00, q01)
33
34    assert GridQubitLineTuple.best_of([], 0) == ()
35    with pytest.raises(NotFoundError):
36        _ = GridQubitLineTuple.best_of([[]], 1)
37    with pytest.raises(NotFoundError):
38        _ = GridQubitLineTuple.best_of([[q00]], 2)
39
40
41def test_line_placement_str():
42    q00 = cirq.GridQubit(0, 0)
43    q01 = cirq.GridQubit(0, 1)
44    q02 = cirq.GridQubit(0, 2)
45    placement = GridQubitLineTuple([q00, q01, q02])
46    assert (
47        str(placement).strip()
48        == """
49(0, 0)━━(0, 1)━━(0, 2)
50    """.strip()
51    )
52
53
54def test_line_placement_to_str():
55    q00 = cirq.GridQubit(0, 0)
56    q01 = cirq.GridQubit(0, 1)
57    q02 = cirq.GridQubit(0, 2)
58    q10 = cirq.GridQubit(1, 0)
59    q11 = cirq.GridQubit(1, 1)
60    placement = GridQubitLineTuple([q02, q01, q00, q10, q11])
61    assert (
62        str(placement).strip()
63        == """
64(0, 0)━━(0, 1)━━(0, 2)
6566(1, 0)━━(1, 1)
67    """.strip()
68    )
69