question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LinCombEstimatorGradient has incorrect sign for imaginary and complex cases.

See original GitHub issue

Environment

  • 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:closed
  • Created 10 months ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Cryoriscommented, Nov 11, 2022

After discussing with @dlasecki @Zoufalc and @a-matsuo it seems that the conclusion is to change the documentation and not the code, because

  1. differentiating left or right is equivalent; we just have to fix a definition
  2. this way, we don’t have to break the code behavior and just update the docs
0reactions
dlaseckicommented, Nov 11, 2022

After discussing with @dlasecki @Zoufalc and @a-matsuo it seems that the conclusion is to change the documentation and not the code, because

  1. differentiating left or right is equivalent; we just have to fix a definition
  2. this way, we don’t have to break the code behavior and just update the docs

Yes, sounds good to me. I will adapt the PR that I opened already.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found