Skip to content

Demonstration notebook for RCS and XEB analysis#469

Open
shashwatk1998 wants to merge 17 commits intoquantumlib:mainfrom
shashwatk1998:rcs_v0
Open

Demonstration notebook for RCS and XEB analysis#469
shashwatk1998 wants to merge 17 commits intoquantumlib:mainfrom
shashwatk1998:rcs_v0

Conversation

@shashwatk1998
Copy link
Copy Markdown
Contributor

No description provided.

Shashwat Kumar and others added 14 commits February 27, 2026 22:58
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>
@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link
Copy Markdown

@deltorobarba deltorobarba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved comments directly to code positions

}
},
{
"cell_type": "code",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

"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."
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.").

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you actually use qsimcirq?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for calculating fidelity in noiseless simulation.

fidelities = results.fidelities_lin(simulator=qsimcirq.QSimSimulator())

Comment on lines +259 to +261
"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)"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sim is unused

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is a little funny. You could have just done print(device)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. Have been following how QVM is defined: https://quantumai.google/cirq/simulate/quantum_virtual_machine

Comment on lines +273 to +278
"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",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of all of these lines, you could just use sim that you got above instead of qvm_sampler

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "Number of cycles, $m$" to match Fig 4b from Nature 574, 505–510 (2019).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do!

Comment on lines +73 to +76
"# 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."
],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of the patches? What are they used for in Nature 574, 505–510 (2019)? Give a bit more motivation here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants