-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython quantunbot.py
More file actions
73 lines (56 loc) · 2.05 KB
/
python quantunbot.py
File metadata and controls
73 lines (56 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Load Iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Data preprocessing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
# Prepare data for the quantum circuit
n_qubits = 2
n_params = n_qubits
# Generate qubits
qubits = [cirq.GridQubit(0, i) for i in range(n_qubits)]
# Prepare parameters
params = sympy.symbols('theta(0:{})'.format(n_qubits))
# Create the quantum circuit
qnn_circuit = create_qnn_circuit(qubits, params)
# Convert the quantum circuit to a TensorFlow layer
qnn_layer = tfq.layers.PQC(qnn_circuit, readout_operators=cirq.PauliString(cirq.Z(qubits[-1])))
# Build the QNN model
model = tf.keras.Sequential([
tf.keras.layers.Dense(n_qubits),
tf.keras.layers.Reshape((n_qubits, 1)),
qnn_layer
])
# Compile the model
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=tf.keras.optimizers.Adam(learning_rate=0.1),
metrics=['accuracy'])
def app():
# Set the title of the app
st.title('Iris Flower Classifier using Quantum Neural Network')
# Ask the user to input values for the features
feature_names = iris.feature_names
features = []
for name in feature_names:
feature = st.number_input(name)
features.append(feature)
X_user = np.array(features).reshape(1, -1)
X_user = scaler.transform(X_user)
# Train the model
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0)
# Evaluate the model
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
# Predict the class of the input
y_user = model.predict(X_user)
class_names = iris.target_names
class_idx = np.argmax(y_user, axis=1)[0]
class_name = class_names[class_idx]
# Display the predicted class
st.write("Predicted class:", class_name)
# Display the model accuracy
st.write("Model accuracy:", accuracy)
# Run the if __name__ == '__main__':
app()