Hi. How can I change data into the format, in which the sine.txt
is formed? https://pennylane.ai/qml/demos/quantum_neural_net.html
Best,
Risto
Hi. How can I change data into the format, in which the sine.txt
is formed? https://pennylane.ai/qml/demos/quantum_neural_net.html
Best,
Risto
Hey @_risto,
Have you tried numpy’s savetxt
function? You might have to specify the delimiter (as far as I can see sine.txt
has a whitespace as delimiter).
Otherwise, you can hand-code any format using the standard python write
functionality.
Hi @_risto,
It’s possible to use any smooth function here, doesn’t have to be sine. However, different function types will show different behaviours when using a quantum model—and quantum models might require different amounts of resources (gates, layers, depth, etc) to fit different classes of functions.
It just happens that a sinusoid is a relatively simple example for a quantum model to fit (due to the underlying complex-number structure of quantum computing), so it is suitable as a toy example.
This is a very Interesting question.
The expressivity of quantum variational circuit has been explained in,
Quantum models as Fourier series — PennyLane
According to this article, the number of qubits and data-encoding layers determine the expressivity.
On the other hand, the number of variational layers would not affect.
Particular, in a single qubit circuit, altough we increase the number of variational layers, the expressivity can’t grow.
To confirm it, I wrote a sample code.
#!pip install pennylane==0.16.0
#!pip install PennyLane-Lightning
import pennylane as qml
from pennylane import numpy as np
from matplotlib import pyplot as plt
num_of_data = 64
X = np.random.uniform(high=2 * np.pi, size=(num_of_data,1))
Y = np.sin(X[:,0]) # Learn sin (x)
######## parameters #############
n_qubits = 2 ## num. of qubit
n_layers = 2 # num of q_layers
dev = qml.device("lightning.qubit", wires=n_qubits, shots=None) # define a device
# Note: lightning.qubits is faster but "pip install PennyLane-Lightning" is required.
# Initial circuit parameters
var_init = np.random.uniform(high=2 * np.pi, size=(n_layers, n_qubits, 3))
# Definition of a device
@qml.qnode(dev, diff_method='adjoint')
# Data encoding and variational ansatz
def quantum_neural_net(var, x):
qml.templates.AngleEmbedding(x, wires=range(n_qubits))
qml.templates.StronglyEntanglingLayers(var, wires=range(n_qubits))
return qml.expval(qml.PauliZ(0))
def square_loss(desired, predictions):
loss = 0
for l, p in zip(desired, predictions):
loss = loss + (l - p) ** 2
loss = loss / len(desired)
return loss
def cost(var, features, desired):
preds = [quantum_neural_net(var, x) for x in features]
return square_loss(desired, preds)
opt = qml.AdamOptimizer(0.05)
import time
hist_cost = []
var = var_init
for it in range(50):
t1 = time.time()
var, _cost = opt.step_and_cost(lambda v: cost(v, X, Y), var)
t2 = time.time()
elapsed_time = t2-t1
print("Iter:"+str(it)+", cost="+str(_cost.numpy()))
print(f"Time:{elapsed_time}")
hist_cost.append(_cost)
plt.plot(10*np.log10(hist_cost),'o-')
Y_pred = [quantum_neural_net(var, x) for x in X]
plt.plot(X[:,0],Y_pred,'o')
This circuit has a data-encoding layer and multiple variational layers.
It can learn sin(x).
However, it can’t learn sin(2x)!
Even if we increase the number of variational layers, sin(2x) can’t be fitted.
This is because the expressivity depends on the number of data-encoding layers, rather than variational layers.
It is effective to increase the number of data-encoding layers, namely, “data re-uploading” technique.
Circuit models should be carefully chosen to learn a target function.
Hi @Kuma-quant
Thank you for your input. What does that mean in practical approach, is it possible to use non-sine and if so, how?
Yes, you can fit a non-sine function if the variational circuit has sufficient expressivity.
The reason why we have focused to a sine function is that, various functions can be approximated as sum of sine functions with diffrent frequencies, known as “Fourier series expansion”.
Fourier series - Wikipedia
Suppose the maximum frequency of a target (non-sine) function is r, in terms of fourier series.
When a variational circuit can approximate sin(rx), we can expect that the function can be fitted.
This is just my opinion and may be wrong, because I’m not expert.