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_ea1bca6bd7b244538155924d185430e6 HEA cluster_761914c07adc400f97483b4a641dae9c Tower Chebyshev FM 9f82efcbae954bdb8002e118d91d8873 0 d7a1ef5c61dd40daa20121d6fe208746 RX(1.0*acos(x)) 9f82efcbae954bdb8002e118d91d8873--d7a1ef5c61dd40daa20121d6fe208746 164f7533298449b6904a1be74c26e5cf 1 b5547e44d2d04256899000b6565f5bad RX(theta₀) d7a1ef5c61dd40daa20121d6fe208746--b5547e44d2d04256899000b6565f5bad d18a15a8e1a14f989db3bb816725beab RY(theta₃) b5547e44d2d04256899000b6565f5bad--d18a15a8e1a14f989db3bb816725beab ef0a6e90dde142a9a8f4e529f1bd659b RX(theta₆) d18a15a8e1a14f989db3bb816725beab--ef0a6e90dde142a9a8f4e529f1bd659b 3fdd7d64134c4fb98a6de9728e6b2c23 ef0a6e90dde142a9a8f4e529f1bd659b--3fdd7d64134c4fb98a6de9728e6b2c23 f6a90569c2d043c09c649074f050f79f 3fdd7d64134c4fb98a6de9728e6b2c23--f6a90569c2d043c09c649074f050f79f 5284cff287f14539878e228ea734dc41 RX(theta₉) f6a90569c2d043c09c649074f050f79f--5284cff287f14539878e228ea734dc41 2909e72d4ea442c285b5fd32de990bd0 RY(theta₁₂) 5284cff287f14539878e228ea734dc41--2909e72d4ea442c285b5fd32de990bd0 c4fc8cd9db6f40898ae02dd9b39e169f RX(theta₁₅) 2909e72d4ea442c285b5fd32de990bd0--c4fc8cd9db6f40898ae02dd9b39e169f df931ef2c31749d8b2dd0b0b071f0c8a c4fc8cd9db6f40898ae02dd9b39e169f--df931ef2c31749d8b2dd0b0b071f0c8a 8512566724894fb4988b51123c1e57e0 df931ef2c31749d8b2dd0b0b071f0c8a--8512566724894fb4988b51123c1e57e0 6fd3fa1ae5ca4bafb48a5101d59e1050 RX(theta₁₈) 8512566724894fb4988b51123c1e57e0--6fd3fa1ae5ca4bafb48a5101d59e1050 038dfeb975954f98aab011dbbc20be99 RY(theta₂₁) 6fd3fa1ae5ca4bafb48a5101d59e1050--038dfeb975954f98aab011dbbc20be99 30b6ace8701f4b12b3153c8a2598214e RX(theta₂₄) 038dfeb975954f98aab011dbbc20be99--30b6ace8701f4b12b3153c8a2598214e 5fb47f28eb2a488287ea50a99c43bd1d 30b6ace8701f4b12b3153c8a2598214e--5fb47f28eb2a488287ea50a99c43bd1d 7f095669ff294a3bab89f3855db978ab 5fb47f28eb2a488287ea50a99c43bd1d--7f095669ff294a3bab89f3855db978ab d95b2a8195624e61ae2713105d026185 7f095669ff294a3bab89f3855db978ab--d95b2a8195624e61ae2713105d026185 6f897e38d47b48b1a884c2b5049e66b1 30fba70303f24452829c63aec6d748a6 RX(2.0*acos(x)) 164f7533298449b6904a1be74c26e5cf--30fba70303f24452829c63aec6d748a6 44084ea8384d4e8fae41003a94210599 2 47be8916a055446b96484ba5ed5428a0 RX(theta₁) 30fba70303f24452829c63aec6d748a6--47be8916a055446b96484ba5ed5428a0 9e5c5cb263d740d2a924ebecbc6e8be7 RY(theta₄) 47be8916a055446b96484ba5ed5428a0--9e5c5cb263d740d2a924ebecbc6e8be7 2a1c0d542638478280a9938afeba4892 RX(theta₇) 9e5c5cb263d740d2a924ebecbc6e8be7--2a1c0d542638478280a9938afeba4892 e1577dd860ca40c9b4e39ac4923af480 X 2a1c0d542638478280a9938afeba4892--e1577dd860ca40c9b4e39ac4923af480 e1577dd860ca40c9b4e39ac4923af480--3fdd7d64134c4fb98a6de9728e6b2c23 e6dc99e678544762b41f0ab0aae6042f e1577dd860ca40c9b4e39ac4923af480--e6dc99e678544762b41f0ab0aae6042f c23dc136a207481aacbda1db7e03cf9c RX(theta₁₀) e6dc99e678544762b41f0ab0aae6042f--c23dc136a207481aacbda1db7e03cf9c 2d5f0ec1497e491d934f3c81595dfd86 RY(theta₁₃) c23dc136a207481aacbda1db7e03cf9c--2d5f0ec1497e491d934f3c81595dfd86 9339144803564e0ca83de95e765f4fff RX(theta₁₆) 2d5f0ec1497e491d934f3c81595dfd86--9339144803564e0ca83de95e765f4fff f50c18bfed474433b29b26b87bd2b5ec X 9339144803564e0ca83de95e765f4fff--f50c18bfed474433b29b26b87bd2b5ec f50c18bfed474433b29b26b87bd2b5ec--df931ef2c31749d8b2dd0b0b071f0c8a 0dea9be5fa5540cfbf66bba74ce1778e f50c18bfed474433b29b26b87bd2b5ec--0dea9be5fa5540cfbf66bba74ce1778e cf510ae31ac64f45abc4a10a40fbdae6 RX(theta₁₉) 0dea9be5fa5540cfbf66bba74ce1778e--cf510ae31ac64f45abc4a10a40fbdae6 dd2f35a62e4a4b95a362a1bf7c466da5 RY(theta₂₂) cf510ae31ac64f45abc4a10a40fbdae6--dd2f35a62e4a4b95a362a1bf7c466da5 9269c179270a48258123327b928ec007 RX(theta₂₅) dd2f35a62e4a4b95a362a1bf7c466da5--9269c179270a48258123327b928ec007 d34535b6a8fd42f6b9aab24f9fb12de9 X 9269c179270a48258123327b928ec007--d34535b6a8fd42f6b9aab24f9fb12de9 d34535b6a8fd42f6b9aab24f9fb12de9--5fb47f28eb2a488287ea50a99c43bd1d 510af6feb14b4084a3757f0c7b9af527 d34535b6a8fd42f6b9aab24f9fb12de9--510af6feb14b4084a3757f0c7b9af527 510af6feb14b4084a3757f0c7b9af527--6f897e38d47b48b1a884c2b5049e66b1 c6136b7ed048419ea7d2982d9e2dcc67 dac0c81352974fea924f0d93a6b68a49 RX(3.0*acos(x)) 44084ea8384d4e8fae41003a94210599--dac0c81352974fea924f0d93a6b68a49 a2fe873e48f44ac090f9c4ee266ddcd7 RX(theta₂) dac0c81352974fea924f0d93a6b68a49--a2fe873e48f44ac090f9c4ee266ddcd7 20c85cac2db3431f96cb63a05c25e247 RY(theta₅) a2fe873e48f44ac090f9c4ee266ddcd7--20c85cac2db3431f96cb63a05c25e247 0de018b4085e482887241c7fb4eedff0 RX(theta₈) 20c85cac2db3431f96cb63a05c25e247--0de018b4085e482887241c7fb4eedff0 7ef521cd9ef441a8b423af4e9c03d393 0de018b4085e482887241c7fb4eedff0--7ef521cd9ef441a8b423af4e9c03d393 b026a031c8fc48d080802f955a8d2cad X 7ef521cd9ef441a8b423af4e9c03d393--b026a031c8fc48d080802f955a8d2cad b026a031c8fc48d080802f955a8d2cad--e6dc99e678544762b41f0ab0aae6042f 16e9266b741449c7a0c254b4cb4a866f RX(theta₁₁) b026a031c8fc48d080802f955a8d2cad--16e9266b741449c7a0c254b4cb4a866f 779c375294e44c1a844380f0d0e33678 RY(theta₁₄) 16e9266b741449c7a0c254b4cb4a866f--779c375294e44c1a844380f0d0e33678 dc635b4d2cde4808afdb23582b0e4f52 RX(theta₁₇) 779c375294e44c1a844380f0d0e33678--dc635b4d2cde4808afdb23582b0e4f52 37373ff7dd1a477396c41f42fb766ef8 dc635b4d2cde4808afdb23582b0e4f52--37373ff7dd1a477396c41f42fb766ef8 1f316af6f5bb4a9b92da181018c3ed62 X 37373ff7dd1a477396c41f42fb766ef8--1f316af6f5bb4a9b92da181018c3ed62 1f316af6f5bb4a9b92da181018c3ed62--0dea9be5fa5540cfbf66bba74ce1778e f3485231a6e14992904f3778d0b64d84 RX(theta₂₀) 1f316af6f5bb4a9b92da181018c3ed62--f3485231a6e14992904f3778d0b64d84 64679028d7314927a0c3d50edbd11091 RY(theta₂₃) f3485231a6e14992904f3778d0b64d84--64679028d7314927a0c3d50edbd11091 34cc16fddd2d4f19887aaa75c9df2d99 RX(theta₂₆) 64679028d7314927a0c3d50edbd11091--34cc16fddd2d4f19887aaa75c9df2d99 da3f040279af4a2ca06ed69ae7cb5eb3 34cc16fddd2d4f19887aaa75c9df2d99--da3f040279af4a2ca06ed69ae7cb5eb3 1152eedd04ba42158f11cbaad9663f62 X da3f040279af4a2ca06ed69ae7cb5eb3--1152eedd04ba42158f11cbaad9663f62 1152eedd04ba42158f11cbaad9663f62--510af6feb14b4084a3757f0c7b9af527 1152eedd04ba42158f11cbaad9663f62--c6136b7ed048419ea7d2982d9e2dcc67

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-17T17:44:34.119033 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-17T17:44:41.489350 image/svg+xml Matplotlib v3.7.5, https://matplotlib.org/

References