Rotosolve and vqe_runner for IBM backend

Hi!

I would like to run a VQE circuit into IBM Qiskit real devices using vqe_runner and Rotosolve built-in algorithm from Pennylane as optimizer. Is this possible? I tried already but it seems it is not possible to perform this combination of VQE and Rotosolve on Qiskit real devices.

Thanks
Sergio

Hi @sergio, thanks for your question!

It should be possible to run a VQE problem using IBM HW and the Rotosolve optimizer from PennyLane. However, if you want to use vqe_runner, I think things become a bit more restrictive. I’d guess that vqe_runner expects a certain format for its optimizers, and Rotosolve potentially does not conform to this.

Would you be able to share with us a minimal (non-)working code example? In particular, showing the error message you receive when trying to do this?

Hi @nathan,

Thank you so much for your answer, indeed I would change vqe_runner if it is necessary to run Rotosolve in IBM HW, please can you tell me how to run Rotosolve on IBM HW by using Pennylane?

I already tried to download HW characteristics on Qiskit like noise, couplers and gates and I run Rotosolve locally on Qiskit (with a custom class) by setting the Qiskit simulator all the HW characteristics, but in Pennylane I get the following error when using vqe_runner with Rotosolve built-in class:

--- Logging error ---
2023-02-20T09:14:16.982883978Z Traceback (most recent call last):
2023-02-20T09:14:16.982922867Z   File "/provider/programruntime/program_starter_wrapper.py", line 91, in execute
2023-02-20T09:14:16.982942574Z     final_result = self.main(backend, self.messenger, **self.user_params)
2023-02-20T09:14:16.982959981Z   File "/code/./program.py", line 182, in main
2023-02-20T09:14:16.982998811Z     res = opt.minimize(
2023-02-20T09:14:16.983018878Z   File "/opt/app-root/lib64/python3.9/site-packages/scipy/optimize/_minimize.py", line 724, in minimize
2023-02-20T09:14:16.983027636Z     raise ValueError('Unknown solver %s' % method)
2023-02-20T09:14:16.983027636Z ValueError: Unknown solver Rotosolve
2023-02-20T09:14:16.983027636Z 
2023-02-20T09:14:16.983043748Z During handling of the above exception, another exception occurred:
2023-02-20T09:14:16.983104224Z 
2023-02-20T09:14:16.983129679Z Traceback (most recent call last):
2023-02-20T09:14:16.983251113Z   File "/usr/lib64/python3.9/logging/__init__.py", line 1083, in emit
2023-02-20T09:14:16.983285813Z     msg = self.format(record)
2023-02-20T09:14:16.983285813Z   File "/usr/lib64/python3.9/logging/__init__.py", line 927, in format
2023-02-20T09:14:16.983330476Z     return fmt.format(record)
2023-02-20T09:14:16.983349062Z   File "/usr/lib64/python3.9/logging/__init__.py", line 663, in format
2023-02-20T09:14:16.983367987Z     record.message = record.getMessage()
2023-02-20T09:14:16.983387696Z   File "/usr/lib64/python3.9/logging/__init__.py", line 367, in getMessage
2023-02-20T09:14:16.983405277Z     msg = msg % self.args
2023-02-20T09:14:16.983405277Z TypeError: not all arguments converted during string formatting
2023-02-20T09:14:16.983441711Z Call stack:
2023-02-20T09:14:16.984803584Z   File "/usr/lib64/python3.9/threading.py", line 937, in _bootstrap
2023-02-20T09:14:16.984873782Z     self._bootstrap_inner()
2023-02-20T09:14:16.984907298Z   File "/usr/lib64/python3.9/threading.py", line 980, in _bootstrap_inner
2023-02-20T09:14:16.984925905Z     self.run()
2023-02-20T09:14:16.984931335Z   File "/opt/app-root/lib64/python3.9/site-packages/anyio/_backends/_asyncio.py", line 867, in run
2023-02-20T09:14:16.984931335Z     result = context.run(func, *args)
2023-02-20T09:14:16.984937256Z   File "/provider/server/main.py", line 75, in execute_program
2023-02-20T09:14:16.984994060Z     starter.execute()
2023-02-20T09:14:16.985018993Z   File "/provider/programruntime/program_starter_wrapper.py", line 95, in execute
2023-02-20T09:14:16.985034959Z     logger.error(
2023-02-20T09:14:16.985057704Z Message: 'Error executing program job cfpjjpksiq6vn3feaj8g'
2023-02-20T09:14:16.985072814Z Arguments: (ValueError('Unknown solver Rotosolve'),)
2023-02-20T09:14:16.985575805Z webserver-starter - ERROR Failed to execute program: Unknown solver Rotosolve
2023-02-20T09:14:16.985582436Z Traceback (most recent call last):
2023-02-20T09:14:16.985598728Z   File "/provider/server/main.py", line 75, in execute_program
2023-02-20T09:14:16.985618699Z     starter.execute()
2023-02-20T09:14:16.985688633Z   File "/provider/programruntime/program_starter_wrapper.py", line 99, in execute
2023-02-20T09:14:16.985699617Z     raise ex
2023-02-20T09:14:16.985699617Z   File "/provider/programruntime/program_starter_wrapper.py", line 91, in execute
2023-02-20T09:14:16.985699617Z     final_result = self.main(backend, self.messenger, **self.user_params)
2023-02-20T09:14:16.985710302Z   File "/code/./program.py", line 182, in main
2023-02-20T09:14:16.985728666Z     res = opt.minimize(
2023-02-20T09:14:16.985750292Z   File "/opt/app-root/lib64/python3.9/site-packages/scipy/optimize/_minimize.py", line 724, in minimize
2023-02-20T09:14:16.985816798Z     raise ValueError('Unknown solver %s' % method)
2023-02-20T09:14:16.985824198Z ValueError: Unknown solver Rotosolve
2023-02-20T09:14:17.003811783Z jaeger_tracing - INFO Span publisher exited
2023-02-20T09:14:17.004446180Z webserver-starter - DEBUG Writing job status to termination marker file: '{"status": "Failed"}'
2023-02-20T09:14:17.005715844Z /pod-data/ CLOSE_WRITE,CLOSE terminated
2023-02-20T09:14:17.009816572Z Termination marker file found. Kill process (7).
2023-02-20T09:14:17.041927952Z /bin/bash: line 3:     7 Killed                  python -m uvicorn server.main:app --port 8081
2023-02-20T09:14:17.041936875Z Termination signal received, exited.

I also attach the full Log: log.txt (91.4 KB)

And this is the code I used:

from pennylane_qiskit import upload_vqe_runner, vqe_runner
import pennylane as qml

def ising_chain_ham(n, gam, pennylane = False):

    if pennylane:
        import pennylane as qml
        from pennylane import numpy as np
    
    from qiskit.opflow import X, Z, I
        
    for i in range(n):
        vecX = [I] * n
        vecZ = [I] * n
        vecX[i] = X
        vecZ[i] = Z
        
        if i == n - 1:
            vecX[0] = X
        else:
            vecX[i+1] = X

        auxX = vecX[0]
        auxZ = vecZ[0]
        
        for a in vecX[1:n]:
            auxX = auxX ^ a
        for b in vecZ[1:n]:
            auxZ = auxZ ^ b
            
        if i == 0:
            H = (auxX) + (gam * auxZ)
        else:
            H = H + (auxX) + (gam * auxZ)

    if pennylane:
        h_matrix = np.matrix(H.to_matrix_op().primitive.data)
        return qml.pauli.pauli_decompose(h_matrix)
    
    return H

n = 4 # número de qubits
gam = 0.3

H = ising_chain_ham(n, gam, pennylane = True) 

n_wires = 4

def ansatz_backend3(parameters):
    qml.StronglyEntanglingLayers(weights=parameters, wires=range(n_wires), ranges=range(1, n_wires), imprimitive=qml.ops.CZ)

shape = qml.StronglyEntanglingLayers.shape(n_layers=n_wires-1, n_wires=n_wires)
weights_entangled = np.random.random(size=shape)

program_id = upload_vqe_runner(hub="ibm-q", group="open", project="main")

job_ansatz = vqe_runner(
    program_id = program_id,
    backend ="ibmq_qasm_simulator",
    hamiltonian = H,
    ansatz = ansatz_backend3,
    x0 = weights_entangled,
    shots = 2048,
    optimizer = "Rotosolve",
    optimizer_config = {"num_steps": 5000},
    kwargs = {"hub": "ibm-q", "group": "open", "project": "main"},
)

job_ansatz.result()
results_ansatz3 = job_ansatz.intermediate_results['function']

Of course it is needed to login with IBM credentials to run the code on the real HW, as you will notice.

Please, may you suggest me a way to run Rotosolve in IBM HW? I would change vqe_runner if needed. Thanks for your support and time! :heart_eyes:

Thanks a lot
Sergio

Hi @Sergio, I’m getting a different error. However I can confirm that the “Rotosolve” optimizer cannot be used. As-per the Qiskit documentation only SPSA and QNSPSA are supported.

Unfortunately since this seems to be a limitation on IBM’s side I wouldn’t know if there’s a workaround.

Do let us know if you find one though.