1# Copyright 2019 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#      http://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.
14import pytest
15
16import cirq
17from cirq.interop.quirk.cells.cell import Cell, ExplicitOperationsCell
18
19
20def test_cell_defaults():
21    class BasicCell(Cell):
22        def with_line_qubits_mapped_to(self, qubits):
23            raise NotImplementedError()
24
25        def gate_count(self) -> int:
26            raise NotImplementedError()
27
28    c = BasicCell()
29    assert c.operations() == ()
30    assert c.basis_change() == ()
31    assert c.controlled_by(cirq.LineQubit(0)) is c
32    x = []
33    c.modify_column(x)
34    assert x == []
35
36
37def test_cell_replace_utils():
38    a, b, c = cirq.NamedQubit.range(3, prefix='q')
39    assert Cell._replace_qubit(cirq.LineQubit(1), [a, b, c]) == b
40    with pytest.raises(ValueError, match='only map from line qubits'):
41        _ = Cell._replace_qubit(cirq.GridQubit(0, 0), [a, b, c])
42    with pytest.raises(ValueError, match='not in range'):
43        _ = Cell._replace_qubit(cirq.LineQubit(-1), [a, b, c])
44    with pytest.raises(ValueError, match='not in range'):
45        _ = Cell._replace_qubit(cirq.LineQubit(999), [a, b, c])
46
47
48def test_explicit_operations_cell_equality():
49    a = cirq.LineQubit(0)
50    eq = cirq.testing.EqualsTester()
51    eq.add_equality_group(ExplicitOperationsCell([], []), ExplicitOperationsCell([]))
52    eq.add_equality_group(ExplicitOperationsCell([cirq.X(a)], []))
53    eq.add_equality_group(ExplicitOperationsCell([], [cirq.Y(a)]))
54
55
56def test_explicit_operations_cell():
57    a, b = cirq.LineQubit.range(2)
58    v = ExplicitOperationsCell([cirq.X(a)], [cirq.S(a)])
59    assert v.operations() == (cirq.X(a),)
60    assert v.basis_change() == (cirq.S(a),)
61    assert v.controlled_by(b) == ExplicitOperationsCell([cirq.X(a).controlled_by(b)], [cirq.S(a)])
62