Overview

Skill Level: Intermediate

Intermedate knowledge of ML and Quantum Computing

A new appointment on the applications of Quantum computing at Machine Learning. In this notebook the theme of clustering is addressed through the K-means algorithm and its quantum computing version. https://github.com/enniogit/Quantum_K-means

Ingredients

Intermediate skills are required on both Python-Machine Learning and Qiskit Quantum Computing. To run the Jupyter notebook, we recommend the Qiskit environment on which the notebook was tested. You can also use the Watson Studio environment but, in this case, the code for reading the datasets changes and you must take into account the compatibility of the software versions when installing the Qiskit libraries with their specific dependencies on the Watson Studio environment.

 

Step-by-step

  1. General Description and quantum recall

    Inspired by the article Quantum K-Means algoritm written by Shan Jin, Xi He, Xiaokai Hou, Li Sun, Dingding Wen, Shaojun Wu, and Xiaoting Wang

    Picture3

    Picture2

  2. The C-swap circuit

    Picture4Picture5

  3. The C-swap circuit cont'

     

    Picture6

     

    Picture14

     

     

     

     

     

     

     

     

     

     

     

     

  4. The C-swap circuit cont' 2

    Picture8

  5. Preparation of libraries

    %matplotlib inline
    # Importing standard Qiskit libraries and configuring account
    from qiskit import QuantumCircuit, execute, Aer, IBMQ
    from qiskit.compiler import transpile, assemble
    from qiskit.tools.jupyter import *
    from qiskit.visualization import *

    import sys
    import types
    import pip
    import numpy as np
    import math
    import pandas as pd

    pi = math.pi

    #Connection with IBM Q systems (both physical and simulation)

    # Loading your IBM Q account(s)
    provider = IBMQ.enable_account(‘xxxxxxxxxxxxxxxxxxxxxx’)
    import getpass, time
    from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
    from qiskit import execute, Aer, IBMQ

    # import basic plot tools
    from qiskit.tools.visualization import circuit_drawer
    from qiskit.tools.visualization import plot_histogram
    from qiskit.tools.visualization import iplot_histogram

    backend = Aer.get_backend(‘qasm_simulator’)

  6. Loading the dataset for quantum analysis

    #WARNING: no multiplication by 10,000 of theta to not distort any calculation

    df_data_1 = pd.read_csv(‘DataForQComparison.csv’)
    df_data_1.head()
    dataset=df_data_1
    dataset[‘Teta’]=np.arctan(dataset[‘Annual Income_k$’].values.astype(int)/dataset[‘Spending Score_1_to_100’].values.astype(int))
    dataset[‘ID’]=dataset.reset_index().index
    dataset.head(10)

    Picture15

     

    dataset.shape

     

    (20,4)

  7. First build and test of quantum circuit

    Picture9

    # Creating Quantum Circuit called “qc” involving your Quantum Register “qr”
    # and your Classical Register “cr”
    qr = QuantumRegister(3)
    cr = ClassicalRegister(3)
    qc = QuantumCircuit ( qr, cr, name=”k_means”)

    #Circuit test
    #from the dataset we get the first and the 7th theta valuest
    i=0
    j=7

    theta_1 = dataset.at[i,’Teta’]theta_2 = dataset.at[i+j,’Teta’]#Compose the quantum circuit via qiskit
    #apply the first Hadamard gate to the ancilla qubit
    qc.h(qr[1])
    #apply the U3 gates to the controlled qubits
    qc.u3(theta_1, pi, pi, qr[0])
    qc.u3(theta_2, pi, pi, qr[2])
    #apply the c-swap circuit
    qc.cswap(qr[1], qr[0], qr[2])
    #apply the second Hadamard gate to the ancilla qubit
    qc.h(qr[1])
    #make the measurement
    qc.measure(qr[1], cr[1])

    job_exp = execute(qc, backend=backend, shots=4096)
    result = job_exp.result()
    print(result)

     

    Result(backend_name=’qasm_simulator’, backend_version=’0.3.2′, date=datetime.datetime(2019, 11, 5, 2, 57, 47, 485604), header=Obj(backend_name=’qasm_simulator’, backend_version=’0.3.2′), job_id=’d2f903af-9fd9-49f5-b9a3-bcdca84b01ac’, metadata={‘max_memory_mb’: 16081, ‘omp_enabled’: True, ‘parallel_experiments’: 1, ‘time_taken’: 0.012738549}, qobj_id=’8700f289-0cf8-4b50-b698-c54b8a945f24’, results=[ExperimentResult(data=ExperimentResultData(counts=Obj(0x0=3378, 0x2=718)), header=Obj(clbit_labels=[[‘c0’, 0], [‘c0’, 1], [‘c0’, 2]], creg_sizes=[[‘c0′, 3]], memory_slots=3, n_qubits=3, name=’k_means’, qreg_sizes=[[‘q0’, 3]], qubit_labels=[[‘q0’, 0], [‘q0’, 1], [‘q0’, 2]]), meas_level=2, metadata={‘measure_sampling’: True, ‘method’: ‘statevector’, ‘parallel_shots’: 1, ‘parallel_state_update’: 8}, seed_simulator=2979060185, shots=4096, status=’DONE’, success=True, time_taken=0.012591112000000002)], status=’COMPLETED’, success=True, time_taken=0.028936147689819336)

     

    circuit_drawer(qc)

     

    Picture17-1

     

    print(result.get_counts(qc))

     

    {‘010’: 718, ‘000’: 3378}

     

    plot_histogram(result.get_counts(qc))

    Picture18

     

    #We can consider the 1 probability like a measurement of the ‘distance’ between the two points.

    Picture10

    # State vector visualizing

    #state_vec_sim_bck=”statevector_simulator”
    state_vec_sim_bck=Aer.get_backend(‘statevector_simulator’)

    n = 3 # number of qubits
    q = QuantumRegister(n)
    c = ClassicalRegister(n)

    i=0
    j=7

    qc = QuantumCircuit(q, c, name=”State_super”)
    #theta_1 = dataset.at[i,’Teta’]theta_1=0
    Theta_2=2
    #theta_2 = dataset.at[i+j,’Teta’]#Compose the quantum circuit via qiskit
    qc.h(q[1])
    qc.h(q[0])
    qc.h(q[2])

    qc.u3(theta_1, pi, pi, q[0])
    qc.u3(theta_2, pi, pi, q[2])
    qc.cswap(q[1], q[0], q[2])
    qc.h(q[1])

    # execute the quantum circuit
    job = execute(qc, backend=state_vec_sim_bck)
    state_superposition = job.result().get_statevector(qc)

    #The overall matrix defining the whole circuit
    #_Coefficients_

    def overlap(state1, state2):
         return round(np.dot(state1.conj(), state2))

    print(state_superposition)

    overlap(state_superposition, state_superposition)

     

    [ 0.70582111+0.j  0.37422109+0.j  0. +0.j  0.33160003+0.j   0.37422109+0.j  0.04262106+0.j -0.33160003+0.j  0.  +0.j]

    Out[15]:

    (1+0j)

     

    #_probability density_

    def state_2_rho(state):
        return np.outer(state, state.conj())

    rho_superposition=state_2_rho(state_superposition)
    print(rho_superposition)

     

    rho_superposition=state_2_rho(state_superposition)

    print(rho_superposition)

     

    [[ 0.49818345+0.j  0.26413314+0.j  0.        +0.j  0.2340503 +0.j    0.26413314+0.j  0.03008284+0.j -0.2340503 -0.j  0.        +0.j]

     [ 0.26413314+0.j  0.14004142+0.j  0.        +0.j  0.12409172+0.j   0.14004142+0.j  0.0159497 +0.j -0.12409172-0.j  0.        +0.j]

     [ 0.        +0.j  0.        +0.j  0.        +0.j  0.        +0.j   0.        +0.j  0.        +0.j  0.        -0.j  0.        +0.j]

     [ 0.2340503 +0.j  0.12409172+0.j  0.        +0.j  0.10995858+0.j   0.12409172+0.j  0.01413314+0.j -0.10995858-0.j  0.        +0.j]

     [ 0.26413314+0.j  0.14004142+0.j  0.        +0.j  0.12409172+0.j   0.14004142+0.j  0.0159497 +0.j -0.12409172-0.j  0.        +0.j]

     [ 0.03008284+0.j  0.0159497 +0.j  0.        +0.j  0.01413314+0.j   0.0159497 +0.j  0.00181655+0.j -0.01413314-0.j  0.        +0.j]

     [-0.2340503 +0.j -0.12409172+0.j  0.        +0.j -0.10995858+0.j  -0.12409172+0.j -0.01413314+0.j  0.10995858+0.j  0.        +0.j]

     [ 0.        +0.j  0.        +0.j  0.        +0.j  0.        +0.j   0.        +0.j  0.        +0.j  0.        -0.j  0.        +0.j]]

     

    Picture11

    from qiskit.tools.visualization import plot_state_city, plot_bloch_multivector, plot_state_paulivec, plot_state_hinton, plot_state_qsphere
    plot_state_city(rho_superposition, figsize=(20,10), title=”Circuit”, color=[‘blue’, ‘orange’])
    #plot_state(rho_superposition,’city’)

    Picture19

     

    #Same as above but as a map

    plot_state_hinton(state_superposition)

    Picture20

     

    #Bloch state of the single qubits

     

    plot_bloch_multivector(state_superposition)

    Picture21

     

    plot_state_qsphere(state_superposition)

    Picture22

     

  8. Massive cycle execution of all possible distance pairs

    #In order to make a confrontation between the Euclidean and quantum distance we perform a double loop cycle in the dataset data getting the distance #(running the circuit) on all pairs of points.

    # Define result dataframe
    df_res = pd.DataFrame(columns=(‘I’, ‘J’,’Annual Income_i’,’Annual Income_j’,’Spending Score_1_to_100_i’,’Spending Score_1_to_100_j’,’Theta_i’, ‘Theta_j’, ‘Quantum_Distance’,’ClassicDistance’))

    #Define a loop to compute the distance between each pair of points

    elements=20

    #main cycle
       for i in range(0,elements):
         for j in range(0,elements):
          # Set the parament theta about different point
           theta_1 = dataset.at[i,’Teta’]       theta_2 = dataset.at[j,’Teta’]       ai_i = dataset.at[i,’Annual Income_k$’]       ai_j = dataset.at[j,’Annual Income_k$’]       sc_i = dataset.at[i,’Spending Score_1_to_100′]       sc_j = dataset.at[j,’Spending Score_1_to_100′]       qr = QuantumRegister(3)
           cr = ClassicalRegister(3)
           qc = QuantumCircuit ( qr, cr, name=”k_means”)

    # Compose the quantum circuit via qiskit
           qc.h(qr[1])

          qc.u3(theta_1, pi, pi, qr[0])
          qc.u3(theta_2, pi, pi, qr[2])
          qc.cswap(qr[1], qr[0], qr[2])
          qc.h(qr[1])
          qc.measure(qr[1], cr[1])

    # eseguo il circuito costruito sulla coppia I,J
         job_exp = execute(qc, backend=backend, shots=2048)
         result = job_exp.result()
    # calculate the ratio between the 0 and tha one result in measure registry
         v1=result.get_counts(qc)[‘000’]     if ‘010’ in result.get_counts(qc):
              v2=result.get_counts(qc)[‘010’]     else:
             v2=0
         prob=v2/(v1+v2) # Probability of obtaining 1 that is “quantum distance”
    # Classic distance calculation
         cl_dist=math.sqrt(((ai_i-ai_j)**2)+((sc_i-sc_j)**2))

    #Final output writing
        df_res = df_res.append({‘I’:i, ‘J’:j,’Annual Income_i’:ai_i,’Annual Income_j’:ai_j,’Spending Score_1_to_100_i’:sc_i,’Spending Score_1_to_100_j’:sc_j,
        ‘Theta_i’:theta_1, ‘Theta_j’:theta_2, ‘Quantum_Distance’:prob,’ClassicDistance’:cl_dist} , ignore_index=True)
    print(‘JOB Ended’)

     

    JOB ended

     

    df_res.to_csv(‘QuantumDistances.csv’,index=False)

    df_res = pd.read_csv(‘QuantumDistances.csv’)

    dataset=df_res
    dataset

    Picture23

    #Calculation Correlation between Quantum and Classic Distance

    cor=dataset[‘Quantum_Distance’].corr(dataset[‘ClassicDistance’],method=’pearson’)
    print(“{0:.0f}%”.format(cor * 100))

     

    61%

     

    #Diagrams of correlation with Seaborn of a dataset with distances only

    dataset.drop([‘Theta_i’,’Theta_j’,’I’,’J’], axis=1, inplace=True)

    # Determine the pairplot matrix for correlation verification
    import seaborn as sns
    sns.set(style=”ticks”, color_codes=True)
    g = sns.pairplot(dataset)

     

    Picture24

     

    Conclusion: the correlation between quantum distance and classical distance is proven although the coefficient is not high because of the statistical errors inherent in quantum measurement

  9. Now we will load another dataset which enhances clusters differences

    df_data_1 = pd.read_csv(‘Quantumnew2.csv’)
    df_data_1.head()
    dataset=df_data_1
    dataset=df_data_1
    #dataset[‘Teta’]=np.arctan(dataset[‘Column2’].values.astype(int)/dataset[‘Column1’].values.astype(int))
    dataset[‘Teta’]=np.arctan(dataset[‘Column2’].values/dataset[‘Column1’].values)
    #dataset[‘ID’]=dataset.reset_index().index
    dataset.head(10)

    Picture25

     

    #Let’s read the dataset in X Matrix

    s=(20,2)
    X=np.zeros(s)
    X[:,0]=dataset[‘ID’]X[:,1]=dataset[‘Teta’]*1.

    #If you zoom out this curve then you will see that last elbow comes at k=3
    #no matter what range we select ex- (1,21) also i will see the same behaviour but if we chose higher range it is little difficult to visualize the ELBOW
    #that is why we usually prefer range (1,21)
    ##Finally we got that k=5
    from sklearn.cluster import KMeans

    #Model Build
    kmeansmodel = KMeans(n_clusters= 3, init=’k-means++’, random_state=42,max_iter=100000,tol=0.00000001, verbose=0)
    y_kmeans= kmeansmodel.fit_predict(X)
    kmeans= kmeansmodel.fit(X)

    # centroids position
    centers_float = kmeans.cluster_centers_
    #centers=np.rint(centers_float).astype(int)
    centers=(centers_float)
    print(‘centroids=’,centers)

     

    centroids= [[3.45000000e+01 4.45854728e-01]

     [6.35000000e+01 5.60125739e-02]

     [3.50000000e+00 1.51194890e+00]]

     

    #Visualizing all the clusters
    import matplotlib.pyplot as plt
    plt.figure(figsize=(20,10))
    plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 50, c = ‘red’, label = ‘Cluster 1’)
    plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 50, c = ‘blue’, label = ‘Cluster 2’)
    plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 50, c = ‘green’, label = ‘Cluster 3’)
    #plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], s = 50, c = ‘green’, label = ‘Cluster 4’)
    #plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], s = 50, c = ‘orange’, label = ‘Cluster 5’)
    plt.scatter(centers[:, 0], centers[:, 1], c=’yellow’, s=200, alpha=0.5,label = ‘Centroids’);
    plt.title(‘Clusters of customers Teta Mode’)
    plt.xlabel(‘Customer ID’)
    plt.ylabel(‘Theta’)
    x = X[:, 0]y = X[:, 1]etichette=dataset[‘ID’].tolist()
    for i, txt in enumerate(etichette):
    plt.annotate(txt, (x[i], y[i]))
    plt.legend()
    plt.grid()
    plt.show()

     

    Picture26

  10. Now let's start dancing with qubits…

    Picture12

     

    def quantumdistance(theta_1,theta_2, shots):
    # prob=np.zeros(3)
    # for j in range(0,3):
         pi = math.pi
         qr = QuantumRegister(3)
         cr = ClassicalRegister(3)
         qc = QuantumCircuit ( qr, cr, name=”k_means”)

    # Achieve the quantum circuit via qiskit
         qc.h(qr[1])

         qc.u3(theta_1, pi, pi, qr[0])
         qc.u3(theta_2, pi, pi, qr[2])
         qc.cswap(qr[1], qr[0], qr[2])
         qc.h(qr[1])
         qc.measure(qr[1], cr[1])

    # eseguo il circuito costruito sulla coppia I,J
         backend = Aer.get_backend(‘qasm_simulator’)
         job_exp = execute(qc, backend=backend, shots=shots)
         result = job_exp.result()
    # calculate the ratio between the 0 and tha one result in measure registry
         v1=result.get_counts(qc)[‘000’]     if ‘010’ in result.get_counts(qc):
              v2=result.get_counts(qc)[‘010’]     else:
         v2=0

         prob=v2/(v1+v2) # Probab

         return prob

    def initcentroids(X,K):
    # Assigns the the initial values of centroids picking randomly from inside the example data
    #
         b=np.size(X[1, :])
         d=np.size(X[:, 1])
         c=(K,b)
         centroids = np.zeros(c)
    # executes a random permutation of the X index
         randidx = np.random.permutation(d)
    # assigns the initial value of centroids to the first k values of the randomized permutation
         centroids = X[randidx[0:K], :];
         return centroids

    def findClosestCentroids(X, centroids, mode):
    #FINDCLOSESTCENTROIDS computes the centroid memberships for every example
    # idx = FINDCLOSESTCENTROIDS (X, centroids) returns the closest centroids
    # in idx for a dataset X where each row is a single example. idx = m x 1
    # vector of centroid assignments (i.e. each entry in range [1..K])
    #

    # Set K
         dimension=np.size(X,0)

         K = np.size(centroids,0);

    # You need to return the following variables correctly.
         s=(dimension)
         idx = np.zeros(s,dtype=int);

         for i in range(0,dimension):
             minimo=10000
             dist=0
             for j in range(0,K):
                if mode==1:
               dist = (X[i,0]-centroids[j,0])**2+(X[i,1]-centroids[j,1])**2
             else:
               dist=quantumdistance(X[i,1],centroids[j,1], 2048)
             if dist <= minimo:
                minimo = dist
                idx[i] = j
         return idx

    def computeCentroids(X,idx,K):
    # Calculates the (new) centroids as the mean of the points having the same membership
    #
         s=(K,2)
         r=(K,1)
         m=np.size(X[:,0])
         centroids=np.zeros(s)
         C=np.zeros(r);
         for j in range (0,K):
              for i in range (1,m):
                 if idx[i] == j:
                   centroids[j,:]=centroids[j,:]+X[i,:];
                   C[j]=C[j]+1;
                   for j in range(0,K):
                     centroids[j,:]=1/C[j]*centroids[j,:]     return centroids

    def runkmeans (X, initial_centroids,max_iters, mode):
    # Executes in sequence : centroids initialization, repeat until max_iters is reached: findclosest
    # points, calculates the new centroids

    # Initialize values
         m = np.size(X[:,0])
         n = np.size(X[1,:])
         d = (m,1)
         K = np.size(initial_centroids, 0)
         centroids = initial_centroids
         previous_centroids = centroids
         idx = np.zeros(d)
    # execute loop
         for i in range (1,max_iters):
    # Output progress
            print(‘K-Means iteration …\n’, i, max_iters)
    # For each example in X, assign it to the closest centroid
            idx = findClosestCentroids(X, centroids, mode)
    # Given the memberships, compute new centroids
            centroids = computeCentroids(X, idx, K)
         return centroids, idx

    init_centroids=initcentroids(X,3)
    a=runkmeans (X, init_centroids,20,0)

    centers=a[0]Quantistic=a[1]print(centers)
    print(Quantistic)

    K-Means iteration …

     1 20

    K-Means iteration …

     2 20

    K-Means iteration …

     3 20

    K-Means iteration …

     4 20

    K-Means iteration …

     5 20

    K-Means iteration …

     6 20

    K-Means iteration …

     7 20

    K-Means iteration …

     8 20

    K-Means iteration …

     9 20

    K-Means iteration …

     10 20

    K-Means iteration …

     11 20

    K-Means iteration …

     12 20

    K-Means iteration …

     13 20

    K-Means iteration …

     14 20

    K-Means iteration …

     15 20

    K-Means iteration …

     16 20

    K-Means iteration …

     17 20

    K-Means iteration …

     18 20

    K-Means iteration …

     19 20

    [[4.00000000e+00 1.50817516e+00]

     [6.35000000e+01 5.60125739e-02]

     [3.45000000e+01 4.45854728e-01]]

    [0 0 0 0 0 0 2 2 2 2 2 2 2 2 1 1 1 1 1 1]

    #Visualizing all the clusters

    plt.figure(figsize=(20,10))
    plt.scatter(X[Quantistic == 0, 0], X[Quantistic == 0, 1], s = 50, c = ‘red’, label = ‘Cluster 1’)
    plt.scatter(X[Quantistic == 1, 0], X[Quantistic == 1, 1], s = 50, c = ‘blue’, label = ‘Cluster 2’)
    plt.scatter(X[Quantistic == 2, 0], X[Quantistic == 2, 1], s = 50, c = ‘green’, label = ‘Cluster 3’)
    #plt.scatter(X[Quantistic == 3, 0], X[Quantistic == 3, 1], s = 50, c = ‘green’, label = ‘Cluster 4’)
    #plt.scatter(X[Quantistic == 4, 0], X[Quantistic == 4, 1], s = 50, c = ‘orange’, label = ‘Cluster 5’)
    plt.scatter(centers[:, 0], centers[:, 1], c=’yellow’, s=200, alpha=0.5,label = ‘Centroids’);
    plt.title(‘Clusters of customers Teta Quantum Mode’)
    plt.xlabel(‘Customer ID’)
    plt.ylabel(‘Theta’)
    x = X[:, 0]y = X[:, 1]etichette=dataset[‘ID’].tolist()
    for i, txt in enumerate(etichette):
         plt.annotate(txt, (x[i], y[i]))
         plt.legend()
         plt.grid()
         plt.show()

     

    Picture26

    Conclusion: There is no difference between the two executions. The value of the centers and the membership of the data to the clusters is strictly equal

  11. Now let's try with our previous problem

    Picture13

    df_data_1 = pd.read_csv(‘DataForQComparison.csv’)

    df_data_1.head()
    dataset=df_data_1
    dataset[‘Teta’]=np.arctan(dataset[‘Annual Income_k$’].values.astype(int)/dataset[‘Spending Score_1_to_100’].values.astype(int))*10
    dataset[‘ID’]=dataset.reset_index().index
    dataset.head(10)

    Picture15

     

     

    s=(20,2)
    X=np.zeros(s)
    X[:,0]=dataset[‘ID’]X[:,1]=dataset[‘Teta’]*10

    #The first run is Classic but we set K=3 istead of 5 

    init_centroids=initcentroids(X,3)
    a=runkmeans (X, init_centroids,20,1)

    centers=a[0]Quantistic=a[1]print(centers)
    print(Quantistic)

     

    K-Means iteration …

     1 20

    K-Means iteration …

     2 20

    K-Means iteration …

     3 20

    K-Means iteration …

     4 20

    K-Means iteration …

     5 20

    K-Means iteration …

     6 20

    K-Means iteration …

     7 20

    K-Means iteration …

     8 20

    K-Means iteration …

     9 20

    K-Means iteration …

     10 20

    K-Means iteration …

     11 20

    K-Means iteration …

     12 20

    K-Means iteration …

     13 20

    K-Means iteration …

     14 20

    K-Means iteration …

     15 20

    K-Means iteration …

     16 20

    K-Means iteration …

     17 20

    K-Means iteration …

     18 20

    K-Means iteration …

     19 20

    [[ 13.66666667 132.39434259]

     [  2.5         40.1524952 ]

     [ 10.88888889  83.03466058]] [1 1 1 1 1 0 2 0 2 2 2 2 2 2 2 2 0 0 0 0]

     

    #Visualizing all the clusters

    plt.figure(figsize=(20,10))
    plt.scatter(X[Quantistic == 0, 0], X[Quantistic == 0, 1], s = 50, c = ‘red’, label = ‘Cluster 1’)
    plt.scatter(X[Quantistic == 1, 0], X[Quantistic == 1, 1], s = 50, c = ‘blue’, label = ‘Cluster 2’)
    plt.scatter(X[Quantistic == 2, 0], X[Quantistic == 2, 1], s = 50, c = ‘green’, label = ‘Cluster 3’)
    plt.scatter(X[Quantistic == 3, 0], X[Quantistic == 3, 1], s = 50, c = ‘magenta’, label = ‘Cluster 4’)
    plt.scatter(X[Quantistic == 4, 0], X[Quantistic == 4, 1], s = 50, c = ‘orange’, label = ‘Cluster 5’)
    plt.scatter(centers[:, 0], centers[:, 1], c=’yellow’, s=200, alpha=0.5,label = ‘Centroids’);
    plt.title(‘Clusters of customers Teta Quantum Mode’)
    plt.xlabel(‘Customer ID’)
    plt.ylabel(‘Theta’)
    x = X[:, 0]y = X[:, 1]etichette=dataset[‘ID’].tolist()
    for i, txt in enumerate(etichette):
         plt.annotate(txt, (x[i], y[i]))
         plt.legend()
         plt.grid()
         plt.show()

    Picture27

     

    #The second run is Quantum

    X[:,1] = X[:,1]/100
    init_centroids=initcentroids(X,3)
    a=runkmeans (X, init_centroids,10,0)

    centers=a[0]Quantistic=a[1]print(centers)
    print(Quantistic)

    K-Means iteration …

     1 10

    K-Means iteration …

     2 10

    K-Means iteration …

     3 10

    K-Means iteration …

     4 10

    K-Means iteration …

     5 10

    K-Means iteration …

     6 10

    K-Means iteration …

     7 10

    K-Means iteration …

     8 10

    K-Means iteration …

     9 10

    [[10.3         0.85802682]

     [15.4         1.36730237]

     [ 2.5         0.40152495]] [2 2 2 2 2 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1]

     

    #Visualizing all the clusters

    plt.figure(figsize=(20,10))
    plt.scatter(X[Quantistic == 0, 0], X[Quantistic == 0, 1], s = 50, c = ‘red’, label = ‘Cluster 1’)
    plt.scatter(X[Quantistic == 1, 0], X[Quantistic == 1, 1], s = 50, c = ‘blue’, label = ‘Cluster 2’)
    plt.scatter(X[Quantistic == 2, 0], X[Quantistic == 2, 1], s = 50, c = ‘green’, label = ‘Cluster 3’)
    plt.scatter(centers[:, 0], centers[:, 1], c=’yellow’, s=200, alpha=0.5,label = ‘Centroids’);
    plt.title(‘Clusters of customers Teta Quantum Mode’)
    plt.xlabel(‘Customer ID’)
    plt.ylabel(‘Theta’)
    x = X[:, 0]y = X[:, 1]etichette=dataset[‘ID’].tolist()
    for i, txt in enumerate(etichette):
         plt.annotate(txt, (x[i], y[i]))
         plt.legend()
         plt.grid()
         plt.show()

    Picture28

    Conclusion: we have been successful! the quantum clustering with 10 iterations has produced the same result as that obtained with the classic processing

Join The Discussion