Hi, looks like PennyLane ver 0.35 improved memory usage for the feed-forward circuits but it broke optimization for qml.SPSAOptimizer()
Attached demonstrator runs in ver 0.34
core@3fa108d51d4a:~/issues$ pip3 list |grep Pen
PennyLane 0.34.0
PennyLane-Cirq 0.34.0
PennyLane-Lightning 0.34.0
PennyLane-Lightning-GPU 0.34.0
PennyLane-qiskit 0.34.0
PennyLane-SF 0.29.0
I see a reasonable output - the code is crunching the data
0: ──RX(0.01)──RY(0.01)──┤↗│ │0⟩──X──┤↗│ │0⟩──X─┤ <Z>
╚════════╝ ║ ║
╚════════╝
run circ with 2 resets ...
input X=[0.011 0.012] Y=1.00 shots=1000 elaT=0.0 sec nReset=2
Optimization loop ...
Step 1: Cost = 1.6848 elaT=0.0 sec
Step 2: Cost = 1.6848 elaT=0.0 sec
Step 3: Cost = 1.6900 elaT=0.0 sec
But when I switch to
core@1f35d1998d35:~/issues$ pip3 list |grep Pen
PennyLane 0.35.1
PennyLane-Cirq 0.34.0
PennyLane_Lightning 0.35.1
PennyLane_Lightning_GPU 0.35.1
PennyLane-qiskit 0.35.1
PennyLane-SF 0.29.0
I only pass testing that circuit can be executed and I get this crash in the params = optimizer.step(..)
Optimization loop ...
Traceback (most recent call last):
File "/PennyLane/issues/./reset_mutip_SPSAOpt.py", line 56, in <module>
params = optimizer.step(lambda p: cost(p, target_value), params)
File "/usr/local/lib/python3.10/dist-packages/pennylane/optimize/spsa.py", line 219, in step
g = self.compute_grad(objective_fn, args, kwargs)
….
File "/usr/lib/python3.10/functools.py", line 889, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/usr/local/lib/python3.10/dist-packages/pennylane/devices/qubit/apply_operation.py", line 268, in apply_mid_measure
sample = np.random.binomial(1, probs[1])
File "numpy/random/mtrand.pyx", line 3486, in numpy.random.mtrand.RandomState.binomial
File "_common.pyx", line 430, in numpy.random._common.check_constraint
ValueError: p < 0, p > 1 or p is NaN
This is complete demonstrator
BTW, if only 1 reset is used the optimizer still works in 0.35
import pennylane as qml
from pennylane import numpy as np
num_qubit=1 ; shots=1000
dev = qml.device('default.qubit', wires=num_qubit,shots=shots)
from time import time
nReset=2
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=0)
for j in range(nReset):
m = qml.measure(0,reset=True)
qml.cond(m, qml.PauliX)(0)
return qml.expval(qml.PauliZ(0) )
# Initialize parameters
params = np.array([0.011, 0.012], requires_grad=True)
print(qml.draw(circuit, decimals=2)(params), '\n')
T0=time()
print(' run circ with %d resets ...'%nReset)
y = circuit(params)
elaT=time()-T0
print('input X=%s Y=%.2f shots=%d elaT=%.1f sec nReset=%d'%(params,y,shots,elaT, nReset))
#.... training code
def cost(params, target_value):
global function_calls
return (circuit(params) - target_value) ** 2
num_steps=3
optimizer=qml.SPSAOptimizer(maxiter=num_steps)
# Set the target expectation value
target_value = -0.3
T0=time()
print('\n Optimization loop ...')
for i in range(num_steps):
params = optimizer.step(lambda p: cost(p, target_value), params)
prev_cost = cost(params, target_value)
elaT=time()-T0
print("Step %d: Cost = %.4f elaT=%.1f sec" % (i+1, float(prev_cost),elaT))
Please advice how to make it work for nReset=30
Thanks, Jan