We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
bug in supremacy test circuit
I ran the function
```
generate_boixo_2018_supremacy_circuits_v2_bristlecone(
        n_rows: int, cz_depth: int, seed: int) -> circuits.Circuit
```
in `cirq.experiments.google_v2_supremacy_circuit` with `n_rows = 1` and got trapped in infinite loops.

I think this is because when we have `n_rows = 1`,  `_make_cz_layer` would never return any cz gate, since there's only one qubit, thus in ` _add_cz_layer`, the loop
```
while not cz_layer:
        qubits = cast(Iterable[devices.GridQubit], circuit.all_qubits())
        cz_layer = list(_make_cz_layer(qubits, layer_index))
        layer_index += 1
```
would never end.

My suggestion would be change `assert 1 <= n_rows <= 11` in `generate_boixo_2018_supremacy_circuits_v2_bristlecone` to `assert 2 <= n_rows <= 11`, since it does not make anysense to have one-qubit cz layer in the first place.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `cirq/experiments/google_v2_supremacy_circuit.py`
Content:
```
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 
15 import random
16 from typing import Callable, Iterable, TypeVar, cast, Sequence
17 
18 from cirq.circuits import InsertStrategy
19 from cirq import circuits, devices, google, ops
20 
21 
22 def generate_boixo_2018_supremacy_circuits_v2(
23         qubits: Iterable[devices.GridQubit], cz_depth: int,
24         seed: int) -> circuits.Circuit:
25     """
26     Generates Google Random Circuits v2 as in github.com/sboixo/GRCS cz_v2.
27     See also https://arxiv.org/abs/1807.10749
28 
29     Args:
30         qubits: qubit grid in which to generate the circuit.
31         cz_depth: number of layers with CZ gates.
32         seed: seed for the random instance.
33 
34     Returns:
35         A circuit corresponding to instance
36         inst_{n_rows}x{n_cols}_{cz_depth+1}_{seed}
37 
38     The mapping of qubits is cirq.GridQubit(j,k) -> q[j*n_cols+k]
39     (as in the QASM mapping)
40     """
41 
42     non_diagonal_gates = [ops.pauli_gates.X**(1/2), ops.pauli_gates.Y**(1/2)]
43     rand_gen = random.Random(seed).random
44 
45     circuit = circuits.Circuit()
46 
47     # Add an initial moment of Hadamards
48     circuit.append(ops.common_gates.H(qubit) for qubit in qubits)
49 
50     layer_index = 0
51     if cz_depth:
52         layer_index = _add_cz_layer(layer_index, circuit)
53         # In the first moment, add T gates when possible
54         for qubit in qubits:
55             if not circuit.operation_at(qubit, 1):
56                 circuit.append(ops.common_gates.T(qubit),
57                                strategy=InsertStrategy.EARLIEST)
58 
59     for moment_index in range(2, cz_depth+1):
60         layer_index = _add_cz_layer(layer_index, circuit)
61         # Add single qubit gates in the same moment
62         for qubit in qubits:
63             if not circuit.operation_at(qubit, moment_index):
64                 last_op = circuit.operation_at(qubit, moment_index-1)
65                 if last_op:
66                     gate = cast(ops.GateOperation, last_op).gate
67                     # Add a random non diagonal gate after a CZ
68                     if gate == ops.CZ:
69                         circuit.append(_choice(rand_gen,
70                                                non_diagonal_gates).on(qubit),
71                                        strategy=InsertStrategy.EARLIEST)
72                     # Add a T gate after a non diagonal gate
73                     elif not gate == ops.T:
74                         circuit.append(ops.common_gates.T(qubit),
75                                        strategy=InsertStrategy.EARLIEST)
76 
77     # Add a final moment of Hadamards
78     circuit.append([ops.common_gates.H(qubit) for qubit in qubits],
79                    strategy=InsertStrategy.NEW_THEN_INLINE)
80 
81     return circuit
82 
83 
84 def generate_boixo_2018_supremacy_circuits_v2_grid(n_rows: int, n_cols: int,
85                                                    cz_depth: int, seed: int
86                                                   ) -> circuits.Circuit:
87     """
88     Generates Google Random Circuits v2 as in github.com/sboixo/GRCS cz_v2.
89     See also https://arxiv.org/abs/1807.10749
90 
91     Args:
92         n_rows: number of rows of a 2D lattice.
93         n_cols: number of columns.
94         cz_depth: number of layers with CZ gates.
95         seed: seed for the random instance.
96 
97     Returns:
98         A circuit corresponding to instance
99         inst_{n_rows}x{n_cols}_{cz_depth+1}_{seed}
100 
101     The mapping of qubits is cirq.GridQubit(j,k) -> q[j*n_cols+k]
102     (as in the QASM mapping)
103     """
104     qubits = [devices.GridQubit(i, j) for i in range(n_rows)
105               for j in range(n_cols)]
106     return generate_boixo_2018_supremacy_circuits_v2(qubits, cz_depth, seed)
107 
108 
109 def generate_boixo_2018_supremacy_circuits_v2_bristlecone(
110         n_rows: int, cz_depth: int, seed: int) -> circuits.Circuit:
111     """
112     Generates Google Random Circuits v2 in Bristlecone.
113     See also https://arxiv.org/abs/1807.10749
114 
115     Args:
116         n_rows: number of rows in a Bristlecone lattice.
117           Note that we do not include single qubit corners.
118         cz_depth: number of layers with CZ gates.
119         seed: seed for the random instance.
120 
121     Returns:
122         A circuit with given size and seed.
123     """
124     def get_qubits(n_rows):
125         def count_neighbors(qubits, qubit):
126             """Counts the qubits that the given qubit can interact with."""
127             possibles = [
128                 devices.GridQubit(qubit.row + 1, qubit.col),
129                 devices.GridQubit(qubit.row - 1, qubit.col),
130                 devices.GridQubit(qubit.row, qubit.col + 1),
131                 devices.GridQubit(qubit.row, qubit.col - 1),
132                 ]
133             return len(list(e for e in possibles if e in qubits))
134 
135         assert 1 <= n_rows <= 11
136         max_row = n_rows - 1
137         dev = google.Bristlecone
138         # we need a consistent order of qubits
139         qubits = list(dev.qubits)
140         qubits.sort()
141         qubits = [q for q in qubits
142                       if  q.row <= max_row and  q.row + q.col < n_rows + 6
143                       and q.row - q.col < n_rows - 5]
144         qubits = [q for q in qubits if count_neighbors(qubits, q) > 1]
145         return qubits
146 
147     qubits = get_qubits(n_rows)
148     return generate_boixo_2018_supremacy_circuits_v2(qubits, cz_depth, seed)
149 
150 
151 T = TypeVar('T')
152 
153 
154 def _choice(rand_gen: Callable[[], float], sequence: Sequence[T]) -> T:
155     """Choose a random element from a non-empty sequence.
156 
157     Use this instead of random.choice, with random.random(), for reproducibility
158     """
159     return sequence[int(rand_gen() * len(sequence))]
160 
161 
162 def _add_cz_layer(layer_index: int, circuit: circuits.Circuit) -> int:
163     cz_layer = None
164     while not cz_layer:
165         qubits = cast(Iterable[devices.GridQubit], circuit.all_qubits())
166         cz_layer = list(_make_cz_layer(qubits, layer_index))
167         layer_index += 1
168 
169     circuit.append(cz_layer, strategy=InsertStrategy.NEW_THEN_INLINE)
170     return layer_index
171 
172 
173 def _make_cz_layer(qubits: Iterable[devices.GridQubit], layer_index: int
174                    ) -> Iterable[ops.Operation]:
175     """
176     Each layer index corresponds to a shift/transpose of this CZ pattern:
177 
178         ●───●   ●   ●   ●───●   ●   ● . . .
179 
180         ●   ●   ●───●   ●   ●   ●───● . . .
181 
182         ●───●   ●   ●   ●───●   ●   ● . . .
183 
184         ●   ●   ●───●   ●   ●   ●───● . . .
185 
186         ●───●   ●   ●   ●───●   ●   ● . . .
187 
188         ●   ●   ●───●   ●   ●   ●───● . . .
189         .   .   .   .   .   .   .   . .
190         .   .   .   .   .   .   .   .   .
191         .   .   .   .   .   .   .   .     .
192 
193     Labelled edges, showing the exact index-to-CZs mapping (mod 8):
194 
195          ●─0─●─2─●─4─●─6─●─0─. . .
196         3│  7│  3│  7│  3│
197          ●─4─●─6─●─0─●─2─●─4─. . .
198         1│  5│  1│  5│  1│
199          ●─0─●─2─●─4─●─6─●─0─. . .
200         7│  3│  7│  3│  7│
201          ●─4─●─6─●─0─●─2─●─4─. . .
202         5│  1│  5│  1│  5│
203          ●─0─●─2─●─4─●─6─●─0─. . .
204         3│  7│  3│  7│  3│
205          .   .   .   .   .   .
206          .   .   .   .   .     .
207          .   .   .   .   .       .
208 
209     Note that, for small devices, some layers will be empty because the layer
210     only contains edges not present on the device.
211     """
212 
213     # map to an internal layer index to match the cycle order of public circuits
214     layer_index_map = [0, 3, 2, 1, 4, 7, 6, 5]
215     internal_layer_index = layer_index_map[layer_index % 8]
216 
217     dir_row = internal_layer_index % 2
218     dir_col = 1 - dir_row
219     shift = (internal_layer_index >> 1) % 4
220 
221     for q in qubits:
222         q2 = devices.GridQubit(q.row + dir_row, q.col + dir_col)
223         if q2 not in qubits:
224             continue  # This edge isn't on the device.
225         if (q.row * (2 - dir_row) + q.col * (2 - dir_col)) % 4 != shift:
226             continue  # No CZ along this edge for this layer.
227 
228         yield ops.common_gates.CZ(q, q2)
229
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/cirq/experiments/google_v2_supremacy_circuit.py b/cirq/experiments/google_v2_supremacy_circuit.py
--- a/cirq/experiments/google_v2_supremacy_circuit.py
+++ b/cirq/experiments/google_v2_supremacy_circuit.py
@@ -132,7 +132,7 @@
                 ]
             return len(list(e for e in possibles if e in qubits))
 
-        assert 1 <= n_rows <= 11
+        assert 2 <= n_rows <= 11
         max_row = n_rows - 1
         dev = google.Bristlecone
         # we need a consistent order of qubits

{"golden_diff": "diff --git a/cirq/experiments/google_v2_supremacy_circuit.py b/cirq/experiments/google_v2_supremacy_circuit.py\n--- a/cirq/experiments/google_v2_supremacy_circuit.py\n+++ b/cirq/experiments/google_v2_supremacy_circuit.py\n@@ -132,7 +132,7 @@\n                 ]\n             return len(list(e for e in possibles if e in qubits))\n \n-        assert 1 <= n_rows <= 11\n+        assert 2 <= n_rows <= 11\n         max_row = n_rows - 1\n         dev = google.Bristlecone\n         # we need a consistent order of qubits\n", "issue": "bug in supremacy test circuit\nI ran the function\r\n```\r\ngenerate_boixo_2018_supremacy_circuits_v2_bristlecone(\r\n        n_rows: int, cz_depth: int, seed: int) -> circuits.Circuit\r\n```\r\nin `cirq.experiments.google_v2_supremacy_circuit` with `n_rows = 1` and got trapped in infinite loops.\r\n\r\nI think this is because when we have `n_rows = 1`,  `_make_cz_layer` would never return any cz gate, since there's only one qubit, thus in ` _add_cz_layer`, the loop\r\n```\r\nwhile not cz_layer:\r\n        qubits = cast(Iterable[devices.GridQubit], circuit.all_qubits())\r\n        cz_layer = list(_make_cz_layer(qubits, layer_index))\r\n        layer_index += 1\r\n```\r\nwould never end.\r\n\r\nMy suggestion would be change `assert 1 <= n_rows <= 11` in `generate_boixo_2018_supremacy_circuits_v2_bristlecone` to `assert 2 <= n_rows <= 11`, since it does not make anysense to have one-qubit cz layer in the first place.\n", "before_files": [{"content": "# Copyright 2018 The Cirq Developers\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     https://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport random\nfrom typing import Callable, Iterable, TypeVar, cast, Sequence\n\nfrom cirq.circuits import InsertStrategy\nfrom cirq import circuits, devices, google, ops\n\n\ndef generate_boixo_2018_supremacy_circuits_v2(\n        qubits: Iterable[devices.GridQubit], cz_depth: int,\n        seed: int) -> circuits.Circuit:\n    \"\"\"\n    Generates Google Random Circuits v2 as in github.com/sboixo/GRCS cz_v2.\n    See also https://arxiv.org/abs/1807.10749\n\n    Args:\n        qubits: qubit grid in which to generate the circuit.\n        cz_depth: number of layers with CZ gates.\n        seed: seed for the random instance.\n\n    Returns:\n        A circuit corresponding to instance\n        inst_{n_rows}x{n_cols}_{cz_depth+1}_{seed}\n\n    The mapping of qubits is cirq.GridQubit(j,k) -> q[j*n_cols+k]\n    (as in the QASM mapping)\n    \"\"\"\n\n    non_diagonal_gates = [ops.pauli_gates.X**(1/2), ops.pauli_gates.Y**(1/2)]\n    rand_gen = random.Random(seed).random\n\n    circuit = circuits.Circuit()\n\n    # Add an initial moment of Hadamards\n    circuit.append(ops.common_gates.H(qubit) for qubit in qubits)\n\n    layer_index = 0\n    if cz_depth:\n        layer_index = _add_cz_layer(layer_index, circuit)\n        # In the first moment, add T gates when possible\n        for qubit in qubits:\n            if not circuit.operation_at(qubit, 1):\n                circuit.append(ops.common_gates.T(qubit),\n                               strategy=InsertStrategy.EARLIEST)\n\n    for moment_index in range(2, cz_depth+1):\n        layer_index = _add_cz_layer(layer_index, circuit)\n        # Add single qubit gates in the same moment\n        for qubit in qubits:\n            if not circuit.operation_at(qubit, moment_index):\n                last_op = circuit.operation_at(qubit, moment_index-1)\n                if last_op:\n                    gate = cast(ops.GateOperation, last_op).gate\n                    # Add a random non diagonal gate after a CZ\n                    if gate == ops.CZ:\n                        circuit.append(_choice(rand_gen,\n                                               non_diagonal_gates).on(qubit),\n                                       strategy=InsertStrategy.EARLIEST)\n                    # Add a T gate after a non diagonal gate\n                    elif not gate == ops.T:\n                        circuit.append(ops.common_gates.T(qubit),\n                                       strategy=InsertStrategy.EARLIEST)\n\n    # Add a final moment of Hadamards\n    circuit.append([ops.common_gates.H(qubit) for qubit in qubits],\n                   strategy=InsertStrategy.NEW_THEN_INLINE)\n\n    return circuit\n\n\ndef generate_boixo_2018_supremacy_circuits_v2_grid(n_rows: int, n_cols: int,\n                                                   cz_depth: int, seed: int\n                                                  ) -> circuits.Circuit:\n    \"\"\"\n    Generates Google Random Circuits v2 as in github.com/sboixo/GRCS cz_v2.\n    See also https://arxiv.org/abs/1807.10749\n\n    Args:\n        n_rows: number of rows of a 2D lattice.\n        n_cols: number of columns.\n        cz_depth: number of layers with CZ gates.\n        seed: seed for the random instance.\n\n    Returns:\n        A circuit corresponding to instance\n        inst_{n_rows}x{n_cols}_{cz_depth+1}_{seed}\n\n    The mapping of qubits is cirq.GridQubit(j,k) -> q[j*n_cols+k]\n    (as in the QASM mapping)\n    \"\"\"\n    qubits = [devices.GridQubit(i, j) for i in range(n_rows)\n              for j in range(n_cols)]\n    return generate_boixo_2018_supremacy_circuits_v2(qubits, cz_depth, seed)\n\n\ndef generate_boixo_2018_supremacy_circuits_v2_bristlecone(\n        n_rows: int, cz_depth: int, seed: int) -> circuits.Circuit:\n    \"\"\"\n    Generates Google Random Circuits v2 in Bristlecone.\n    See also https://arxiv.org/abs/1807.10749\n\n    Args:\n        n_rows: number of rows in a Bristlecone lattice.\n          Note that we do not include single qubit corners.\n        cz_depth: number of layers with CZ gates.\n        seed: seed for the random instance.\n\n    Returns:\n        A circuit with given size and seed.\n    \"\"\"\n    def get_qubits(n_rows):\n        def count_neighbors(qubits, qubit):\n            \"\"\"Counts the qubits that the given qubit can interact with.\"\"\"\n            possibles = [\n                devices.GridQubit(qubit.row + 1, qubit.col),\n                devices.GridQubit(qubit.row - 1, qubit.col),\n                devices.GridQubit(qubit.row, qubit.col + 1),\n                devices.GridQubit(qubit.row, qubit.col - 1),\n                ]\n            return len(list(e for e in possibles if e in qubits))\n\n        assert 1 <= n_rows <= 11\n        max_row = n_rows - 1\n        dev = google.Bristlecone\n        # we need a consistent order of qubits\n        qubits = list(dev.qubits)\n        qubits.sort()\n        qubits = [q for q in qubits\n                      if  q.row <= max_row and  q.row + q.col < n_rows + 6\n                      and q.row - q.col < n_rows - 5]\n        qubits = [q for q in qubits if count_neighbors(qubits, q) > 1]\n        return qubits\n\n    qubits = get_qubits(n_rows)\n    return generate_boixo_2018_supremacy_circuits_v2(qubits, cz_depth, seed)\n\n\nT = TypeVar('T')\n\n\ndef _choice(rand_gen: Callable[[], float], sequence: Sequence[T]) -> T:\n    \"\"\"Choose a random element from a non-empty sequence.\n\n    Use this instead of random.choice, with random.random(), for reproducibility\n    \"\"\"\n    return sequence[int(rand_gen() * len(sequence))]\n\n\ndef _add_cz_layer(layer_index: int, circuit: circuits.Circuit) -> int:\n    cz_layer = None\n    while not cz_layer:\n        qubits = cast(Iterable[devices.GridQubit], circuit.all_qubits())\n        cz_layer = list(_make_cz_layer(qubits, layer_index))\n        layer_index += 1\n\n    circuit.append(cz_layer, strategy=InsertStrategy.NEW_THEN_INLINE)\n    return layer_index\n\n\ndef _make_cz_layer(qubits: Iterable[devices.GridQubit], layer_index: int\n                   ) -> Iterable[ops.Operation]:\n    \"\"\"\n    Each layer index corresponds to a shift/transpose of this CZ pattern:\n\n        \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf . . .\n\n        \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf . . .\n\n        \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf . . .\n\n        \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf . . .\n\n        \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf . . .\n\n        \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf . . .\n        .   .   .   .   .   .   .   . .\n        .   .   .   .   .   .   .   .   .\n        .   .   .   .   .   .   .   .     .\n\n    Labelled edges, showing the exact index-to-CZs mapping (mod 8):\n\n         \u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500. . .\n        3\u2502  7\u2502  3\u2502  7\u2502  3\u2502\n         \u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500. . .\n        1\u2502  5\u2502  1\u2502  5\u2502  1\u2502\n         \u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500. . .\n        7\u2502  3\u2502  7\u2502  3\u2502  7\u2502\n         \u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500. . .\n        5\u2502  1\u2502  5\u2502  1\u2502  5\u2502\n         \u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500. . .\n        3\u2502  7\u2502  3\u2502  7\u2502  3\u2502\n         .   .   .   .   .   .\n         .   .   .   .   .     .\n         .   .   .   .   .       .\n\n    Note that, for small devices, some layers will be empty because the layer\n    only contains edges not present on the device.\n    \"\"\"\n\n    # map to an internal layer index to match the cycle order of public circuits\n    layer_index_map = [0, 3, 2, 1, 4, 7, 6, 5]\n    internal_layer_index = layer_index_map[layer_index % 8]\n\n    dir_row = internal_layer_index % 2\n    dir_col = 1 - dir_row\n    shift = (internal_layer_index >> 1) % 4\n\n    for q in qubits:\n        q2 = devices.GridQubit(q.row + dir_row, q.col + dir_col)\n        if q2 not in qubits:\n            continue  # This edge isn't on the device.\n        if (q.row * (2 - dir_row) + q.col * (2 - dir_col)) % 4 != shift:\n            continue  # No CZ along this edge for this layer.\n\n        yield ops.common_gates.CZ(q, q2)\n", "path": "cirq/experiments/google_v2_supremacy_circuit.py"}], "after_files": [{"content": "# Copyright 2018 The Cirq Developers\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     https://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport random\nfrom typing import Callable, Iterable, TypeVar, cast, Sequence\n\nfrom cirq.circuits import InsertStrategy\nfrom cirq import circuits, devices, google, ops\n\n\ndef generate_boixo_2018_supremacy_circuits_v2(\n        qubits: Iterable[devices.GridQubit], cz_depth: int,\n        seed: int) -> circuits.Circuit:\n    \"\"\"\n    Generates Google Random Circuits v2 as in github.com/sboixo/GRCS cz_v2.\n    See also https://arxiv.org/abs/1807.10749\n\n    Args:\n        qubits: qubit grid in which to generate the circuit.\n        cz_depth: number of layers with CZ gates.\n        seed: seed for the random instance.\n\n    Returns:\n        A circuit corresponding to instance\n        inst_{n_rows}x{n_cols}_{cz_depth+1}_{seed}\n\n    The mapping of qubits is cirq.GridQubit(j,k) -> q[j*n_cols+k]\n    (as in the QASM mapping)\n    \"\"\"\n\n    non_diagonal_gates = [ops.pauli_gates.X**(1/2), ops.pauli_gates.Y**(1/2)]\n    rand_gen = random.Random(seed).random\n\n    circuit = circuits.Circuit()\n\n    # Add an initial moment of Hadamards\n    circuit.append(ops.common_gates.H(qubit) for qubit in qubits)\n\n    layer_index = 0\n    if cz_depth:\n        layer_index = _add_cz_layer(layer_index, circuit)\n        # In the first moment, add T gates when possible\n        for qubit in qubits:\n            if not circuit.operation_at(qubit, 1):\n                circuit.append(ops.common_gates.T(qubit),\n                               strategy=InsertStrategy.EARLIEST)\n\n    for moment_index in range(2, cz_depth+1):\n        layer_index = _add_cz_layer(layer_index, circuit)\n        # Add single qubit gates in the same moment\n        for qubit in qubits:\n            if not circuit.operation_at(qubit, moment_index):\n                last_op = circuit.operation_at(qubit, moment_index-1)\n                if last_op:\n                    gate = cast(ops.GateOperation, last_op).gate\n                    # Add a random non diagonal gate after a CZ\n                    if gate == ops.CZ:\n                        circuit.append(_choice(rand_gen,\n                                               non_diagonal_gates).on(qubit),\n                                       strategy=InsertStrategy.EARLIEST)\n                    # Add a T gate after a non diagonal gate\n                    elif not gate == ops.T:\n                        circuit.append(ops.common_gates.T(qubit),\n                                       strategy=InsertStrategy.EARLIEST)\n\n    # Add a final moment of Hadamards\n    circuit.append([ops.common_gates.H(qubit) for qubit in qubits],\n                   strategy=InsertStrategy.NEW_THEN_INLINE)\n\n    return circuit\n\n\ndef generate_boixo_2018_supremacy_circuits_v2_grid(n_rows: int, n_cols: int,\n                                                   cz_depth: int, seed: int\n                                                  ) -> circuits.Circuit:\n    \"\"\"\n    Generates Google Random Circuits v2 as in github.com/sboixo/GRCS cz_v2.\n    See also https://arxiv.org/abs/1807.10749\n\n    Args:\n        n_rows: number of rows of a 2D lattice.\n        n_cols: number of columns.\n        cz_depth: number of layers with CZ gates.\n        seed: seed for the random instance.\n\n    Returns:\n        A circuit corresponding to instance\n        inst_{n_rows}x{n_cols}_{cz_depth+1}_{seed}\n\n    The mapping of qubits is cirq.GridQubit(j,k) -> q[j*n_cols+k]\n    (as in the QASM mapping)\n    \"\"\"\n    qubits = [devices.GridQubit(i, j) for i in range(n_rows)\n              for j in range(n_cols)]\n    return generate_boixo_2018_supremacy_circuits_v2(qubits, cz_depth, seed)\n\n\ndef generate_boixo_2018_supremacy_circuits_v2_bristlecone(\n        n_rows: int, cz_depth: int, seed: int) -> circuits.Circuit:\n    \"\"\"\n    Generates Google Random Circuits v2 in Bristlecone.\n    See also https://arxiv.org/abs/1807.10749\n\n    Args:\n        n_rows: number of rows in a Bristlecone lattice.\n          Note that we do not include single qubit corners.\n        cz_depth: number of layers with CZ gates.\n        seed: seed for the random instance.\n\n    Returns:\n        A circuit with given size and seed.\n    \"\"\"\n    def get_qubits(n_rows):\n        def count_neighbors(qubits, qubit):\n            \"\"\"Counts the qubits that the given qubit can interact with.\"\"\"\n            possibles = [\n                devices.GridQubit(qubit.row + 1, qubit.col),\n                devices.GridQubit(qubit.row - 1, qubit.col),\n                devices.GridQubit(qubit.row, qubit.col + 1),\n                devices.GridQubit(qubit.row, qubit.col - 1),\n                ]\n            return len(list(e for e in possibles if e in qubits))\n\n        assert 2 <= n_rows <= 11\n        max_row = n_rows - 1\n        dev = google.Bristlecone\n        # we need a consistent order of qubits\n        qubits = list(dev.qubits)\n        qubits.sort()\n        qubits = [q for q in qubits\n                      if  q.row <= max_row and  q.row + q.col < n_rows + 6\n                      and q.row - q.col < n_rows - 5]\n        qubits = [q for q in qubits if count_neighbors(qubits, q) > 1]\n        return qubits\n\n    qubits = get_qubits(n_rows)\n    return generate_boixo_2018_supremacy_circuits_v2(qubits, cz_depth, seed)\n\n\nT = TypeVar('T')\n\n\ndef _choice(rand_gen: Callable[[], float], sequence: Sequence[T]) -> T:\n    \"\"\"Choose a random element from a non-empty sequence.\n\n    Use this instead of random.choice, with random.random(), for reproducibility\n    \"\"\"\n    return sequence[int(rand_gen() * len(sequence))]\n\n\ndef _add_cz_layer(layer_index: int, circuit: circuits.Circuit) -> int:\n    cz_layer = None\n    while not cz_layer:\n        qubits = cast(Iterable[devices.GridQubit], circuit.all_qubits())\n        cz_layer = list(_make_cz_layer(qubits, layer_index))\n        layer_index += 1\n\n    circuit.append(cz_layer, strategy=InsertStrategy.NEW_THEN_INLINE)\n    return layer_index\n\n\ndef _make_cz_layer(qubits: Iterable[devices.GridQubit], layer_index: int\n                   ) -> Iterable[ops.Operation]:\n    \"\"\"\n    Each layer index corresponds to a shift/transpose of this CZ pattern:\n\n        \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf . . .\n\n        \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf . . .\n\n        \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf . . .\n\n        \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf . . .\n\n        \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf . . .\n\n        \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf   \u25cf   \u25cf   \u25cf\u2500\u2500\u2500\u25cf . . .\n        .   .   .   .   .   .   .   . .\n        .   .   .   .   .   .   .   .   .\n        .   .   .   .   .   .   .   .     .\n\n    Labelled edges, showing the exact index-to-CZs mapping (mod 8):\n\n         \u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500. . .\n        3\u2502  7\u2502  3\u2502  7\u2502  3\u2502\n         \u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500. . .\n        1\u2502  5\u2502  1\u2502  5\u2502  1\u2502\n         \u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500. . .\n        7\u2502  3\u2502  7\u2502  3\u2502  7\u2502\n         \u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500. . .\n        5\u2502  1\u2502  5\u2502  1\u2502  5\u2502\n         \u25cf\u25000\u2500\u25cf\u25002\u2500\u25cf\u25004\u2500\u25cf\u25006\u2500\u25cf\u25000\u2500. . .\n        3\u2502  7\u2502  3\u2502  7\u2502  3\u2502\n         .   .   .   .   .   .\n         .   .   .   .   .     .\n         .   .   .   .   .       .\n\n    Note that, for small devices, some layers will be empty because the layer\n    only contains edges not present on the device.\n    \"\"\"\n\n    # map to an internal layer index to match the cycle order of public circuits\n    layer_index_map = [0, 3, 2, 1, 4, 7, 6, 5]\n    internal_layer_index = layer_index_map[layer_index % 8]\n\n    dir_row = internal_layer_index % 2\n    dir_col = 1 - dir_row\n    shift = (internal_layer_index >> 1) % 4\n\n    for q in qubits:\n        q2 = devices.GridQubit(q.row + dir_row, q.col + dir_col)\n        if q2 not in qubits:\n            continue  # This edge isn't on the device.\n        if (q.row * (2 - dir_row) + q.col * (2 - dir_col)) % 4 != shift:\n            continue  # No CZ along this edge for this layer.\n\n        yield ops.common_gates.CZ(q, q2)\n", "path": "cirq/experiments/google_v2_supremacy_circuit.py"}]}