This guide provides precise explanations of Python concepts critical for understanding and building Hybrid Quantum Physics-Informed Neural Networks (QPINNs).
1. Python FunctionsΒΆ
def f(x):
return x**2
- Why: Used to encapsulate reusable logic.
- In QPINNs: Define loss functions, PDE residuals, etc.
2. DecoratorsΒΆ
@decorator
def my_function():
pass
- Why: Wrap/modify function behavior.
- In QPINNs: Used in
@qml.qnode
to define quantum circuits.
3. Object-Oriented Programming (OOP)ΒΆ
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(1, 1)
- Why: Organize model architecture and parameters.
- In QPINNs: Define neural nets as modules (both classical and quantum).
4. NumPyΒΆ
import numpy as np
x = np.linspace(0, 1, 100)
- Why: Handle numerical arrays and meshgrids.
- In QPINNs: Generate collocation points, initial/boundary conditions.
5. Autograd in PyTorchΒΆ
x = torch.tensor([2.0], requires_grad=True)
y = x**2
y.backward()
- Why: Compute derivatives automatically.
- In QPINNs: Needed for PDE residuals like
u_xx
,u_tt
, etc.
6. Loss FunctionsΒΆ
loss = mse(predicted, actual)
- Why: Quantify prediction error.
- In QPINNs: Include physics-based loss terms (IC, BC, PDE residual).
7. OptimizersΒΆ
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
- Why: Update model parameters via gradients.
- In QPINNs: Optimize classical and quantum weights together.
8. Integration with PennyLaneΒΆ
@qml.qnode(dev, interface='torch')
def circuit(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0))
- Why: Define quantum layers within PyTorch.
- In QPINNs: Hybrid classical-quantum neural nets.
9. Collocation Points & SamplingΒΆ
X_f = np.random.rand(100, 2) # [x, t]
- Why: Evaluate physics residual across the domain.
- In QPINNs: Needed to compute the PDE loss.
10. VisualizationΒΆ
plt.plot(x, u_pred)
- Why: Debug and analyze performance.
- In QPINNs: Plot true vs predicted solution and loss evolution.
π Recommended LibrariesΒΆ
torch
β Deep learning frameworkpennylane
β Quantum ML librarymatplotlib
β Plottingnumpy
β Numerical computing
βββ
C. Albornoz, G. Alonso, M. Andrenkov, P. Angara, A. Asadi, A. Ballon, S. Bapat, L. Botelho, I. De Vlugt, O. Di Matteo, P. Downing, P. Finlay, A. Fumagalli, A. Gardhouse, J. Geoffrion, N. Girard, A. Hayes, J. Izaac, R. Janik, T. Kalajdzievski, A. Kanwar Singh, A. Khomchenko, N. Killoran, I. KureΔiΔ, O. Landon-Cardinal, A. Martin, D. Nino, A. Otto, C. Pere, J. Pickering, K. Renaud, J. Soni, D. Wakeham, L. Young. PennyLane Codebook. 2024. https://
pennylane .ai /codebook