Skip to content

Commit

Permalink
Run notebooks through nbfmt
Browse files Browse the repository at this point in the history
Builds are failing on GitHub because some of the notebooks need to be
reformatted. Possibly this is a consequence of a version dependency
having changed somewhere. (The requirements.txt file does not pin
versions, and since the CI workflows use the same requirements.txt, a
new version of something there could influence the results.)
  • Loading branch information
mhucka committed Feb 16, 2025
1 parent 694523a commit cfb4d94
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 176 deletions.
158 changes: 91 additions & 67 deletions docs/kpz/kpz.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"cell_type": "code",
"execution_count": 1,
"id": "bfbe98f2",
"metadata": {},
"metadata": {
"id": "a3cdc6c47599"
},
"outputs": [],
"source": [
"##### Copyright 2023 The Cirq Developers"
Expand All @@ -14,7 +16,10 @@
"cell_type": "code",
"execution_count": 2,
"id": "161a0876",
"metadata": {},
"metadata": {
"cellView": "form",
"id": "906e07f6e562"
},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
Expand All @@ -33,15 +38,19 @@
{
"cell_type": "markdown",
"id": "9d29cea2",
"metadata": {},
"metadata": {
"id": "d895d2965a7e"
},
"source": [
"# Kardar-Parisi-Zhang Dynamics in the Heisenberg Spin Chain"
]
},
{
"cell_type": "markdown",
"id": "c4a1ff2c",
"metadata": {},
"metadata": {
"id": "c5cb019fcfbc"
},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
Expand All @@ -62,7 +71,9 @@
{
"cell_type": "markdown",
"id": "1de2dd3b",
"metadata": {},
"metadata": {
"id": "70f3cee60e01"
},
"source": [
"Before beginning, we will import the necessary modules into the notebook."
]
Expand All @@ -71,7 +82,9 @@
"cell_type": "code",
"execution_count": 3,
"id": "cef7a354",
"metadata": {},
"metadata": {
"id": "68eb3591185f"
},
"outputs": [],
"source": [
"try:\n",
Expand Down Expand Up @@ -99,7 +112,9 @@
{
"cell_type": "markdown",
"id": "f4110f55",
"metadata": {},
"metadata": {
"id": "186968984e10"
},
"source": [
"### Motivation and Background\n",
"\n",
Expand All @@ -124,7 +139,9 @@
"cell_type": "code",
"execution_count": 4,
"id": "b7477208",
"metadata": {},
"metadata": {
"id": "afd23812124c"
},
"outputs": [],
"source": [
"num_cycles = 2\n",
Expand All @@ -139,7 +156,9 @@
{
"cell_type": "markdown",
"id": "af80864e",
"metadata": {},
"metadata": {
"id": "53c75bc5619a"
},
"source": [
"By default, this uses `2*num_cycles` qubits, which is the minimum number of qubits needed for the transferred magnetization to be independent of system size (in the experiment we use 46 qubits and go to cycle 23). The `KPZExperiment` comes with the `num_init_states` initial bitstrings prepared:"
]
Expand All @@ -148,7 +167,9 @@
"cell_type": "code",
"execution_count": 5,
"id": "41c5584d",
"metadata": {},
"metadata": {
"id": "4194e9071392"
},
"outputs": [
{
"data": {
Expand All @@ -174,7 +195,9 @@
{
"cell_type": "markdown",
"id": "afabba4b",
"metadata": {},
"metadata": {
"id": "b65444f94259"
},
"source": [
"It also prepares the circuits that we need to run. For example, the circuit corresponding to the first initial state is:"
]
Expand All @@ -183,7 +206,9 @@
"cell_type": "code",
"execution_count": 6,
"id": "da71a7b2",
"metadata": {},
"metadata": {
"id": "00fc65e3cb43"
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -218,7 +243,9 @@
{
"cell_type": "markdown",
"id": "3fd3828b",
"metadata": {},
"metadata": {
"id": "0be4b1e23a86"
},
"source": [
"To run the experiment, we can either use `KPZExperiment.run_experiment()` or `KPZExperiment.run_experiment_amplitudes()`. In either case, we have to input a Cirq sampler. `run_experiment()` samples bitstrings, closer to what is done in the experiment, whereas `run_experiment_amplitudes()` uses amplitudes from a statevector simulation (and hence requires a `cirq.SimulatesAmplitudes` sampler). In the actual experiment, we apply a post-selection to mitigate against effects of $T_1$; this is not done here because this tutorial uses noiseless simulations.\n",
"\n",
Expand All @@ -229,7 +256,9 @@
"cell_type": "code",
"execution_count": 7,
"id": "d3a05a0b",
"metadata": {},
"metadata": {
"id": "1e320cda8aa4"
},
"outputs": [
{
"name": "stderr",
Expand All @@ -250,7 +279,9 @@
{
"cell_type": "markdown",
"id": "3df95b0e",
"metadata": {},
"metadata": {
"id": "babe2e86ac00"
},
"source": [
"The measured bitstrings are contained in `res_bitstrings.bitstrs`, which has shape `(num_init_states, reps, num_qubits)`"
]
Expand All @@ -259,7 +290,9 @@
"cell_type": "code",
"execution_count": 8,
"id": "b3d73f20",
"metadata": {},
"metadata": {
"id": "f9778320f6a5"
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -327,7 +360,9 @@
{
"cell_type": "markdown",
"id": "944ae2c9",
"metadata": {},
"metadata": {
"id": "b1321a162049"
},
"source": [
"As you can see, the number of excitations is conserved, as expected because fSim gates are number conserving."
]
Expand All @@ -336,7 +371,9 @@
"cell_type": "code",
"execution_count": 9,
"id": "0d41941f",
"metadata": {},
"metadata": {
"id": "6271edef408e"
},
"outputs": [],
"source": [
"num_excitations = np.sum(res_bitstrs.bitstrs, axis=2)\n",
Expand All @@ -348,7 +385,9 @@
{
"cell_type": "markdown",
"id": "c00b683a",
"metadata": {},
"metadata": {
"id": "9d4e374a5e4d"
},
"source": [
"The transferred magnetization, $\\mathcal{M}$ is computed by counting how many excitations are on the right side of the chain in the final state, contained in `res_bitstrs.num_right_final` (shape `(num_init_states, reps)`), and subtracting the number that were on the right initially, `res_bitstrs.num_right_initial` (shape `(num_init_states,)`), and then multiplying by 2. It is stored in `res_bitstrs.transferred_magnetization` (shape `(num_init_states, reps)`). We can plot a histogram of the transferred magnetization with:"
]
Expand All @@ -357,7 +396,9 @@
"cell_type": "code",
"execution_count": 10,
"id": "0627dbd5",
"metadata": {},
"metadata": {
"id": "74bed681e879"
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -387,7 +428,9 @@
{
"cell_type": "markdown",
"id": "a78c9395",
"metadata": {},
"metadata": {
"id": "27f0fda1436d"
},
"source": [
"The same histogram can be obtained without any error from finite `reps` by using `res_amplitudes`:"
]
Expand All @@ -396,7 +439,9 @@
"cell_type": "code",
"execution_count": 11,
"id": "18e2234d",
"metadata": {},
"metadata": {
"id": "68ad7583b8e7"
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -426,7 +471,9 @@
{
"cell_type": "markdown",
"id": "6d44c004",
"metadata": {},
"metadata": {
"id": "57f7723b3a54"
},
"source": [
"Finally, we are interested in the mean, variance, skewness, and kurtosis of this distribution. These are stored in `res_amplitudes.mean`, `res_amplitudes.variance`, `res_amplitudes.skewness`, and `res_amplitudes.kurtosis`, and similarly for `res_bitstrs`.\n",
"\n",
Expand All @@ -437,7 +484,9 @@
"cell_type": "code",
"execution_count": 12,
"id": "30507cb5",
"metadata": {},
"metadata": {
"id": "245cad3944b5"
},
"outputs": [
{
"name": "stdout",
Expand All @@ -460,7 +509,9 @@
{
"cell_type": "markdown",
"id": "d6c84778",
"metadata": {},
"metadata": {
"id": "a1644a042bd2"
},
"source": [
"For the paper, we ran exact density matrix simulations up to cycle 8, which give, for this choice of $t=2$, $\\mu=0.5$, and $(\\theta, \\phi) = (0.4\\pi, 0.8\\pi)$,\n",
"\n",
Expand All @@ -479,7 +530,9 @@
"cell_type": "code",
"execution_count": 13,
"id": "e572c635",
"metadata": {},
"metadata": {
"id": "b45b27be9ee3"
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -636,7 +689,9 @@
{
"cell_type": "markdown",
"id": "decdc752",
"metadata": {},
"metadata": {
"id": "e096bf94b21b"
},
"source": [
"Note that runtime is increasing exponentially $t$. The runtime, beyond $t=13$ or so, can be improved using GPUs if you have access to them, either using [qsim's built-in GPU backend](https://quantumai.google/reference/python/qsimcirq/QSimOptions), or [NVIDIA's cuQuantum](https://docs.nvidia.com/cuda/cuquantum/latest/appliance/cirq.html), the latter of which allows you to pool the resources of multiple GPUs. For instructions on picking hardware, see [this](https://quantumai.google/qsim/choose_hw) page, and for a tutorial on setting up the GPU backends, see [this](https://quantumai.google/qsim/tutorials/gcp_gpu) page. For the paper, we perform simulations to cycle 18 using 8 NVIDIA A100 GPUs.\n",
"\n",
Expand All @@ -647,7 +702,9 @@
"cell_type": "code",
"execution_count": 14,
"id": "46514834",
"metadata": {},
"metadata": {
"id": "e01de6de7b3b"
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -700,51 +757,18 @@
"\n",
"plt.tight_layout()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b51f1408",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0cdcec41",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9dd7aa82",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"name": "kpz.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
"nbformat_minor": 0
}
Loading

0 comments on commit cfb4d94

Please sign in to comment.