Skip to content

About

Time evolution of the 1D quantum harmonic oscillator with Trotterized quantum circuits in Qiskit

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Quantum Harmonic Oscillator Simulation: Trotterization

This repository contains a comprehensive, beginner-friendly Python project that simulates the time evolution of a particle in a 1D Quantum Harmonic Oscillator potential using Quantum Phase Estimation techniques, specifically Trotterized Quantum Circuit Evolution.

It compares the simulated quantum circuit evolution against the exact analytical solution to analyze the accuracy, fidelity, and resource requirements of the quantum simulation.


Table of Contents

  1. Theoretical Background
  2. Project Structure
  3. Setup and Installation
  4. Running the Simulation
  5. Understanding the Output

Theoretical Background

1. The Quantum Harmonic Oscillator

The system consists of a particle of mass $m$ subjected to a quadratic restoring force (a spring-like potential). The potential $V(x)$ is defined as:

$$ V(x) = \frac{1}{2} m \omega^2 x^2 $$

Where $\omega$ is the angular frequency of the oscillator. Unlike the infinite potential well, this potential extends from $x = -\infty$ to $x = \infty$.

The exact wavefunctions (eigenstates) and energy levels (eigenvalues) for this system are given analytically by:

$$ \phi_n(x) = \frac{1}{\sqrt{2^n n!}} \left(\frac{m\omega}{\pi\hbar}\right)^{1/4} \exp\left(-\frac{m\omega x^2}{2\hbar}\right) H_n\left(\sqrt{\frac{m\omega}{\hbar}} x\right) $$

Where $H_n(y)$ are the Hermite polynomials. The quantized energy levels are evenly spaced:

$$ E_n = \hbar \omega \left(n + \frac{1}{2}\right) \quad \text{for } n = 0, 1, 2, \ldots $$

Any initial state $|\psi(0)\rangle$ can be expanded in this basis to undergo exact time evolution:

$$ |\psi(t)\rangle = \sum_n c_n e^{-i E_n t / \hbar} |\phi_n\rangle $$

2. Hamiltonian Discretization

To simulate this on a quantum computer, we must first discretize continuous space into a grid of $N$ points (where $N = 2^q$ for $q$ qubits). We bound the grid tightly (for example, from $-x_{\max}$ to $x_{\max}$) because the wavefunctions decay exponentially at large $|x|$.

We use the finite difference method to approximate the kinetic energy operator $\hat{T} = -\frac{\hbar^2}{2m} \frac{d^2}{dx^2}$. On a grid with spacing $\Delta x$:

$$ T_{i,i} = \frac{\hbar^2}{m \Delta x^2}, \quad T_{i,i\pm1} = -\frac{\hbar^2}{2m \Delta x^2} $$

The potential energy $\hat{V}$ is simply a diagonal matrix in the position basis:

$$ V_{i,i} = \frac{1}{2} m \omega^2 x_i^2 $$

The full Hamiltonian is $\hat{H} = \hat{T} + \hat{V}$.

3. Pauli Decomposition

Quantum computers operate using fundamental gates acting on qubits. Any Hermitian matrix (like our discretized $H$) can be decomposed into a weighted sum of Pauli strings ($I, X, Y, Z$):

$$ H = \sum_{k=1}^M c_k P_k $$

Where $c_k$ are real coefficients and $P_k$ are tensor products of Pauli matrices acting on the $q$ qubits. For the harmonic oscillator, the combination of a tridiagonal kinetic term and a diagonal quadratic potential results in a specific combination of mostly $Z$ strings (from $V$) and $X/Y$ strings (from $T$).

4. Trotterization

The time evolution operator is $U(t) = e^{-iHt/\hbar}$. Because the Pauli strings $P_k$ generally do not commute, we cannot simply split the exponent. We approximate the evolution over a small time step $\Delta t$ using the Suzuki-Trotter expansion.

First-Order Trotter:

