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.11k stars 2.35k forks source link

QuantumCircuit.measure_active gives a creg the name "measure" that is a reserved QASM word #7866

Closed kdk closed 1 year ago

kdk commented 2 years ago

Environment

What is happening?

https://github.com/Qiskit/qiskit-terra/issues/4023 found an issue that QuantumCircuit.measure_all creates a ClassicalRegister called measure, which is a reserved keyword in OpenQASM2+3. This was changed to meas for measure_active in #4079 , and addressed somewhat more generally for OpenQASM3 export in #7744 .

How can we reproduce the issue?

qc = qk.QuantumCircuit(3)
qc.x([0,1,2])
qc.measure_active()

QuantumCircuit.from_qasm_str(qc.qasm())

raises

QasmError: "Expected an ID, received 'measure'"

What should happen?

It would be good to:

Any suggestions?

No response

vtomole commented 2 years ago

We've run into this issue as well:

import qiskit
import qiskit_experiments.library.quantum_volume as qv
from qiskit.compiler import transpile

rand_circs = qv.QuantumVolume(qubits=[0,1,2]).circuits()

t_rand_circ = transpile(rand_circs[0], basis_gates=['u1','u2','u3','cx'])
print(t_rand_circ.qasm())
# prints
# OPENQASM 2.0;
# include "qelib1.inc";
# qreg q[2];
# creg measure[2];
# u3(0.95275496,-0.40048302,2.3396091) q[0];
# u3(1.4710132,1.4238801,-0.9022253) q[1];
# cx q[0],q[1];
# u3(0.7076726,-pi/2,-pi/2) q[0];
# u3(2.1567055,2.5482935,0.68681294) q[1];
# cx q[0],q[1];
# u3(0.28381753,-pi,pi/2) q[0];
# u3(1.0477967,0.64780817,-2.5581967) q[1];
# cx q[0],q[1];
# u3(1.4541338,-0.35391366,-1.7202243) q[0];
# u3(1.9771718,0.44431931,-0.27642371) q[1];
# barrier q[0],q[1];
# measure q[0] -> measure[0];
# measure q[1] -> measure[1];

qiskit.QuantumCircuit.from_qasm_str(t_rand_circ.qasm()) #raises qiskit.qasm.exceptions.QasmError: "Expected an ID, received 'measure'"
jakelishman commented 2 years ago

Thanks for bringing this up again. This one particular case should hopefully be able to be handled by a simple "escape name" type of function in the exporter, as opposed to the OpenQASM 3.0 exporter's symbol-table version. I'm happy to accept/help with PRs to fix this, though.

vtomole commented 2 years ago

"escape name" type of function in the exporter

What's an "escape name"? Is it basically a way to tell the exporter to write _measure (with underscore) instead of the existing measure?

jakelishman commented 2 years ago

Yeah, essentially. We already do some of this, if a gate claims its name is something like BLAH!£%& - that's not a valid OQ 2 identifier because of the leading capital letter and the symbols, so we "escape" it to gate_BLAH____. That same function just needs a bit more logic to ensure it recognises that the OQ 2 reserved keywords are also invalid identifiers, and we need to apply it to registers as well as gates.

jakelishman commented 1 year ago

Fixed by #9100