<jupyter_start><jupyter_text>Anomaly Detection with Adaptive Fourier Features and DMKDE Quantum Algorithm in Real Quantum Computer  Imports and Data load<jupyter_code>!pip install qiskit==0.35.0
!pip install pylatexenc<jupyter_output>Collecting qiskit==0.35.0
  Downloading qiskit-0.35.0.tar.gz (13 kB)
Collecting qiskit-terra==0.20.0
  Downloading qiskit_terra-0.20.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.5 MB)
[K     |████████████████████████████████| 6.5 MB 3.3 MB/s 
[?25hCollecting qiskit-aer==0.10.3
  Downloading qiskit_aer-0.10.3-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (18.0 MB)
[K     |████████████████████████████████| 18.0 MB 78 kB/s 
[?25hCollecting qiskit-ibmq-provider==0.18.3
  Downloading qiskit_ibmq_provider-0.18.3-py3-none-any.whl (238 kB)
[K     |████████████████████████████████| 238 kB 49.2 MB/s 
[?25hCollecting qiskit-ignis==0.7.0
  Downloading qiskit_ignis-0.7.0-py3-none-any.whl (200 kB)
[K     |████████████████████████████████| 200 kB 44.7 MB/s 
[?25hRequirement already satisfied: scipy>=1.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-aer==0.10.3->qiskit==0.35.0) (1.4.1)
Requirement already satisfied: numpy>=1.16.3 in /usr/local/lib/python3[...]<jupyter_text>Mount Google Drive<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Mounted at /content/drive<jupyter_text>Load from drive .mat file<jupyter_code>!pip install --upgrade --no-cache-dir gdown
#Loading .mat Cardiotocography dataset file
!gdown 1j4qIus2Bl44Om0UiOu4o4f__wVwUeDfP
import numpy as np
from time import time
from sklearn.kernel_approximation import RBFSampler
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt


# cardio = np.load("Cardiotocography.npy")

from scipy import io

cardio = io.loadmat("cardio.mat")

cardio["X"].shape, cardio["y"].shape<jupyter_output><empty_output><jupyter_text>Preprocessingnp.load object --> X, y (scaled)normal: '1'  anomalies: '0'<jupyter_code>from sklearn.preprocessing import MinMaxScaler
from scipy.stats import zscore

def preprocessing_cardio(data):
  features, labels = cardio["X"], cardio["y"]
  labels = 1 - labels

  # scaler = MinMaxScaler()
  # scaler.fit(features)
  # features = scaler.transform(features)

  return features, labels

cardio_X, cardio_y = preprocessing_cardio(cardio)

cardio_X.shape, cardio_y.shape<jupyter_output><empty_output><jupyter_text>Random Fourier Featuresparameters: gamma, dimensions, random_stateX --> rff(X)<jupyter_code>from sklearn.kernel_approximation import RBFSampler
"""
  Code from https://arxiv.org/abs/2004.01227
"""
class QFeatureMap: 

  def get_dim(self, num_features):
    pass

  def batch2wf(self, X):
    pass

  def batch2dm(self, X):
    psi = self.batch2wf(X)
    rho = np.einsum('...i,...j', psi, np.conj(psi))
    return rho

class QFeatureMap_rff(QFeatureMap):

  def __init__(self, rbf_sampler):
    self.rbf_sampler = rbf_sampler
    self.weights = np.array(rbf_sampler.random_weights_)
    self.offset = np.array(rbf_sampler.random_offset_)
    self.dim = rbf_sampler.get_params()['n_components']

  def get_dim(self, num_features):
    return self.dim

  def batch2wf(self, X):
    vals = np.dot(X, self.weights) + self.offset
    vals = np.cos(vals)
    vals *= np.sqrt(2.) / np.sqrt(self.dim)
    norms = np.linalg.norm(vals, axis=1)
    psi = vals / norms[:, np.newaxis]
    return psi
# Create the RandomFourierFeature map
def rff(X, dim, gamma): 
    feature_map_fourier = RBFSampler(gamma=gamma, n_components=dim, random_state=2)
    X_feat_train = feature_map_fourier.fit(cardio_X)

    rffmap = QFeatureMap_rff(rbf_sampler=feature_map_fourier)
    Crff = rffmap.batch2wf(cardio_X)

    return Crff<jupyter_output><empty_output><jupyter_text>Train test split<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(cardio_X, cardio_y, test_size=0.2, stratify=cardio_y, random_state=42)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, stratify=y_train, random_state=42)

print(f"shape of X_train: {X_train.shape} X_test: {X_test.shape} X_val {X_val.shape}")
n_classes = np.bincount(y_test.ravel().astype(np.int64))

print(f"classes: 0: {n_classes[0]} 1: {n_classes[1]} %-anomalies: {n_classes[0] / (n_classes[0] + n_classes[1])}")
#print(f"classes: 0: {n_classes[0]} 1: {n_classes[1]} %-anomalies: {n_classes[1] / (n_classes[0] + n_classes[1])}")<jupyter_output>shape of X_train: (1098, 21) X_test: (367, 21) X_val (366, 21)
classes: 0: 35 1: 332 %-anomalies: 0.09536784741144415<jupyter_text>Quantum Prediction Density Matrix BuildPure State: x_train --> U (matrix)Mixed State: X_train --> lambda (vec) , U (matrix)<jupyter_code>def pure_state(Ctrain):
    phi_train = np.sum(Ctrain, axis=0)
    phi_train = phi_train / np.linalg.norm(phi_train)

    size_U = len(phi_train)
    U_train = np.zeros((size_U, size_U))
    x_1 = phi_train
    U_train[:, 0] = x_1
    for i in range(1, size_U):
        x_i = np.random.randn(size_U)
        for j in range(0, i):
            x_i -= x_i.dot(U_train[:, j]) * U_train[:, j]
        x_i = x_i / np.linalg.norm(x_i)
        U_train[:, i] = x_i

    return U_train
def mixed_state(Ctrain):
    
    Z_train = np.outer(Ctrain[0], Ctrain[0])
    for i in range(1, len(Ctrain)):
        Z_train += np.outer(Ctrain[i], Ctrain[i])


    Z_train *= 1/len(Ctrain)

    lambda_P1_temp, U_train = np.linalg.eigh(Z_train)

    return lambda_P1_temp, U_train
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import Aer, execute

from sklearn.metrics import classification_report<jupyter_output><empty_output><jupyter_text>Quantum Prediction with Adaptive RFF  Clone the QMC from GitHUB<jupyter_code>!pip install git+https://github.com/fagonzalezo/qmc.git<jupyter_output>Collecting git+https://github.com/fagonzalezo/qmc.git
  Cloning https://github.com/fagonzalezo/qmc.git to /tmp/pip-req-build-q8o_4u57
  Running command git clone -q https://github.com/fagonzalezo/qmc.git /tmp/pip-req-build-q8o_4u57
Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from qmc==0.0.1) (1.7.3)
Requirement already satisfied: numpy>=1.19.2 in /usr/local/lib/python3.7/dist-packages (from qmc==0.0.1) (1.21.6)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.7/dist-packages (from qmc==0.0.1) (1.0.2)
Requirement already satisfied: tensorflow>=2.2.0 in /usr/local/lib/python3.7/dist-packages (from qmc==0.0.1) (2.8.0)
Requirement already satisfied: typeguard in /usr/local/lib/python3.7/dist-packages (from qmc==0.0.1) (2.7.1)
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.7/dist-packages (from tensorflow>=2.2.0->qmc==0.0.1) (0.25.0)
Requirement already satisfied: libclang>=9.0.1 in /usr/[...]<jupyter_text>Adaptive RFF<jupyter_code>import tensorflow as tf
import numpy as np
import qmc.tf.layers as layers
import qmc.tf.models as models
import pylab as pl
num_samples = 100000
rnd_idx1 = np.random.randint(X_train.shape[0],size=(num_samples, ))
rnd_idx2 = np.random.randint(X_train.shape[0],size=(num_samples, ))
#x_train_rff = [X_train[rnd_idx1], X_train[rnd_idx2]]
x_train_rff = np.concatenate([X_train[rnd_idx1][:, np.newaxis, ...], 
                          X_train[rnd_idx2][:, np.newaxis, ...]], 
                         axis=1) 
dists = np.linalg.norm(x_train_rff[:, 0, ...] - x_train_rff[:, 1, ...], axis=1)
print(dists.shape)
pl.hist(dists)
print(np.quantile(dists, 0.001))
rnd_idx1 = np.random.randint(X_test.shape[0],size=(num_samples, ))
rnd_idx2 = np.random.randint(X_test.shape[0],size=(num_samples, ))
#x_test_rff = [X_test[rnd_idx1], X_test[rnd_idx2]]
x_test_rff = np.concatenate([X_test[rnd_idx1][:, np.newaxis, ...], 
                          X_test[rnd_idx2][:, np.newaxis, ...]], 
                         axis=1) 
def gauss_kernel_arr(x, y, gamma):
    return np.exp(-gamma * np.linalg.norm(x - y, axis=1) ** 2)
import tensorflow as tf

class QFeatureMapAdaptRFF(layers.QFeatureMapRFF):
    def __init__(
                 self,
                 gamma_trainable=True,
                 weights_trainable=True,
                 **kwargs
                 ):
        self.g_trainable = gamma_trainable
        self.w_trainable = weights_trainable
        super().__init__(**kwargs)

    def build(self, input_shape):
        rbf_sampler = RBFSampler(
            gamma=0.5,
            n_components=self.dim,
            random_state=self.random_state)
        x = np.zeros(shape=(1, self.input_dim))
        rbf_sampler.fit(x)
        self.gamma_val = tf.Variable(
            initial_value=self.gamma,
            dtype=tf.float32,
            trainable=self.g_trainable,
            name="rff_gamma")
        self.rff_weights = tf.Variable(
            initial_value=rbf_sampler.random_weights_,
            dtype=tf.float32,
            trainable=self.w_trainable,
            name="rff_weights")
        self.offset = tf.Variable(
            initial_value=rbf_sampler.random_offset_,
            dtype=tf.float32,
            trainable=self.w_trainable,
            name="offset")
        self.built = True

    def call(self, inputs):
        vals = tf.sqrt(2 * self.gamma_val) * tf.matmul(inputs, self.rff_weights) + self.offset # old framework
        vals = tf.cos(vals)
        vals = vals * tf.sqrt(2. / self.dim) # old framework 
        norms = tf.linalg.norm(vals, axis=-1)
        psi = vals / tf.expand_dims(norms, axis=-1)
        return psi

class DMRFF(tf.keras.Model):
    def __init__(self,
                 dim_x,
                 num_rff,
                 gamma=1,
                 random_state=None):
        super().__init__()
        self.rff_layer = QFeatureMapAdaptRFF(input_dim=dim_x, dim=num_rff, gamma=gamma, random_state=random_state, gamma_trainable=False)

    def call(self, inputs):
        x1 = inputs[:, 0]
        x2 = inputs[:, 1]
        phi1 = self.rff_layer(x1)
        phi2 = self.rff_layer(x2)
        dot = tf.einsum('...i,...i->...', phi1, phi2) ** 2
        return dot

def calc_rbf(dmrff, x1, x2):
    return dmrff.predict(np.concatenate([x1[:, np.newaxis, ...], 
                                         x2[:, np.newaxis, ...]], 
                                        axis=1),
                         batch_size=256)

sigma = np.quantile(dists, 0.01)
gamma = 1/(2 * sigma ** 2)
gamma_index = 7 # index 7 corresponds to gamma = 2**(-7)
gammas = 1/(2**(np.arange(11)))
print(gammas)

n_rffs = 4
print(f'Gamma: {gammas[gamma_index ]}') 
# y_train_rff = gauss_kernel_arr(x_train_rff[:, 0, ...], x_train_rff[:, 1, ...], gamma=gamma) # Original code
# y_test_rff = gauss_kernel_arr(x_test_rff[:, 0, ...], x_test_rff[:, 1, ...], gamma=gamma) # Original code
y_train_rff = gauss_kernel_arr(x_train_rff[:, 0, ...], x_train_rff[:, 1, ...], gamma=gammas[gamma_index ])
y_test_rff = gauss_kernel_arr(x_test_rff[:, 0, ...], x_test_rff[:, 1, ...], gamma=gammas[gamma_index ])
dmrff = DMRFF(dim_x=21, num_rff=n_rffs, gamma=gammas[gamma_index ], random_state=np.random.randint(10000)) # original rs = 0
#dmrff = DMRFF(dim_x=21, num_rff=n_rffs, gamma=gamma / 2, random_state=np.random.randint(10000)) # original rs = 0
dm_rbf = calc_rbf(dmrff, x_test_rff[:, 0, ...], x_test_rff[:, 1, ...])
pl.plot(y_test_rff, dm_rbf, '.')
dmrff.compile(optimizer="adam", loss='mse')
dmrff.evaluate(x_test_rff, y_test_rff, batch_size=16)
print(f'Mean: {np.mean(dmrff.rff_layer.rff_weights)}')
print(f'Std: {np.std(dmrff.rff_layer.rff_weights)}')
print(f'Gamma: {dmrff.rff_layer.gamma_val.numpy()}')
pl.hist(dmrff.rff_layer.rff_weights.numpy().flatten(), bins=30);
dmrff.fit(x_train_rff, y_train_rff, validation_split=0.1, epochs=40, batch_size=128)
dm_rbf = calc_rbf(dmrff, x_test_rff[:, 0, ...], x_test_rff[:, 1, ...])
pl.plot(y_test_rff, dm_rbf, '.')
dmrff.evaluate(x_test_rff, y_test_rff, batch_size=128)
print(f'Mean: {np.mean(dmrff.rff_layer.rff_weights)}')
print(f'Std: {np.std(dmrff.rff_layer.rff_weights)}')
print(f'Gamma: {dmrff.rff_layer.gamma_val.numpy()}')
pl.hist(dmrff.rff_layer.rff_weights.numpy().flatten(), bins=30);
X_feat_train = dmrff.rff_layer.call(tf.cast(X_train, tf.float32))
X_feat_test = dmrff.rff_layer.call(tf.cast(X_test, tf.float32))
X_feat_val = dmrff.rff_layer.call(tf.cast(X_val, tf.float32))

X_feat_train = np.float64((X_feat_train).numpy())
X_feat_test = np.float64((X_feat_test).numpy())
X_feat_val = np.float64((X_feat_val).numpy())

X_feat_train = X_feat_train / np.linalg.norm(X_feat_train, axis = 1).reshape(-1, 1)
X_feat_test = X_feat_test / np.linalg.norm(X_feat_test, axis = 1).reshape(-1, 1)
X_feat_val = X_feat_val / np.linalg.norm(X_feat_val, axis = 1).reshape(-1, 1)

X_feat_train.shape, X_feat_test.shape, X_feat_val.shape<jupyter_output><empty_output><jupyter_text>IBM Real Computer Attempt  Pretrained Adp Feautures<jupyter_code>X_feat_train = np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/adpFeatures_4t4_Cardio_train.npy")
X_feat_test = np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/adpFeatures_4t4_Cardio_test.npy")
X_feat_val = np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/adpFeatures_4t4_Cardio_val.npy")

X_feat_train.shape, X_feat_test.shape, X_feat_val.shape<jupyter_output><empty_output><jupyter_text>First part<jupyter_code>!pip install qiskit_ibm_runtime
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, IBMQ, execute, transpile, Aer, assemble
from qiskit.tools.monitor import job_monitor

## Diego's token
TOKEN = '6d2fce5f8e30428840130bd3d576edf3571be02e4d12be8cdd08c7b102699a1931a8fd93be9472b020978fb0fe33d48e2521340e91ea04c0e3c1930cdfbcacf7'
from qiskit import IBMQ

IBMQ.save_account(TOKEN, overwrite=True) 
provider = IBMQ.load_account()
device = provider.get_backend("ibmq_santiago")
available_cloud_backends = provider.backends() 
print('\nHere is the list of cloud backends that are available to you:')
for i in available_cloud_backends: print(i)<jupyter_output>Here is the list of cloud backends that are available to you:
ibmq_qasm_simulator
ibmq_armonk
ibmq_santiago
ibmq_bogota
ibmq_lima
ibmq_belem
ibmq_quito
simulator_statevector
simulator_mps
simulator_extended_stabilizer
simulator_stabilizer
ibmq_manila<jupyter_text>Qiskit Runtime<jupyter_code>from qiskit_ibm_runtime import QiskitRuntimeService, Sampler

# Save your credentials on disk.
QiskitRuntimeService.save_account(channel='ibm_quantum', token=TOKEN)

service = QiskitRuntimeService()
# gamma = [2**-7]
# dim = 4
# num_exps = 1   

# feature_map_fourier = RBFSampler(gamma=gamma[0], n_components=dim)
# feature_map_fourier.fit(X_train)

# rffmap = QFeatureMap_rff(rbf_sampler=feature_map_fourier)
# X_feat_train = rffmap.batch2wf(X_train)
# X_feat_val = rffmap.batch2wf(X_val)
# X_feat_test = rffmap.batch2wf(X_test)

# print(X_feat_train.shape)
# print(X_feat_val.shape)
# print(X_feat_test.shape)<jupyter_output><empty_output><jupyter_text>Mixed 4x4  Validation<jupyter_code>from qiskit import transpile

eigvals, U = mixed_state(X_feat_train)

qclist_rff_mixed_val = []

for i in range(len(X_feat_val)): 
    qc = QuantumCircuit(4, 2)

    qc.initialize(X_feat_val[i], [0, 1])
    qc.initialize(np.sqrt(eigvals), [2, 3])
    qc.isometry(U.T, [], [0, 1]) 

    qc.cnot(3, 1)
    qc.cnot(2, 0)

    qc.measure(0, 0)
    qc.measure(1, 1)

    qclist_rff_mixed_val.append(transpile(qc, device))
indices_rff = list(range(123))

with Sampler(circuits=qclist_rff_mixed_val[0:123], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_mixed_val1 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_mixed_val1)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_mixed_val1_expsantiago.npy", results_rff_mixed_val1)

indices_rff = list(range(123))

with Sampler(circuits=qclist_rff_mixed_val[123:246], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_mixed_val2 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_mixed_val2)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_mixed_val2_expsantiago.npy", results_rff_mixed_val2)

indices_rff = list(range(120))

with Sampler(circuits=qclist_rff_mixed_val[246:], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_mixed_val3 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_mixed_val3)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_mixed_val3_expsantiago.npy", results_rff_mixed_val3)
#results_rff_mixed_val1 = np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_mixed_val1_exp2.npy")
results_rff_mixed_val = np.concatenate((results_rff_mixed_val1, results_rff_mixed_val2, results_rff_mixed_val3), axis=0)

thredhold_mixed = np.percentile(results_rff_mixed_val, q = 9.54)
print(thredhold_mixed)<jupyter_output><empty_output><jupyter_text>Test<jupyter_code>from qiskit import transpile

eigvals, U = mixed_state(X_feat_train)

qclist_rff_mixed_test = []

for i in range(len(X_feat_test)): 
    qc = QuantumCircuit(4, 2)

    qc.initialize(X_feat_test[i], [0, 1])
    qc.initialize(np.sqrt(eigvals), [2, 3])
    qc.isometry(U.T, [], [0, 1]) 

    qc.cnot(3, 1)
    qc.cnot(2, 0)

    qc.measure(0, 0)
    qc.measure(1, 1)

    qclist_rff_mixed_test.append(transpile(qc, device))
indices_rff = list(range(123))

with Sampler(circuits=qclist_rff_mixed_test[0:123], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_mixed_test1 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_mixed_test1)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_mixed_test1_expsantiago.npy", results_rff_mixed_test1)

indices_rff = list(range(122))

with Sampler(circuits=qclist_rff_mixed_test[123:245], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_mixed_test2 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_mixed_test2)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_mixed_test2_expsantiago.npy", results_rff_mixed_test2)

indices_rff = list(range(122))

with Sampler(circuits=qclist_rff_mixed_test[245:], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_mixed_test3 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_mixed_test3)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_mixed_test3_expsantiago.npy", results_rff_mixed_test3)

results_rff_mixed_test = np.concatenate((results_rff_mixed_test1, results_rff_mixed_test2, results_rff_mixed_test3), axis=0)
y_pred_mixed = results_rff_mixed_test > thredhold_mixed

print(classification_report(y_test, y_pred_mixed, digits=4))

len(results_rff_mixed_val)
print(f"AUC = {round(roc_auc_score(y_test, results_rff_mixed_test), 4)}")<jupyter_output>AUC = 0.9418<jupyter_text>Pure 4x4  Validation<jupyter_code>U_pure = pure_state(X_feat_train)

qclist_rff_pure_val = []

for i in range(len(X_feat_val)): 
    qc = QuantumCircuit(2, 2)

    qc.initialize(X_feat_val[i], [0, 1])
    qc.isometry(U_pure.T, [], [0, 1]) # ArbRot as a isometry

    qc.measure(0, 0)
    qc.measure(1, 1)

    qclist_rff_pure_val.append(transpile(qc, device))
indices_rff = list(range(123))

with Sampler(circuits=qclist_rff_pure_val[0:123], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_pure_val1 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_pure_val1)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_val1_expsantiago.npy", results_rff_pure_val1)

indices_rff = list(range(123))

with Sampler(circuits=qclist_rff_pure_val[123:246], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_pure_val2 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_pure_val2)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_val2_expsantiago.npy", results_rff_pure_val2)

indices_rff = list(range(120))

with Sampler(circuits=qclist_rff_pure_val[246:], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_pure_val3 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_pure_val3)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_val3_expsantiago.npy", results_rff_pure_val3)

results_rff_pure_val = np.concatenate((results_rff_pure_val1, results_rff_pure_val2, results_rff_pure_val3), axis=0)

thredhold_pure = np.percentile(results_rff_pure_val, q = 9.54)
print(thredhold_pure)<jupyter_output>0.4974568<jupyter_text>Test<jupyter_code>U_pure = pure_state(X_feat_train)

qclist_rff_pure_test = []

for i in range(len(X_feat_test)): 
    qc = QuantumCircuit(2, 2)

    qc.initialize(X_feat_test[i], [0, 1])
    qc.isometry(U_pure.T, [], [0, 1]) # ArbRot as a isometry

    qc.measure(0, 0)
    qc.measure(1, 1)

    qclist_rff_pure_test.append(transpile(qc, device))
indices_rff = list(range(123))

with Sampler(circuits=qclist_rff_pure_test[0:123], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_pure_test1 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_pure_test1)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_test1_expsantiago.npy", results_rff_pure_test1)

indices_rff = list(range(122))

with Sampler(circuits=qclist_rff_pure_test[123:245], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_pure_test2 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_pure_test2)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_test2_expsantiago.npy", results_rff_pure_test2)

indices_rff = list(range(122))

with Sampler(circuits=qclist_rff_pure_test[245:], service=service, options={ "backend": "ibmq_santiago" }) as sampler:
    result = sampler(circuit_indices=indices_rff, shots=5000)
    dists = result.quasi_dists

results_rff_pure_test3 = [dists[i]['00'] for i in range(len(dists))]
print(results_rff_pure_test3)
np.save("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_test3_expsantiago.npy", results_rff_pure_test3)

results_rff_pure_val1 = np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_val1.npy")
results_rff_pure_val2 = np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_val2.npy")
results_rff_pure_val3 =  np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_val3.npy")

results_rff_pure_val1 = np.sqrt(results_rff_pure_val1)
results_rff_pure_val2 = np.sqrt(results_rff_pure_val2)
results_rff_pure_val3 = np.sqrt(results_rff_pure_val3)

results_rff_pure_val = np.concatenate((results_rff_pure_val1, results_rff_pure_val2, results_rff_pure_val3), axis=0)

results_rff_pure_val1.shape, results_rff_pure_val2.shape, results_rff_pure_val3.shape, results_rff_pure_val.shape
thredhold_pure = np.percentile(results_rff_pure_val, q = 9.54)
print(thredhold_pure)
results_rff_pure_test1 = np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_test1.npy")
results_rff_pure_test2 = np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_test2.npy")
results_rff_pure_test3 =  np.load("/content/drive/MyDrive/TesisMaestria/ResearchData/results_aff_pure_test3.npy")


results_rff_pure_test1 = np.sqrt(results_rff_pure_test1)
results_rff_pure_test2 = np.sqrt(results_rff_pure_test2)
results_rff_pure_test3 = np.sqrt(results_rff_pure_test3)

results_rff_pure_test = np.concatenate((results_rff_pure_test1, results_rff_pure_test2, results_rff_pure_test3), axis=0)

results_rff_pure_test1.shape, results_rff_pure_test2.shape, results_rff_pure_test3.shape, results_rff_pure_test.shape
results_rff_pure_test = np.concatenate((results_rff_pure_test1, results_rff_pure_test2, results_rff_pure_test3), axis=0)

y_pred_pure = results_rff_pure_test > thredhold_pure

print(classification_report(y_test, y_pred_pure, digits=4))
print(f"AUC = {round(roc_auc_score(y_test, results_rff_pure_test), 4)}")<jupyter_output><empty_output><jupyter_text>Classical Pred AdpRFF<jupyter_code>from sklearn.metrics import roc_curve, f1_score
from sklearn.metrics import classification_report

def classification(preds_val, preds_test, y_test):

    thredhold = np.percentile(preds_val, q = 9.54)
    y_pred = preds_test > thredhold
    return classification_report(y_test, y_pred, digits=4)
#gamma = dmrff.rff_layer.gamma_val.numpy()
dim = 4

print(f"{dim}x{dim} Pure, experiment AdaptiveRFF")
#print("Gamma:", gamma)

## Training pure state and create the Unitary matrix to initialize such state

psi_train = X_feat_train.sum(axis = 0)
psi_train = psi_train / np.linalg.norm(psi_train)

preds_val_expected = np.sqrt((X_feat_val @ psi_train)**2)
preds_test_expected = np.sqrt((X_feat_test @ psi_train)**2)

print(classification(preds_val_expected, preds_test_expected, y_test))
print(f"AUC = {round(roc_auc_score(y_test, preds_test_expected), 4)}")
#gamma = dmrff.rff_layer.gamma_val.numpy()
dim = 4

print(f"{dim}x{dim} mixed, experiment AdaptiveRFF")
#print("Gamma:", gamma)

## Training mixed state and create the Unitary matrix to initialize such state
rho_train = np.zeros((dim, dim))


#for i in range(1000):
for i in range(len(X_feat_train)):
  rho_train += np.outer(X_feat_train[i], X_feat_train[i])

rho_train = rho_train / len(X_feat_train)

# Classical prediction
preds_val_mixed = np.zeros(len(X_feat_val))

for i in range(len(X_feat_val)):
  preds_val_mixed[i] = X_feat_val[i].T @ rho_train @ X_feat_val[i]

preds_test_mixed = np.zeros(len(X_feat_test))

for i in range(len(X_feat_test)):
  preds_test_mixed[i] = X_feat_test[i].T @ rho_train @ X_feat_test[i]

print(classification(preds_val_mixed, preds_test_mixed, y_test))
print(f"AUC = {round(roc_auc_score(y_test, preds_test_mixed), 4)}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13474333717429282, 'fraction_numerical': 0.034457123740846744, 'mean_word_length': 5.339302544769086, 'pattern_counts': {'":': 12, '<': 62, '<?xml version=': 0, '>': 79, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '495489', 'license': 'Apache-2.0', 'n_tokens_mistral': 11674, 'n_tokens_neox': 10762, 'n_words': 1616, 'path': 'Paper Experiments/Anomaly_Detection_AdaptiveFF_Real_Quantum_Computer.ipynb', 'repo_name': 'diegour1/QuantumAnomalyDetection', 'size': '26903'}