Hey @i_am_confusion !
ChatGPT
My apologies for the delayed response. Regarding your question, you’re missing the qnode
method before your ansatz circuit; otherwise, an error will occur. This method is crucial because qnode
connects the values that will be optimized, such as the expected value. Without this method, it won’t calculate correctly using something like expval(Z[0])
, as the expected numerical value won’t be obtained. Therefore, your code should be structured as follows.
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def ansatzz(parameters):
qml.RX(parameters[0], wires=0)
qml.RY(parameters[1], wires=1)
qml.CNOT(wires=[0,1])
qml.CNOT(wires=[1,2])
qml.CNOT(wires=[2,0])
qml.RY(parameters[2], wires=0)
qml.RY(parameters[3], wires=1)
qml.RY(parameters[4], wires=2)
return qml.expval(qml.PauliZ(0))
def cost(parameters):
return ansatzz(parameters)
To verify that it works, I used the same tutorial I sent you in my first response. The following code demonstrates how to do it:
from pennylane import AdamOptimizer
n_steps = 200
theta = np.random.rand(5, 1, requires_grad=True)
costs_list = []
opt = AdamOptimizer()
for i in range(1, n_steps+1):
if i%100==0: print("Running... Current step: ", i)
theta = opt.step(cost, theta)
costs_list.append(cost(theta))
tell me if is working in your side, and I’m using the pennylane versión’s 0.38.1
qml.about()
Name: PennyLane
Version: 0.38.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: GitHub - 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.
Author:
Author-email:
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning
Platform info: Linux-6.1.85±x86_64-with-glibc2.35
Python version: 3.10.12
Numpy version: 1.26.4
Scipy version: 1.13.1
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.38.0)
- default.clifford (PennyLane-0.38.1)
- default.gaussian (PennyLane-0.38.1)
- default.mixed (PennyLane-0.38.1)
- default.qubit (PennyLane-0.38.1)
- default.qubit.autograd (PennyLane-0.38.1)
- default.qubit.jax (PennyLane-0.38.1)
- default.qubit.legacy (PennyLane-0.38.1)
- default.qubit.tf (PennyLane-0.38.1)
- default.qubit.torch (PennyLane-0.38.1)
- default.qutrit (PennyLane-0.38.1)
- default.qutrit.mixed (PennyLane-0.38.1)
- default.tensor (PennyLane-0.38.1)
- null.qubit (PennyLane-0.38.1)
I hope is this a solution for you, if not let me know how to solve it