1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] how to build a kernel matrix that doesn't recursively call the quantum kernel

Discussão em 'Python' iniciado por Stack, Setembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    from qiskit_ibm_runtime import QiskitRuntimeService, Session
    from qiskit_ibm_runtime import Sampler

    token = "my ibm token "
    cafile = 'Security file'


    service = QiskitRuntimeService(channel="ibm_quantum", token=token, verify=cafile)

    from qiskit_aer import Aer, AerSimulator
    def quantum_kernel(x1,x2):
    n = len(x1)
    circuit1 = QuantumCircuit(n)
    circuit2 = QuantumCircuit(n)

    circuit1 = feature_map(circuit1,x1)
    circuit2 = feature_map(circuit2,x2)

    circuit = QuantumCircuit(2*n + 1,1)

    circuit.append(circuit1, range(n))
    circuit.append(circuit2, range(n, 2*n))

    circuit.h(2*n)
    for i in range(n):
    circuit.cswap(2*n, i, n+i)
    circuit.h(2*n)

    circuit.measure(2*n,0)



    backend = service.least_busy(operational=True, simulator=False)

    transpiled_circuit = transpile(circuit , backend = backend)

    sampler = Sampler(backend)
    job = sampler.run([transpiled_circuit])
    print(f"job id : {job.job_id()}")
    result = job.result()
    #print(result)

    pub_result = result[0]
    bit_array = pub_result.data.c


    import numpy as np
    bit_data = np.array(bit_array._array)


    flat_bits = bit_data.flatten()


    counts = {'0': 0, '1': 0}


    for bit in flat_bits:
    if bit == 0:
    counts['0'] += 1
    elif bit == 1:
    counts['1'] += 1


    zero_prob = counts.get('0', 0) / 4096


    kernel_value = 2 * zero_prob - 1
    return kernel_value



    num_samples = len(norm_data)

    quantum_kernel_matrix = np.zeros((num_samples, num_samples))

    for i in range(num_samples):
    for j in range(num_samples):
    x1 = norm_data
    x2 = norm_data[j]
    kernel_value = quantum_kernel(x1,x2)
    quantum_kernel_matrix[i,j] = kernel_value



    print("quantum kernel matrix :")
    print(quantum_kernel_matrix)


    the above is the code that I have written to compute the kernel value and the matrix but the problem is this code is assigning different quantum jobs for computing each pair (i,j) of the kernel matrix can somebody help me so that the code won't recursively call the quantum kernel but gives me the matrix.

    Continue reading...

Compartilhe esta Página