$$ U(\Delta t) \approx \prod_{k=1}^{M} \exp\left(-i c_k P_k \frac{\Delta t}{\hbar}\right) $$

Second-Order Trotter: (More accurate, symmetric)

$$ U(\Delta t) \approx \prod_{k=1}^{M} \exp\left(-i c_k P_k \frac{\Delta t}{2\hbar}\right)\prod_{k=M}^{1} \exp\left(-i c_k P_k \frac{\Delta t}{2\hbar}\right) $$

The full simulation repeats this Trotter step $r$ times to reach the total time $t = r \Delta t$.

5. Circuit Implementation

Each exponential term $\exp(-i \theta P_k)$ is implemented in a quantum circuit using a Parity Gadget (CNOT staircase). For example, if $P_k = X \otimes Z \otimes Y$:

  1. Apply basis change gates to map $X \to Z$ and $Y \to Z$.
  2. Use a sequence of CNOT gates to encode the parity of the qubits into a target qubit.
  3. Apply a single $R_z(2\theta)$ rotation on the target qubit.
  4. Uncompute the CNOTs and the basis change gates.

Project Structure

  • main.py : The central script. Configures parameters, initializes a displaced (or coherent) state, orchestrates the simulation steps, and calls plotting functions.
  • physics.py : Computes the exact, analytical benchmark. Handles eigenvalues, Hermite polynomial eigenstates, and analytical time-evolution.
  • trotter.py : Handles Hamiltonian discretization (kinetic + quadratic potential), Pauli decomposition, and performs fast matrix-based Trotter evolution.
  • circuit.py : Translates the Trotter steps into actual Qiskit QuantumCircuit objects and counts logical gate resources (depth, CNOTs).
  • analysis.py : Calculates fidelity, $L_2$ errors, and verifies physical constraints (energy and norm conservation).
  • visualization.py : Contains functions for plotting static graphs and generating animated GIFs of the wavepacket evolution.
  • output/ : Directory where all generated plots, diagrams, and animations are saved.

Setup and Installation

  1. Clone the repository:

    git clone https://github.com/TSS99/QHO_Simulation_Trotterization.git
    cd QHO_Simulation_Trotterization
  2. Create a Python virtual environment (recommended):

    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  3. Install dependencies: This project relies on numpy, scipy, matplotlib, and qiskit.

    pip install -r requirements.txt

Running the Simulation

Simply execute the main script:

python main.py

The script will automatically:

  1. Initialize a Gaussian wavepacket (often offset so it oscillates back and forth).
  2. Calculate the exact analytical time-evolution.
  3. Discretize the harmonic oscillator Hamiltonian and decompose it into Pauli strings.
  4. Run the Trotterized quantum simulation.
  5. Compute fidelities and errors.
  6. Generate resource metrics for varying grid sizes.
  7. Save plots and an animation in the output/ directory.

Understanding the Output

After running the simulation, check the output/ folder for these files:

  • initial_state.png: Compares the continuous analytical wavepacket with the discretized starting state on the grid.
  • probability_snapshots.png: Side-by-side comparison of the probability density between the Exact Evolution and the Trotterized Quantum Circuit at specific intervals as the wavepacket moves.
  • fidelity_vs_time.png and error_vs_time.png: Tracks how closely the quantum simulation matches the exact answer as time progresses.
  • physics_validation.png: Verifies that the Trotter simulation preserves total probability (norm $\approx 1$) and energy ($\langle H \rangle \approx \text{const}$).
  • convergence_dt.png (or vs $N$): Shows how scaling the grid size or Trotter steps improves the fidelity.
  • resource_scaling.png: Plots the required number of CNOT blocks and circuit depth required on a real quantum computer against grid size.
  • evolution_animation.gif: An animated GIF showing the wavepacket oscillation inside the parabolic potential.
  • circuit_diagram.png: A snapshot of the generated Qiskit circuit for a small subset of the simulation.

About

Time evolution of the 1D quantum harmonic oscillator with Trotterized quantum circuits in Qiskit

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages