Help adapting the VQT tutorial to 2 qubits instead of 4 qubits

I’m trying to adapt the VQT tutorial code to 2 qubits. I changed the number of qubits, obviously, and the circuit. However, my cost function is returning two values. How to fix this? Is my circuit inadequate? How to change the coupling? Should I change another part besides the circuit?

Here is the circuit:

ev = qml.device("lightning.qubit", wires=nr_qubits)

def quantum_circuit(rotation_params, coupling_params, sample=None):
    qml.BasisStatePreparation(sample, wires=range(nr_qubits))

    for i in range(len(rotation_params)):
        qml.RX(rotation_params[i][0], wires=0)
        qml.RX(rotation_params[i][1], wires=1)
        qml.CRX(coupling_params[i][0], wires=[0, 1])
    return qml.expval(qml.Hermitian(ham_matrix, wires=range(nr_qubits)))

qnode = qml.QNode(quantum_circuit, dev, interface="autograd")
rotation_params = [[[1, 1], [1, 1], [1, 1]] for i in range(0, depth)]
coupling_params = [[1] for i in range(0, depth)]
print(qml.draw(qnode, expansion_strategy="device", show_matrices=True)(rotation_params, coupling_params, sample=[1, 0]))
1 Like

Hi @Ana_Clara_Neves , thanks for the question :slight_smile: If you do a print below the for, you will see that rotation_params[i][0] takes two values, while qml.RX only needs one. What PennyLane understands in this case is that you want to run two different circuits, one with the first parameter and one for the second. That is why it is returning two expected values.
Let me know if you have any problems :muscle:

1 Like