Skip to content

Solving a 1D ODE

In this tutorial we will show how to use Qadence to solve a basic 1D Ordinary Differential Equation (ODE) with a QNN using Differentiable Quantum Circuits (DQC) 1.

Consider the following non-linear ODE and boundary condition:

\[ \frac{df}{dx}= 5\times(4x^3+x^2-2x-\frac12), \qquad f(0)=0 \]

It admits an exact solution:

\[ f(x)=5\times(x^4+\frac13x^3-x^2-\frac12x) \]

Our goal will be to find this solution for \(x\in[-1, 1]\).

For the purpose of this tutorial, we will compute the derivative of the circuit using torch.autograd. The point of the DQC algorithm is to use differentiable circuits with parameter shift rules. In Qadence, PSR is implemented directly as custom overrides of the derivative function in the autograd engine, and thus we can later change the derivative method for the model itself if we wish.

Defining the loss function

The essential part of solving this problem is to define the right loss function to represent our goal. In this case, we want to define a model that has the capacity to learn the target solution, and we want to minimize: - The derivative of this model in comparison with the exact derivative in the equation; - The output of the model at the boundary in comparison with the value for the boundary condition;

We can write it like so:

Different loss criterions could be considered, and we could also play with the balance between the sum of the two loss terms. For now, let's proceed with the definition above.

Note that so far we have not used any quantum specific assumption, and we could in principle use the same loss function with a classical neural network.

Defining a QNN with Qadence

Now, we can finally use Qadence to write a QNN. We will use a feature map to encode the input values, a trainable ansatz circuit, and an observable to measure as the output.

We used a Chebyshev feature map with a tower-like scaling of the input reupload, and a standard hardware-efficient ansatz. You can check the qml constructors tutorial to see how you can customize these components. In the observable, for now we consider the simple case of measuring the magnetization of the first qubit.

from qadence.draw import display

