LinCombEstimatorGradient has incorrect sign for imaginary and complex cases.
See original GitHub issueEnvironment
- Qiskit Terra version: 0.23.0
- Python version: 3.9
- Operating system: MacOS
What is happening?
Together with @Cryoris, we noticed that if the IMAG
option is set, the resulting gradient has a spurious (undocumented) -
sign.
Same problem seems to affect the COMPLEX
option.
How can we reproduce the issue?
The code
from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
import numpy as np
from qiskit.algorithms.gradients import DerivativeType, LinCombEstimatorGradient
from qiskit.primitives import Estimator
from qiskit.quantum_info import Pauli
estimator = Estimator()
gradient = LinCombEstimatorGradient(estimator, derivative_type=DerivativeType.IMAG)
c = QuantumCircuit(1)
c.rz(Parameter("p"), 0)
grad = gradient.run([c],[Pauli("I")], [[0.0]])
print(grad.result())
prints
EstimatorGradientResult(gradients=[array([-1.])], metadata=[{'parameters': [Parameter(p)], 'derivative_type': <DerivativeType.IMAG: 'imag'>}], options=Options())
What should happen?
For the code example above, the result should be:
EstimatorGradientResult(gradients=[array([1.])], metadata=[{'parameters': [Parameter(p)], 'derivative_type': <DerivativeType.IMAG: 'imag'>}], options=Options())
Any suggestions?
This code fragment
if self._derivative_type == DerivativeType.REAL:
op1 = SparsePauliOp.from_list([("Z", 1)])
elif self._derivative_type == DerivativeType.IMAG:
op1 = SparsePauliOp.from_list([("Y", -1)])
elif self._derivative_type == DerivativeType.COMPLEX:
op1 = SparsePauliOp.from_list([("Z", 1)])
op2 = SparsePauliOp.from_list([("Y", -1)])
should be replaced with
if self._derivative_type == DerivativeType.REAL:
op1 = SparsePauliOp.from_list([("Z", 1)])
elif self._derivative_type == DerivativeType.IMAG:
op1 = SparsePauliOp.from_list([("Y", 1)])
elif self._derivative_type == DerivativeType.COMPLEX:
op1 = SparsePauliOp.from_list([("Z", 1)])
op2 = SparsePauliOp.from_list([("Y", 1)])
It also seems that unit tests for IMAG
and COMPLEX
options are missing.
Issue Analytics
- State:
- Created 10 months ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Fixed sign bug in docs in LinCombEstimatorGradient. #9113
Successfully merging this pull request may close these issues. LinCombEstimatorGradient has incorrect sign for imaginary and complex cases. 5 ...
Read more >SAT Mathematics : Working with Imaginary Numbers
First of all, you must carefully observe the minus sign in the middle of the expression, which means that you are subtracting the...
Read more >complex numbers - What is wrong with my proof: $-1 = 1
The flaw is in assuming that the rule √x√y=√xy holds with imaginary numbers. You just show us a counter-example.
Read more >Multiplying complex numbers (article) - Khan Academy
We used the distributive property to multiply a real number by a complex number. Let's try something a little more complicated. Multiplying a...
Read more >Imaginary Numbers Are Reality - Nautilus
The combination of the two is known as a “complex number” (it's complex as in “military-industrial complex,” speaking of combination—of real and ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After discussing with @dlasecki @Zoufalc and @a-matsuo it seems that the conclusion is to change the documentation and not the code, because
Yes, sounds good to me. I will adapt the PR that I opened already.