<jupyter_start><jupyter_text>Loading tha manipulation of data<jupyter_code>data = np.asanyarray(pd.read_csv('./LabelledTheta.csv', delimiter=',', header=None))

NUM_CHANNELS = 14
ACTUAL_VALUES = 38
CONST_VALUES = 64 - 38
NUM_THETA_VALUES_PER_CHANNEL = ACTUAL_VALUES + CONST_VALUES
NUM_QUBITS = 6

data
def normalize(X_data):
    for i in range(len(X_data)):
        mu = X_data[i].mean()
        sigma = X_data[i].std()

        X_data[i] = (X_data[i] - mu) / sigma

        for j in range(NUM_CHANNELS):
              for k in range(NUM_THETA_VALUES_PER_CHANNEL):
                idx = j * NUM_THETA_VALUES_PER_CHANNEL + k
                if X_data[i][idx] > 2:
                    X_data[i][idx] = 2
                elif X_data[i][idx] < -2:
                    X_data[i][idx] = -2

                if k >= ACTUAL_VALUES:
                    X_data[i][idx] = -2.5

        X_data[i] = ((X_data[i] / 5) + 0.5)
            
    return X_data
# TODO: change the function for different encoding
def get_angles(X_data):
    X_angles = []
    for i in range(len(X_data)):
        X_i = []
        for j in range(NUM_CHANNELS):
            for q in range(NUM_QUBITS):
                probab_sum = 0.0
                for k in range(NUM_THETA_VALUES_PER_CHANNEL):
                    idx = j * NUM_THETA_VALUES_PER_CHANNEL + k

                    if k & (0x1 << q):
                        probab_sum += X_data[i][idx]

                X_i.append(2 * np.arcsin(np.sqrt(probab_sum)))

        X_angles.append(np.array(X_i))

    return X_angles
def convert_to_probability(X_data):
    X_data_probab = []
    for i in range(len(X_data)):
        X_i = []
        for j in range(NUM_CHANNELS):
            channel_j = []
            for k in range(NUM_THETA_VALUES_PER_CHANNEL):
                idx = j * NUM_THETA_VALUES_PER_CHANNEL + k
                channel_j.append(X_data[i][idx])

            total_ch_j = sum(channel_j)
            channel_j = np.array(channel_j) / total_ch_j

            for k in range(NUM_THETA_VALUES_PER_CHANNEL):
                X_i.append(channel_j[k])
    
        X_data_probab.append(np.array(X_i))

    return X_data_probab
def arrange_channelwise(X_data):
    X_data_channelwise = []
    for i in range(NUM_CHANNELS):
        X_i = []
        for j in range(len(X_data)):
            X_j = []
            for k in range(NUM_QUBITS):
                idx = i * NUM_QUBITS + k
                X_j.append(X_data[j][idx])
                
            X_i.append(np.array(X_j))
            
        X_data_channelwise.append(np.array(X_i))
        
    return X_data_channelwise
X = []
Y = []
for i in range(len(data)):
    X.append(data[i][1:-1])
    Y.append(data[i][-1])

index = np.random.permutation(range(len(X)))
    
X_norm = normalize(X)
X_probab = convert_to_probability(X_norm)
X_ang = np.array(get_angles(X_probab))#[index[:200]]
print(type(X_ang), type(X_ang[0]))
print(len(X_ang), len(X_ang[0]))
X_chwise = arrange_channelwise(X_ang)
Y_bin = np.array([1 if y == 'Like' else -1 for y in Y])#[index[:200]]

print(len(X_chwise), len(X_chwise[0]), len(X_chwise[0][0]))
train_idxs = int(len(X_ang)*0.7)+1
test_idxs = int(train_idxs + len(X_ang)*0.2)

X_train = X_ang[:train_idxs]
Y_train = Y_bin[:train_idxs]

X_test = X_ang[train_idxs:test_idxs]
Y_test = Y_bin[train_idxs:test_idxs]

X_val = X_ang[test_idxs:]
Y_val = Y_bin[test_idxs:]
X_train_chwise = arrange_channelwise(X_train)
X_test_chwise = arrange_channelwise(X_test)
X_val_chwise = arrange_channelwise(X_val)

print(len(X_train_chwise), len(X_train_chwise[0]))
print(len(X_test_chwise), len(X_test_chwise[0]))
print(len(X_val_chwise), len(X_val_chwise[0]))<jupyter_output>14 732
14 209
14 104<jupyter_text>Creating the quantum network<jupyter_code>def layer(W, num_wires):
    for i in range(num_wires):
        qml.Rot(W[i, 0], W[i, 1], W[i, 2], wires=i)
        
    for j in range(num_wires):
        if j != num_wires-1:
            qml.CNOT(wires=[j, j+1])
        else:
            qml.CNOT(wires=[j, 0])
def statePreparation(a):
    for i in range(len(a)):
        qml.RY(a[i], wires=i)
        qml.Hadamard(wires=i)
        if i == 5:
            qml.CZ(wires=[i, 0])
        else:
            qml.CZ(wires=[i, i+1])
dev = qml.device('default.qubit', wires=NUM_QUBITS)

@qml.qnode(dev)
def circuit(weights, angles):
    statePreparation(angles)
    
    for W in weights:
        layer(W, NUM_QUBITS)
        
    return qml.expval(qml.PauliZ(0))<jupyter_output><empty_output><jupyter_text>Some helper functions<jupyter_code>def square_loss(labels, predictions):
    loss = 0
    for l, p in zip(labels, predictions):
        loss += ((l - p)**2)
        
    loss /= len(labels)
    return loss

################################################################################################
def accuracy(labels, predictions):
    loss = 0
    pred = [1.0 if p > 0 else -1.0 for p in predictions]
    for l, p in zip(labels, pred):
        if abs(l - p) == 0:
            loss += 1
            
    loss /= len(labels)
    return loss<jupyter_output><empty_output><jupyter_text>Building the variational classifier<jupyter_code>def variational_classifier(var, ang=None):
    weights = var[0]
    bias = var[1]
    return circuit(weights, ang) + bias
def cost(weights, features, labels):
    predictions = [variational_classifier(weights, f) for f in features]
    return square_loss(labels, predictions)
def opt_cost(weights, features, labels):
    predictions = [variational_classifier(weights, f) for f in features]
    acc = accuracy(labels, predictions)
    if acc > 0:
      return (square_loss(labels, predictions) / accuracy(labels, predictions))**2
    else:
      return (square_loss(labels, predictions)/0.001)**2<jupyter_output><empty_output><jupyter_text>Fitting the model<jupyter_code>NUM_LAYERS = 10
bias = 0.03
var_init = (np.random.randn(NUM_LAYERS, NUM_QUBITS, 3), bias)
var_init
def fit_model(x, y, init_weights, batch_size, epochs, opt, validation_data, cost_calc_data):
    var = init_weights
    history = {'cost':[], 'acc_train':[], 'acc_val':[], 'iter_history':[]}
    num_iter = 50
    
    for e in range(epochs):
        history['iter_history'].append({'cost':[], 'acc_train':[], 'acc_val':[]})
        print("EPOCH: ", e+1)
        for it in range(num_iter):
            # Update the weights by one optimizer step
            batch_index = np.random.randint(0, train_idxs, (batch_size,))
            X_train_batch = x[batch_index]
            Y_train_batch = y[batch_index]
            var = opt.step(lambda v: opt_cost(v, X_train_batch, Y_train_batch), var)

            # Compute predictions on train and validation set
            print('Train iter: ', it+1, end='\t')
            predictions_train = [np.sign(variational_classifier(var, f)) for f in x]
            print('Test iter: ', it+1, end='\t')
            predictions_val = [np.sign(variational_classifier(var, f)) for f in validation_data[0]]
            
            
            # Compute accuracy on train and validation set
            history['iter_history'][e]['acc_train'].append(accuracy(y, predictions_train))
            history['iter_history'][e]['acc_val'].append(accuracy(validation_data[1], predictions_val))
            history['iter_history'][e]['cost'].append(cost(var, cost_calc_data[0], cost_calc_data[1]))
            
            # Print iter-wise value
            print(
                "Iter: {:5d} | Cost: {:0.7f} | Acc train: {:0.7f} | Acc validation: {:0.7f} "
                "".format(it + 1, history['iter_history'][e]['cost'][-1], history['iter_history'][e]['acc_train'][-1],
                history['iter_history'][e]['acc_val'][-1])
            )
        
        # Store parameters for later use
        history['acc_train'].append(sum(history['iter_history'][e]['acc_train'])/num_iter)
        history['acc_val'].append(sum(history['iter_history'][e]['acc_val'])/num_iter)
        history['cost'].append(sum(history['iter_history'][e]['cost'])/num_iter)

        print(
            "\n\nEpoch: {:5d} | Cost: {:0.7f} | Acc train: {:0.7f} | Acc validation: {:0.7f} "
            "".format(e + 1, history['cost'][-1], history['acc_train'][-1], history['acc_val'][-1])
        )
        
        return history, var
def evaluate_model(weights, x_test, y_test):
  predictions_test = [np.sign(variational_classifier(weights, f)) for f in x_test]
  results = (cost(weights, x_test, y_test), accuracy(y_test, predictions_test))
  print(
      f'*Evalute*: Cost: {results[0]} | Accuracy: {results[1]}'
  )
# from google.colab import drive
# drive.mount('/content/drive')
# def export_weights(filename, weights):
#   out_file = open('./drive/My Drive/Quantum Project/'+filename, 'w+')

#   for v in weights[0]:
#     for q in v:
#       for ang in q:
#         out_file.write(f'{ang}, ')
#       out_file.write('\n')
#     out_file.write('\n')

#   out_file.write(f'\n\n {weights[1]}')

#   out_file.close()


# export_weights('init_weights.csv', var_init)
learn_rate = 0.1
# Channel 3 training
ch3_history, ch3_weights = fit_model(x=X_train_chwise[2],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[2], Y_test),
                         cost_calc_data=(X_chwise[2], Y_bin))

evaluate_model(ch3_weights, X_val_chwise[2], Y_val)
# export_weights('ch3_weights.csv', ch3_weights)

# Channel 4 training
ch4_history, ch4_weights = fit_model(x=X_train_chwise[3],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[3], Y_test),
                         cost_calc_data=(X_chwise[3], Y_bin))

evaluate_model(ch4_weights, X_val_chwise[3], Y_val)
# export_weights('ch4_weights.csv', ch4_weights)
# Channel 5 training
ch5_history, ch5_weights = fit_model(x=X_train_chwise[4],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[4], Y_test),
                         cost_calc_data=(X_chwise[4], Y_bin))

evaluate_model(ch5_weights, X_val_chwise[4], Y_val)
# export_weights('ch5_weights.csv', ch5_weights)

# Channel 6 training
ch6_history, ch6_weights = fit_model(x=X_train_chwise[5],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[5], Y_test),
                         cost_calc_data=(X_chwise[5], Y_bin))

evaluate_model(ch6_weights, X_val_chwise[5], Y_val)
# export_weights('ch6_weights.csv', ch6_weights)
learn_rate = 0.1

# Channel 1 training
ch1_history, ch1_weights = fit_model(x=X_train_chwise[0],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[0], Y_test),
                         cost_calc_data=(X_chwise[0], Y_bin))

evaluate_model(ch1_weights, X_val_chwise[0], Y_val)
# export_weights('ch1_weights.csv', ch1_weights)

# Channel 2 training
ch2_history, ch2_weights = fit_model(x=X_train_chwise[1],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[1], Y_test),
                         cost_calc_data=(X_chwise[1], Y_bin))

evaluate_model(ch2_weights, X_val_chwise[1], Y_val)
# export_weights('ch2_weights.csv', ch2_weights)

# Channel 3 training
ch3_history, ch3_weights = fit_model(x=X_train_chwise[2],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[2], Y_test),
                         cost_calc_data=(X_chwise[2], Y_bin))

evaluate_model(ch3_weights, X_val_chwise[2], Y_val)
# export_weights('ch3_weights.csv', ch3_weights)

# Channel 4 training
ch4_history, ch4_weights = fit_model(x=X_train_chwise[3],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[3], Y_test),
                         cost_calc_data=(X_chwise[3], Y_bin))

evaluate_model(ch4_weights, X_val_chwise[3], Y_val)
# export_weights('ch4_weights.csv', ch4_weights)

# Channel 5 training
ch5_history, ch5_weights = fit_model(x=X_train_chwise[4],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[4], Y_test),
                         cost_calc_data=(X_chwise[4], Y_bin))

evaluate_model(ch5_weights, X_val_chwise[4], Y_val)
# export_weights('ch5_weights.csv', ch5_weights)

# Channel 6 training
ch6_history, ch6_weights = fit_model(x=X_train_chwise[5],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[5], Y_test),
                         cost_calc_data=(X_chwise[5], Y_bin))

evaluate_model(ch6_weights, X_val_chwise[5], Y_val)
# export_weights('ch6_weights.csv', ch6_weights)

