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.35k stars 604 forks source link

Installation error #4047

Closed waudinio27 closed 2 months ago

waudinio27 commented 1 year ago

Expected behavior

I want to run the portfolio example to give it a try

Actual behavior

I get an error message - for sure it is nothing big but I do not understand it.

Additional information

It is something about pytorch .....

Source code

import pennylane as qml
from pennylane import numpy as np

# define parameters in H
N = 6      # number of total assets
gamma = 1  # risk aversion coefficient
B = 3      # budget
P = 1.0    # penalty

ZZ = [qml.PauliZ(i)@qml.PauliZ(j) for i in range(N) for j in range(i+1,N)]
ZZ_coeff = [0.5*(gamma*Sigma.values[i][j] + P) for i in range(N) for j in range(i+1,N)]
Z = [qml.PauliZ(i) for i in range(N)]
Z_coeff = [-0.5*gamma*(sum(Sigma.values[i][:])) + 0.5*R[i] - 0.5*P*(N-2*B) for i in range(N)]
C = 0.25*gamma*(sum(sum(Sigma.values)) + np.trace(Sigma))- 0.5*sum(R) + 0.25*P*(N + (N-2*B)**2)

# Construct the problen Hamiltonian
obs = ZZ + Z
coeffs = ZZ_coeff + Z_coeff
H = qml.Hamiltonian(coeffs, obs, grouping_type="qwc")

Tracebacks

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 import pennylane as qml
      2 from pennylane import numpy as np
      4 # define parameters in H

File ~\anaconda3\lib\site-packages\pennylane\__init__.py:29, in <module>
     26 from pennylane.queuing import apply, QueuingContext
     28 import pennylane.fourier
---> 29 import pennylane.kernels
     30 import pennylane.math
     31 import pennylane.operation

File ~\anaconda3\lib\site-packages\pennylane\kernels\__init__.py:18, in <module>
      1 # Copyright 2018-2021 Xanadu Quantum Technologies Inc.
      2 
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 r"""
     15 This subpackage defines functions that relate to quantum kernel methods.
     16 """
---> 18 from .cost_functions import (
     19     polarity,
     20     target_alignment,
     21 )
     22 from .postprocessing import (
     23     threshold_matrix,
     24     displace_matrix,
   (...)
     27     mitigate_depolarizing_noise,
     28 )
     29 from .utils import (
     30     kernel_matrix,
     31     square_kernel_matrix,
     32 )

File ~\anaconda3\lib\site-packages\pennylane\kernels\cost_functions.py:19, in <module>
     14 """
     15 This file contains functionalities for kernel related costs.
     16 See `here <https://www.doi.org/10.1007/s10462-012-9369-4>`_ for a review.
     17 """
     18 from pennylane import numpy as np
---> 19 from ..math import frobenius_inner_product
     20 from .utils import square_kernel_matrix
     23 def polarity(
     24     X,
     25     Y,
   (...)
     29     normalize=False,
     30 ):

File ~\anaconda3\lib\site-packages\pennylane\math\__init__.py:36, in <module>
     14 """
     15 This package contains unified functions for framework-agnostic tensor and array
     16 manipulation. Given the input tensor-like object, the call is dispatched
   (...)
     32 * JAX
     33 """
     34 import autoray as ar
---> 36 from .multi_dispatch import (
     37     _multi_dispatch,
     38     multi_dispatch,
     39     block_diag,
     40     concatenate,
     41     diag,
     42     dot,
     43     frobenius_inner_product,
     44     get_trainable_indices,
     45     ones_like,
     46     scatter_element_add,
     47     stack,
     48     tensordot,
     49     unwrap,
     50     where,
     51 )
     53 from .quantum import cov_matrix, marginal_prob
     55 from .utils import (
     56     allclose,
     57     allequal,
   (...)
     63     requires_grad,
     64 )

File ~\anaconda3\lib\site-packages\pennylane\math\multi_dispatch.py:24, in <module>
     21 from autoray import numpy as np
     22 from numpy import ndarray
---> 24 from . import single_dispatch  # pylint:disable=unused-import
     25 from .utils import cast, get_interface, requires_grad
     28 def _multi_dispatch(values):

File ~\anaconda3\lib\site-packages\pennylane\math\single_dispatch.py:329, in <module>
    323 try:
    324     if semantic_version.match(">=1.10", _i("torch").__version__):
    325         # Autoray uses the deprecated torch.symeig as an alias for eigh, however this has
    326         # been deprecated in favour of torch.linalg.eigh.
    327         # autoray.py:84: UserWarning: torch.symeig is deprecated in favor of torch.linalg.eigh
    328         # and will be removed in a future PyTorch release.
--> 329         del ar.autoray._FUNCS["torch", "linalg.eigh"]
    330 except ImportError:
    331     pass

KeyError: ('torch', 'linalg.eigh')

1
# Design the ansatz
2
p = 2     # circuit repetitions

System information

I use jupyter notebook with windows and an intel processor i7

Existing GitHub issues

josh146 commented 1 year ago

Hi @waudinio27! What version of Autoray do you have installed locally? Could you try upgrading it, e.g.,

pip install autoray --upgrade
waudinio27 commented 1 year ago

Yes it it s locally. It did not work this way. But I will try it with a clean installation in a virtual env. :-D Thank you for your effort. Have a nice sunday.

isaacdevlugt commented 1 year ago

Hey @waudinio27! Any luck here?

DSGuala commented 2 months ago

Hey @waudinio27 As this issue is quite old, we'll be closing it. Feel free to open another issue if you still have problems.