-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Qasm exporter creates invalid Qasm for subcircuits with classical registers #7750
Comments
There is no possible valid OpenQASM 2 programme for the circuit you're describing; there's no subroutine-like construct that can take classical parameters. The error message could be better, but there is no way to produce valid OpenQASM 2 in this situation, because the object has no way of being represented. The bug here is that the QASM 2 exporter should have rejected the circuit out-of-hand. I haven't looked at the QASM 2 exporter code for a while, but I think it tries to export all
The way you have added the subcircuit to your circuit, it's as an OpenQASM 3 can represent non-unitary subroutines, but at the moment Terra's QASM 3 exporter is quite limited in what it can support, since that language spec is still evolving and so is Terra's capability to represent dynamic circuits. |
I attach this bug report here, since closely related (instead of opening a new one) Environment
What is happening?Exporting a circuit with a sub-circuit via How can we reproduce the issue?Run this python script: from qiskit import QuantumCircuit
q = QuantumCircuit(2)
q.initialize('00')
print(q.qasm(filename="my.qasm"))
round_trip = QuantumCircuit.from_qasm_str(q.qasm()) Produces this output and error: Converting to qasm and back for circuit circuit-114
┌───┐ ┌─┐ ┌──────────────┐
q_0: ┤ H ├──■──┤M├───┤0 ├
└───┘┌─┴─┐└╥┘┌─┐│ │
q_1: ─────┤ X ├─╫─┤M├┤1 ├
└───┘ ║ └╥┘│ circuit-115 │
c_0: ═══════════╩══╬═╡0 ╞
║ │ │
c_1: ══════════════╩═╡1 ╞
└──────────────┘
OPENQASM 2.0;
include "qelib1.inc";
gate circuit_115 q0,q1 { h q0; cx q0,q1; measure q0; measure q1; }
qreg q[2];
creg c[2];
h q[0];
cx q[0],q[1];
measure q[0] -> c[0];
measure q[1] -> c[1];
circuit_115 q[0],q[1],c[0],c[1];
Error near line 3 Column 15
Traceback (most recent call last):
File "myfile.py", line 16, in <module>
QuantumCircuit().from_qasm_str(circuit2.qasm())
File "...qiskit/circuit/quantumcircuit.py", line 2529, in from_qasm_str
return _circuit_from_qasm(qasm)
File "...qiskit/circuit/quantumcircuit.py", line 4964, in _circuit_from_qasm
ast = qasm.parse()
File "...qiskit/qasm/qasm.py", line 53, in parse
return qasm_p.parse(self._data)
File "...qiskit/qasm/qasmparser.py", line 1137, in parse
self.parser.parse(data, lexer=self.lexer, debug=self.parse_deb)
File "...ply/yacc.py", line 333, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "...ply/yacc.py", line 1120, in parseopt_notrack
p.callable(pslice)
File "...qiskit/qasm/qasmparser.py", line 397, in p_id_e
raise QasmError("Expected an ID, received '" + str(program[1].value) + "'")
qiskit.qasm.exceptions.QasmError: "Expected an ID, received 'measure'"
The qasm contains also a second error leading to an error, thus the fact that What should happen?I would expect a valid qasm since the circuit is theoretically representable in qasm. Any suggestions?What about expanding the definition and injecting the basic instructions directly in the main code, without a separate definition, thus avoiding the problem with the |
Environment
What is happening?
Converting a circuit with a subcircuit to qasm fails, when this subcircuit contains also classical registers.
How can we reproduce the issue?
Run this:
Output:
Convert to qasm:
Output:
Now, if we want to reuse this qasm and load it:
We get the following error:
What should happen?
I would have expected the exported qasm to be a valid qasm (aka to be read again) and also give me back the original exported circuit if I decide to store my circuit on a file as qasm and pass it to someone else.
Any suggestions?
I would have expected the following QASM:
But this is also inaccurate, since in the original
subcircuit
might as well use the classical registers to perform some measurement, whereas with this qasm representation it is not possible to express this.Thus, I am not sure on how we should proceed and I very curious to listen to your feedback on this.
Thanks in advance
The text was updated successfully, but these errors were encountered: