I got an error "ModuleNotFoundError: No module named 'pyquil.api._base_connection'. "How do I fix that problem?

Hello! If applicable, put your complete code example down below. Make sure that your code:

  • is 100% self-contained — someone can copy-paste exactly what is here and run it to
    reproduce the behaviour you are observing
  • includes comments
# Put code here
!pip install pennylane
!pip install pennylane-ionq
!pip install pennylane-forest
import pennylane as qml
from pennylane import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

data = load_iris()
X, y = data.data, data.target

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.3, random_state=42)

num_qubits = 4 

def circuit(params, x):
    qml.templates.AngleEmbedding(x, wires=range(num_qubits))
    qml.templates.BasicEntanglerLayers(params, wires=range(num_qubits))
    return [qml.expval(qml.PauliZ(i)) for i in range(num_qubits)]

def cost(params, X, y):
    loss = 0
    for i in range(len(X)):
        loss += np.sum((circuit(params[i], X[i]) - y[i]) ** 2)
    return loss / len(X)

init_params = np.random.rand(len(X_train), num_qubits, 4)  

#  IonQ Simulator
dev_ionq = qml.device("ionq.qpu", wires=num_qubits)

#  Rigetti Simulator
dev_rigetti = qml.device("forest.qvm", device='2q-qvm', wires=num_qubits)

opt = qml.GradientDescentOptimizer(stepsize=0.4)

steps = 100
params_ionq = init_params
params_rigetti = init_params
loss_values_ionq = []  
loss_values_rigetti = []  

for i in range(steps):
    # IonQ Simulator
    params_ionq = opt.step(lambda p: cost(p, X_train, y_train), params_ionq)
    loss_ionq = cost(params_ionq, X_train, y_train)

    # Rigetti Simulator
    params_rigetti = opt.step(lambda p: cost(p, X_train, y_train), params_rigetti)
    loss_rigetti = cost(params_rigetti, X_train, y_train)

    print(f"Step{i + 1}, Loss IonQ: {loss_ionq},")
print(f"Step {i + 1}, Loss IonQ: {loss_ionq}, Loss Rigetti: {loss_rigetti}")
print("IonQ:", params_ionq)
print(" Rigetti:", params_rigetti)

If you want help with diagnosing an error, please put the full error message below:

Put full error message here

--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[13], line 44 41 dev_ionq = qml.device(“ionq.qpu”, wires=num_qubits) 43 # Sử dụng Rigetti Simulator —> 44 dev_rigetti = qml.device(“forest.qvm”, device=‘2q-qvm’, wires=num_qubits) 47 opt = qml.GradientDescentOptimizer(stepsize=0.4) 49 steps = 100 File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pennylane_init_.py:328, in device(name, *args, **kwargs) 325 options.update(kwargs) 327 # loads the device class → 328 plugin_device_class = plugin_devices[name].load() 330 if Version(version()) not in SimpleSpec(plugin_device_class.pennylane_requires): 331 raise DeviceError( 332 f"The {name} plugin requires PennyLane versions {plugin_device_class.pennylane_requires}, " 333 f"however PennyLane version {version} is installed." 334 ) File [C:\Program](file:///C:/Program) Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\site-packages\pkg_resources_init_.py:2471, in EntryPoint.load(self, require, *args, **kwargs) 2469 if require: 2470 self.require(*args, **kwargs) → 2471 return self.resolve() File [C:\Program](file:///C:/Program) Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\site-packages\pkg_resources_init_.py:2477, in EntryPoint.resolve(self)

—> 38 from pyquil.api._base_connection import ForestConnection 39 from pyquil.api._config import PyquilConfig 41 from pyquil.quil import DefGate ModuleNotFoundError: No module named ‘pyquil.api._base_connection’

And, finally, make sure to include the versions of your packages. Specifically, show us the output of `qml.about()`. 
#Name: PennyLane
Version: 0.31.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
License: Apache License 2.0
Location: C:\Users\hoann\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml
Required-by: PennyLane-Forest, PennyLane-IonQ, PennyLane-Lightning, PennyLane-qiskit, PennyLane-Rigetti

Platform info:           Windows-10-10.0.19045-SP0
Python version:          3.11.4
Numpy version:           1.23.5
Scipy version:           1.10.0
Installed devices:
- default.gaussian (PennyLane-0.31.0)
- default.mixed (PennyLane-0.31.0)
- default.qubit (PennyLane-0.31.0)
- default.qubit.autograd (PennyLane-0.31.0)
- default.qubit.jax (PennyLane-0.31.0)
- default.qubit.tf (PennyLane-0.31.0)
- default.qubit.torch (PennyLane-0.31.0)
- default.qutrit (PennyLane-0.31.0)
- null.qubit (PennyLane-0.31.0)
- forest.numpy_wavefunction (PennyLane-Forest-0.6.0)
- forest.qpu (PennyLane-Forest-0.6.0)
- forest.qvm (PennyLane-Forest-0.6.0)
- forest.wavefunction (PennyLane-Forest-0.6.0)

Hello @hoannguyen! Welcome to the forum! :slight_smile:

Would mind sending me a minimal version of the code so I can reproduce the error?

Yes, of course.

Vào 4:26, Th 5, 3 thg 8, 2023 ludmilaaasb via Xanadu Discussion Forum <notifications@pennylane.discoursemail.com> đã viết:

I got an error while using rigetti Simulator. I have already installed the lastest version of pennylane-forest)and checked It exist. There is my code “dev_rigetti=qml.device(“forest.qvm”,device=‘2q-qvm’,wires=num_qubits, noise=True)”

Vào 8:04, Th 5, 3 thg 8, 2023 Nguyễn Ngọc, Hoàn <19521524@gm.uit.edu.vn> đã viết:


So, first thing, I would like to suggest this video. It is going to help you to send the exact amount of information we need to better evaluate the problems in your code and answer your questions! :slight_smile:

Unfortunately, this is not sufficient to understand what is going on. I would like to ask you again to send a minimal, self-contained version of your code and the error/output you getting.

However, if you are facing some doubts about the PennyLane-Rigetti Plugin, I strongly suggest checking the documentation page. It has the installation instructions and more details about the qvm device.

Note that, according to the documentation, the name of the device is rigetti.qvm.

Does it help? :slight_smile:

1 Like

Thank you. After your suggestion, I read the document and fixed that by downloading forest SDK and and changing the device forest.qvm to rigetti.qvm.

Vào 9:54, Th 5, 3 thg 8, 2023 ludmilaaasb via Xanadu Discussion Forum <notifications@pennylane.discoursemail.com> đã viết:

1 Like