Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.04k stars 2.32k forks source link

A translation about `initialize` from qiskit in OpenQASM cannot translate back into qiskit #8048

Open weucode opened 2 years ago

weucode commented 2 years ago

Environment

What is happening?

After got the OpenQASM code about initialize operator, it will fail to use from_qasm_file/from_qasm_str to translate it back into qiskit code. Part of error message is shown below:

File "D:\Program Files\pycharm_project\qiskit-benchmarks\venv\lib\site-packages\ply\yacc.py", line 1120, in parseopt_notrack p.callable(pslice) File "D:\Program Files\pycharm_project\qiskit-benchmarks\venv\lib\site-packages\qiskit\qasm\qasmparser.py", line 400, in p_id_e raise QasmError("Expected an ID, received '" + str(program[1].value) + "'") qiskit.qasm.exceptions.QasmError: "Expected an ID, received 'reset'"

How can we reproduce the issue?

from qiskit import QuantumCircuit

circuit = QuantumCircuit(1) circuit.initialize('0', circuit.qubits) qasm_str = circuit.qasm() print(qasm_str) same_circuit = QuantumCircuit(1) same_circuit = same_circuit.from_qasm_str(qasm_str) print(same_circuit)

What should happen?

Run successfully.

Any suggestions?

No response

jakelishman commented 2 years ago

Sorry for the slow reply! This is very related to #7351 and the reasons in https://github.com/Qiskit/qiskit-terra/issues/7750#issuecomment-1062405591. The gist is that initialize is a non-unitary instruction (it includes a reset), and so we can't actually write it as a valid OpenQASM 2 gate. It's a bug that the exporter here puts a reset instruction within a gate - it should probably just fail at that point.

There won't be a way to round-trip a circuit with initialize in it from QASM 2 even if we do some tricks in the export, because the language just can't represent that instruction without it being inlined into the circuit, sorry. We could do some tricks with the (unitary) StatePreparation class, but extending the OpenQASM 2 support is a low priority for us right now, sorry.

harshitta-gandhi commented 1 year ago

Hey, @jakelishman, I too am facing the same issue while converting a pennylane circuit to qasm. What do you think, in the circuit, is causing this error to prop up? And what is the probable timeline for this bug to be resolved?