Installing lightning.qubit in a MacOS pc

Hi everyone, I would like to ask you how I can install the lightning.qubit in my macbookair.

My mac has 1.6 GHz dual core intel core i5 and installs Sonoma 14.1.1. For this, I tried to install the lightning.qubit while constructing an environment with anaconda3, as follows,

conda create -n test -y python=3.9
conda run -n test pip install pennylane-lightning

Then, activating the environment “test”, as follows,

conda activate test

we run the following codes in the python,

import pennylane as qml
dev = qml.device(“lightning.qubit”, wires=2)

Then, I got the following warning,

/Users/test/anaconda3/envs/qlstm-env/lib/python3.9/site-packages/pennylane_lightning/lightning_qubit/lightning_qubit.py:824: UserWarning: Pre-compiled binaries for lightning.qubit are not available. Falling back to using the Python-based default.qubit implementation. To manually compile from source, follow the instructions at Installation — Lightning 0.34.0-dev20 documentation.

This would say that the lightning.qubit is not used.

Following the last comment of the warning, I tried
PL_BACKEND=lightning_qubit pip install pybind11 pennylane-lightning --no-binary :all:
, but I obtained the same result.

The other useful information I can share would be the result of qml.about() :

Name: PennyLane
Version: 0.33.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for differentiable programming of quantum computers. Train a quantum computer the same way as a neural network.
Author:
Author-email:
License: Apache License 2.0
Location: /Users/test/anaconda3/envs/qlstm-env/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning

Platform info: macOS-10.16-x86_64-i386-64bit
Python version: 3.9.18
Numpy version: 1.26.2
Scipy version: 1.11.4
Installed devices:

  • default.gaussian (PennyLane-0.33.1)
  • default.mixed (PennyLane-0.33.1)
  • default.qubit (PennyLane-0.33.1)
  • default.qubit.autograd (PennyLane-0.33.1)
  • default.qubit.jax (PennyLane-0.33.1)
  • default.qubit.legacy (PennyLane-0.33.1)
  • default.qubit.tf (PennyLane-0.33.1)
  • default.qubit.torch (PennyLane-0.33.1)
  • default.qutrit (PennyLane-0.33.1)
  • null.qubit (PennyLane-0.33.1)
  • lightning.qubit (PennyLane-Lightning-0.33.1)

Finally, I note that, even if I install the pennylane at a different python version, including 3.10 and 3.11, I get the same result (lightning.qubit does not work).

Could anyone teach me how I can successfully install the lightning.qubit ?

Thank you for your advice.

Hey @tkashi! Welcome to the forum :slight_smile:

Apologies for the delayed response. I’ll look into this ASAP!

Hi @tkashi, welcome to the forum!

You don’t need to install PennyLane-Lightning separately, it already comes when you install PennyLane! You can actually see it at the bottom where it says installed devices. lightning.qubit is the cpu simulator that comes with PennyLane-Lightning.

I hope this answers your question!

Try out a simple circuit to test that you can indeed use this device and feel free to post another message here in case you run into further issues.

Hi @CatalinaAlbornoz, thank you for your reply.

Unfortunately, even if I install just pennylane, I get the same result.

I looked into codes, for example, available in GitHub - PennyLaneAI/pennylane-lightning at 8101e42dd84f91ddd4476f90f6c3aaf061924aa7, particularly, pennylane_lightning/lightning_qubit/lightning_qubit.py, and found that the codes in the lightning_qubit.py of
"
from pennylane_lightning.core.lightning_base import (
LightningBase,
LightningBaseFallBack,
_chunk_iterable,
)
"
can be loaded in the python, but the forthcoming codes of
"
from pennylane_lightning.lightning_qubit_ops import (
allocate_aligned_array,
get_alignment,
best_alignment,
MeasurementsC64,
StateVectorC64,
MeasurementsC128,
StateVectorC128,
backend_info,
)
"
cannot be loaded correctly. Because of this, the UserWarning, described above, seems to appear.

