QLSTM circuit configuration

Hello! I’ve put together a QLSTM model per the following paper: https://arxiv.org/pdf/2310.17032.pdf. If possible, could you provide feedback on the circuit configuration for the purpose of time series forecasting?

The model is converging and returning good MSE scores but the final post-processed results seem to be stuck within a limited range.

# Define the angle translation function
def get_angles(x):
    # Split the data into chunks
    chunk1 = x[0] * 0.1 / 2
    chunk2 = x[1] * 0.1 / 2
    chunk3 = x[2] * 0.1 / 2
    chunk4 = x[3] * 0.1 / 2
    chunk5 = x[4] * 0.1 / 2
    
    angle1 = np.sin(chunk1)
    angle2 = np.sin(chunk2)
    angle3 = np.sin(chunk3)
    angle4 = np.sin(chunk4)
    angle5 = np.sin(chunk5)
    
    return angle1, angle2, angle3, angle4, angle5

# Define the device
dev = qml.device('default.qubit', wires=4)

# Define the state preparation function.
def state_preparation(a):
    qml.Hadamard(wires=0)
    qml.RY(a[0], wires=0)
    qml.RZ(a[0], wires=0)
    
    qml.Hadamard(wires=1)
    qml.RY(a[1], wires=1)
    qml.RZ(a[1], wires=1)
    
    qml.Hadamard(wires=2)
    qml.RY(a[2], wires=2)
    qml.RZ(a[2], wires=2)
    
    qml.Hadamard(wires=3)
    qml.RY(a[3], wires=3)
    qml.RZ(a[3], wires=3)

# Define the variational layer
@qml.qnode(dev)
def layer(layer_weights):
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[1, 2])
    qml.CNOT(wires=[2, 3])
    
    qml.CNOT(wires=[3, 0])
    qml.CNOT(wires=[0, 2])
    qml.CNOT(wires=[1, 3])
    
    qml.CNOT(wires=[2, 0])
    qml.CNOT(wires=[3, 1])
    
    for i in range(4):
        qml.RX(layer_weights[i], i)
        qml.RY(layer_weights[i], i)
        qml.RZ(layer_weights[i], i)
    
    return [qml.expval(qml.PauliY(0))]

# Define the variational quantum circuit with the state preparation routine
# and the layer structure.
@qml.qnode(dev)
def circuit(weights, x):
    state_preparation(x)
    
    for layer_weights in weights:
        layer(layer_weights)
        
    return qml.expval(qml.PauliZ(0))

# Define the full QLSTM model as the output of the quantum circuit
# plus the trainable bias
def QLSTM(weights, bias, x):
    return circuit(weights, x) + bias

# Define the square loss (MSE) function
def square_loss(labels, predictions):
    # Use a call to qml.math.stack to allow subtracting the arrays directly
    return np.mean((labels - qml.math.stack(predictions)) ** 2)

# Define the cost function
def cost(weights, bias, X, Y):
    predictions = [QLSTM(weights, bias, x) for x in X]
    return square_loss(Y, predictions)

The model takes in a sequence features data i.e. 3, 4, 5, 6, 7 and is expected to return a prediction i.e, 8 after processing the expectation values.

Any guidance is greatly appreciated.

Hi @camaya714, welcome to the Forum!

I don’t know the answer to your question but you can try writing to the authors of the paper and asking for feedback directly from them. They’re the people who will know it better!

You can also look here and here which are repositories of some external projects doing QLSTM with PennyLane. You can compare their code to yours to see what they’re doing differently. Please note that these projects are from several years ago so the code might need to be updated. You can learn more about the original project here.

I hope this helps you!