|
| 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 | +"""Define detuning gates for Analog Experiment usage.""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from typing import AbstractSet, Any, TYPE_CHECKING, TypeAlias |
| 19 | + |
| 20 | +import sympy |
| 21 | +import tunits as tu |
| 22 | + |
| 23 | +import cirq |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + import numpy as np |
| 27 | + |
| 28 | +# The gate is intended for the google internal use, hence the typing style |
| 29 | +# follows more on the t-unit + symbol instead of float + symbol style. |
| 30 | +ValueOrSymbol: TypeAlias = tu.Value | sympy.Basic |
| 31 | +FloatOrSymbol: TypeAlias = float | sympy.Basic |
| 32 | + |
| 33 | +# A sentile for not finding the key in resolver. |
| 34 | +NOT_FOUND = "__NOT_FOUND__" |
| 35 | + |
| 36 | + |
| 37 | +@cirq.value_equality(approximate=True) |
| 38 | +class AnalogDetuneQubit(cirq.ops.Gate): |
| 39 | + """A step function that steup a qubit to the frequency according to analog model. |
| 40 | +
|
| 41 | + Pulse shape: |
| 42 | +
|
| 43 | + .. svgbob:: |
| 44 | + :align: center |
| 45 | + | ,--------|---- amp (calculated from target freq using analog model) |
| 46 | + | / | |
| 47 | + prev_amp---|-' - - - - -| - - |
| 48 | + ^ |
| 49 | + | |-w -| | |
| 50 | + | |---length --| |
| 51 | + | |
| 52 | + --------------------------(calculated from previous qubit freq using analog model) |
| 53 | +
|
| 54 | + Note the step is held at amp with infinite length. This gate is typically used by concatenating |
| 55 | + multiple instances. To ensure the curve is continuous and avoids sudden jumps, you need |
| 56 | + prev_freq to compensate for the previous Detune gate. If not provided, no compensation |
| 57 | + will be applied, i.e. start from 0. If the target_freq is None and prev_freq provided, |
| 58 | + this Detune gate will reset the qubit freq back to absolute amp=0 according to prev_freq. |
| 59 | + """ |
| 60 | + |
| 61 | + def __init__( |
| 62 | + self, |
| 63 | + length: ValueOrSymbol, |
| 64 | + w: ValueOrSymbol, |
| 65 | + target_freq: ValueOrSymbol | None = None, |
| 66 | + prev_freq: ValueOrSymbol | None = None, |
| 67 | + neighbor_coupler_g_dict: dict[str, ValueOrSymbol] | None = None, |
| 68 | + prev_neighbor_coupler_g_dict: dict[str, ValueOrSymbol] | None = None, |
| 69 | + linear_rise: bool = True, |
| 70 | + ): |
| 71 | + """Inits AnalogDetuneQubit. |
| 72 | + Args: |
| 73 | + length: The duration of gate. |
| 74 | + w: Width of the step envelope raising edge. |
| 75 | + target_freq: The target frequecy for the qubit at end of detune gate. |
| 76 | + prev_freq: Previous detuning frequecy to compensate beginning of detune gate. |
| 77 | + neighbor_coupler_g_dict: A dictionary has coupler name like "c_q0_0_q1_0" |
| 78 | + as key and the coupling strength `g` as the value. |
| 79 | + prev_neighbor_coupler_g_dict: A dictionary has the same format as |
| 80 | + `neighbor_coupler_g_dict` one but the value is provided for |
| 81 | + coupling strength g at previous step. |
| 82 | + linear_rise: If True the rising edge will be a linear function, |
| 83 | + otherwise it will be a smoothed function. |
| 84 | + """ |
| 85 | + self.length = length |
| 86 | + self.w = w |
| 87 | + self.target_freq = target_freq |
| 88 | + self.prev_freq = prev_freq |
| 89 | + self.neighbor_coupler_g_dict = neighbor_coupler_g_dict |
| 90 | + self.prev_neighbor_coupler_g_dict = prev_neighbor_coupler_g_dict |
| 91 | + self.linear_rise = linear_rise |
| 92 | + |
| 93 | + def _unitary_(self) -> np.ndarray: |
| 94 | + return NotImplemented # pragma: no cover |
| 95 | + |
| 96 | + def num_qubits(self) -> int: |
| 97 | + return 1 |
| 98 | + |
| 99 | + def _is_parameterized_(self) -> bool: |
| 100 | + def _is_parameterized_dict(dict_with_value: dict[str, ValueOrSymbol] | None) -> bool: |
| 101 | + if dict_with_value is None: |
| 102 | + return False # pragma: no cover |
| 103 | + return any(cirq.is_parameterized(v) for v in dict_with_value.values()) |
| 104 | + |
| 105 | + return ( |
| 106 | + cirq.is_parameterized(self.length) |
| 107 | + or cirq.is_parameterized(self.w) |
| 108 | + or cirq.is_parameterized(self.target_freq) |
| 109 | + or cirq.is_parameterized(self.prev_freq) |
| 110 | + or _is_parameterized_dict(self.neighbor_coupler_g_dict) |
| 111 | + or _is_parameterized_dict(self.prev_neighbor_coupler_g_dict) |
| 112 | + ) |
| 113 | + |
| 114 | + def _parameter_names_(self) -> AbstractSet[str]: |
| 115 | + def dict_param_name(dict_with_value: dict[str, ValueOrSymbol] | None) -> AbstractSet[str]: |
| 116 | + if dict_with_value is None: |
| 117 | + return set() |
| 118 | + return {v.name for v in dict_with_value.values() if cirq.is_parameterized(v)} |
| 119 | + |
| 120 | + return ( |
| 121 | + cirq.parameter_names(self.length) |
| 122 | + | cirq.parameter_names(self.w) |
| 123 | + | cirq.parameter_names(self.target_freq) |
| 124 | + | cirq.parameter_names(self.prev_freq) |
| 125 | + | dict_param_name(self.neighbor_coupler_g_dict) |
| 126 | + | dict_param_name(self.prev_neighbor_coupler_g_dict) |
| 127 | + ) |
| 128 | + |
| 129 | + def _resolve_parameters_( |
| 130 | + self, resolver: cirq.ParamResolverOrSimilarType, recursive: bool |
| 131 | + ) -> AnalogDetuneQubit: |
| 132 | + # A shortcut for value resolution to avoid tu.unit compare with float issue. |
| 133 | + def _direct_symbol_replacement(x, resolver: cirq.ParamResolver): |
| 134 | + if isinstance(x, sympy.Symbol): |
| 135 | + value = resolver.param_dict.get(x.name, NOT_FOUND) |
| 136 | + if value == NOT_FOUND: |
| 137 | + value = resolver.param_dict.get(x, NOT_FOUND) |
| 138 | + if value != NOT_FOUND: |
| 139 | + return value |
| 140 | + return x # pragma: no cover |
| 141 | + return x |
| 142 | + |
| 143 | + resolver_ = cirq.ParamResolver(resolver) |
| 144 | + return AnalogDetuneQubit( |
| 145 | + length=_direct_symbol_replacement(self.length, resolver_), |
| 146 | + w=_direct_symbol_replacement(self.w, resolver_), |
| 147 | + target_freq=_direct_symbol_replacement(self.target_freq, resolver_), |
| 148 | + prev_freq=_direct_symbol_replacement(self.prev_freq, resolver_), |
| 149 | + neighbor_coupler_g_dict=( |
| 150 | + { |
| 151 | + k: _direct_symbol_replacement(v, resolver_) |
| 152 | + for k, v in self.neighbor_coupler_g_dict.items() |
| 153 | + } |
| 154 | + if self.neighbor_coupler_g_dict |
| 155 | + else None |
| 156 | + ), |
| 157 | + prev_neighbor_coupler_g_dict=( |
| 158 | + { |
| 159 | + k: _direct_symbol_replacement(v, resolver_) |
| 160 | + for k, v in self.prev_neighbor_coupler_g_dict.items() |
| 161 | + } |
| 162 | + if self.prev_neighbor_coupler_g_dict |
| 163 | + else None |
| 164 | + ), |
| 165 | + linear_rise=self.linear_rise, |
| 166 | + ) |
| 167 | + |
| 168 | + def __repr__(self) -> str: |
| 169 | + return ( |
| 170 | + f'AnalogDetuneQubit(length={self.length}, ' |
| 171 | + f'w={self.w}, ' |
| 172 | + f'target_freq={self.target_freq}, ' |
| 173 | + f'prev_freq={self.prev_freq}, ' |
| 174 | + f'neighbor_coupler_g_dict={self.neighbor_coupler_g_dict}, ' |
| 175 | + f'prev_neighbor_coupler_g_dict={self.prev_neighbor_coupler_g_dict})' |
| 176 | + ) |
| 177 | + |
| 178 | + def _value_equality_values_(self) -> Any: |
| 179 | + return ( |
| 180 | + self.length, |
| 181 | + self.w, |
| 182 | + self.target_freq, |
| 183 | + self.prev_freq, |
| 184 | + self.neighbor_coupler_g_dict, |
| 185 | + self.prev_neighbor_coupler_g_dict, |
| 186 | + self.linear_rise, |
| 187 | + ) |
| 188 | + |
| 189 | + def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> str: |
| 190 | + return f"AnalogDetune(freq={self.target_freq})" |
| 191 | + |
| 192 | + def _json_dict_(self): |
| 193 | + return cirq.obj_to_dict_helper( |
| 194 | + self, |
| 195 | + [ |
| 196 | + 'length', |
| 197 | + 'w', |
| 198 | + 'target_freq', |
| 199 | + 'prev_freq', |
| 200 | + 'neighbor_coupler_g_dict', |
| 201 | + 'prev_neighbor_coupler_g_dict', |
| 202 | + 'linear_rise', |
| 203 | + ], |
| 204 | + ) |
0 commit comments