-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Truncating floats in QASM dump alters circuit's functionality #7166
Comments
This is very true, and I'm not a fan of We can look into raising the number of digits here. Due to some unfortunate coupling with other parts of the code, I think it possibly has a couple of knock-on changes too. Our new OpenQASM 3 exporter is a bit more separated out from the Terra internals, which will hopefully mean things like this will be far more controllable once that arrives (see #6565, due to land in Terra 0.19). I will say that "dumping a file to |
I see your point that OpenQASM 2 is not intended as a lossless serialisation option. However, in this case, where this is not an inherent limitation of the OpenQASM 2 specification, I believe it would be beneficial to fix this. Great to see, that OpenQASM 3 export is coming. Looking forward to that! |
pi_check serves a nice purpose in finding fractions of pi, whether for visualization or qasm output. The ndigits is just there to tell pi_check what to do with the float if no fraction of pi is found. In visualizations, this is set to something that looks good in the display. For qasm, it really should be up to the user how many float decimals are included in the output, since I doubt everyone needs 17, so I'd recommend adding a kwarg to Instruction.qasm, to let the user choose. Of course, it doesn't solve the problem that OpenQASM 2 and QuantumCircuit are not one-to-one, but it would give users more flexibility. |
I tend to agree that having lossless-as-possible mode for qasm export is a useful enhancement. Both for the reason of interfacing with external tools, and also for debug purposes. |
I'm on board with adding more precision to QASM 2 output, I'm just not certain the best way to go about it here. We can't (officially) add anything to the signature of A couple of ways we could go:
None of those are super nice to me, but there may be other options. The last is probably the nicest, but it doesn't solve the other underlying problem that calls to This functionality will (or should) be available in the OpenQASM 3 exporter. The way internal instructions provide their QASM definition (at the moment) is by returning AST nodes, so that's a lossless conversion, and the printer is completely separated, and can take many more options - one of the ones already implemented is to completely disable all the |
The second option certainly provides the most flexibility. Having |
The problem is that |
I get that. But then I would suggest the easiest workaround/fix that does not need any kind of deprecation would just be to change the magic In this fashion, no compatibility is broken and the export to OpenQASM 2 gets a lot more accurate / less lossy. |
@jakelishman If @burgholzer's short-term solution is acceptable, I can take care of it. |
I just ran into this bug/peculiarity of |
Sorry this fell through the cracks. I'm ok with accepting a PR that changes |
No worries. Would you also be willing to change |
Information
What is the current behavior?
In #4276, the
pi_check
function was introduced for pretty printing the list of parameters when dumping aQuantumCircuit
toqasm
, seehttps://github.com/Qiskit/qiskit-terra/blob/637acc0c408d8199dd58780cc3dafabc14e61a0f/qiskit/circuit/instruction.py#L450-L463
This also introduced a fixed truncation (
ndigits=8
) of floating point numbers that are not close to any fraction or fraction of pi. However, 8 digits is not enough to uniquely identify a double precision floating point number. You need up to 17 significant digits for that. This leads to the following situation:Let's say I have a circuit that contains, e.g., a phase gate with more than 8 significant digits in its rotation angle.
Then, I dump the circuit to a
.qasm
file and read that file back into aQuantumCircuit
.If I compare both unitary functionalities (i.e., the respective unitary matrices), these will no longer be equal due to the numerical error introduced by the truncation.
Steps to reproduce the problem
yields
What is the expected behavior?
Merely dumping a file to
.qasm
should not alter a circuit's functionality.Suggested solutions
A quick fix for this would be to just change the default value for the
ndigits
parameter in the respective function call to17
.The text was updated successfully, but these errors were encountered: