Skip to article frontmatterSkip to article content

🐍 Essential Python Concepts for Learning Hybrid Quantum PINNs

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

2. DecoratorsΒΆ

@decorator
def my_function():
    pass

3. Object-Oriented Programming (OOP)ΒΆ

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = nn.Linear(1, 1)

4. NumPyΒΆ

import numpy as np
x = np.linspace(0, 1, 100)

5. Autograd in PyTorchΒΆ

x = torch.tensor([2.0], requires_grad=True)
y = x**2
y.backward()

6. Loss FunctionsΒΆ

loss = mse(predicted, actual)

7. OptimizersΒΆ

optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

8. Integration with PennyLaneΒΆ

@qml.qnode(dev, interface='torch')
def circuit(params):
    qml.RX(params[0], wires=0)
    return qml.expval(qml.PauliZ(0))

9. Collocation Points & SamplingΒΆ

X_f = np.random.rand(100, 2)  # [x, t]

10. VisualizationΒΆ

plt.plot(x, u_pred)

β€˜β€™β€™

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