|
| 1 | +# Copyright 2025 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 re |
| 16 | +from typing import Hashable, Optional, TYPE_CHECKING |
| 17 | + |
| 18 | +import sympy |
| 19 | + |
| 20 | +from cirq import ops |
| 21 | +from cirq.transformers import transformer_api, transformer_primitives |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + import cirq |
| 25 | + |
| 26 | + |
| 27 | +@transformer_api.transformer |
| 28 | +def symbolize_single_qubit_gates_by_indexed_tags( |
| 29 | + circuit: 'cirq.AbstractCircuit', |
| 30 | + *, |
| 31 | + context: Optional['cirq.TransformerContext'] = None, |
| 32 | + tag_prefix: Optional[str] = "TO-PHXZ", |
| 33 | +) -> 'cirq.Circuit': |
| 34 | + """Symbolizes single qubit operations by indexed tags prefixed by tag_prefix. |
| 35 | +
|
| 36 | + Example: |
| 37 | + >>> q0, q1 = cirq.LineQubit.range(2) |
| 38 | + >>> c = cirq.Circuit(\ |
| 39 | + cirq.X(q0).with_tags("phxz_0"),\ |
| 40 | + cirq.CZ(q0,q1),\ |
| 41 | + cirq.Y(q0).with_tags("phxz_1"),\ |
| 42 | + cirq.X(q0)) |
| 43 | + >>> print(c) |
| 44 | + 0: ───X[phxz_0]───@───Y[phxz_1]───X─── |
| 45 | + │ |
| 46 | + 1: ───────────────@─────────────────── |
| 47 | + >>> new_circuit = cirq.symbolize_single_qubit_gates_by_indexed_tags(\ |
| 48 | + c, tag_prefix="phxz") |
| 49 | + >>> print(new_circuit) |
| 50 | + 0: ───PhXZ(a=a0,x=x0,z=z0)───@───PhXZ(a=a1,x=x1,z=z1)───X─── |
| 51 | + │ |
| 52 | + 1: ──────────────────────────@────────────────────────────── |
| 53 | +
|
| 54 | + Args: |
| 55 | + circuit: Input circuit to apply the transformations on. The input circuit is not mutated. |
| 56 | + context: `cirq.TransformerContext` storing common configurable options for transformers. |
| 57 | + tag_prefix: The prefix of the tag. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + Copy of the transformed input circuit. |
| 61 | + """ |
| 62 | + |
| 63 | + def _map_func(op: 'cirq.Operation', _): |
| 64 | + """Maps an op with tag `{tag_prefix}_i` to a symbolzied `PhasedXZGate(xi,zi,ai)`.""" |
| 65 | + tags: set[Hashable] = set(op.tags) |
| 66 | + tag_id: None | int = None |
| 67 | + for tag in tags: |
| 68 | + if re.fullmatch(f"{tag_prefix}_\\d+", str(tag)): |
| 69 | + if tag_id is None: |
| 70 | + tag_id = int(str(tag).rsplit("_", maxsplit=-1)[-1]) |
| 71 | + else: |
| 72 | + raise ValueError(f"Multiple tags are prefixed with {tag_prefix}.") |
| 73 | + if tag_id is None: |
| 74 | + return op |
| 75 | + tags.remove(f"{tag_prefix}_{tag_id}") |
| 76 | + phxz_params = { |
| 77 | + "x_exponent": sympy.Symbol(f"x{tag_id}"), |
| 78 | + "z_exponent": sympy.Symbol(f"z{tag_id}"), |
| 79 | + "axis_phase_exponent": sympy.Symbol(f"a{tag_id}"), |
| 80 | + } |
| 81 | + |
| 82 | + return ops.PhasedXZGate(**phxz_params).on(*op.qubits).with_tags(*tags) |
| 83 | + |
| 84 | + return transformer_primitives.map_operations( |
| 85 | + circuit.freeze(), |
| 86 | + _map_func, |
| 87 | + deep=context.deep if context else False, |
| 88 | + tags_to_ignore=context.tags_to_ignore if context else [], |
| 89 | + ).unfreeze(copy=False) |
0 commit comments