Hello, I’m trying to calculate inner products of quantum states directly from
qml.state() and build kernel matrix. My optimizer is
And I got
TypeError: must be real number, not ArrayBox when assign quantum state vectors to an numpy array element wise. It seems like the returned state is
ArrayBox type and cannot be assigned to a numpy array element.
Is there any way to solve this?
I also tried to use
ArrayBox._value to assign. However in this way the weights just stayed the same and didn’t update.
Below are my codes:
import pennylane as qml from pennylane import numpy as np n_qubits = 5 n_layers = 2 dev = qml.device("default.qubit", wires=n_qubits) from pennylane.templates import QAOAEmbedding @qml.qnode(dev, diff_method='backprop') def circuit(weights, feature): QAOAEmbedding(features=feature, weights=weights, wires=range(n_qubits)) return qml.state() from pennylane.init import qaoa_embedding_uniform weights = qaoa_embedding_uniform(n_layers,n_qubits) #print(weights) N = 10 d = 4 X = np.random.uniform(size=(N,d)) #print(X) # map x to quantum state |x> def encoder(weights, X): M = X.shape features = np.empty((M,2**n_qubits),dtype='complex_') for i in range(M): # failed here features[i] = circuit(weights,X[i]) # features[i] = circuit(weights,X[i])._value return features # kernel matrix def matrix(features): # <x_i|x_j> t1 = features @ features.conj().T # |<x_i|x_j>|^2 t2 = np.multiply(t1,t1.conj()) return np.absolute(t2) def cost_naive(weights,X=None): features = encoder(weights,X) K = matrix(features) return K.sum() optim = qml.AdamOptimizer() for i in range(10): weights, loss = optim.step_and_cost(cost_naive,weights,X=X) print(weights) print(loss)