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.31k stars 595 forks source link

[BUG] Different behaviour `from pennylane import numpy` vs `import numpy` #3974

Closed erick-xanadu closed 1 year ago

erick-xanadu commented 1 year ago

Expected behavior

I have a test case where defining numpy as from pennylane import numpy differs from import numpy. I expect them to be the same. I now understand that from pennylane import numpy and import numpy will behave different. However, this might still be a bug. The error was not raised before this Monday, which might imply that it is related with recent changes regarding return types.

Actual behavior

Different behaviour, with from pennylane import numpy there's an exception raised. With import numpy the program succeeds.` The error was raised.

Additional information

This is on pennylane main using this commit 2efbb3b07d3665a2a208bed777c9120940a47bdd

Source code

import pennylane as qml
from pennylane import numpy as np

symbols = ["H", "H", "H"]
coordinates = np.array([0.028, 0.054, 0.0, 0.986, 1.610, 0.0, 1.855, 0.002, 0.0])

# Building the molecular hamiltonian for the trihydrogen cation
hamiltonian, qubits = qml.qchem.molecular_hamiltonian(symbols, coordinates, charge=1)

# The Hartree-Fock State 
hf = qml.qchem.hf_state(electrons=2, orbitals=6)

# Define the device, using lightning.qubit device
dev = qml.device("lightning.qubit", wires=qubits)

@qml.qnode(dev, diff_method="adjoint")
def cost_func(params):
    qml.BasisState(hf, wires=range(qubits))
    qml.DoubleExcitation(params[0], wires=[0, 1, 2, 3])
    qml.DoubleExcitation(params[1], wires=[0, 1, 4, 5])
    return qml.expval(hamiltonian)

# **Step 3**

# In[4]:

def workflow(params, ntrials):
    opt = qml.GradientDescentOptimizer(stepsize=0.4)

    for n in range(ntrials):
        params, prev_energy = opt.step_and_cost(cost_func, params)
        print(f"--- Step: {n}, Energy: {cost_func(params):.8f}")

    return params

theta = workflow(np.array([0.0, 0.0]), 10)

Tracebacks

/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/tape/qscript.py:569: UserWarning: The get_operation will soon be updated to also return the index of the trainable operation in the tape. If you want to switch to the new behavior, you can pass `return_op_index=True`
  warnings.warn(
Traceback (most recent call last):
  File "/home/eo/Code/bug/test.py", line 37, in <module>
    theta = workflow(np.array([0.0, 0.0]), 10)
  File "/home/eo/Code/bug/test.py", line 32, in workflow
    params, prev_energy = opt.step_and_cost(cost_func, params)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/optimize/gradient_descent.py", line 59, in step_and_cost
    g, forward = self.compute_grad(objective_fn, args, kwargs, grad_fn=grad_fn)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/optimize/gradient_descent.py", line 117, in compute_grad
    grad = g(*args, **kwargs)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/_grad.py", line 115, in __call__
    grad_value, ans = grad_fn(*args, **kwargs)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/autograd/wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/_grad.py", line 141, in _grad_with_forward
    grad_value = vjp(vspace(ans).ones())
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/autograd/core.py", line 14, in vjp
    def vjp(g): return backward_pass(g, end_node)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/autograd/core.py", line 21, in backward_pass
    ingrads = node.vjp(outgrad[0])
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/autograd/core.py", line 67, in <lambda>
    return lambda g: (vjp(g),)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/interfaces/autograd.py", line 451, in grad_fn
    vjps = _compute_vjps_autograd(jacs, dy, multi_measurements, device.shot_vector)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/interfaces/autograd.py", line 537, in _compute_vjps_autograd
    shot_vjps.append(qml.gradients.compute_vjp_single_new(d, j))
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/gradients/vjp.py", line 140, in compute_vjp_single_new
    res = qml.math.tensordot(jac, dy_row, [[0], [0]])
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/math/multi_dispatch.py", line 151, in wrapper
    return fn(*args, **kwargs)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/pennylane/math/multi_dispatch.py", line 387, in tensordot
    return np.tensordot(tensor1, tensor2, axes=axes, like=like)
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/autoray/autoray.py", line 79, in do
    return get_lib_fn(backend, fn)(*args, **kwargs)
  File "<__array_function__ internals>", line 180, in tensordot
  File "/home/eo/Code/workspace-latest/env/lib/python3.10/site-packages/numpy/core/numeric.py", line 1116, in tensordot
    raise ValueError("shape-mismatch for sum")
ValueError: shape-mismatch for sum

System information

Name: PennyLane
Version: 0.30.0.dev0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/eo/Code/catalyst-latest/env/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml
Required-by: pennylane-catalyst, PennyLane-Lightning

Platform info:           Linux-5.19.0-38-generic-x86_64-with-glibc2.35
Python version:          3.10.6
Numpy version:           1.23.5
Scipy version:           1.10.1
Installed devices:
- default.gaussian (PennyLane-0.30.0.dev0)
- default.mixed (PennyLane-0.30.0.dev0)
- default.qubit (PennyLane-0.30.0.dev0)
- default.qubit.autograd (PennyLane-0.30.0.dev0)
- default.qubit.jax (PennyLane-0.30.0.dev0)
- default.qubit.tf (PennyLane-0.30.0.dev0)
- default.qubit.torch (PennyLane-0.30.0.dev0)
- default.qutrit (PennyLane-0.30.0.dev0)
- null.qubit (PennyLane-0.30.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.29.0)

Existing GitHub issues

timmysilv commented 1 year ago

chatted with Erick offline, and I want to add the detail that this didn't raise an error with the old return system.

rmoyard commented 1 year ago

https://github.com/PennyLaneAI/pennylane-lightning/pull/427/files#diff-7ab66ad23bebdcb3e384f6b56710236d00fde29be3bc49a641e29c99385f0305R616 I think should be solved by @albi3ro PR on lightning.

albi3ro commented 1 year ago

Yep. This problem is due to lightning not supporting the new return types specification. This should be fixed shortly by: https://github.com/PennyLaneAI/pennylane-lightning/pull/427

erick-xanadu commented 1 year ago

Thank you all! Feel free to close this as a duplicate then :)