<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged jax</title>
<link>https://coderlegion.com/tag/jax</link>
<description>Powered by Question2Answer</description>
<item>
<title>Differentiable Quantum Simulation at TPU Scale: Inside the JAX Quantum Research Suite</title>
<link>https://coderlegion.com/19546/differentiable-quantum-simulation-at-tpu-scale-inside-the-jax-quantum-research-suite</link>
<description>&lt;h2 id=&quot;differentiable-quantum-simulation-at-tpu-scale-inside-the-jax-quantum-research-suite&quot;&gt;Differentiable Quantum Simulation at TPU Scale: Inside the JAX Quantum Research Suite&lt;/h2&gt;
&lt;p&gt;Classical simulation of quantum systems is the bedrock of quantum algorithm design. However, as researchers push the limits of the Noisy Intermediate-Scale Quantum (NISQ) era, they face two major roadblocks: &lt;strong&gt;memory scaling limits&lt;/strong&gt; for large qubit counts, and the &lt;strong&gt;gradient bottleneck&lt;/strong&gt; in training variational quantum algorithms.&lt;/p&gt;
&lt;p&gt;To address these challenges, we introduce the &lt;strong&gt;JAX Quantum Research Suite&lt;/strong&gt;—a high-performance, differentiable quantum circuit simulator designed to run seamlessly from entry-level consumer GPUs up to distributed Cloud TPU VM clusters. &lt;/p&gt;
&lt;p&gt;&lt;em&gt;This work and its large-scale benchmarks are supported by the &lt;strong&gt;Google TPU Research Cloud (TRC) Program&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;the-core-philosophy-why-jax-amp-cloud-tpus-&quot;&gt;The Core Philosophy: Why JAX &amp;amp; Cloud TPUs?&lt;/h3&gt;
&lt;p&gt;Traditional simulators like Google's &lt;code&gt;qsim&lt;/code&gt; or NVIDIA's &lt;code&gt;cuQuantum&lt;/code&gt; are highly optimized in C++/CUDA but lack native, end-to-end differentiability. On the other hand, frameworks like PennyLane often rely on the &lt;strong&gt;Parameter-Shift Rule (PSR)&lt;/strong&gt; to compute gradients. For a circuit with $P$ parameters, PSR requires $2P$ distinct circuit evaluations, creating an $O(P)$ bottleneck that makes training deep variational circuits extremely slow.&lt;/p&gt;
&lt;p&gt;By building our simulator in pure JAX (&lt;code&gt;jax.numpy&lt;/code&gt; and &lt;code&gt;jax.lax&lt;/code&gt;), we compile quantum gate operations directly into XLA (Accelerated Linear Algebra) High-Level Operations bytecode. This grants us four major capabilities out of the box:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Monolithic JIT Compilation (&lt;code&gt;@jax.jit&lt;/code&gt;)&lt;/strong&gt;: Bypasses Python interpreter overhead, running at compiled speeds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reverse-Mode Automatic Differentiation (&lt;code&gt;jax.grad&lt;/code&gt;)&lt;/strong&gt;: Computes all $P$ gradients in a single backward pass—an $O(1)$ scaling factor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Seamless Vectorization (&lt;code&gt;jax.vmap&lt;/code&gt;)&lt;/strong&gt;: Parallelizes execution over batches of parameters or quantum trajectories (e.g., Monte Carlo noise simulation) in a single fused hardware kernel.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Out-of-the-Box Multi-Device Distribution (&lt;code&gt;jax.sharding.PositionalSharding&lt;/code&gt;)&lt;/strong&gt;: Splits massive statevectors across distributed HBM without writing a single line of C++ or MPI code.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h3 id=&quot;three-key-engineering-contributions-for-scale&quot;&gt;Three Key Engineering Contributions for Scale&lt;/h3&gt;
&lt;p&gt;Simulating large systems or deep circuits on TPUs introduces severe compiler and memory limitations. We solved these with three targeted engineering optimizations:&lt;/p&gt;
&lt;h4 id=&quot;1-multi-device-positional-sharding&quot;&gt;1. Multi-Device Positional Sharding&lt;/h4&gt;
&lt;p&gt;A 33-qubit statevector requires &lt;strong&gt;64 GB&lt;/strong&gt; of memory, while a 36-qubit statevector requires &lt;strong&gt;549.76 GB&lt;/strong&gt;. To scale beyond the memory limits of a single device, we partition the statevector's leading dimensions across physical TPU chips using &lt;code&gt;PositionalSharding&lt;/code&gt;. Local gates run at maximum memory bandwidth, and cross-shard operations are handled via XLA collective communication over TPU Inter-Chip Interconnects (ICI).&lt;/p&gt;
&lt;h4 id=&quot;2-o-1-compiler-graph-size-via-lax-fori-loop&quot;&gt;2. $O(1)$ Compiler Graph Size via &lt;code&gt;lax.fori_loop&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;Standard Python loops inside JIT compilation unroll fully. For deep circuits, this results in millions of HLO nodes, causing the compiler host CPU to crash with Out-Of-Memory (OOM) errors before execution even begins. By wrapping our layers in &lt;code&gt;jax.lax.fori_loop&lt;/code&gt;, we compile the step logic once, keeping the compiler graph size $O(1)$ regardless of circuit depth.&lt;/p&gt;
&lt;h4 id=&quot;3-o-1-backpropagation-memory-via-jax-checkpoint&quot;&gt;3. $O(1)$ Backpropagation Memory via &lt;code&gt;jax.checkpoint&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;Evaluating gradients in reverse-mode autodiff requires storing intermediate states for the backward pass. For large-scale multi-layer variational circuits, this quickly runs out of High Bandwidth Memory (HBM). By wrapping layers in &lt;code&gt;jax.checkpoint&lt;/code&gt;, intermediate states are discarded during the forward pass and recomputed on-the-fly, reducing the memory footprint from $O(\text{depth})$ to $O(1)$ at the cost of a single extra forward pass.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;benchmark-highlights&quot;&gt;Benchmark Highlights&lt;/h3&gt;
&lt;p&gt;We validated our simulator across three hardware tiers: an entry-level consumer GPU (NVIDIA RTX 2050, 4 GB VRAM), a 16-chip Cloud TPU v5e mesh, and a 64-chip Cloud TPU v6e cluster.&lt;/p&gt;
&lt;h4 id=&quot;1-algorithmic-gradient-speedups&quot;&gt;1. Algorithmic Gradient Speedups&lt;/h4&gt;
&lt;p&gt;On a 15-qubit Hardware-Efficient Ansatz (120 trainable parameters) on CPU:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;jax.grad&lt;/code&gt; (Reverse-Mode AD)&lt;/strong&gt;: Computes all gradients in &lt;strong&gt;37.5 ms&lt;/strong&gt; (stable post-JIT mean).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parameter-Shift Rule (PSR)&lt;/strong&gt;: Requires &lt;strong&gt;1,826 ms&lt;/strong&gt; to compute the same gradients.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Result&lt;/strong&gt;: A &lt;strong&gt;48.7× gradient speedup&lt;/strong&gt; over PSR. On a smaller 50-parameter GPU circuit, this gap expands to &lt;strong&gt;~75×&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;vs. PennyLane JAX&lt;/strong&gt;: Our simulator achieves a &lt;strong&gt;4× speedup&lt;/strong&gt; over PennyLane's own JAX backend because our monolithic kernel unrolling avoids Python-level dispatch overheads.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;2-maximum-qubit-thresholds-by-hardware&quot;&gt;2. Maximum Qubit Thresholds by Hardware&lt;/h4&gt;
&lt;p&gt;By utilizing distributed memory, we pushed statevector simulation limits to their theoretical boundaries:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Hardware &lt;/th&gt;&lt;th&gt;Max Qubits Simulated &lt;/th&gt;&lt;th&gt;Statevector Memory &lt;/th&gt;&lt;th&gt;Algorithm / Experiment&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;NVIDIA RTX 2050 (4 GB VRAM)&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;29 Qubits &lt;/td&gt;&lt;td&gt;4.29 GB &lt;/td&gt;&lt;td&gt;Statevector Baseline&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Cloud TPU v5e-16 (256 GB HBM)&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;33 Qubits &lt;/td&gt;&lt;td&gt;64.00 GB &lt;/td&gt;&lt;td&gt;Shor's Order-Finding QFT&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Cloud TPU v6e-64 (~2.0 TB HBM)&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;36 Qubits &lt;/td&gt;&lt;td&gt;549.76 GB &lt;/td&gt;&lt;td&gt;Grover's Search Algorithm&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;(Note: For random circuit sampling (RCS) at 37 qubits, we utilize tensor-network amplitude sampling via TensorCircuit with a JAX backend, scaling to 2,048 samples per batch on TPU v6e-64.)&lt;/em&gt;&lt;/p&gt;
&lt;h4 id=&quot;3-matrix-product-states-mps-amp-svd-stability&quot;&gt;3. Matrix Product States (MPS) &amp;amp; SVD Stability&lt;/h4&gt;
&lt;p&gt;For systems exceeding statevector limits (512 to 1024 qubits), we implemented a differentiable Matrix Product State (MPS) simulator. Differentiating through Singular Value Decomposition (SVD) in JAX is notoriously unstable due to Wirtinger calculus singularities at degenerate singular values. We resolved this by implementing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;SVD Jitter&lt;/strong&gt;: Injecting $10^{-9}$ complex noise to break singular value degeneracy.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Site-Level Normalization&lt;/strong&gt;: Preventing exponential amplitude drift.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Wirtinger Gradient Clipping&lt;/strong&gt;: Keeping real components bounded to avoid NaN divergence.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These fixes enabled stable convergence of a &lt;strong&gt;1024-qubit MPS-VQE&lt;/strong&gt; over 10,000 training epochs.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;get-started-in-10-lines-of-code&quot;&gt;Get Started in 10 Lines of Code&lt;/h3&gt;
&lt;p&gt;Here is how simple it is to build a circuit, compile it, and compute exact gradients using the JAX Quantum Research Suite:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;from jax_qsim.circuit import Circuit
import jax, jax.numpy as jnp

