Machine Learning Applications in Physics

Introduction to Physics & ML

Machine Learning is revolutionizing physics research, from data analysis to theoretical predictions.

Basic ML Setup for Physics


import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split

# Generate synthetic physics data
def generate_physics_data(n_samples=1000):
    # Simple harmonic motion with noise
    t = np.linspace(0, 10, n_samples)
    omega = 2.0
    x = np.sin(omega * t)
    x_noisy = x + np.random.normal(0, 0.1, n_samples)
    return t.reshape(-1, 1), x_noisy.reshape(-1, 1)

# Prepare data
X, y = generate_physics_data()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
                    

Neural Networks for Physics

Learn how to use neural networks to model physical systems.

Physics-Informed Neural Network


import tensorflow as tf

# Create a simple physics-informed neural network
def create_pinn_model():
    model = tf.keras.Sequential([
        tf.keras.layers.Dense(64, activation='tanh'),
        tf.keras.layers.Dense(32, activation='tanh'),
        tf.keras.layers.Dense(1)
    ])
    
    model.compile(optimizer='adam', loss='mse')
    return model

# Custom physics-informed loss function
def physics_loss(y_true, y_pred):
    # Example: enforce conservation of energy
    kinetic = tf.gradients(y_pred, t)[0]
    potential = y_pred
    total_energy = kinetic**2 + potential**2
    return tf.reduce_mean(tf.square(total_energy - 1.0))

# Train model
model = create_pinn_model()
model.fit(X_train, y_train, epochs=100, validation_split=0.2)
                    

Data Analysis in Physics

Apply ML techniques to analyze experimental physics data.

Particle Classification


from sklearn.ensemble import RandomForestClassifier
import numpy as np

# Generate particle physics data
def generate_particle_data(n_samples=1000):
    # Features: energy, momentum, charge
    energy = np.random.normal(100, 10, n_samples)
    momentum = np.random.normal(90, 5, n_samples)
    charge = np.random.choice([-1, 0, 1], n_samples)
    
    # Labels: particle type (0: electron, 1: muon, 2: pion)
    labels = np.zeros(n_samples)
    labels[energy > 105] = 1
    labels[momentum > 95] = 2
    
    X = np.column_stack([energy, momentum, charge])
    return X, labels

# Train classifier
X, y = generate_particle_data()
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X, y)
                    

Practice Exercises

Exercise 1: Quantum State Prediction

Build a neural network to predict quantum states from measurement data.

Exercise 2: Phase Transition Detection

Implement a machine learning model to detect phase transitions in a physical system.

Additional Resources