Demonstration notebook for RCS and XEB analysis#469
Demonstration notebook for RCS and XEB analysis#469shashwatk1998 wants to merge 17 commits intoquantumlib:mainfrom
Conversation
This configures Gemini Code Assist to be a little bit less noisy and verbose. Based on experiences with other repos, I find this makes Gemini more useful.
And make it bit more apparent how is `RCSResults._fidelities_lin` set. Fixes mypy recirq/random_circuit_sampling | grep random_circuit
Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
| } | ||
| }, | ||
| { | ||
| "cell_type": "code", |
There was a problem hiding this comment.
The metadata in the JSON is configured for Google Colab ("colab": {"provenance": []}). So since this notebook is Colab-friendly, should we add a commented-out '!pip install cirq cirq-google qsimcirq' cell at the top and also a comment on how to install recirq + restarting kernel? It might save external users a few steps if they try to run this directly from the repo.
| "source": [ | ||
| "# Calculate linear XEB fidelities\n", | ||
| "\n", | ||
| "To calculate fidelities via noiseless simulation, we can employ the standard `cirq.Simulator()`. For experiments involving larger system sizes, the more performance-optimized `qsimcirq.QSimSimulator()` can be used to improve execution efficiency." |
There was a problem hiding this comment.
In the "Calculate linear XEB fidelities" markdown cell, you state: "To calculate fidelities via noiseless simulation, we can employ the standard cirq.Simulator()". However, the code cell right after it actually uses qsimcirq.QSimSimulator(). You might just want to slightly tweak the markdown text so it matches your code execution seamlessly (e.g., "Here, we use the performance-optimized qsimcirq.QSimSimulator() to improve execution efficiency.").
There was a problem hiding this comment.
Thanks, done!
| "source": [ | ||
| "# Plot the results\n", | ||
| "\n", | ||
| "Visualize the XEB results for each patch across the specified circuit depths. The plot reflects the mean fidelity calculated across all random circuit instances for each (patch, depth) configuration." |
There was a problem hiding this comment.
The plotted fidelity is perfectly flat at around 1.0 because characterize=False and used for default, noiseless cirq.Simulator(). On real quantum hardware experiment (or a noisy simulation), errors would accumulate as the circuit gets deeper. Since this is a tutorial, a beginner who is familiar with real hardware RCS might be confused why there's no exponential decay curve as one would expect on real QC hardware. Would it be worth adding a quick sentence above the plot explicitly stating that we see a flat 1.0 line because this is an ideal, noiseless simulation? Unless you think users immediately understand this behavior for noiseless simulations. Here a proposal: "Note on Ideal vs. Noisy Simulation: You might notice that the plotted linear XEB fidelity is flat at around 1.0 across all circuit depths. This behavior is exactly what we expect from an ideal, noiseless simulation. In a real quantum hardware experiment (or a noisy simulation), incoherent errors and decoherence accumulate at each gate layer, resulting in a characteristic exponential decay curve as the circuit gets deeper. Since our simulated qubits do not suffer from any noise, the fidelity remains at ~1.0 (with microscopic fluctuations resulting purely from finite sampling variance across our 10,000 repetitions)."
There was a problem hiding this comment.
@shashwatk1998 maybe it's worth adding results from the noisy simulator too? You could use the QVM here https://quantumai.google/cirq/simulate/qvm_basic_example. If the number of qubits isn't too large, it might run faster with cirq.DensityMatrixSimulator instead of the statevector simulators, which will use quantum trajectories.
There was a problem hiding this comment.
Added the note and also added noisy simulation
| "cell_type": "code", | ||
| "source": [ | ||
| "# To run this notebook in Google Colab, uncomment and run the following line:\n", | ||
| "#!pip install cirq cirq_google qsimcirq\n", |
There was a problem hiding this comment.
Do you actually use qsimcirq?
There was a problem hiding this comment.
Yes, for calculating fidelity in noiseless simulation.
fidelities = results.fidelities_lin(simulator=qsimcirq.QSimSimulator())
| "noise_props = cirq_google.engine.load_device_noise_properties(processor_id)\n", | ||
| "noise_model = cirq_google.NoiseModelFromGoogleNoiseProperties(noise_props)\n", | ||
| "sim = cirq.DensityMatrixSimulator(noise=noise_model)" |
There was a problem hiding this comment.
sim is used in the definition of sim_processor
sim_processor = cirq_google.engine.SimulatedLocalProcessor( processor_id=processor_id, sampler=sim, device=device, calibrations={cal.timestamp // 1000: cal} )
| ")\n", | ||
| "sim_engine = cirq_google.engine.SimulatedLocalEngine([sim_processor])\n", | ||
| "qvm_sampler = sim_engine.get_sampler(processor_id)\n", | ||
| "print(sim_engine.get_processor(processor_id).get_device())" |
There was a problem hiding this comment.
This line is a little funny. You could have just done print(device)
There was a problem hiding this comment.
Fair. Have been following how QVM is defined: https://quantumai.google/cirq/simulate/quantum_virtual_machine
| "cal = cirq_google.engine.load_median_device_calibration(processor_id)\n", | ||
| "sim_processor = cirq_google.engine.SimulatedLocalProcessor(\n", | ||
| " processor_id=processor_id, sampler=sim, device=device, calibrations={cal.timestamp // 1000: cal}\n", | ||
| ")\n", | ||
| "sim_engine = cirq_google.engine.SimulatedLocalEngine([sim_processor])\n", | ||
| "qvm_sampler = sim_engine.get_sampler(processor_id)\n", |
There was a problem hiding this comment.
Instead of all of these lines, you could just use sim that you got above instead of qvm_sampler
There was a problem hiding this comment.
Agreed, I had followed how it is in qvm documentation: https://quantumai.google/cirq/simulate/quantum_virtual_machine
Will update to use sim directly.
| "\n", | ||
| "\n", | ||
| "ax.set_xlim(min(noisy_experiment.depths) - 2, max(noisy_experiment.depths) + 2)\n", | ||
| "ax.set_xlabel('Cycle Depth', fontsize=14)\n", |
There was a problem hiding this comment.
Maybe "Number of cycles,
There was a problem hiding this comment.
Sure, will do!
| "# Define the patches\n", | ||
| "\n", | ||
| "Select sets of connected qubits to serve as patches. These patches must be disjoint and should not share any common qubits." | ||
| ], |
There was a problem hiding this comment.
What is the purpose of the patches? What are they used for in Nature 574, 505–510 (2019)? Give a bit more motivation here.
There was a problem hiding this comment.
Thanks, will add description.
| "for i in range(len(patches_on_device)):\n", | ||
| " x = noisy_experiment.depths\n", | ||
| " y = [np.mean(noisy_fidelities[(i, d)]) for d in x]\n", | ||
| " ax.plot(x, y, marker='o', markersize=8, linewidth=2, label=f'Patch {i}')\n", |
There was a problem hiding this comment.
Can we add error bars to this plot and then put it on a log-y scale, restricting to cycle numbers where you can distinguish the fidelity from 0?
No description provided.