# Initialize a 4-qubit circuit
c = Circuit(num_qubits=4)
c.h(0).cnot(0, 1).ry(2, param_index=0).rz(3, param_index=1)

# Compile the execution and gradient functions
@jax.jit
def loss_fn(params):
    state = c.run(params, state_type='statevector')
    # Measure expectation value of Z on qubit 1
    return jnp.real(state[2])  # Simple toy expectation

grad_fn = jax.jit(jax.grad(loss_fn))

# Evaluate gradients instantly in a single backward pass
params = jnp.array([0.5, 1.2])
gradients = grad_fn(params)
print(&quot;Gradients:&quot;, gradients)
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;h3 id=&quot;acknowledgements-amp-open-source&quot;&gt;Acknowledgements &amp;amp; Open Source&lt;/h3&gt;
&lt;p&gt;This project is fully open-source and released under the MIT License. &lt;/p&gt;
&lt;p&gt;We extend our deepest gratitude to the &lt;strong&gt;Google TPU Research Cloud (TRC) Program&lt;/strong&gt; for providing the TPU v5e and TPU v6e resources that made our distributed scaling benchmarks possible.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/AshiteshSingh/Tpu-Accelerated-Quantum-JAX&quot;&gt;Codebase &amp;amp; Benchmarks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://ashitesh.me&quot;&gt;For More Details&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/19546/differentiable-quantum-simulation-at-tpu-scale-inside-the-jax-quantum-research-suite</guid>
<pubDate>Wed, 03 Jun 2026 08:45:51 +0000</pubDate>
</item>
</channel>
</rss>