Coding implementation of quantum state evolution, de-harmonic and entanglement dynamics using Qutip
In this advanced qutip Tutorial, we explore the rich dynamics of quantum systems using Python and Qutip frameworks. First, we will prepare the basic single quantities and two points (including bell pairs) and then proceed with key quantum operations such as Pauli matrix, Hadamard Gates, and CNOT. From there, we will simulate Rabi oscillations in a driven two-stage system, study the evolution of coherent states in quantum harmonic oscillators, and deformation of the model in an open system. We will visualize phase space trajectories through the Wigner function and quantify the entanglement generation between coupled qubits. At the end of this tutorial, you will build a complete workflow in the COLAB environment for state preparation, time evolution, open system dynamics and entanglement analysis. Check The complete code is here.
!pip install qutip matplotlib numpy
import numpy as np
import matplotlib.pyplot as plt
from qutip import *
print("🔬 Advanced QuTip Tutorial: Quantum Dynamics & Entanglement")
print("=" * 60)
During the setup phase, we install QUTIP with Numpy and Matplotlib to ensure that our Colab environment has all the necessary libraries for quantum simulation. This step lays the foundation for subsequent imports and ensures the repeatability of our results. Check The complete code is here.
print("n1. Creating Quantum States")
ground = basis(2, 0)
excited = basis(2, 1)
plus = (ground + excited).unit()
minus = (ground - excited).unit()
print(f"Ground state |0⟩: {ground.dag()}")
print(f"Superposition |+⟩: {plus.dag()}")
bell_phi_plus = (tensor(ground, ground) + tensor(excited, excited)).unit()
bell_psi_minus = (tensor(ground, excited) - tensor(excited, ground)).unit()
print(f"nBell state |Φ+⟩ = (|00⟩ + |11⟩)/√2")
rho_bell = bell_phi_plus * bell_phi_plus.dag()
print(f"Entanglement measure: {concurrence(rho_bell):.3f}")
We first define the computational base states |0 and |1 and then construct their superpositions |+ and | – to illustrate the basic quantum operation. We then create the bell state to prove the maximum entanglement, computing its concurrency to quantify the entanglement. Check The complete code is here.
print("n2. Quantum Gates and Operations")
sx, sy, sz = sigmax(), sigmay(), sigmaz()
print(f"Pauli-X matrix:n{sx}")
hadamard = (sx + sz) / np.sqrt(2)
cnot = tensor(fock_dm(2, 0), qeye(2)) + tensor(fock_dm(2, 1), sx)
h_ground = hadamard * ground
print(f"nH|0⟩ = {h_ground.dag()}")
We use Pauli operators σₓ, σᵧ and σ_z as basic building blocks for quantum rotation and reflection. Using these, we build Hadamard Gate for overlay generation and CNOT gates, for entanglement operations, applying them to the state we prepared. Check The complete code is here.
print("n3. Quantum Dynamics: Rabi Oscillations")
omega_0 = 1.0
omega_r = 0.5
H = 0.5 * omega_0 * sz + 0.5 * omega_r * sx
t_list = np.linspace(0, 4*np.pi/omega_r, 100)
psi0 = ground
result = mesolve(H, psi0, t_list, [], [])
excited_pop = [expect(fock_dm(2, 1), state) for state in result.states]
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(t_list, excited_pop, 'b-', linewidth=2)
plt.xlabel('Time (ℏ/ω)')
plt.ylabel('Excited State Population')
plt.title('Rabi Oscillations')
plt.grid(True, alpha=0.3)
We model the driven two-stage system using the Hamiltonian term σ_z and σₓ to simulate the driven two-stage system. Through the basic state under this Hamiltonian state, we track the excited population oscillations and visualize them in the full rabies cycle. Check The complete code is here.
print("n4. Quantum Harmonic Oscillator")
N = 20
a = destroy(N)
H_ho = a.dag() * a + 0.5
alpha = 2.0
psi0_coh = coherent(N, alpha)
t_list_ho = np.linspace(0, 2*np.pi, 50)
result_ho = mesolve(H_ho, psi0_coh, t_list_ho, [], [])
x_op = (a + a.dag()) / np.sqrt(2)
p_op = 1j * (a.dag() - a) / np.sqrt(2)
x_expect = [expect(x_op, state) for state in result_ho.states]
p_expect = [expect(p_op, state) for state in result_ho.states]
plt.subplot(1, 2, 2)
plt.plot(x_expect, p_expect, 'r-', linewidth=2)
plt.plot(x_expect[0], p_expect[0], 'go', markersize=8, label="Start")
plt.plot(x_expect[-1], p_expect[-1], 'ro', markersize=8, label="End")
plt.xlabel('⟨x⟩')
plt.ylabel('⟨p⟩')
plt.title('Coherent State Phase Space')
plt.legend()
plt.grid(True, alpha=0.3)
plt.axis('equal')
plt.tight_layout()
plt.show()
We will extend the study to N-level harmonic oscillators, initializing a coherent state and developing according to standard Hamiltonians. We compute and plot phase trajectories⟨X⟩ and⟨P to observe classical motion in quantum states. Check The complete code is here.
print("n5. Quantum Decoherence and Open Systems")
gamma = 0.2
n_th = 0.1
c_ops = [np.sqrt(gamma * (1 + n_th)) * a, np.sqrt(gamma * n_th) * a.dag()]
psi0_sq = squeeze(N, 0.5) * basis(N, 0)
t_list_damp = np.linspace(0, 10, 100)
result_damp = mesolve(H_ho, psi0_sq, t_list_damp, c_ops, [])
n_expect = [expect(a.dag() * a, state) for state in result_damp.states]
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.plot(t_list_damp, n_expect, 'g-', linewidth=2)
plt.xlabel('Time')
plt.ylabel('⟨n⟩')
plt.title('Photon Number Decay')
plt.grid(True, alpha=0.3)
We introduce dissipation through the collapse operator through the collapse harmonic oscillator and simulate interaction with the thermal environment. We then evolved the initial extrusion state and monitored the attenuation of the number of photons⟨n⟩ to illustrate the deformation effect. Check The complete code is here.
print("n6. Wigner Function Visualization")
final_state = result_damp.states[-1]
xvec = np.linspace(-4, 4, 50)
W_final = wigner(final_state, xvec, xvec)
plt.subplot(1, 2, 2)
plt.contourf(xvec, xvec, W_final, 20, cmap='RdBu')
plt.colorbar(label="W(x,p)")
plt.xlabel('x')
plt.ylabel('p')
plt.title('Wigner Function (Final State)')
plt.tight_layout()
plt.show()
We compute the Wigner quasi-probability distribution of the final damping state on the grid in phase space. By performing contour playback of W(x,p), we gain intuitive insights into non-cellular features and the effect of anti-harmonic solutions on state coherence. Check The complete code is here.
print("n7. Entanglement Dynamics")
omega1, omega2 = 1.0, 1.1
g = 0.1
H_coupled = (omega1/2 * tensor(sz, qeye(2)) +
omega2/2 * tensor(qeye(2), sz) +
g * tensor(sx, sx))
psi0_prod = tensor(plus, ground)
t_list_ent = np.linspace(0, 20, 200)
result_ent = mesolve(H_coupled, psi0_prod, t_list_ent, [], [])
entanglement = [concurrence(state * state.dag()) for state in result_ent.states]
plt.figure(figsize=(8, 5))
plt.plot(t_list_ent, entanglement, 'purple', linewidth=2)
plt.xlabel('Time')
plt.ylabel('Concurrence')
plt.title('Entanglement Generation in Coupled Qubits')
plt.grid(True, alpha=0.3)
plt.ylim(0, 1)
plt.show()
We pair two qubits with σₓ⊗σₓ and evolve an initial product state to measure concurrency in each time step. This allows us to observe the attenuation of stacking and entanglement in real time, a function of coupling strength and dissonance. Check The complete code is here.
print("n8. Summary of Advanced Features Demonstrated:")
print("✓ Quantum state preparation and manipulation")
print("✓ Time evolution with mesolve()")
print("✓ Rabi oscillations in two-level systems")
print("✓ Coherent states and harmonic oscillators")
print("✓ Open quantum systems with decoherence")
print("✓ Wigner function visualization")
print("✓ Entanglement quantification and dynamics")
print(f"n🎯 Tutorial complete! Explored {len(t_list_ent)} time steps")
print("Try modifying parameters to explore different quantum phenomena!")
print("n💡 Advanced Exercises:")
print("1. Implement quantum error correction codes")
print("2. Simulate quantum algorithms (Grover, Shor)")
print("3. Explore cavity QED with Jaynes-Cummings model")
print("4. Study quantum phase transitions")
print("5. Implement quantum feedback control")
We review key demonstrations, state preparation, gate operations, time evolution, antiharmonic modeling, phase space visualization, and entanglement quantization, and highlight how Qutip’s Mesolve() and built-in features simplify these workflows. Finally, we propose advanced exercises to deepen the exploration of quantum phenomena.
In summary, using Qutip’s intuitive API, we experience the cornerstone phenomena of quantum mechanics, from superposition and entanglement to decorative and phase visualization. Along the way, we prepared quantum states, applied the gate, solved the time-dependent dynamics with Mesolve(), and quantified the entanglement with concurrent quantization. Our examples of coherent states and damping oscillators reveal classical analogs in quantum phase space, while coupling volume simulations show the growth of entangled real-time. We encourage you to adjust parameters such as coupling advantages, suppression rates, and initial state to deepen your understanding.
Check The complete code is here. Check out ours anytime Tutorials, codes and notebooks for github pages. Also, please stay tuned for us twitter And don’t forget to join us 100K+ ml reddit And subscribe Our newsletter.
Sana Hassan, a consulting intern at Marktechpost and a dual-degree student at IIT Madras, is passionate about applying technology and AI to address real-world challenges. He is very interested in solving practical problems, and he brings a new perspective to the intersection of AI and real-life solutions.