Qiskit / qiskit-ibm-transpiler

https://pypi.org/project/qiskit-transpiler-service/
Apache License 2.0
13 stars 4 forks source link

Transpiler service alters circuit parameter names #47

Open jyu00 opened 2 weeks ago

jyu00 commented 2 weeks ago

Circuit returned by the transpiler service has its parameter names altered. For example:


from qiskit.circuit.library import RealAmplitudes
from qiskit_transpiler_service.transpiler_service import TranspilerService

circuit = RealAmplitudes(num_qubits=2, reps=1)
print(f"Before parameters: {circuit.parameters}")

transpiler = TranspilerService(backend_name="ibmq_manila", optimization_level=1)
isa_circ = transpiler.run(circuit)
print(f"After parameters: {isa_circ.parameters}")

This prints

Before parameters: ParameterView([ParameterVectorElement(θ[0]), ParameterVectorElement(θ[1]), ParameterVectorElement(θ[2]), ParameterVectorElement(θ[3])])

After parameters: ParameterView([Parameter(_θ_0_), Parameter(_θ_1_), Parameter(_θ_2_), Parameter(_θ_3_)])
jyu00 commented 2 weeks ago

A workaround is to have a mapping of the before/after parameter names using they position. For example, one can track that parameter θ[0] is at position 0. So any parameter values that were supposed to go to θ[0] will now go to the new position 0 parameter named _θ_0_

y4izus commented 2 weeks ago

Thank you for submitting the issue @jyu00 ! Since it belongs to our internal repo instead of this one, I will create the issue there and close this one, ok?

jyu00 commented 2 weeks ago

I opened this issue here so other people who encounter the same issue can see the progress. For that reason, I'd prefer to leave this open until it's fixed.

victor-villar commented 2 days ago

Hi @jyu00 ! I've been checking and this seems to be a problem with the QASM3 importer/exporter. If I just serialize to qasm3 and deserialize again, without doing anything else in between, the same problem arises:

from qiskit.circuit.library import RealAmplitudes

circuit = RealAmplitudes(num_qubits=2, reps=1)
print(f"Before parameters: {circuit.parameters}")

circuit_q = qasm3.loads(qasm3.dumps(circuit))
print(f"After parameters: {circuit_q.parameters}")

This prints:

Before parameters: ParameterView([ParameterVectorElement(θ[0]), ParameterVectorElement(θ[1]), ParameterVectorElement(θ[2]), ParameterVectorElement(θ[3])])
After parameters: ParameterView([Parameter(_θ_0_), Parameter(_θ_1_), Parameter(_θ_2_), Parameter(_θ_3_)])