|
| 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 functools |
| 16 | +import re |
| 17 | + |
| 18 | +import cirq |
| 19 | +from cirq_google.experimental.analog_experiments import analog_trajectory_util as atu |
| 20 | +from cirq_google.ops import analog_detune_gates as adg, wait_gate as wg |
| 21 | +from cirq_google.study import symbol_util as su |
| 22 | + |
| 23 | + |
| 24 | +def _get_neighbor_freqs( |
| 25 | + qubit_pair: tuple[str, str], qubit_freq_dict: dict[str, su.ValueOrSymbol | None] |
| 26 | +) -> tuple[su.ValueOrSymbol | None, su.ValueOrSymbol | None]: |
| 27 | + """Get neighbor freqs from qubit_freq_dict given the pair.""" |
| 28 | + sorted_pair = sorted(qubit_pair, key=_to_grid_qubit) |
| 29 | + return (qubit_freq_dict[sorted_pair[0]], qubit_freq_dict[sorted_pair[1]]) |
| 30 | + |
| 31 | + |
| 32 | +@functools.cache |
| 33 | +def _to_grid_qubit(qubit_name: str) -> cirq.GridQubit: |
| 34 | + match = re.compile(r"^q(\d+)_(\d+)$").match(qubit_name) |
| 35 | + if match is None: |
| 36 | + raise ValueError(f"Invalid qubit name format: '{qubit_name}'. Expected 'q<row>_<col>'.") |
| 37 | + return cirq.GridQubit(int(match[1]), int(match[2])) |
| 38 | + |
| 39 | + |
| 40 | +def _coupler_name_from_qubit_pair(qubit_pair: tuple[str, str]) -> str: |
| 41 | + sorted_pair = sorted(qubit_pair, key=_to_grid_qubit) |
| 42 | + return f"c_{sorted_pair[0]}_{sorted_pair[1]}" |
| 43 | + |
| 44 | + |
| 45 | +def _get_neighbor_coupler_freqs( |
| 46 | + qubit_name: str, coupler_g_dict: dict[tuple[str, str], su.ValueOrSymbol] |
| 47 | +) -> dict[str, su.ValueOrSymbol]: |
| 48 | + """Get neighbor coupler coupling strength g given qubit name.""" |
| 49 | + return { |
| 50 | + _coupler_name_from_qubit_pair(pair): g |
| 51 | + for pair, g in coupler_g_dict.items() |
| 52 | + if qubit_name in pair |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +class GenericAnalogCircuitBuilder: |
| 57 | + """Class for making arbitrary analog circuits. The circuit is defined by an |
| 58 | + AnalogTrajectory object. The class constructs the circuit from AnalogDetune |
| 59 | + pulses, which automatically calculate the necessary bias amps to both qubits |
| 60 | + and couplers, using tu.Values from analog calibration whenever available. |
| 61 | +
|
| 62 | + Attributes: |
| 63 | + trajectory: AnalogTrajectory object defining the circuit |
| 64 | + g_ramp_shaping: coupling ramps are shaped according to ramp_shape_exp if True |
| 65 | + qubits: list of qubits in the circuit |
| 66 | + pairs: list of couplers in the circuit |
| 67 | + ramp_shape_exp: exponent of g_ramp (g proportional to t^ramp_shape_exp) |
| 68 | + interpolate_coupling_cal: interpolates between calibrated coupling tu.Values if True |
| 69 | + linear_qubit_ramp: if True, the qubit ramp is linear. if false, a cosine shaped |
| 70 | + ramp is used. |
| 71 | + """ |
| 72 | + |
| 73 | + def __init__( |
| 74 | + self, |
| 75 | + trajectory: atu.AnalogTrajectory, |
| 76 | + g_ramp_shaping: bool = False, |
| 77 | + ramp_shape_exp: int = 1, |
| 78 | + interpolate_coupling_cal: bool = False, |
| 79 | + linear_qubit_ramp: bool = True, |
| 80 | + ): |
| 81 | + self.trajectory = trajectory |
| 82 | + self.g_ramp_shaping = g_ramp_shaping |
| 83 | + self.ramp_shape_exp = ramp_shape_exp |
| 84 | + self.interpolate_coupling_cal = interpolate_coupling_cal |
| 85 | + self.linear_qubit_ramp = linear_qubit_ramp |
| 86 | + |
| 87 | + def make_circuit(self) -> cirq.Circuit: |
| 88 | + """Assemble moments described in trajectory.""" |
| 89 | + prev_freq_map = self.trajectory.full_trajectory[0] |
| 90 | + moments = [] |
| 91 | + for freq_map in self.trajectory.full_trajectory[1:]: |
| 92 | + if freq_map.is_wait_step: |
| 93 | + targets = [_to_grid_qubit(q) for q in self.trajectory.qubits] |
| 94 | + wait_gate = wg.WaitGateWithUnit( |
| 95 | + freq_map.duration, qid_shape=cirq.qid_shape(targets) |
| 96 | + ) |
| 97 | + moment = cirq.Moment(wait_gate.on(*targets)) |
| 98 | + else: |
| 99 | + moment = self.make_one_moment(freq_map, prev_freq_map) |
| 100 | + moments.append(moment) |
| 101 | + prev_freq_map = freq_map |
| 102 | + |
| 103 | + return cirq.Circuit.from_moments(*moments) |
| 104 | + |
| 105 | + def make_one_moment( |
| 106 | + self, freq_map: atu.FrequencyMap, prev_freq_map: atu.FrequencyMap |
| 107 | + ) -> cirq.Moment: |
| 108 | + """Make one moment of analog detune qubit and coupler gates given freqs.""" |
| 109 | + qubit_gates = [] |
| 110 | + for q, freq in freq_map.qubit_freqs.items(): |
| 111 | + qubit_gates.append( |
| 112 | + adg.AnalogDetuneQubit( |
| 113 | + length=freq_map.duration, |
| 114 | + w=freq_map.duration, |
| 115 | + target_freq=freq, |
| 116 | + prev_freq=prev_freq_map.qubit_freqs.get(q), |
| 117 | + neighbor_coupler_g_dict=_get_neighbor_coupler_freqs(q, freq_map.couplings), |
| 118 | + prev_neighbor_coupler_g_dict=_get_neighbor_coupler_freqs( |
| 119 | + q, prev_freq_map.couplings |
| 120 | + ), |
| 121 | + linear_rise=self.linear_qubit_ramp, |
| 122 | + ).on(_to_grid_qubit(q)) |
| 123 | + ) |
| 124 | + coupler_gates = [] |
| 125 | + for p, g_max in freq_map.couplings.items(): |
| 126 | + # Currently skipping the step if these are the same. |
| 127 | + # However, change in neighbor qubit freq could potentially change coupler amp |
| 128 | + if g_max == prev_freq_map.couplings[p]: |
| 129 | + continue |
| 130 | + |
| 131 | + coupler_gates.append( |
| 132 | + adg.AnalogDetuneCouplerOnly( |
| 133 | + length=freq_map.duration, |
| 134 | + w=freq_map.duration, |
| 135 | + g_0=prev_freq_map.couplings[p], |
| 136 | + g_max=g_max, |
| 137 | + g_ramp_exponent=self.ramp_shape_exp, |
| 138 | + neighbor_qubits_freq=_get_neighbor_freqs(p, freq_map.qubit_freqs), |
| 139 | + prev_neighbor_qubits_freq=_get_neighbor_freqs(p, prev_freq_map.qubit_freqs), |
| 140 | + interpolate_coupling_cal=self.interpolate_coupling_cal, |
| 141 | + ).on(*sorted([_to_grid_qubit(p[0]), _to_grid_qubit(p[1])])) |
| 142 | + ) |
| 143 | + |
| 144 | + return cirq.Moment(qubit_gates + coupler_gates) |
0 commit comments