# display(circuit)
%3 cluster_d3952d72c6a24494b01cda4caf06cd32 HEA cluster_687c4bc76ac745e68d2217fc866ffcfb Tower Chebyshev FM 81bad600e023493e9bc1e710c4787639 0 e505b53ed2b74eba98fc32f403635fb8 RX(1.0*acos(x)) 81bad600e023493e9bc1e710c4787639--e505b53ed2b74eba98fc32f403635fb8 a453694f1f4e45b38be028dcb70ddef3 1 d4fe2d5cc20243c7becb151eb54d2bdb RX(theta₀) e505b53ed2b74eba98fc32f403635fb8--d4fe2d5cc20243c7becb151eb54d2bdb 52995369390a40d89fe208870ae30194 RY(theta₃) d4fe2d5cc20243c7becb151eb54d2bdb--52995369390a40d89fe208870ae30194 9c8e6492662c4f2cb25ae08ead868c15 RX(theta₆) 52995369390a40d89fe208870ae30194--9c8e6492662c4f2cb25ae08ead868c15 b4d534d9d139477f9796b909f7ec07a3 9c8e6492662c4f2cb25ae08ead868c15--b4d534d9d139477f9796b909f7ec07a3 8e7dfeb93fa04055ac877bda49aa9826 b4d534d9d139477f9796b909f7ec07a3--8e7dfeb93fa04055ac877bda49aa9826 86cb481cc03c4515b25d58b61f76a571 RX(theta₉) 8e7dfeb93fa04055ac877bda49aa9826--86cb481cc03c4515b25d58b61f76a571 39ec27db6e7a4f319b59bfe7f4d77110 RY(theta₁₂) 86cb481cc03c4515b25d58b61f76a571--39ec27db6e7a4f319b59bfe7f4d77110 4d25532766d34686b1777a318a90edd3 RX(theta₁₅) 39ec27db6e7a4f319b59bfe7f4d77110--4d25532766d34686b1777a318a90edd3 58e8839ce09e4529b69c1c9d742d7a6f 4d25532766d34686b1777a318a90edd3--58e8839ce09e4529b69c1c9d742d7a6f a9b887ae07bc43b797d25f76a916a291 58e8839ce09e4529b69c1c9d742d7a6f--a9b887ae07bc43b797d25f76a916a291 0f2ca1e3af1a4a31b0aeed3b6d2cb53a RX(theta₁₈) a9b887ae07bc43b797d25f76a916a291--0f2ca1e3af1a4a31b0aeed3b6d2cb53a bfbe489f069445c7b069a74db5a0751a RY(theta₂₁) 0f2ca1e3af1a4a31b0aeed3b6d2cb53a--bfbe489f069445c7b069a74db5a0751a c6cf2c859a7c406086aa86a6887156bf RX(theta₂₄) bfbe489f069445c7b069a74db5a0751a--c6cf2c859a7c406086aa86a6887156bf 4ea93314350d40a6b3a00296a38ae291 c6cf2c859a7c406086aa86a6887156bf--4ea93314350d40a6b3a00296a38ae291 d809200a4d304dd29d17f0266338a7a9 4ea93314350d40a6b3a00296a38ae291--d809200a4d304dd29d17f0266338a7a9 cd46f6596a094b02bc5fafca380eecc6 d809200a4d304dd29d17f0266338a7a9--cd46f6596a094b02bc5fafca380eecc6 de7f4e7732004583b357c21be8293582 5d408fa0f78f4f0389bb4f4ff6de7df4 RX(2.0*acos(x)) a453694f1f4e45b38be028dcb70ddef3--5d408fa0f78f4f0389bb4f4ff6de7df4 4644ad97f8ec4324a05a25471d597c07 2 2b928fe07def4030beeb0f364e84ad15 RX(theta₁) 5d408fa0f78f4f0389bb4f4ff6de7df4--2b928fe07def4030beeb0f364e84ad15 df91987f29844127992ebee663459039 RY(theta₄) 2b928fe07def4030beeb0f364e84ad15--df91987f29844127992ebee663459039 19509b8907c648e69117e93ead747658 RX(theta₇) df91987f29844127992ebee663459039--19509b8907c648e69117e93ead747658 fce2f81c139e46db9efc60e7fbf54fe1 X 19509b8907c648e69117e93ead747658--fce2f81c139e46db9efc60e7fbf54fe1 fce2f81c139e46db9efc60e7fbf54fe1--b4d534d9d139477f9796b909f7ec07a3 36af7a35d0a740b1962be2463f9f0d76 fce2f81c139e46db9efc60e7fbf54fe1--36af7a35d0a740b1962be2463f9f0d76 4c17827faca54ebaa679e869609d7fe4 RX(theta₁₀) 36af7a35d0a740b1962be2463f9f0d76--4c17827faca54ebaa679e869609d7fe4 6ea66e8030b74439affb7b34113e387c RY(theta₁₃) 4c17827faca54ebaa679e869609d7fe4--6ea66e8030b74439affb7b34113e387c 6870e97566b8475db5449f3284f3de50 RX(theta₁₆) 6ea66e8030b74439affb7b34113e387c--6870e97566b8475db5449f3284f3de50 5f02b259712b4b40a76196a13a933fc6 X 6870e97566b8475db5449f3284f3de50--5f02b259712b4b40a76196a13a933fc6 5f02b259712b4b40a76196a13a933fc6--58e8839ce09e4529b69c1c9d742d7a6f c88f6dc5348545c291d7761cfaa7ce4d 5f02b259712b4b40a76196a13a933fc6--c88f6dc5348545c291d7761cfaa7ce4d d050639ff82241488e53cfd664a48cb3 RX(theta₁₉) c88f6dc5348545c291d7761cfaa7ce4d--d050639ff82241488e53cfd664a48cb3 06888dc817c446b8acf2dd1a102446be RY(theta₂₂) d050639ff82241488e53cfd664a48cb3--06888dc817c446b8acf2dd1a102446be b7195ec399554135bf6679ea1f8b734e RX(theta₂₅) 06888dc817c446b8acf2dd1a102446be--b7195ec399554135bf6679ea1f8b734e 77e4071b89b844699eadabcbac0f78f8 X b7195ec399554135bf6679ea1f8b734e--77e4071b89b844699eadabcbac0f78f8 77e4071b89b844699eadabcbac0f78f8--4ea93314350d40a6b3a00296a38ae291 955fc455ab3a42868a907ca91cde4036 77e4071b89b844699eadabcbac0f78f8--955fc455ab3a42868a907ca91cde4036 955fc455ab3a42868a907ca91cde4036--de7f4e7732004583b357c21be8293582 5bbc69077b64405eb3598544d68956d1 7691941f3b06406e8801b7c2912fef06 RX(3.0*acos(x)) 4644ad97f8ec4324a05a25471d597c07--7691941f3b06406e8801b7c2912fef06 2745a85f7b624a84b266cbb604221a31 RX(theta₂) 7691941f3b06406e8801b7c2912fef06--2745a85f7b624a84b266cbb604221a31 b5c4c507f3d446859b39bb6b522e2587 RY(theta₅) 2745a85f7b624a84b266cbb604221a31--b5c4c507f3d446859b39bb6b522e2587 e30def5c65504ff298e0f0db7fe3ae87 RX(theta₈) b5c4c507f3d446859b39bb6b522e2587--e30def5c65504ff298e0f0db7fe3ae87 125569a0a1974a4e83abda2b3eb12f13 e30def5c65504ff298e0f0db7fe3ae87--125569a0a1974a4e83abda2b3eb12f13 e0b85e3251d54f0d929e8a9f4757d4ef X 125569a0a1974a4e83abda2b3eb12f13--e0b85e3251d54f0d929e8a9f4757d4ef e0b85e3251d54f0d929e8a9f4757d4ef--36af7a35d0a740b1962be2463f9f0d76 14466c38eea946c1885cbcff2c259c5d RX(theta₁₁) e0b85e3251d54f0d929e8a9f4757d4ef--14466c38eea946c1885cbcff2c259c5d 9f663fa2e26f4b1eaf61b7ff9e644a6e RY(theta₁₄) 14466c38eea946c1885cbcff2c259c5d--9f663fa2e26f4b1eaf61b7ff9e644a6e e1439206c31a44bd9bc3c33b69407c32 RX(theta₁₇) 9f663fa2e26f4b1eaf61b7ff9e644a6e--e1439206c31a44bd9bc3c33b69407c32 228107ea9e7a47e4a569c2f6582cb950 e1439206c31a44bd9bc3c33b69407c32--228107ea9e7a47e4a569c2f6582cb950 bde02b0dbd6b47a49a1d6465020951f4 X 228107ea9e7a47e4a569c2f6582cb950--bde02b0dbd6b47a49a1d6465020951f4 bde02b0dbd6b47a49a1d6465020951f4--c88f6dc5348545c291d7761cfaa7ce4d 3bcd9d47e484493f9adc91fd7209b6fc RX(theta₂₀) bde02b0dbd6b47a49a1d6465020951f4--3bcd9d47e484493f9adc91fd7209b6fc dd4ab402297b407a910826282b7ca190 RY(theta₂₃) 3bcd9d47e484493f9adc91fd7209b6fc--dd4ab402297b407a910826282b7ca190 103081b9bc5c4a8bb8925ab0e31198f9 RX(theta₂₆) dd4ab402297b407a910826282b7ca190--103081b9bc5c4a8bb8925ab0e31198f9 2a22e2b2169240da987520c80b324cc7 103081b9bc5c4a8bb8925ab0e31198f9--2a22e2b2169240da987520c80b324cc7 7bfea4aec7774f3785be8f675689556a X 2a22e2b2169240da987520c80b324cc7--7bfea4aec7774f3785be8f675689556a 7bfea4aec7774f3785be8f675689556a--955fc455ab3a42868a907ca91cde4036 7bfea4aec7774f3785be8f675689556a--5bbc69077b64405eb3598544d68956d1