# Channel 7 training
ch7_history, ch7_weights = fit_model(x=X_train_chwise[6],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[6], Y_test),
                         cost_calc_data=(X_chwise[6], Y_bin))

evaluate_model(ch7_weights, X_val_chwise[6], Y_val)
# export_weights('ch7_weights.csv', ch7_weights)

# Channel 8 training
ch8_history, ch8_weights = fit_model(x=X_train_chwise[7],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[7], Y_test),
                         cost_calc_data=(X_chwise[7], Y_bin))

evaluate_model(ch8_weights, X_val_chwise[7], Y_val)
# export_weights('ch8_weights.csv', ch8_weights)

# Channel 9 training
ch9_history, ch9_weights = fit_model(x=X_train_chwise[8],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[8], Y_test),
                         cost_calc_data=(X_chwise[8], Y_bin))

evaluate_model(ch9_weights, X_val_chwise[8], Y_val)
# export_weights('ch9_weights.csv', ch9_weights)

# Channel 10 training
ch10_history, ch10_weights = fit_model(x=X_train_chwise[9],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[9], Y_test),
                         cost_calc_data=(X_chwise[9], Y_bin))

evaluate_model(ch10_weights, X_val_chwise[9], Y_val)
# export_weights('ch10_weights.csv', ch10_weights)

# Channel 11 training
ch11_history, ch11_weights = fit_model(x=X_train_chwise[10],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[10], Y_test),
                         cost_calc_data=(X_chwise[10], Y_bin))

evaluate_model(ch11_weights, X_val_chwise[10], Y_val)
# export_weights('ch11_weights.csv', ch11_weights)

# Channel 12 training
ch12_history, ch12_weights = fit_model(x=X_train_chwise[11],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[11], Y_test),
                         cost_calc_data=(X_chwise[11], Y_bin))

evaluate_model(ch12_weights, X_val_chwise[11], Y_val)
# export_weights('ch12_weights.csv', ch12_weights)

# Channel 13 training
ch13_history, ch13_weights = fit_model(x=X_train_chwise[12],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[12], Y_test),
                         cost_calc_data=(X_chwise[12], Y_bin))

evaluate_model(ch13_weights, X_val_chwise[12], Y_val)
# export_weights('ch13_weights.csv', ch13_weights)

# Channel 14 training
ch14_history, ch14_weights = fit_model(x=X_train_chwise[13],
                         y=Y_train,
                         init_weights=var_init,
                         batch_size=2,
                         epochs=1,
                         opt=AdamOptimizer(learn_rate),
                         validation_data=(X_test_chwise[13], Y_test),
                         cost_calc_data=(X_chwise[13], Y_bin))

evaluate_model(ch14_weights, X_val_chwise[13], Y_val)
# export_weights('ch14_weights.csv', ch14_weights)


# Plotting it
plt.plot(ch1_history['acc_val'], label='Ch-1')
plt.plot(ch2_history['acc_val'], label='Ch-2')
plt.plot(ch3_history['acc_val'], label='Ch-3')
plt.plot(ch4_history['acc_val'], label='Ch-4')
plt.plot(ch5_history['acc_val'], label='Ch-5')
plt.plot(ch6_history['acc_val'], label='Ch-6')
plt.plot(ch7_history['acc_val'], label='Ch-7')
plt.plot(ch8_history['acc_val'], label='Ch-8')
plt.plot(ch9_history['acc_val'], label='Ch-9')
plt.plot(ch10_history['acc_val'], label='Ch-10')
plt.plot(ch11_history['acc_val'], label='Ch-11')
plt.plot(ch12_history['acc_val'], label='Ch-12')
plt.plot(ch13_history['acc_val'], label='Ch-13')
plt.plot(ch14_history['acc_val'], label='Ch-14')
plt.title('Hybrid QCNN performance')
plt.xlabel('Epochs')
plt.legend()
plt.ylabel('Validation Accuracy')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12408344521326035, 'fraction_numerical': 0.022565320665083134, 'mean_word_length': 2.1937664907651717, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '525919', 'license': 'Apache-2.0', 'n_tokens_mistral': 6751, 'n_tokens_neox': 6357, 'n_words': 1019, 'path': 'Quantum_Layer_Pennylane_3.ipynb', 'repo_name': 'AbeerVaishnav13/QNN-for-predicting-state-of-mind', 'size': '19366'}