Hi,
I used the original example (HamLib: Max-3-SAT ( HamLib: Max-3-SAT )) but made a minor change, specifically replacing qml.BasisStatePreparation with qml.BasisState.
However, the example code doesn’t work.
# Put code here
import pennylane as qml
from pennylane import numpy as np
[ds] = qml.data.load("hamlib-max-3-sat")
ham = ds.hamiltonians[1]
dev = qml.device("default.qubit", wires=4)
@qml.qnode(dev)
def circuit(basis_state):
qml.BasisState(basis_state, wires=range(4))
return qml.expval(ham)
# clauses satisfied when all variables are false
circuit([0,0,0,0]) # output: array(7.)
# clauses satisfied when some variables are true
circuit([0,0,1,1]) # output: array(5.)
If you want help with diagnosing an error, please put the full error message below:
# Put full error message here
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[6], line 2
1 [ds] = qml.data.load("hamlib-max-3-sat")
----> 2 ham = ds.hamiltonians[1]
4 dev = qml.device("default.qubit", wires=4)
6 @qml.qnode(dev)
7 def circuit(basis_state):
File c:\Users\ABC\AppData\Local\Programs\Python\Python313\Lib\site-packages\pennylane\data\attributes\list.py:112, in DatasetList.__getitem__(self, index)
109 index = len(self) + index
111 if not 0 <= index < len(self):
--> 112 raise IndexError(index)
114 return self._mapper[str(index)].get_value()
IndexError: 1
And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().
Name: PennyLane
Version: 0.41.1
Summary: 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.
Home-page: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages
Requires: appdirs, autograd, autoray, cachetools, diastatic-malt, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, tomlkit, typing-extensions
Required-by: PennyLane-Catalyst, PennyLane-qiskit, PennyLane_Lightning
Platform info: macOS-26.1-arm64-arm-64bit
Python version: 3.12.8
Numpy version: 2.0.2
Scipy version: 1.13.0
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.41.0)
- nvidia.custatevec (PennyLane-Catalyst-0.9.0)
- nvidia.cutensornet (PennyLane-Catalyst-0.9.0)
- oqc.cloud (PennyLane-Catalyst-0.9.0)
- softwareq.qpp (PennyLane-Catalyst-0.9.0)
- default.clifford (PennyLane-0.41.1)
- default.gaussian (PennyLane-0.41.1)
- default.mixed (PennyLane-0.41.1)
- default.qubit (PennyLane-0.41.1)
- default.qutrit (PennyLane-0.41.1)
- default.qutrit.mixed (PennyLane-0.41.1)
- default.tensor (PennyLane-0.41.1)
- null.qubit (PennyLane-0.41.1)
- reference.qubit (PennyLane-0.41.1)
- qiskit.aer (PennyLane-qiskit-0.41.0.post0)
- qiskit.basicaer (PennyLane-qiskit-0.41.0.post0)
- qiskit.basicsim (PennyLane-qiskit-0.41.0.post0)
- qiskit.remote (PennyLane-qiskit-0.41.0.post0)