|
| 1 | +# Copyright 2022 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 cirq |
| 16 | + |
| 17 | + |
| 18 | +class MapLargeExpandedCircuit: |
| 19 | + param_names = ["num_qubits", "num_moments"] |
| 20 | + params = ([100, 500, 1000], [100, 1000, 4000]) |
| 21 | + timeout = 600 # Change timeout to 2 minutes instead of default 60 seconds. |
| 22 | + |
| 23 | + def setup(self, num_qubits: int, num_moments: int): |
| 24 | + qubits = cirq.LineQubit.range(num_qubits) |
| 25 | + one_q_x_moment = cirq.Moment(cirq.X(q) for q in qubits[::2]) |
| 26 | + one_q_y_moment = cirq.Moment(cirq.Y(q) for q in qubits[1::2]) |
| 27 | + two_q_cx_moment = cirq.Moment( |
| 28 | + cirq.CNOT(q1, q2) for q1, q2 in zip(qubits[::4], qubits[1::4]) |
| 29 | + ) |
| 30 | + two_q_cz_moment = cirq.Moment(cirq.CZ(q1, q2) for q1, q2 in zip(qubits[::4], qubits[1::4])) |
| 31 | + self.circuit = cirq.Circuit( |
| 32 | + [one_q_x_moment, two_q_cx_moment, one_q_y_moment, two_q_cz_moment] * (num_moments // 4) |
| 33 | + ) |
| 34 | + |
| 35 | + def time_map_moments(self, num_qubits: int, _): |
| 36 | + all_qubits = cirq.LineQubit.range(num_qubits) |
| 37 | + |
| 38 | + def map_func(m: cirq.Moment, _) -> cirq.Moment: |
| 39 | + new_ops = [op.with_tags("old op") for op in m.operations] |
| 40 | + new_ops += [ |
| 41 | + cirq.Z(q).with_tags("new op") |
| 42 | + for q in all_qubits |
| 43 | + if not m.operates_on_single_qubit(q) |
| 44 | + ] |
| 45 | + return cirq.Moment(new_ops) |
| 46 | + |
| 47 | + _ = cirq.map_moments(circuit=self.circuit, map_func=map_func) |
| 48 | + |
| 49 | + def time_map_operations_apply_tag(self, *_): |
| 50 | + def map_func(op: cirq.Operation, _) -> cirq.Operation: |
| 51 | + return op.with_tags("old op") |
| 52 | + |
| 53 | + _ = cirq.map_operations(circuit=self.circuit, map_func=map_func) |
| 54 | + |
| 55 | + def time_map_operations_to_optree(self, *_): |
| 56 | + def map_func(op: cirq.Operation, _) -> cirq.OP_TREE: |
| 57 | + return [op, op] |
| 58 | + |
| 59 | + _ = cirq.map_operations(circuit=self.circuit, map_func=map_func) |
| 60 | + |
| 61 | + def time_map_operations_to_optree_and_unroll(self, *_): |
| 62 | + def map_func(op: cirq.Operation, _) -> cirq.OP_TREE: |
| 63 | + return [op, op] |
| 64 | + |
| 65 | + _ = cirq.map_operations_and_unroll(circuit=self.circuit, map_func=map_func) |
0 commit comments