torchquantum icon indicating copy to clipboard operation
torchquantum copied to clipboard

TypeError when using Qiskit plugin to convert circuit with a ParameterExpression

Open Vivekyy opened this issue 1 year ago • 4 comments

Getting the following TypeError when attempting to utilize the Qiskit plugin to convert a Qiskit circuit containing a ParameterExpression:

TypeError: ParameterExpression with unbound parameters ({ParameterVectorElement(θ[0])}) cannot be cast to a float.

Code to reproduce the error:

from qiskit.circuit.library import RealAmplitudes
from torchquantum.plugins import qiskit2tq

ansatz = RealAmplitudes(2, reps=1)
tq_layer = qiskit2tq(ansatz)

Vivekyy avatar Sep 26 '23 08:09 Vivekyy

Hey! Thanks for pointing this out! As of now, we can't yet directly convert ParameterVectorElement to TQ. (I can see about creating a quick workaround if you'd like!) However, for this specific example, we do have a TQ version of RealAmplitudes recently merged which can be accessed like a layer.

https://github.com/mit-han-lab/torchquantum/blob/cb661f698d81b7bcd116f3b0b16ff5c0f3bc6ec1/torchquantum/layer/nlocal.py#L256

Marking this issue so we can get to it more thoroughly. The main idea would be to add Parameter/ParameterVector/etc. support for the tq2qiskit operation.

Would be great to get a fix for this! Our application is predeminantly dealing with passing torchquantum a custom transpiled circuit, so it may need for this fix to be done.

Vivekyy avatar Oct 15 '23 22:10 Vivekyy

As a note, a temporary workaround for this issue is to just bind parameters before passing a circuit into qiskit2tq. I'm not sure if this is sufficient for a general fix, but it does work for our case at least.

Seperately, I ran into a bug that occurs when layouts are unchanged during transpilation. This was an easier fix, so I'll make a quick pull request for that.

Vivekyy avatar Oct 17 '23 05:10 Vivekyy