|
| 1 | +# --- |
| 2 | +# jupyter: |
| 3 | +# jupytext: |
| 4 | +# cell_metadata_filter: -all |
| 5 | +# custom_cell_magics: kql |
| 6 | +# text_representation: |
| 7 | +# extension: .py |
| 8 | +# format_name: percent |
| 9 | +# format_version: '1.3' |
| 10 | +# jupytext_version: 1.17.2 |
| 11 | +# kernelspec: |
| 12 | +# display_name: kirin-workspace (3.12.10) |
| 13 | +# language: python |
| 14 | +# name: python3 |
| 15 | +# --- |
| 16 | + |
| 17 | +# %% [markdown] |
| 18 | +# # GHZ State preparation and noise |
| 19 | +# |
| 20 | +# In this example, we will illustrate how to work with `bloqade`'s heuristic noise models of Gemini class digital quantum processors by applying them to a circuit that prepares a GHZ state. |
| 21 | + |
| 22 | +# %% [markdown] |
| 23 | +# ## Primer on Gemini noise models |
| 24 | +# |
| 25 | +# In `bloqade`, there are two classes of heuristic noise models: one-zone models such as `GeminiOneZoneNoiseModel` and a two-zone model `GeminiTwoZoneNoiseModel`. |
| 26 | +# These are inspired by two distinct approaches to implement a quantum circuit on hardware and are designed to get a sense of the influence of noise on Gemini class hardware. |
| 27 | +# |
| 28 | +# On the one hand, the one-zone model assumes a single-zone layout where qubits remain in the gate zone throughout the computation. |
| 29 | +# On the other hand, the two-zone model incorporates a storage zone and assumes that qubits are transported between gate and storage regions. |
| 30 | +# |
| 31 | +# Both models are informed by benchmark data on the device but are intentionally conservative. |
| 32 | +# Specifically, they tend to overestimate noise due to the lack of knowledge about optimized move schedules, which leads to overestimating move-induced errors. |
| 33 | +# |
| 34 | +# At this stage, we recommend interpreting the two models as providing a range for expected noise levels on Gemini-class devices, rather than precise predictions. They are useful for gaining intuition about noise sensitivity and for benchmarking algorithmic robustness to errors, using hardware-informed but simplistic assumptions. |
| 35 | +# |
| 36 | +# Note, that there are actually two additional one-zone noise models, `GeminiOneZoneNoiseModelCorrelated` and `GeminiOneZoneNoiseModelConflictGraphMoves`. |
| 37 | +# As the names suggest, the former also takes into account correlated noise, whereas the latter takes into account more realistic move schedules. |
| 38 | +# In the following example, we will not be considering these two, but they are interchangeable with the used noise models (up to the fact, that the conflict graph moves require you to specify qubits as `cirq.GridQubit`s). |
| 39 | + |
| 40 | +# %% [markdown] |
| 41 | +# ## Noise model implementations |
| 42 | +# |
| 43 | +# For now, these noise models are implemented as [`cirq.NoiseModel`](https://quantumai.google/reference/python/cirq/NoiseModel) classes, so that you can use with any circuit you build using `cirq`. |
| 44 | +# They are part of the [`bloqade.cirq_utils`](../../../reference/cirq_utils) submodule. |
| 45 | +# |
| 46 | +# Support for using these models with e.g. [squin](../dialects_and_kernels.md) will follow in the future. |
| 47 | +# However, you can already rely on [interoperability with cirq](../cirq_interop) in order to convert (noisy) circuits to squin kernels and use other parts of the compiler pipeline. |
| 48 | + |
| 49 | +# %% [markdown] |
| 50 | +# ## GHZ preparation and noise |
| 51 | +# |
| 52 | +# Now, let's get started with the actual example. |
| 53 | +# |
| 54 | +# As a first step, we will define a function that builds a GHZ circuit in cirq that has a depth linear in the number of qubits. |
| 55 | +# |
| 56 | + |
| 57 | +# %% |
| 58 | +import cirq |
| 59 | +import numpy as np |
| 60 | +import matplotlib.pyplot as plt |
| 61 | +from bloqade.cirq_utils import noise, transpile |
| 62 | + |
| 63 | +from bloqade import squin |
| 64 | + |
| 65 | + |
| 66 | +def ghz_circuit(n: int) -> cirq.Circuit: |
| 67 | + qubits = cirq.LineQubit.range(n) |
| 68 | + |
| 69 | + # Step 1: Hadamard on the first qubit |
| 70 | + circuit = cirq.Circuit(cirq.H(qubits[0])) |
| 71 | + |
| 72 | + # Step 2: CNOT chain from qubit i to i+1 |
| 73 | + for i in range(n - 1): |
| 74 | + circuit.append(cirq.CNOT(qubits[i], qubits[i + 1])) |
| 75 | + |
| 76 | + return circuit |
| 77 | + |
| 78 | + |
| 79 | +# %% [markdown] |
| 80 | +# ### Closer look at a basic circuit |
| 81 | + |
| 82 | +# %% [markdown] |
| 83 | +# Here's what this circuit looks like for `n=3` qubits: |
| 84 | + |
| 85 | +# %% |
| 86 | +ghz_circuit_3 = ghz_circuit(3) |
| 87 | +print(ghz_circuit_3) |
| 88 | + |
| 89 | +# %% [markdown] |
| 90 | +# So far so good. |
| 91 | +# Now, we will convert the circuit above to a noisy one using bloqade's `cirq_utils` submodule. |
| 92 | +# |
| 93 | +# Specifically, we can use the `noise.transform_circuit` utility function with a noise model of our choice. |
| 94 | + |
| 95 | +# %% |
| 96 | +noise_model = noise.GeminiOneZoneNoiseModel() |
| 97 | +noisy_ghz_circuit_3 = noise.transform_circuit(ghz_circuit_3, model=noise_model) |
| 98 | +print(noisy_ghz_circuit_3) |
| 99 | + |
| 100 | +# %% [markdown] |
| 101 | +# As you can see, we have successfully added noise. |
| 102 | +# However, the circuit also looks very different in terms of its gates. |
| 103 | +# |
| 104 | +# This is because `noise.transform_circuit` does actually two things: |
| 105 | +# |
| 106 | +# 1. Since we want to consider a circuit that is compatible with the Gemini architecture, we need to transform it to the native gate set first. This set consists of (phased) X gates and CZ gates only. |
| 107 | +# 2. Once we have a native circuit, noise is injected according to the chosen noise model. |
| 108 | +# |
| 109 | +# To clarify, here is how you would convert the circuit without using the `noise.transform_circuit` utility function: |
| 110 | + |
| 111 | +# %% |
| 112 | +native_ghz_3 = transpile(ghz_circuit_3) |
| 113 | +print(native_ghz_3) |
| 114 | + |
| 115 | +# %% [markdown] |
| 116 | +# Note that `transpile` basically just wraps cirq's own `cirq.optimize_for_target_gateset(circuit, gateset=cirq.CZTargetGateset())`, with some additional benefits (such as filtering out empty moments). |
| 117 | +# |
| 118 | +# Using this native circuit, we can obtain the same noisy circuit as before by simply using cirq's `cirq.Circuit.with_noise` method. |
| 119 | + |
| 120 | +# %% |
| 121 | +noisy_ghz_circuit_3 = native_ghz_3.with_noise(noise_model) |
| 122 | +print(noisy_ghz_circuit_3) |
| 123 | + |
| 124 | +# %% [markdown] |
| 125 | +# ### Studying the fidelity |
| 126 | +# |
| 127 | +# Now that we have got the basics down, we can compute the fidelity of noisy circuits with different qubit numbers. |
| 128 | +# By fidelity, we simply mean the overlap of the final state with the perfect GHZ state expected from the noise-less version of the circuit. |
| 129 | +# |
| 130 | +# The corresponding density matrices are obtained using `cirq`'s simulator. |
| 131 | +# |
| 132 | +# We will do the simulation using two different noise models, the one-zone model used above and also the two-zone model. |
| 133 | + |
| 134 | +# %% [markdown] |
| 135 | +# <div class="admonition note"> |
| 136 | +# <p class="admonition-title">Fidelity calculation</p> |
| 137 | +# <p> |
| 138 | +# In the following, we will simply use the expectation value of the noisy density matrix computed against the noiseless one as a proxy for fidelity. |
| 139 | +# This is a suboptimal choice, but we wanted to keep the example simple. |
| 140 | +# Feel free to substitute the fidelity calculation by the fidelity of your choice (e.g. the Uhlmann fidelity) |
| 141 | +# </p> |
| 142 | +# </div> |
| 143 | + |
| 144 | +# %% |
| 145 | +qubits = range(3, 9) |
| 146 | + |
| 147 | +one_zone_model = noise.GeminiOneZoneNoiseModel() |
| 148 | +two_zone_model = noise.GeminiTwoZoneNoiseModel() |
| 149 | +simulator = cirq.DensityMatrixSimulator() |
| 150 | + |
| 151 | +fidelities_one_zone = [] |
| 152 | +fidelities_two_zone = [] |
| 153 | +for n in qubits: |
| 154 | + circuit = ghz_circuit(n) |
| 155 | + one_zone_circuit = noise.transform_circuit(circuit, model=one_zone_model) |
| 156 | + two_zone_circuit = noise.transform_circuit(circuit, model=two_zone_model) |
| 157 | + |
| 158 | + rho = simulator.simulate(circuit).final_density_matrix |
| 159 | + rho_one_zone = simulator.simulate(one_zone_circuit).final_density_matrix |
| 160 | + rho_two_zone = simulator.simulate(two_zone_circuit).final_density_matrix |
| 161 | + |
| 162 | + fidelity_one_zone = np.trace(rho @ rho_one_zone) |
| 163 | + fidelity_two_zone = np.trace(rho @ rho_two_zone) |
| 164 | + |
| 165 | + fidelities_one_zone.append(fidelity_one_zone) |
| 166 | + fidelities_two_zone.append(fidelity_two_zone) |
| 167 | + |
| 168 | +# %% [markdown] |
| 169 | +# Now, let's have a look at the results. |
| 170 | + |
| 171 | +# %% |
| 172 | +plt.plot(qubits, fidelities_one_zone, "o", label="one-zone model") |
| 173 | +plt.plot(qubits, fidelities_two_zone, "x", label="two-zone model") |
| 174 | +plt.xlabel("Number of qubits") |
| 175 | +plt.ylabel("Fidelity") |
| 176 | +plt.legend() |
| 177 | + |
| 178 | +# %% [markdown] |
| 179 | +# <div align="center"> |
| 180 | +# <picture> |
| 181 | +# <img src="../noisy_ghz_fidelity.svg" > |
| 182 | +# </picture> |
| 183 | +# </div> |
| 184 | + |
| 185 | +# %% [markdown] |
| 186 | +# We can see that in both cases the fidelity goes down when increasing the number of qubits. |
| 187 | +# |
| 188 | +# Interestingly, there is a cross-over point where the two-zone model starts to exhibit a better fidelity. |
| 189 | +# This is because as the number of qubits grows, the error introduced on idle qubits inside the gate zone is larger in the one-zone model since all qubits are always inside the gate zone. |
| 190 | +# Whereas, in the two-zone model, qubits are moved between the gate and storage zones. |
| 191 | +# |
| 192 | +# You could now think about how to optimize the circuits in order to reduce their sensitivity for noise. |
| 193 | +# For example, you can [reduce the circuit depth](../ghz) |
| 194 | + |
| 195 | +# %% [markdown] |
| 196 | +# ### Modifying the noise |
| 197 | +# |
| 198 | +# There are a number of parameters that govern the effect a noise model introduces into a circuit. |
| 199 | +# These can all be set independently to adapt the noise model to your specific application. |
| 200 | +# |
| 201 | +# In general, there are noise parameters for the following noise processes: |
| 202 | +# |
| 203 | +# * Depolarization due to gate application. |
| 204 | +# * Depolarization due to movement, both applied to moving atoms and idle atoms (a.k.a. sitter errors). |
| 205 | +# * Atom loss errors. |
| 206 | +# |
| 207 | +# <div class="admonition note"> |
| 208 | +# <p class="admonition-title">Atom loss</p> |
| 209 | +# <p> |
| 210 | +# Please note, that atom loss is currently not supported, i.e. it's not considered in the noise models. |
| 211 | +# We plan to add that in the future. |
| 212 | +# </p> |
| 213 | +# </div> |
| 214 | +# |
| 215 | +# The noise processes are further split into local and global noise channels and separated by their cause. |
| 216 | +# |
| 217 | +# For a full list of noise parameters and a description of each one, please refer to the move noise model in [`bloqade.qasm2.dialects.noise.model.MoveNoiseModelABC`](../../../reference/qasm2/#bloqade.qasm2.dialects.noise.model.MoveNoiseModelABC) |
| 218 | +# |
| 219 | +# We can use those parameters in order to modify the strength of the noise. |
| 220 | +# |
| 221 | +# For example, say you want to introduce an extra penalty for moving qubits around in order to study how you can reduce movements. To do so, let's re-use the fidelity calculation using the two-zone model from above, but modify movement errors. |
| 222 | +# We can query the default move errors from the `cirq` noise model: |
| 223 | + |
| 224 | +# %% |
| 225 | +default_model = noise.GeminiTwoZoneNoiseModel() |
| 226 | +px, py, pz = default_model.mover_px, default_model.mover_py, default_model.mover_pz |
| 227 | +print( |
| 228 | + f"The noise Pauli channel associated with moving atoms is (px, py, pz) = ({px,py,pz})." |
| 229 | +) |
| 230 | + |
| 231 | +# %% [markdown] |
| 232 | +# |
| 233 | +# Then we can instantiate a noise model with modified parameters: |
| 234 | +# %% |
| 235 | +modified_two_zone_model = noise.GeminiTwoZoneNoiseModel( |
| 236 | + mover_px=2e-3, |
| 237 | + mover_py=2e-3, |
| 238 | + mover_pz=3e-3, |
| 239 | +) |
| 240 | +fidelities_modified_two_zone = [] |
| 241 | +for n in qubits: |
| 242 | + circuit = ghz_circuit(n) |
| 243 | + noisy_circuit = noise.transform_circuit(circuit, model=modified_two_zone_model) |
| 244 | + rho = simulator.simulate(circuit).final_density_matrix |
| 245 | + rho_noisy = simulator.simulate(noisy_circuit).final_density_matrix |
| 246 | + fidelities_modified_two_zone.append(np.trace(rho @ rho_noisy)) |
| 247 | + |
| 248 | +# %% |
| 249 | +plt.plot(qubits, fidelities_one_zone, "o", label="one-zone model") |
| 250 | +plt.plot(qubits, fidelities_modified_two_zone, "x", label="modified two-zone model") |
| 251 | +plt.xlabel("Number of qubits") |
| 252 | +plt.ylabel("Fidelity") |
| 253 | +plt.legend() |
| 254 | + |
| 255 | +# %% [markdown] |
| 256 | +# <div align="center"> |
| 257 | +# <picture> |
| 258 | +# <img src="../noisy_ghz_modified.svg" > |
| 259 | +# </picture> |
| 260 | +# </div> |
| 261 | + |
| 262 | +# %% [markdown] |
| 263 | +# As you can see, the fidelities no longer cross over since the increased movement noise now eliminates the advantage of the two-zone model for the considered numbers of qubits. |
| 264 | + |
| 265 | +# %% [markdown] |
| 266 | +# ## Interoperability with squin |
| 267 | +# |
| 268 | +# Finally, we want to point out that you can also use the generated noisy circuits to obtain a squin kernel function. |
| 269 | +# |
| 270 | +# This is useful if you want to use other features of the bloqade pipeline. |
| 271 | +# For example, it would allow you to run the `pyqrack` simulator instead of `cirq`'s own, which can be more efficient. |
| 272 | + |
| 273 | +# %% |
| 274 | +circuit = ghz_circuit(5) |
| 275 | +noisy_circuit = noise.transform_circuit(circuit, model=noise.GeminiOneZoneNoiseModel()) |
| 276 | + |
| 277 | +# %% |
| 278 | +kernel = squin.cirq.load_circuit(circuit, kernel_name="kernel") |
| 279 | +noisy_kernel = squin.cirq.load_circuit(noisy_circuit, kernel_name="noisy_kernel") |
| 280 | +kernel.print() |
0 commit comments