Skip to content

Commit

Permalink
Create neural_network_quantum_with_explainability.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 13, 2024
1 parent 8ac5bde commit 6a580de
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ai_engine/neural_network_quantum_with_explainability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import tensorflow as tf
from qiskit import QuantumCircuit, execute
from lime import lime_tabular

class QuantumNeuralNetworkWithExplainability:
def __init__(self, input_shape, num_classes):
self.model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=input_shape),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(num_classes, activation='softmax')
])
self.quantum_circuit = QuantumCircuit(5, 5)
self.explainer = lime_tabular.LimeTabularExplainer(self.model, num_features=10)

def train(self, X_train, y_train, epochs=10, batch_size=32):
self.model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.2)
self.quantum_circuit.barrier()
job = execute(self.quantum_circuit, backend='qasm_simulator', shots=1024)
result = job.result()
self.model.set_weights(result.get_statevector())

def predict(self, X_test):
return self.model.predict(X_test)

def explain(self, X_test):
return self.explainer.explain_instance(X_test, num_features=10)

0 comments on commit 6a580de

Please sign in to comment.