Hello!
I’m currently creating a QNode, cost
, with the JAX interface.
def get_observables(N):
observables = []
# Coupling operators
for i in range(N-1):
observables.append(qml.PauliZ(i) @ qml.PauliZ(i+1))
# Identity operator
for i in range(N):
observables.append(qml.Identity(i))
return observables
def get_coeffs(params, N):
coeffs = []
# Coupling coeffs
for i in range(N-1):
coeffs.append((params[0])**2/params[1])
# Constant coeffs
for i in range(N):
coeffs.append(params[1])
return coeffs
def create_Hamiltonian(params):
coeffs = get_coeffs(params, nqubits)
obs = get_observables(nqubits)
# H = qml.dot(coeffs, obs)
H = qml.Hamiltonian(coeffs, obs)
return H
def get_Sy(nqubits, a):
S_0 = nqubits/2
c = 0
for i in range(nqubits):
c += (1/(2*S_0))*qml.PauliY(wires=i)
return a * c
def create_params(L, scale = 0.1):
params = jnp.array([])
print(type(params))
for i in range(L):
J = scale*np.random.uniform()
O = 1.0
theta = scale*np.random.uniform()
# Convert the list to a NumPy array
param_array = jnp.array([J, O, theta])
params = jnp.append(params, param_array)
print(type(params))
return params
def U1(params):
start_index = 0
num_trotter_steps = 10
for i in range(L):
new_params = params[start_index:start_index + 3]
H = create_Hamiltonian(new_params[0:2])
qml.evolve(H, num_steps = num_trotter_steps)
for j in range(nqubits):
qml.RX(new_params[2], wires=j) # Change params to make sure that theta value changes for each L
start_index += 3 # Put state_index in again
dev = qml.device("default.qubit.jax", wires=nqubits, shots=None)
# @jax.jit
# @qml.qnode(dev, interface='jax')
def circuit(params, a, phi):
print(type(params))
print(type(a))
for i in range(nqubits): # Making the initial CSS
qml.Hadamard(wires=i)
U1(params)
for z in range(nqubits): # Perturbation
qml.RY(phi, wires = z)
qml.adjoint(U1)(params)
# expectation_values = [qml.expval(qml.PauliY(wires=i)) for i in range(nqubits)]
c = get_Sy(nqubits, a)
return qml.expval(c)
# @jax.jit
@qml.qnode(dev, interface='jax')
def cost(params, a, phi):
circuit_output = circuit(params, a, phi)
mse = np.mean((phi-circuit_output)**2)
return mse
I set the parameters and am trying to calculate the gradients via jax.value_and_grad
.
L = 1
params = create_params(L)
phi = jnp.array(0.001)
a = jnp.array(0.001)
val, grads = jax.value_and_grad(cost)(params, a, phi)
which gives me the error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/.../.ipynb
phi = jnp.array(0.001)
/.../.ipynb
a = jnp.array(0.001)
----> /.../.ipynb
val, grads = jax.value_and_grad(cost)(params, a, phi)
[... skipping hidden 8 frame]
File ~/.../, in QNode.__call__(self, *args, **kwargs)
967 kwargs[\"shots\"] = _get_device_shots(self._original_device)
969 # construct the tape
--> 970 self.construct(args, kwargs)
972 cache = self.execute_kwargs.get(\"cache\", False)
973 using_custom_cache = (
974 hasattr(cache, \"__getitem__\")
975 and hasattr(cache, \"__setitem__\")
976 and hasattr(cache, \"__delitem__\")
977 )
File ~/.../ in QNode.construct(self, args, kwargs)
853 self.interface = qml.math.get_interface(*args, *list(kwargs.values()))
855 with qml.queuing.AnnotatedQueue() as q:
--> 856 self._qfunc_output = self.func(*args, **kwargs)
858 self._tape = QuantumScript.from_queue(q, shots)
860 params = self.tape.get_parameters(trainable_only=False)
/.../.ipynb Cell 9 line 1
@qml.qnode(dev, interface='jax')
def cost(params, a, phi):
--> circuit_output = circuit(params, a, phi)
mse = np.mean((phi-circuit_output)**2)
return mse
/.../.ipynb Cell 9 line 1
qml.adjoint(U1)(params)
# expectation_values = [qml.expval(qml.PauliY(wires=i)) for i in range(nqubits)]
--> c = get_Sy(nqubits, a)
return qml.expval(c)
/.../.ipynb Cell 9 line 4
for i in range(nqubits):
c += (1/(2*S_0))*qml.PauliY(wires=i)
---> return a * c
TypeError: unsupported operand type(s) for *: 'ArrayImpl' and 'Hamiltonian'
This is what happens when I type qml.about()
.
Name: PennyLane
Version: 0.33.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /.../python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning
Platform info: macOS-13.4.1-x86_64-i386-64bit
Python version: 3.11.5
Numpy version: 1.26.2
Scipy version: 1.11.4
Installed devices:
- default.gaussian (PennyLane-0.33.1)
- default.mixed (PennyLane-0.33.1)
- default.qubit (PennyLane-0.33.1)
- default.qubit.autograd (PennyLane-0.33.1)
- default.qubit.jax (PennyLane-0.33.1)
- default.qubit.legacy (PennyLane-0.33.1)
- default.qubit.tf (PennyLane-0.33.1)
- default.qubit.torch (PennyLane-0.33.1)
- default.qutrit (PennyLane-0.33.1)
- null.qubit (PennyLane-0.33.1)
- lightning.qubit (PennyLane-Lightning-0.33.1)
I’m unsure what it is that is causing this error since I’m doing similar things with params
as I am with a
inside the circuit
QNode.