PennyLaneAI / pennylane-qiskit

The PennyLane-Qiskit plugin integrates the Qiskit quantum computing framework and IBM Q with PennyLane.
https://docs.pennylane.ai/projects/qiskit
Apache License 2.0
181 stars 64 forks source link

Problem with equal parameters in vqe_runner #208

Closed KetpuntoG closed 5 months ago

KetpuntoG commented 2 years ago

Describe the bug (Bug found by Matthew Weiss)

When I use the same parameter twice within an anstaz, when I send it to the vqe_runner, they are optimized separately, so the equal relationship between them is not maintained.

Reason for the behavior

the quantum circuit is transformed to a tape (concrete values are assigned)


tape = qml.transforms.make_tape(ansatz)(np.array(tape_param)).expand(
                depth=5, stop_at=lambda obj: obj.name in QiskitDevice._operation_map
            )

        params = tape.get_parameters()
        trainable_params = []

        for p in params:
            if qml.math.requires_grad(p):
                trainable_params.append(p)
heyredhat commented 2 years ago

Example:

from qiskit import IBMQ
import pennylane as qml
from pennylane import numpy as np
from pennylane_qiskit import upload_vqe_runner, vqe_runner

provider = IBMQ.enable_account(*TOKEN*)

program_id = upload_vqe_runner(hub="ibm-q", group="open", project="main")
H = qml.Hamiltonian([0.5, 0.5, 0.5, 0.5],\
                    [qml.Identity(0) @ qml.Identity(1),\
                     qml.PauliX(0) @ qml.PauliX(1),\
                     qml.PauliY(0) @ qml.PauliY(1),\
                     qml.PauliZ(0) @ qml.PauliZ(1)])

def vqe_circuit(params):
    a, b = params
    qml.RX(a, 0)
    qml.RZ(b, 0)
    qml.RX(a, 1)
    qml.RZ(b, 1)

job = vqe_runner(
    program_id=program_id,
    backend="ibmq_qasm_simulator",
    hamiltonian=H,
    ansatz=vqe_circuit,
    x0=np.random.randn(2),
    shots=8192,
    optimizer="SPSA",
    optimizer_config={"maxiter": 100},
    kwargs={"hub": "ibm-q", "group": "open", "project": "main"},
)

This gives UserWarning: In order to match the tape expansion, the number of parameters has been changed. Moreover:

job.result()
     fun: 0.0003662109375
 message: 'Optimization terminated successfully.'
    nfev: 300
     nit: 100
 success: True
       x: array([3.15633508, 6.56843918, 6.30372469, 7.8613945 ])

Instead of optimizing over the two parameters (a and b), reusing each twice, it's optimized over four, treating each use of a parameter separately.

lillian542 commented 5 months ago

Closing because Qiskit has removed the VQE runner, so it is no longer part of the plugin.