-
Notifications
You must be signed in to change notification settings - Fork 604
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
[BUG] Precision error in two-qubit decomposition of qml.QubitUnitary #5841
Comments
Hi @abhishekabhishek, thank you for opening this bug. What precision were you looking for? Is it more a matter of being aware of the precision beforehand? Or do you need a precision of 1e-9 or similar? |
Hi @CatalinaAlbornoz, I was expecting a precision of at least 1e-8 since that's what |
Ah good catch @abhishekabhishek ! Yes I agree that mentioning it in the docs is important. Thanks for the feedback! |
The problem seems to lie in the optimization we are using to reduce the number of CNOTs. At least the code above works out if one deactivates this optimization and always decomposes to the most general circuits containing 3 CNOTs... |
I took a closer look at this (as the original author, I feel responsible for the issues here! 😅). I tried with/without the small perturbation in precision that was added in #5448, but this was not the problem, and there is precision loss in both cases. I think what it boils down to is, the matrix used here is a special case (controlled-phase gate) whose decomposition requires only 2 CNOTs. You can check that the following circuit identity holds, up to a global phase: Applying some simple circuit identities on the single-qubit rotations, we can see that the However, I'm a bit stumped about why it works with the 3 CNOT case which has just as many angles. It could be a matter of how the rotations are derived from the matrix products computed internally by functions like |
Indeed, looking at all these extra rotations and setting the angles that are very close to edit: I was able to solve the issue using |
Perhaps a simple check like if qml.math.isclose(np.sin(theta), 0):
theta = np.pi in those angle extraction functions would be useful for "resetting" things that are close-to-multiples-of-pi to pi? It could be a "good first issue". (Though TBH I could have sworn I have implemented something like that in the past, but maybe that was in somewhere in The Ionizer) |
The above will no longer be differentiable at these special points (an issue I've been fighting with elsewhere (#5715) as well 😅 ) |
Right, of course... 🤦♀️ |
these kind of numerical-value-based checks/"fixes" can also sometimes lead to problems with jitting |
Expected behavior
The two-qubit decomposition for qml.QubitUnitary should be valid upto a high-precision.
Actual behavior
The two-qubit decomposition for qml.QubitUnitary is only valid upto around
1e-7
. This leads to an error in matrix equivalence checking if theatol
is not specified since defaultatol = 1e-8
innp.allclose
.Additional information
This seems related to the now closed issue #5308 and its corresponding fix PR #5448.
Source code
Tracebacks
No response
System information
Name: PennyLane Version: 0.36.0 Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network. Home-page: https://github.com/PennyLaneAI/pennylane Author: Author-email: License: Apache License 2.0 Location: /home/abhishekabhishek/anaconda3/envs/raccoon/lib/python3.11/site-packages Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions Required-by: PennyLane-qiskit, PennyLane_Lightning Platform info: Linux-6.5.0-18-generic-x86_64-with-glibc2.35 Python version: 3.11.9 Numpy version: 1.26.4 Scipy version: 1.13.0 Installed devices: - lightning.qubit (PennyLane_Lightning-0.36.0) - default.clifford (PennyLane-0.36.0) - default.gaussian (PennyLane-0.36.0) - default.mixed (PennyLane-0.36.0) - default.qubit (PennyLane-0.36.0) - default.qubit.autograd (PennyLane-0.36.0) - default.qubit.jax (PennyLane-0.36.0) - default.qubit.legacy (PennyLane-0.36.0) - default.qubit.tf (PennyLane-0.36.0) - default.qubit.torch (PennyLane-0.36.0) - default.qutrit (PennyLane-0.36.0) - default.qutrit.mixed (PennyLane-0.36.0) - null.qubit (PennyLane-0.36.0) - qiskit.aer (PennyLane-qiskit-0.35.1) - qiskit.basicaer (PennyLane-qiskit-0.35.1) - qiskit.ibmq (PennyLane-qiskit-0.35.1) - qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.35.1) - qiskit.ibmq.sampler (PennyLane-qiskit-0.35.1) - qiskit.remote (PennyLane-qiskit-0.35.1)
Existing GitHub issues
The text was updated successfully, but these errors were encountered: