from pennylane import numpy as np
dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def circuit(params, data):
qml.AngleEmbedding(data, wires=[0, 1, 2])
qml.StronglyEntanglingLayers(params, wires=[0, 1, 2])
return qml.expval(qml.PauliZ(2))
data = np.random.random([3], requires_grad=False)
params = np.random.random(qml.StronglyEntanglingLayers.shape(3, 3), requires_grad=True)
def cost(params, single_sample):
return (1 - circuit(params, single_sample)) ** 2
opt = qml.QNSPSAOptimizer()
for it in range(10):
cost_fn = lambda p: cost(p, data)
metric_fn = lambda p: qml.metric_tensor(circuit, approx="block-diag")(p, data)
params, loss = opt.step_and_cost(cost_fn, params, metric_tensor_fn=metric_fn)
print(f"Epoch: {it} | Loss: {loss} |")
the code works with all other optimizers. but i need to use spsa and qpsa. it gives ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[25], line 24
21 cost_fn = lambda p: cost(p, data)
22 metric_fn = lambda p: qml.metric_tensor(circuit, approx=“block-diag”)(p, data)
—> 24 params, loss = opt.step_and_cost(cost_fn, params, metric_tensor_fn=metric_fn)
26 print(f"Epoch: {it} | Loss: {loss} |")
File ~/.conda/envs/cent7/2020.11-py38/xyz/lib/python3.8/site-packages/pennylane/optimize/qnspsa.py:184, in QNSPSAOptimizer.step_and_cost(self, cost, *args, **kwargs)
171 def step_and_cost(self, cost, *args, **kwargs):
172 r""“Update trainable parameters with one step of the optimizer and return
173 the corresponding objective function value after the step.
174
(…)
182 function output prior to the step
183 “””
→ 184 params_next = self._step_core(cost, args, kwargs)
186 if not self.blocking:
187 loss_curr = cost(*args, **kwargs)
File ~/.conda/envs/cent7/2020.11-py38/xyz/lib/python3.8/site-packages/pennylane/optimize/qnspsa.py:212, in QNSPSAOptimizer._step_core(self, cost, args, kwargs)
209 all_tensor_dirs =
210 for _ in range(self.resamplings):
211 # grad_tapes contains 2 tapes for the gradient estimation
→ 212 grad_tapes, grad_dirs = self._get_spsa_grad_tapes(cost, args, kwargs)
213 # metric_tapes contains 4 tapes for tensor estimation
214 metric_tapes, tensor_dirs = self._get_tensor_tapes(cost, args, kwargs)
File ~/.conda/envs/cent7/2020.11-py38/xyz/lib/python3.8/site-packages/pennylane/optimize/qnspsa.py:355, in QNSPSAOptimizer._get_spsa_grad_tapes(self, cost, args, kwargs)
352 args_plus[index] = arg + self.finite_diff_step * direction
353 args_minus[index] = arg - self.finite_diff_step * direction
→ 355 cost.construct(args_plus, kwargs)
356 tape_plus = cost.tape.copy(copy_operations=True)
357 cost.construct(args_minus, kwargs)
AttributeError: ‘function’ object has no attribute ‘construct’. I used regularization also. But did not work. Can you please look into it.
and output of qml.about()
Name: PennyLane
Version: 0.28.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for differentiable programming of quantum computers. Train a quantum computer the same way as a neural network.
Author:
Author-email:
License: Apache License 2.0
Location: /home/bhatia87/.conda/envs/cent7/2020.11-py38/xyz/lib/python3.8/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning
Platform info: Linux-3.10.0-1160.108.1.el7.x86_64-x86_64-with-glibc2.10
Python version: 3.8.5
Numpy version: 1.21.0
Scipy version: 1.9.3
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)
- lightning.qubit (PennyLane-Lightning-0.30.0)