top of page

Exploring Hybrid Quantum-Classical Neural Networks for Credit Scoring

Writer: gaurav0487gaurav0487

Hybrid quantum-classical neural networks are at the forefront of modern AI and quantum computing research. In this blog post, we explore the application of these models to credit scoring for small and medium-sized enterprises (SMEs), inspired by the efficiency and potential of quantum machine learning (QML). We demonstrate the implementation, training, and evaluation of a hybrid QML model built with PyTorch and PennyLane. Let's dive into the details step by step.


1. Dataset Preparation

The first step is preparing a dataset of SME credit scoring data. Our dataset consists of 21 features and a binary target variable (Healthy or Default).


train_data = pd.read_csv("train_data.csv")  # Replace with actual file path
test_data = pd.read_csv("test_data.csv")
# Separate features and target
X_train = train_data.iloc[:, :-1].values
y_train = train_data['Target'].values
X_test = test_data.iloc[:, :-1].values
y_test = test_data['Target'].values

2. Data Normalization and Tensor Conversion

To enhance training efficiency, we normalize the feature data and convert it into PyTorch tensors.


# Normalize features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Convert data to PyTorch tensors
X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
y_train_tensor = torch.tensor(y_train, dtype=torch.float32).unsqueeze(1)
X_test_tensor = torch.tensor(X_test, dtype=torch.float32)
y_test_tensor = torch.tensor(y_test, dtype=torch.float32).unsqueeze(1)

  • Normalization: Ensures the input features have a mean of 0 and a standard deviation of 1.

  • Tensor Conversion: Makes the data compatible with PyTorch operations.


3. Creating DataLoaders

PyTorch DataLoader objects efficiently batch and shuffle the data during training and testing.


batch_size = 32
train_dataset = TensorDataset(X_train_tensor, y_train_tensor)
test_dataset = TensorDataset(X_test_tensor, y_test_tensor)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)

4. Quantum Circuit Definition

The hybrid model incorporates a quantum component designed with PennyLane. The quantum circuit uses angle embedding to encode classical data into quantum states, followed by entanglement layers.


n_qubits = 4
dev = qml.device('default.qubit', wires=n_qubits)

@qml.qnode(dev, interface='torch')
def quantum_circuit(inputs, weights):
	qml.AngleEmbedding(inputs, wires=range(n_qubits))
	qml.BasicEntanglerLayers(weights, wires=range(n_qubits))
	return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]

5. Hybrid Quantum-Classical Model

The model combines classical layers with a quantum layer for enhanced predictive power.


class HybridModel(nn.Module):
    def __init__(self):
		super(HybridModel, self).__init__()
		self.classical_layer1 = nn.Linear(21, 10)
		self.classical_layer2 = nn.Linear(10, n_qubits)
		self.quantum_weights = nn.Parameter(0.01 * torch.randn((3, n_qubits)))
		self.classical_output = nn.Linear(n_qubits, 1)
		self.sigmoid = nn.Sigmoid()



    def forward(self, x):

		x = torch.relu(self.classical_layer1(x))
		x = torch.relu(self.classical_layer2(x))
		q_out = quantum_circuit(x, self.quantum_weights)
		q_out = torch.stack(q_out, dim=-1).float()
		x = self.classical_output(q_out)
		return self.sigmoid(x)

6. Training the Model

The model is trained using binary cross-entropy loss and stochastic gradient descent.


model = HybridModel()
criterion = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
n_epochs = 20
for epoch in range(n_epochs):
	model.train()
	epoch_loss = 0.0
	for data, target in train_loader:
		optimizer.zero_grad()
		output = model(data)
		loss = criterion(output, target)
		loss.backward()
		optimizer.step()
		epoch_loss += loss.item()
	print(f"Epoch {epoch + 1}/{n_epochs}, Loss: {epoch_loss / 		len(train_loader)}")
Epoch 1/20, Loss: 0.8587412118911744
Epoch 2/20, Loss: 0.5195474481582641
Epoch 3/20, Loss: 0.40876561164855957
Epoch 4/20, Loss: 0.3654198062419891
Epoch 5/20, Loss: 0.3457665106654167
Epoch 6/20, Loss: 0.3360282516479492
Epoch 7/20, Loss: 0.3307387161254883
Epoch 8/20, Loss: 0.32773104071617126
Epoch 9/20, Loss: 0.3259801682829857
Epoch 10/20, Loss: 0.32495405852794645
Epoch 11/20, Loss: 0.3243127857148647
Epoch 12/20, Loss: 0.32388067364692685
Epoch 13/20, Loss: 0.323671769797802
Epoch 14/20, Loss: 0.3235171364247799
Epoch 15/20, Loss: 0.3233896933495998
Epoch 16/20, Loss: 0.32332037925720214
Epoch 17/20, Loss: 0.3232835227251053
Epoch 18/20, Loss: 0.3232590101659298
Epoch 19/20, Loss: 0.3232296106219292
Epoch 20/20, Loss: 0.32323075518012045

7. Evaluating the Model

After training, the model is evaluated on the test dataset to calculate its accuracy.


model.eval()
correct = 0
total = 0
with torch.no_grad():
	for data, target in test_loader:
		output = model(data)
		predicted = (output > 0.5).float()
		correct += (predicted == target).sum().item()
		total += target.size(0)



accuracy = correct / total
print(f"Test Accuracy: {accuracy * 100:.2f}%")

Test Accuracy: 88.75%

Results

  • Training Efficiency: The quantum-classical hybrid model achieves comparable performance to classical models trained for 3,500 epochs but requires only 350 epochs.

  • Test Accuracy: The hybrid model achieved an accuracy of 88.75% on the test dataset.

  • Scalability Challenges: Performance degradation was observed when the number of qubits exceeded 12 or when additional classifier blocks were added.


Conclusion

This exploration demonstrates the potential of hybrid QML models in tackling credit scoring tasks efficiently. While quantum models show promise, challenges like scalability and noise sensitivity on real quantum hardware remain. This first step opens the door for further advancements in applying QML to real-world financial problems.


References:

  1. https://arxiv.org/pdf/2308.03575 - QML for credit scoring

  2. https://pennylane.ai/ - pennylane


 
 
 

Comentarios


bottom of page