Skip to content
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] qml.ctrl does not modify parameter_frequencies correctly #4898

Closed
1 task done
dwierichs opened this issue Nov 29, 2023 · 0 comments · Fixed by #4899
Closed
1 task done

[BUG] qml.ctrl does not modify parameter_frequencies correctly #4898

dwierichs opened this issue Nov 29, 2023 · 0 comments · Fixed by #4899
Labels
bug 🐛 Something isn't working

Comments

@dwierichs
Copy link
Contributor

Expected behavior

qml.ctrl(op) can be differentiated with the parameter-shift rule if op can be differentiated with the parameter-shift rule.
The differentiation yields the correct derivative.

Actual behavior

The differentiation of qml.ctrl(op) will be wrong whenever the generator of the original op is a SparseHamiltonian and has more than 1 eigenvalue.

Additional information

This is because qml.eigvals is used to compute the parameter_frequencies of qml.ctrl(op) and qml.eigvals has a kwarg k, which defaults to 1 and determines the number of eigenvalues that are being computed for SparseHamiltonian inputs.

Source code

>>> @qml.qnode(qml.device("default.qubit"))
>>> def circuit(x):
...     qml.Hadamard(0)
...     qml.Hadamard(1)
...     qml.Hadamard(2)
...     qml.ctrl(qml.DoubleExcitationMinus(x, [1, 2, 3, 4]), 0)
...     return qml.expval(qml.dot([1, 1], [qml.PauliY(0), qml.PauliZ(3)]))

>>> x = pnp.array(0.7, requires_grad=True)
>>> circuit(x)
>>> print(qml.grad(circuit)(x))
>>> print(qml.gradients.param_shift(circuit)(x))

-0.43279197822247817
-0.3522647673177669

Tracebacks

No response

System information

pl dev

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@dwierichs dwierichs added the bug 🐛 Something isn't working label Nov 29, 2023
dwierichs added a commit that referenced this issue Nov 30, 2023
…`SparseHamiltonian` (#4899)

**Context:**
The `parameter_frequencies` of `qml.ctrl(op)` are obtained using
`qml.eigvals`. If `op.generator` is a `SparseHamiltonian`, `qml.eigvals`
only returns one eigenvalue because of the default setting of the kwarg
`k`.

**edit:** The same problem appears for the default
`qml.operation.parameter_frequencies`.

**Description of the Change:**
This PR explicitly sets the `k` kwarg, in order to obtain all
eigenvalues of the generator, even if it is a `SparseHamiltonian`.

**edit:** Also sets `k` in `qml.operation.parameter_frequencies`

**Benefits:**
parameter-shift rule for `qml.ctrl(op)` works, also for `op` being a
`DoubleExcitation`, e.g.

**Possible Drawbacks:**
N/A

**Related GitHub Issues:**
Fixed #4898

---------

Co-authored-by: lillian542 <38584660+lillian542@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
1 participant