PennyLaneAI / pennylane

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.
https://pennylane.ai
Apache License 2.0
2.27k stars 585 forks source link

[BUG] Discrepancy in output values of the example of `qml.pulse.drive` #6025

Open Shiro-Raven opened 1 month ago

Shiro-Raven commented 1 month ago

Expected behavior

Output of the code example in the qml.pulse.hardware_hamiltonian.drive function should be consistent with the output from the docs.

Actual behavior

Instead of receiving the docs output:

>>> params = [2.4]
>>> circuit(params)
Array(0.32495208, dtype=float64)
>>> jax.grad(circuit)(params)
[Array(1.31956098, dtype=float64)]

The following is returned:

>>> circuit(params)
Array(-0.17375104, dtype=float64)
>>> jax.grad(circuit)(params)
[Array(13.66916253, dtype=float64, weak_type=True)]

Additional information

No response

Source code

import pennylane as qml
import jax
from jax import numpy as jnp

import jax

jax.config.update("jax_enable_x64", True)
wires = [0, 1, 2, 3]
H_int = sum([qml.X(i) @ qml.X((i + 1) % len(wires)) for i in wires])

amplitude = lambda p, t: p * jnp.sin(jnp.pi * t)
phase = jnp.pi / 2
H_d = qml.pulse.drive(amplitude, phase, wires)

dev = qml.device("default.qubit.jax", wires=wires)

@qml.qnode(dev, interface="jax")
def circuit(params):
    qml.evolve(H_int + H_d)(params, t=[0, 10])
    return qml.expval(qml.Z(0))

params = [2.4]
print(circuit(params))
print(jax.grad(circuit)(params))

Tracebacks

No response

System information

Name: PennyLane
Version: 0.37.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: /Users/ahmed.darwish/Desktop/Code/xanadu-dev/lib/python3.9/site-packages
Editable project location: /Users/ahmed.darwish/Desktop/Code/pennylane
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: amazon-braket-pennylane-plugin, PennyLane-Catalyst, PennyLane-qiskit, PennyLane_Lightning, PennyLane_Lightning_Kokkos

Platform info:           macOS-14.5-arm64-arm-64bit
Python version:          3.9.19
Numpy version:           1.26.4
Scipy version:           1.12.0
Installed devices:
- default.clifford (PennyLane-0.38.0.dev0)
- default.gaussian (PennyLane-0.38.0.dev0)
- default.mixed (PennyLane-0.38.0.dev0)
- default.qubit (PennyLane-0.38.0.dev0)
- default.qubit.autograd (PennyLane-0.38.0.dev0)
- default.qubit.jax (PennyLane-0.38.0.dev0)
- default.qubit.legacy (PennyLane-0.38.0.dev0)
- default.qubit.tf (PennyLane-0.38.0.dev0)
- default.qubit.torch (PennyLane-0.38.0.dev0)
- default.qutrit (PennyLane-0.38.0.dev0)
- default.qutrit.mixed (PennyLane-0.38.0.dev0)
- default.tensor (PennyLane-0.38.0.dev0)
- null.qubit (PennyLane-0.38.0.dev0)
- nvidia.custatevec (PennyLane-Catalyst-0.7.0.dev0)
- nvidia.cutensornet (PennyLane-Catalyst-0.7.0.dev0)
- oqc.cloud (PennyLane-Catalyst-0.7.0.dev0)
- softwareq.qpp (PennyLane-Catalyst-0.7.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.37.0.dev42)
- lightning.kokkos (PennyLane-Lightning-Kokkos-0.37.0.dev42)
- braket.aws.ahs (amazon-braket-pennylane-plugin-1.28.0)
- braket.aws.qubit (amazon-braket-pennylane-plugin-1.28.0)
- braket.local.ahs (amazon-braket-pennylane-plugin-1.28.0)
- braket.local.qubit (amazon-braket-pennylane-plugin-1.28.0)
- qiskit.aer (PennyLane-qiskit-0.37.0.dev0)
- qiskit.basicaer (PennyLane-qiskit-0.37.0.dev0)
- qiskit.basicsim (PennyLane-qiskit-0.37.0.dev0)
- qiskit.ibmq (PennyLane-qiskit-0.37.0.dev0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.37.0.dev0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.37.0.dev0)
- qiskit.remote (PennyLane-qiskit-0.37.0.dev0)

Existing GitHub issues

trbromley commented 1 month ago

From discussions, it looks like the code example just doesn't account for a factor of $2 \pi$, so we can just update the docstrings of drive().