Open bsiegelwax opened 1 year ago
Hi @bsiegelwax! Sorry about the trouble π The from_qasm function requires the pennylane-qiskit
plugin to work, and you can install it using pip install pennylane-qiskit
. Hopefully that works!
PS: your local error is probably coming from a funny combination of old and new packages from PennyLane and Qiskit alike. To get it all up-to-date, the safest bet is to do pip install -U pennylane pennylane-qiskit
to get the latest versions of everything (and their dependencies). If it's still happening, updating the various qiskit packages to latest wouldn't hurt π
pennylane-qiskit is installed and all libraries are up-to-date. It should at least work on Xanadu Cloud, but it doesn't.
Hi again, thanks for bearing with me. Perhaps your python environments are mixed up (conda gets me turned around, personally), but in your screenshot of qml.about()
there are no qiskit devices listed. If the plugin is installed correctly, you'll see a few qiskit devices popped up, and you'll be able to create a device like qml.device("qiskit.aer", wires=2)
(you don't need to use that device here, it'll just verify that the plugin is installed).
As for what's actually causing issues, I just ran your code on pennylane.xanadu.ai myself to see what's up, and you'll need to make 2 changes:
qml.from_qasm(qasm)
creates a callable function, and you need to call it. See the example in the docs linked above for how to do that@qml.qnode(dev)
def circuit_from_qasm(qasm):
qml.from_qasm(qasm)()
return qml.state()
Hope that clears things up π
qml.device("qiskit.aer", wires=2)
results in <AerDevice device (wires=2, shots=1024) at 0x7fd99eb1e350>
in IBM Quantum Lab, where you can be sure Qiskit is installed. Your updated code results in:
Traceback (most recent call last):
Cell In[7], line 22
result = circuit_from_qasm(qasm)
File /opt/conda/lib/python3.10/site-packages/pennylane/qnode.py:974 in __call__
self.construct(args, kwargs)
File /opt/conda/lib/python3.10/site-packages/pennylane/qnode.py:872 in construct
self._tape = make_qscript(self.func, shots)(*args, **kwargs)
File /opt/conda/lib/python3.10/site-packages/pennylane/tape/qscript.py:1531 in wrapper
result = fn(*args, **kwargs)
Cell In[7], line 19 in circuit_from_qasm
qml.from_qasm(qasm)()
File /opt/conda/lib/python3.10/site-packages/pennylane/io.py:132 in from_qasm
return load(quantum_circuit, format="qasm")
File /opt/conda/lib/python3.10/site-packages/pennylane/io.py:67 in load
raise ValueError(
ValueError: Converter does not exist. Make sure the required plugin is installed and supports conversion.
This works in Xanadu Cloud and only in Xanadu Cloud, which isn't helpful since Xanadu Cloud will be offline in 2 weeks.
import pennylane as qml
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0],q[1];
"""
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def circuit_from_qasm(qasm):
qml.from_qasm(qasm)()
return qml.state()
result = circuit_from_qasm(qasm)
result
I've noticed it uses an older version of Pennylane, lending support to my theory that updates broke it.
Name: PennyLane
Version: 0.28.0
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: /opt/conda/envs/pennylane/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Cirq, PennyLane-Lightning, PennyLane-qiskit, pennylane-qulacs, PennyLane-SF
Platform info: Linux-5.4.209-116.367.amzn2.x86_64-x86_64-with-glibc2.31
Python version: 3.9.15
Numpy version: 1.23.5
Scipy version: 1.10.0
Installed devices:
- default.gaussian (PennyLane-0.28.0)
- default.mixed (PennyLane-0.28.0)
- default.qubit (PennyLane-0.28.0)
- default.qubit.autograd (PennyLane-0.28.0)
- default.qubit.jax (PennyLane-0.28.0)
- default.qubit.tf (PennyLane-0.28.0)
- default.qubit.torch (PennyLane-0.28.0)
- default.qutrit (PennyLane-0.28.0)
- null.qubit (PennyLane-0.28.0)
- cirq.mixedsimulator (PennyLane-Cirq-0.28.0)
- cirq.pasqal (PennyLane-Cirq-0.28.0)
- cirq.qsim (PennyLane-Cirq-0.28.0)
- cirq.qsimh (PennyLane-Cirq-0.28.0)
- cirq.simulator (PennyLane-Cirq-0.28.0)
- lightning.qubit (PennyLane-Lightning-0.28.2)
- strawberryfields.fock (PennyLane-SF-0.20.1)
- strawberryfields.gaussian (PennyLane-SF-0.20.1)
- strawberryfields.gbs (PennyLane-SF-0.20.1)
- strawberryfields.remote (PennyLane-SF-0.20.1)
- strawberryfields.tf (PennyLane-SF-0.20.1)
- qiskit.aer (PennyLane-qiskit-0.28.0)
- qiskit.basicaer (PennyLane-qiskit-0.28.0)
- qiskit.ibmq (PennyLane-qiskit-0.28.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.28.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.28.0)
- qulacs.simulator (pennylane-qulacs-0.28.0)
I set up a qBraid environment with the same versions of everything as Xanadu Cloud:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[1], line 21
18 qml.from_qasm(qasm)()
19 return qml.state()
---> 21 result = circuit_from_qasm(qasm)
22 result
File /opt/.qbraid/environments/qbraid_000000/pyenv/lib/python3.9/site-packages/pennylane/qnode.py:800, in QNode.__call__(self, *args, **kwargs)
797 set_shots(self._original_device, override_shots)(self._update_gradient_fn)()
799 # construct the tape
--> 800 self.construct(args, kwargs)
802 cache = self.execute_kwargs.get("cache", False)
803 using_custom_cache = (
804 hasattr(cache, "__getitem__")
805 and hasattr(cache, "__setitem__")
806 and hasattr(cache, "__delitem__")
807 )
File /opt/.qbraid/environments/qbraid_000000/pyenv/lib/python3.9/site-packages/pennylane/qnode.py:711, in QNode.construct(self, args, kwargs)
708 def construct(self, args, kwargs):
709 """Call the quantum function with a tape context, ensuring the operations get queued."""
--> 711 self._tape = make_qscript(self.func)(*args, **kwargs)
712 self._tape._queue_category = "_ops"
713 self._qfunc_output = self.tape._qfunc_output
File /opt/.qbraid/environments/qbraid_000000/pyenv/lib/python3.9/site-packages/pennylane/tape/qscript.py:1346, in make_qscript.<locals>.wrapper(*args, **kwargs)
1344 def wrapper(*args, **kwargs):
1345 with AnnotatedQueue() as q:
-> 1346 result = fn(*args, **kwargs)
1348 qscript = QuantumScript.from_queue(q)
1349 qscript._qfunc_output = result
Cell In[1], line 18, in circuit_from_qasm(qasm)
16 @qml.qnode(dev)
17 def circuit_from_qasm(qasm):
---> 18 qml.from_qasm(qasm)()
19 return qml.state()
File /opt/.qbraid/environments/qbraid_000000/pyenv/lib/python3.9/site-packages/pennylane/io.py:133, in from_qasm(quantum_circuit)
104 def from_qasm(quantum_circuit: str):
105 """Loads quantum circuits from a QASM string using the converter in the
106 PennyLane-Qiskit plugin.
107
(...)
131 function: the PennyLane template created based on the QASM string
132 """
--> 133 return load(quantum_circuit, format="qasm")
File /opt/.qbraid/environments/qbraid_000000/pyenv/lib/python3.9/site-packages/pennylane/io.py:68, in load(quantum_circuit_object, format)
65 # calls the load function of the converter on the quantum circuit object
66 return plugin_converter(quantum_circuit_object)
---> 68 raise ValueError(
69 "Converter does not exist. Make sure the required plugin is installed "
70 "and supports conversion."
71 )
ValueError: Converter does not exist. Make sure the required plugin is installed and supports conversion.
I just confirmed locally that this works on the latest PL and PL-Qiskit, so this is likely an installation issue. If things still don't seem to work right with the latest version of everything, check out the PennyLane forum to get the help you need there!
In what environment did you get it to work? I used the latest versions locally, in IBM Quantum Lab, and in qBraid. This issue should at least remain open as from_qasm() does not work in qBraid, which hosts both Qiskit and Pennylane.
just my local machine with the latest stuff installed into a pyenv. I'm not sure about the qBraid environment you're in (as it's unfamiliar to me), but it doesn't appear to have the pennylane-qiskit plugin present. In general, we don't provide guarantees for running on all other providers and their cloud environments, but perhaps the qBraid team will have insights into installing python packages on their cloud env?
Hi @bsiegelwax, thank you for opening this issue! It looks like qBraid is having some issues when you install packages. I'm not sure if this is something happening just for the free version (the one I'm using) or if it's for every version. You can try contacting qBraid directly to get support from them on this.
Given that qBraid is not working for you at the moment, my recommendation would be to use Google Colab. This is also free and it works a lot like Xanadu Cloud. You can even upload the notebooks that you downloaded from Xanadu Cloud. The downside with respect to Xanadu Cloud is that you need to install the packages you need at the beginning of every notebook, but it's really a minor thing.
So when you open a new notebook in Google Colab you should run the following at the beginning of your notebook:
!pip install pennylane pennylane-qiskit
Here you will be installing both PennyLane and the PennyLane-Qiskit plugin. If you needed other packages such as qiskit-aer you would need to install them in the same way.
After they're installed you will need to click on Runtime -> Restart runtime
You can now comment the line(s) with your installs and run it only when you need to start a new session in the notebook.
Now you're ready to add your code! I ran your code in Google Colab and it works well.
Alternatively, if you prefer not to use Google Colab, you can run your notebooks on your own computer (locally).
To install PennyLane locally itβs strongly recommended that you create a new virtual environment to prevent you from having any installation issues. You can create a virtual environment with Conda and install PennyLane as follows:
conda create --name <name_of_your_environment> python=3.10
conda activate <name_of_your_environment>
python -m pip install pennylane jupyter pennylane-qiskit
Note that you will be installing 3 packages here: PennyLane, Jupyter, and the PennyLane-Qiskit plugin. Also, note that where it says <name_of_your_environment>
you can choose any name that you want.
After this you can write jupyter notebook
in your terminal and you will be ready to create programs using PennyLane.
You can also watch the video here for detailed PennyLane installation instructions.
I will reopen this issue, and if you're able to successfully run your code on Google Colab or locally please let me know so I can close it.
We would love to know your thoughts about PennyLane and any installation issues you may have encountered. We have a new PennyLane survey and would appreciate if you could fill it in, it should only take you a couple of minutes.
I hope this helps and please feel free to post any questions you may have in our discussion forum!
Here's the solution: https://github.com/SchrodingersSangru/pennylane-qiskit
Hi @bsiegelwax, thank you for posting this here. While making your own changes in the code is ok, we cannot guarantee that everything will work as expected after you have modified the code. Did you get to try out the options I mentioned? And did they work for you?
@bsiegelwax small update. I think there may be a versioning issue in qBraid which is causing this. I've opened a bug report there so I'll let you know of their progress on this.
This is actually for a book, and the star of the section is QASM, hence my interest in from_qasm(). So, it needs to work with a simple pip install and an import. If I could direct people to Xanadu Cloud, that would be fine. In the absence of that, the link I shared hopefully works everywhere. But, sending readers to Colab with instructions is not viable. A qBraid solution would be fine, too, as long as it works with a pip install and an import.
Thank you for the context @bsiegelwax ! Is there a specific reason why you would prefer to avoid Google Colab? This is very valuable feedback for us.
Usually we would consider it the easiest solution since you just need to run !pip install pennylane pennylane-qiskit
at the beginning of the notebook in order to make it work. But if there's another constraint that we are not aware of we would love to hear it.
We also have a PennyLane survey where you can let us know your thoughts about PennyLane so that we can keep bringing you amazing features :star_struck:. We appreciate your feedback!
Brand new Colab notebook. The first code block is the pip install you provided. The second code block is the code that worked in Xanadu Cloud.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
[<ipython-input-3-c86081f772ee>](https://localhost:8080/#) in <cell line: 21>()
19 return qml.state()
20
---> 21 result = circuit_from_qasm(qasm)
22 result
5 frames
[/usr/local/lib/python3.10/dist-packages/pennylane/io.py](https://localhost:8080/#) in load(quantum_circuit_object, format)
65 return plugin_converter(quantum_circuit_object)
66
---> 67 raise ValueError(
68 "Converter does not exist. Make sure the required plugin is installed "
69 "and supports conversion."
ValueError: Converter does not exist. Make sure the required plugin is installed and supports conversion.
import pennylane as qml
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0],q[1];
"""
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def circuit_from_qasm(qasm):
qml.from_qasm(qasm)()
return qml.state()
result = circuit_from_qasm(qasm)
result
Hi @bsiegelwax, it could be the case that the Colab runtime needs to be restarted after the !pip install
cell is run, for Python to correctly recognise the newly installed packages.
Do you mind running the installation cell, restarting the runtime, and then importing pennylane and trying again?
The good news is that this works in Colab. But, I tried it locally and in qBraid and it doesn't work anywhere else. So, it's not an ideal solution, but at least Pennylane aficionados have an option.
Hi @bsiegelwax, It's great that you got it to work in Colab. I tested locally and it works for me. This is why I think there's a versioning or package issue in the environment you're currently using. Did you try creating a new virtual environment with Conda and Python 3.10?
The issue with qBraid is indeed non-ideal. I hope they work on the issue soon.
I have not tried a virtual environment because the solution has to be as universal as possible. I don't know if readers will be using Windows or Linux or Python 3.x or Python 3.y. So, the ideal solution would be a pip install and import in any Python 3 environment, and short of that at least a pip install and import in an environment where my code will work with near certainty. So, the Colab restart is still one step more than I would like there to be, but it outranks setting up a new environment.
I understand @bsiegelwax. Unfortunately environments are indeed important to increase the chances of everything working as expected. When people install everything in their base environment the likelihood of running into issues is higher. This is why whenever I do trainings and workshops I always share the instructions that I shared with you, so that everyone can run their code without issues. That being said, I totally agree with you that Google Colab is an easy option where all you need is pip install, restart and import.
I'm glad this will work for you for your book!
Expected behavior
I expected OpenQASM 2.0 to be imported as a circuit.
Actual behavior
My local environment returns
ImportError: cannot import name 'RunnerResult' from 'qiskit_ibm_runtime.constants'
. IBM Quantum Lab returnsValueError: Converter does not exist. Make sure the required plugin is installed and supports conversion.
. qBraid also returnsValueError: Converter does not exist. Make sure the required plugin is installed and supports conversion.
. Xanadu Cloud does not return an error, but the imported circuit is empty.Additional information
100% reproducibility
Source code
Tracebacks
System information
Existing GitHub issues