This is all that I currently find related to the unsuccessful installation. Could you teach me how to correct the issues under this situation?

Thank you.

Indeed, I did have issues trying to build PennyLane lightning from source and from installing it via installing PennyLane (pip install pennylane), both on an M1 mac (I’ll bring this up to our performance team to see what the solution is). Both of these attempts used python 3.9.

Having said that, I didn’t have any issues with python 3.11. Here’s what I did.

  1. Started from a fresh virtual environment (always a good idea when debugging these things):
$ python3 -m venv /path/to/virtualenv/

NB: the python3 alias is my interpreter for python 3.11.

  1. Activate the venv and install pennylane:
$ source /path/to/virtualenv/bin/activate
$ python3 -m pip install pennylane
  1. Run this to make sure it works:
>>> import pennylane as qml
>>> dev = qml.device("lightning.qubit", wires=3)
>>> @qml.qnode(dev)
... def circuit():
...     qml.Hadamard(wires=0)
...     return qml.state()
...
>>> circuit()
tensor([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.        +0.j,
        0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.        +0.j], requires_grad=True)

Here’s my output of pip freeze (lists all of the packages in my virtual environment):

appdirs==1.4.4
appnope==0.1.3
asttokens==2.4.1
autograd==1.6.2
autoray==0.6.7
cachetools==5.3.2
certifi==2023.11.17
charset-normalizer==3.3.2
comm==0.2.0
debugpy==1.8.0
decorator==5.1.1
executing==2.0.1
future==0.18.3
idna==3.6
ipykernel==6.27.1
ipython==8.18.1
jedi==0.19.1
jupyter_client==8.6.0
jupyter_core==5.5.1
matplotlib-inline==0.1.6
nest-asyncio==1.5.8
networkx==3.2.1
numpy==1.26.2
packaging==23.2
parso==0.8.3
PennyLane==0.33.1
PennyLane-Lightning==0.33.1
pexpect==4.9.0
platformdirs==4.1.0
prompt-toolkit==3.0.43
psutil==5.9.7
ptyprocess==0.7.0
pure-eval==0.2.2
Pygments==2.17.2
python-dateutil==2.8.2
pyzmq==25.1.2
requests==2.31.0
rustworkx==0.13.2
scipy==1.11.4
semantic-version==2.10.0
six==1.16.0
stack-data==0.6.3
toml==0.10.2
tornado==6.4
traitlets==5.14.0
typing_extensions==4.9.0
urllib3==2.1.0
wcwidth==0.2.12

Let me know if this helps! As I said, I’ll speak with our performance team to see why this doesn’t seem as straightforward as it should be :slight_smile:

Hi @tkashi, let’s try the following test. If something doesn’t work please let us know at what step and the error that you get.

  1. Open your terminal
  2. Create a new Conda environment with: conda create --name <name_of_your_environment> python=3.10
    (Note that where it says <name_of_your_environment> you can choose any name that you want)
  3. Activate the environment with: conda activate <name_of_your_environment>
  4. Install PennyLane with: python -m pip install pennylane
  5. Install other useful packages like Jupyter and Matplotlib: python -m pip install jupyter matplotlib
  6. Write jupyter notebook in your terminal
  7. Create a new notebook and run the code below:
import pennylane as qml

dev = qml.device('lightning.qubit',wires=2)

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0,1])
    return qml.probs()

print(circuit())
qml.draw_mpl(circuit)();

Let me know how it goes!

Thank you very much for both responses.

Following the first suggestion of setting up the environment with python, rather than conda, I finally could install the lightning.qubit successfully. Many thanks ! On the other hand, I could not install it in the latter way. The same userWarning appeared. Thus, some problems seem to happen in the conda. I do not know what they are exactly (btw, in my case, I am using 23.11.0), but anyway, I am happy that the lightning can be used in an environment.

This is very interesting information @tkashi . Thanks for sharing the results of these tests here.
We will investigate further in the new year. In the meantime, I’m glad you could get it running!

Happy coding!