Training the model

Now that the model is defined we can proceed with the training. the QNN class can be used like any other torch.nn.Module. Here we write a simple training loop, but you can also look at the ml tools tutorial to use the convenience training functions that Qadence provides.

To train the model, we will select a random set of collocation points uniformly distributed within \(-1.0< x <1.0\) and compute the loss function for those points.

Note the values of \(x\) are only picked from \(x\in[-0.99, 0.99]\) since we are using a Chebyshev feature map, and derivative of \(\text{acos}(x)\) diverges at \(-1\) and \(1\).

Plotting the results

import matplotlib.pyplot as plt

def f_exact(x: torch.Tensor) -> torch.Tensor:
    return 5*(x**4 + (1/3)*x**3 - x**2 - 0.5*x)

x_test = torch.arange(xmin, xmax, step = 0.01).unsqueeze(1)

result_exact = f_exact(x_test).flatten()

result_model = model(x_test).flatten().detach()

plt.plot(x_test, result_exact, label = "Exact solution")
plt.plot(x_test, result_model, label = " Trained model")
2024-06-16T15:43:32.061327 image/svg+xml Matplotlib v3.7.5, https://matplotlib.org/

Clearly, the result is not optimal.

Improving the solution

One point to consider when defining the QNN is the possible output range, which is bounded by the spectrum of the chosen observable. For the magnetization of a single qubit, this means that the output is bounded between -1 and 1, which we can clearly see in the plot.

One option would be to define the observable as the total magnetization over all qubits, which would allow a range of -3 to 3.

And we again plot the result:

x_test = torch.arange(xmin, xmax, step = 0.01).unsqueeze(1)

result_exact = f_exact(x_test).flatten()

result_model = model(x_test).flatten().detach()

plt.plot(x_test, result_exact, label = "Exact solution")
plt.plot(x_test, result_model, label = "Trained model")
2024-06-16T15:43:39.373091 image/svg+xml Matplotlib v3.7.5, https://matplotlib.org/

References